Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "glut.h"
- #include <ctype.h>
- // camera position
- float camY = 5.f;
- float camZ = 10.f;
- void init()
- {
- // rendering states
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_NORMALIZE);
- glShadeModel(GL_SMOOTH);
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- GLfloat clrWhite[] = { 1.0f, 1.0f, 1.0f };
- GLfloat posLight[] = { 3.0f, 3.0f, 0.0f, 1.0f };
- glLightfv(GL_LIGHT0, GL_AMBIENT, clrWhite); // ambient light colour
- glLightfv(GL_LIGHT0, GL_DIFFUSE, clrWhite); // diffuse light colour
- glLightfv(GL_LIGHT0, GL_POSITION, posLight); // light position
- }
- void render()
- {
- static float theta = 0.0f;
- // clear screen and buffers
- glClearColor(0.18f, 0.25f, 0.22f, 1.0f); // deep grey background
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // setup the view point
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(0.0, camY, camZ,
- 0.0, camY - 5, camZ - 15,
- 0.0, 1.0, 0.0);
- //camera
- glRotatef(theta, 0.0f, 1.0f, 0.0f);
- //material
- float clr2[] = { 0.0f, 0.0f, 0.0f };
- float clr[] = { 0.8f, 0.8f, 0.8f };
- //render
- glTranslatef(3.0f, -1.0f, 1.0f);
- glRotatef(-theta, 1.0f, 1.0f, 0.0f);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, clr);
- glutSolidTeapot(2.0);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, clr2);
- glutWireTeapot(2.02);
- //animation
- static GLint prev_time = 0;
- int time = glutGet(GLUT_ELAPSED_TIME);
- theta += (time - prev_time) * 0.01;
- prev_time = time;
- glutPostRedisplay();
- // essential for double-buffering technique
- glutSwapBuffers();
- }
- void reshape(int w, int h)
- {
- // find screen aspect ratio
- float ratio = w * 1.0 / h; // we hope that h is not zero
- // setup the projection matrix
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glViewport(0, 0, w, h);
- gluPerspective(60.0f, ratio, 1.0f, 100.0f);
- // set model/view matrix for rendering
- glMatrixMode(GL_MODELVIEW);
- }
- void onKeyDown(unsigned char key, int x, int y)
- {
- switch (tolower(key))
- {
- case 'w': camZ -= 0.4; break;
- case 's': camZ += 0.4; break;
- case 'x': camY += 0.4; break;
- case 'z': camY -= 0.4; break;
- }
- }
- int main(int argc, char **argv)
- {
- // init GLUT and create Window
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
- glutInitWindowPosition(100, 100);
- glutInitWindowSize(800, 600);
- glutCreateWindow("CI5520 3D Graphics Programming");
- // register callbacks
- glutDisplayFunc(render);
- glutReshapeFunc(reshape);
- glutKeyboardFunc(onKeyDown);
- // init light and everything – not a GLUT or callback function!
- init();
- // enter GLUT event processing cycle
- glutMainLoop();
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement