Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <GLUT/glut.h>
- #include <OpenGL/gl.h>
- int indices[6][4] = { {1, 2, 6, 5}, {1, 4, 5, 0}, {5, 6, 7, 4}, {2, 6, 7, 3}, {0, 4, 7, 3}, {1, 0, 3, 2}};
- float verts[8][3] = { {-0.5,-0.5,0.5}, {-0.5,0.5,0.5}, {0.5,0.5,0.5}, {0.5,-0.5,0.5}, {-0.5,-0.5,-0.5}, {-0.5,0.5,-0.5}, {0.5,0.5,-0.5}, {0.5,-0.5,-0.5} };
- float cols[6][3] = { {1,0,0}, {0,1,1}, {1,1,0}, {0,1,0}, {0,0,1}, {1,0,1} };
- float pos[] = {0,1,0};
- float camPos[] = {5, 5, 10};
- float angle = 0.0f;
- //draw one face of a cube
- void drawFaceIndex(int index)
- {
- glBegin(GL_POLYGON);
- for(int i=0; i<4; i++)
- {
- int vIndex = indices[index][i];
- glVertex3fv(verts[vIndex]);
- }
- glEnd();
- }
- //draw a cube
- void drawCubeIndex(void)
- {
- for(int idx=0; idx<6; idx++)
- {
- glColor3fv(cols[idx]);
- drawFaceIndex(idx);
- }
- }
- void init(void)
- {
- glClearColor(0, 0, 0, 0);
- glColor3f(1, 1, 1);
- glMatrixMode(GL_PROJECTION);
- gluPerspective(45, 1, 1, 100);
- }
- //display function - GLUT display callback function
- void display(void)
- {
- float origin[3] = {0,0,0};
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clears the screen
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(camPos[0], camPos[1], camPos[2], 0,0,0, 0,1,0);
- glPushMatrix();
- drawCubeIndex();
- glTranslatef(2, 0, 0);
- glPopMatrix();
- glutSwapBuffers();
- }
- void kbd(unsigned char key, int x, int y)
- {
- //if the "q" key is pressed, quit the program
- if(key == 'q' || key == 'Q')
- {
- exit(0);
- }
- }
- void special(int key, int x, int y)
- {
- /* arrow key presses move the camera */
- switch(key)
- {
- case GLUT_KEY_LEFT:
- camPos[0]-=0.1;
- break;
- case GLUT_KEY_RIGHT:
- camPos[0]+=0.1;
- break;
- case GLUT_KEY_UP:
- camPos[2] -= 0.1;
- break;
- case GLUT_KEY_DOWN:
- camPos[2] += 0.1;
- break;
- case GLUT_KEY_HOME:
- camPos[1] += 0.1;
- break;
- case GLUT_KEY_END:
- camPos[1] -= 0.1;
- break;
- }
- glutPostRedisplay();
- }
- //main function - program entry point
- int main(int argc, char** argv)
- {
- glutInit(&argc, argv); //starts up GLUT
- glutInitWindowSize(600, 600);
- glutCreateWindow("Window"); //creates the window
- glutDisplayFunc(display); //registers "display" as the display callback function
- glutKeyboardFunc(kbd);
- glutSpecialFunc(special);
- glutMainLoop(); //starts the event loop
- return(0); //return may not be necessary on all compilers
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement