| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- #include "Header.h"
- //Prototypes
- void bindFuncOpenGL(void);
- void configureOpenGL(void);
- void loadModels(void);
- static int Width;
- static int Height;
- float lastFrameTime = 0;
- bool keys[255];
- vector<ObjModel*> models;
- int currentModel = 0;
- struct Camera
- {
- float posX = -30;
- float posY = -30;
- float posZ = -30;
- float rotX = 0;
- float rotY = 0;
- } camera;
- void display()
- {
- glClearColor(0.6f, 0.6f, 1, 1);
- glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(60.0f, (float)Width / Height, 0.5, 300);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glRotatef(camera.rotX, 1, 0, 0);
- glRotatef(camera.rotY, 0, 1, 0);
- glTranslatef(camera.posX, camera.posY, camera.posZ);
- //Draw here
- models[currentModel]->draw();
- glDisable(GL_TEXTURE_2D);
- glutSwapBuffers();
- }
- void move(float angle, float fac, bool heigth)
- {
- camera.posX += (float)cos((camera.rotY + angle) / 180 * M_PI) * fac;
- camera.posZ += (float)sin((camera.rotY + angle) / 180 * M_PI) * fac;
- if(heigth)
- camera.posY += (float)sin((camera.rotX + angle) / 180 * M_PI) * fac;
- }
- void idle()
- {
- float frameTime = glutGet(GLUT_ELAPSED_TIME) / 1000.0f;
- float deltaTime = frameTime - lastFrameTime;
- lastFrameTime = frameTime;
- float speed = 10;
- if (keys['a']) move(0, deltaTime*speed, false);
- if (keys['d']) move(180, deltaTime*speed, false);
- if (keys['w']) move(90, deltaTime*speed, true);
- if (keys['s']) move(270, deltaTime*speed, true);
- glutPostRedisplay();
- }
- void mousemotion(int x, int y)
- {
- int dx = x - Width / 2;
- int dy = y - Height / 2;
- if ((dx != 0 || dy != 0) && abs(dx) < 400 && abs(dy) < 400)
- {
- camera.rotY += dx / 10.0f;
- camera.rotX += dy / 10.0f;
- glutWarpPointer(Width / 2, Height / 2);
- }
- }
- void keyboard(unsigned char key, int, int)
- {
- if (key == 27)
- exit(0);
- keys[key] = true;
- }
- void keyboardup(unsigned char key, int, int)
- {
- keys[key] = false;
- }
- int main(int argc, char* argv[])
- {
- //Init GLUT
- glutInit(&argc, argv);
- //Configre OpenGL and FreeGLut
- configureOpenGL();
- //Bind functions
- bindFuncOpenGL();
- //Init models
- loadModels();
- //Start the main loop
- glutMainLoop();
- return 0;
- }
- void bindFuncOpenGL()
- {
- glutDisplayFunc(display);
- glutIdleFunc(idle);
- glutReshapeFunc([](int w, int h) { Width = w; Height= h; glViewport(0, 0, w, h); });
- //Keyboard
- glutKeyboardFunc(keyboard);
- glutKeyboardUpFunc(keyboardup);
- //Mouse
- //glutMouseFunc(mouse);
- glutPassiveMotionFunc(mousemotion);
-
- }
- void configureOpenGL()
- {
- //Init window and glut display mode
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- glutInitWindowSize(800, 600);
- glutCreateWindow("Crystal Point");
- glutFullScreen();
- //Depth testing
- glEnable(GL_DEPTH_TEST);
- //Alpha blending
- glEnable(GL_BLEND);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-
- //Alpha testing
- glEnable(GL_ALPHA_TEST);
- glAlphaFunc(GL_GREATER, 0.01f);
- //Lighting
- GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
- GLfloat mat_shininess[] = { 50.0 };
- GLfloat light_position[] = { 30.0, 30.0, 30.0, 0.0 };
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glShadeModel(GL_SMOOTH);
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- //Cursor
- glutWarpPointer(Width / 2, Height / 2);
- glutSetCursor(GLUT_CURSOR_NONE);
- }
- void loadModels()
- {
- models.push_back(new ObjModel("models/ship/shipA_OBJ.obj"));
- }
|