|
| 1 | +#include <GL/glut.h> |
| 2 | + |
| 3 | +//Setting Camera |
| 4 | +float eye[] = {0.0, 0.0, 5.0}; |
| 5 | +float center[] = {0.0, 0.0, 0.0}; |
| 6 | +float up[] = {0.0, 1.0, 0.0}; |
| 7 | +float alpha = 60; |
| 8 | +float theta = 60; |
| 9 | +void DB() |
| 10 | +{ |
| 11 | + glPushMatrix(); |
| 12 | + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); |
| 13 | + glBegin(GL_POLYGON); |
| 14 | + glVertex3f(0.0, 0.0, 0.0); |
| 15 | + glVertex3f(1.0, 0.0, 0.0); |
| 16 | + glVertex3f(1.0, 1.0, 0.0); |
| 17 | + glVertex3f(0.0, 1.0, 0.0); |
| 18 | + glEnd(); |
| 19 | + glPopMatrix(); |
| 20 | +} |
| 21 | +void draw_block(float xs, float ys) |
| 22 | +{ |
| 23 | + glPushMatrix(); |
| 24 | + glScalef(xs, ys, 1.0); |
| 25 | + glTranslatef(0.0, -0.5, 0.0); |
| 26 | + DB(); |
| 27 | + glPopMatrix(); |
| 28 | +} |
| 29 | + |
| 30 | +void draw_scene() |
| 31 | +{ |
| 32 | + glPushMatrix(); |
| 33 | + glTranslatef(1.0, 3.5, 0.0); |
| 34 | + draw_block(2, 5); |
| 35 | + glTranslatef(1.0, 1.5, 0.0); |
| 36 | + glRotatef(alpha-90, 0.0, 0.0, 1.0); |
| 37 | + draw_block(3, 1); |
| 38 | + glTranslatef(3.0, 0.0, 0.0); |
| 39 | + glRotatef(theta, 0.0, 0.0, 1.0); |
| 40 | + draw_block(3, 1); |
| 41 | + glPopMatrix(); |
| 42 | +} |
| 43 | +void display() |
| 44 | +{ |
| 45 | + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 46 | + glLoadIdentity(); |
| 47 | + draw_scene(); |
| 48 | + glutSwapBuffers(); |
| 49 | +} |
| 50 | + |
| 51 | +void init() |
| 52 | +{ |
| 53 | + glEnable(GL_DEPTH_TEST); |
| 54 | + glMatrixMode(GL_PROJECTION); |
| 55 | + glOrtho(0, 10, 0, 10, 1, 10); |
| 56 | + // gluPerspective(120.0, 1.0, 1.0, 10.0); |
| 57 | + gluLookAt(eye[0], eye[1], eye[2], |
| 58 | + center[0], center[1], center[2], |
| 59 | + up[0], up[1], up[2]); |
| 60 | + glMatrixMode(GL_MODELVIEW); |
| 61 | +} |
| 62 | + |
| 63 | +int main(int argc, char **argv) |
| 64 | +{ |
| 65 | + glutInit(&argc, argv); |
| 66 | + glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); |
| 67 | + glutCreateWindow("red 3D lighted sphere"); |
| 68 | + glutDisplayFunc(display); |
| 69 | + init(); |
| 70 | + glutMainLoop(); |
| 71 | + return 0; |
| 72 | +} |
0 commit comments