Last active
December 10, 2015 10:28
-
-
Save kopiro/4420984 to your computer and use it in GitHub Desktop.
Advanced OpenGL++ base
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // g++ src.cpp -lglut -lGL -lGLU; ./a.out | |
| #include <iostream> | |
| #include <cmath> | |
| #include <GL/glut.h> | |
| #include <GL/glu.h> | |
| #include <GL/gl.h> | |
| // CAMERA position | |
| GLfloat CAMERA_X = 0; | |
| GLfloat CAMERA_Y = 0; | |
| GLfloat CAMERA_Z = 10; | |
| GLfloat CAMERA_RX = 0; | |
| GLfloat CAMERA_RY = 0; | |
| GLfloat CAMERA_RZ = 0; | |
| // MOUSE location | |
| GLfloat MOUSE_X = 0; | |
| GLfloat MOUSE_Y = 0; | |
| // PERSPECTIVE vars | |
| GLfloat PERSP_ANGLE = 20; | |
| GLfloat PERSP_MINDIST = 0.1f; | |
| GLfloat PERSP_MAXDIST = 1000.0f; | |
| GLfloat light0_position[] = { 0,0,1, 0 }; | |
| void drawScene() | |
| { | |
| glColor3f(1,0,0); | |
| glutSolidTeapot(1); | |
| } | |
| void idleFunc() | |
| { | |
| // glutPostRedisplay(); | |
| } | |
| void renderScene() | |
| { | |
| glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); | |
| glMatrixMode(GL_MODELVIEW); | |
| glLoadIdentity(); | |
| gluLookAt(CAMERA_X,CAMERA_Y,CAMERA_Z, 0,0,0, 0,1,0); | |
| glRotatef(CAMERA_RX, 1,0,0); | |
| glRotatef(CAMERA_RY, 0,1,0); | |
| glRotatef(CAMERA_RZ, 0,0,1); | |
| glLightfv(GL_LIGHT0, GL_POSITION, light0_position); | |
| drawScene(); | |
| glutSwapBuffers(); | |
| } | |
| void passiveMotionHandler(int x, int y) | |
| { | |
| MOUSE_X = x; | |
| MOUSE_Y = y; | |
| } | |
| void motionHandler(int x, int y) | |
| { | |
| glutPostRedisplay(); | |
| } | |
| void pickedHandler(GLint hits, GLuint buffer[]) | |
| { | |
| // hits is the number of hits, buffer[ (N*4)-1 ] are the IDs picked | |
| glutPostRedisplay(); | |
| } | |
| void pickingFunction(int x, int y) | |
| { | |
| GLuint buff[64] = {0}; | |
| GLint hits, view[4]; | |
| glSelectBuffer(64, buff); | |
| glGetIntegerv(GL_VIEWPORT, view); | |
| glRenderMode(GL_SELECT); | |
| glInitNames(); | |
| glPushName(0); | |
| glMatrixMode(GL_PROJECTION); | |
| glPushMatrix(); | |
| glLoadIdentity(); | |
| gluPickMatrix(x, y, 1.0, 1.0, view); | |
| gluPerspective(PERSP_ANGLE, view[2]*1.0f/view[3], PERSP_MINDIST, PERSP_MAXDIST); | |
| glMatrixMode(GL_MODELVIEW); | |
| glutSwapBuffers(); | |
| renderScene(); | |
| glMatrixMode(GL_PROJECTION); | |
| glPopMatrix(); | |
| hits = glRenderMode(GL_RENDER); | |
| if (hits>0) pickedHandler(hits, buff); | |
| glMatrixMode(GL_MODELVIEW); | |
| } | |
| void mouseHandler(int button, int state, int x, int y) | |
| { | |
| if (button==GLUT_LEFT_BUTTON && state==GLUT_DOWN) pickingFunction(x, y); | |
| } | |
| void keysHandler(unsigned char k, int x, int y) | |
| { | |
| glutPostRedisplay(); | |
| } | |
| void specialKeysHandler(int k, int x, int y) | |
| { | |
| glutPostRedisplay(); | |
| if (k==GLUT_KEY_LEFT) CAMERA_RY += 1; | |
| else if (k==GLUT_KEY_RIGHT) CAMERA_RY -= 1; | |
| else if (k==GLUT_KEY_UP) CAMERA_RX += 1; | |
| else if (k==GLUT_KEY_DOWN) CAMERA_RX -= 1; | |
| } | |
| void reshapeScene(int w, int h) | |
| { | |
| glMatrixMode(GL_PROJECTION); | |
| glLoadIdentity(); | |
| glViewport(0, 0, w, h); | |
| gluPerspective(PERSP_ANGLE, w*1.0f/h, PERSP_MINDIST, PERSP_MAXDIST); | |
| glMatrixMode(GL_MODELVIEW); | |
| } | |
| void initDisplayLists() | |
| { | |
| /* init display lists here, call with glCallList(N); | |
| glNewList(N, GL_COMPILE); | |
| ... | |
| glEndList(); | |
| */ | |
| } | |
| void initLights() | |
| { | |
| // Lights | |
| glEnable(GL_LIGHTING); | |
| float ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f }; | |
| glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight); | |
| glEnable(GL_LIGHT0); | |
| GLfloat light0_ambientLight[] = { 0.5f, 0.5f, 0.5f, 1.0f }; | |
| GLfloat light0_diffuseLight[] = { 0.5f, 0.5f, 0.2f, 1.0f }; | |
| GLfloat light0_specularLight[] = { 0.5f, 0.5f, 0.5f, 1.0f }; | |
| glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambientLight); | |
| glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuseLight); | |
| glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specularLight); | |
| glLightfv(GL_LIGHT0, GL_POSITION, light0_position); | |
| // Materials | |
| glEnable(GL_COLOR_MATERIAL); | |
| glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); | |
| float specReflection[] = { 1,1,1,1 }; | |
| glMaterialfv(GL_FRONT, GL_SPECULAR, specReflection); | |
| glMateriali(GL_FRONT, GL_SHININESS, 40); | |
| // Fog | |
| // glEnable(GL_FOG); | |
| glFogi(GL_FOG_MODE, GL_EXP2); | |
| GLfloat fog[] = { 1,1,1, 1 }; | |
| glFogfv(GL_FOG_COLOR, fog); | |
| glFogf(GL_FOG_DENSITY, 0.05f); | |
| } | |
| void init() | |
| { | |
| glEnable(GL_DEPTH_TEST); | |
| glEnable(GL_BLEND); | |
| glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | |
| glShadeModel(GL_SMOOTH); | |
| //glEnable(GL_NORMALIZE); | |
| //glEnable(GL_CULL_FACE); | |
| initDisplayLists(); | |
| initLights(); | |
| } | |
| int main(int argc, char** argv) | |
| { | |
| glutInit(&argc, argv); | |
| glutInitDisplayMode(GLUT_RGB|GLUT_DEPTH|GLUT_DOUBLE); | |
| glutInitWindowSize(1000, 600); | |
| glutCreateWindow("Base | OpenGL"); | |
| glutDisplayFunc(renderScene); | |
| glutReshapeFunc(reshapeScene); | |
| glutMouseFunc(mouseHandler); | |
| glutMotionFunc(motionHandler); | |
| glutPassiveMotionFunc(passiveMotionHandler); | |
| glutKeyboardFunc(keysHandler); | |
| glutSpecialFunc(specialKeysHandler); | |
| glutIdleFunc(idleFunc); | |
| init(); | |
| glutMainLoop(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment