|
| 1 | +#include <GL/gl.h> // The GL Header File |
| 2 | +#include <GL/glut.h> // The GL Utility Toolkit (Glut) Header |
| 3 | +#include <math.h> |
| 4 | + |
| 5 | +float rtri; // Angle For The Triangle |
| 6 | +float rquad; // Angle For The Quad |
| 7 | + |
| 8 | +//----------------------------------------------------------------------------- |
| 9 | +void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar) |
| 10 | +{ |
| 11 | + GLdouble xmin, xmax, ymin, ymax; |
| 12 | + |
| 13 | + ymax = zNear * tan(fovy * M_PI / 360.0f); |
| 14 | + ymin = -ymax; |
| 15 | + xmin = ymin * aspect; |
| 16 | + xmax = ymax * aspect; |
| 17 | + |
| 18 | + glFrustum(xmin, xmax, ymin, ymax, zNear, zFar); |
| 19 | +} |
| 20 | + |
| 21 | +void init(GLvoid) // Create Some Everyday Functions |
| 22 | +{ |
| 23 | + |
| 24 | + glShadeModel(GL_SMOOTH); // Enable Smooth Shading |
| 25 | + glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background |
| 26 | + glClearDepth(1.0f); // Depth Buffer Setup |
| 27 | + glEnable(GL_DEPTH_TEST); // Enables Depth Testing |
| 28 | + glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do |
| 29 | + //glEnable(GL_COLOR_MATERIAL); |
| 30 | + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); |
| 31 | +} |
| 32 | + |
| 33 | +void display(void) // Create The Display Function |
| 34 | +{ |
| 35 | + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer |
| 36 | + glLoadIdentity(); // Reset The Current Modelview Matrix |
| 37 | + glPushMatrix(); |
| 38 | + glTranslatef(-1.5f, 0.0f, -6.0f); // Move Left 1.5 Units And Into The Screen 6.0 |
| 39 | + glRotatef(rtri, 0.0f, 1.0f, 0.0f); // Rotate The Triangle On The Y axis |
| 40 | + glBegin(GL_TRIANGLES); // Drawing Using Triangles |
| 41 | + glColor3f(1.0f, 0.0f, 0.0f); // Set The Color To Red |
| 42 | + glVertex3f(0.0f, 1.0f, 0.0f); // Top |
| 43 | + glColor3f(0.0f, 1.0f, 0.0f); // Set The Color To Green |
| 44 | + glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left |
| 45 | + glColor3f(0.0f, 0.0f, 1.0f); // Set The Color To Blue |
| 46 | + glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right |
| 47 | + glEnd(); // Finished Drawing The Triangle |
| 48 | + |
| 49 | + glLoadIdentity(); // Reset The Current Modelview Matrix |
| 50 | + glTranslatef(1.5f, 0.0f, -6.0f); // Move Right 1.5 Units And Into The Screen 6.0 |
| 51 | + glRotatef(rquad, 1.0f, 0.0f, 0.0f); // Rotate The Quad On The X axis |
| 52 | + glColor3f(0.5f, 0.5f, 1.0f); // Set The Color To Blue One Time Only |
| 53 | + glBegin(GL_QUADS); // Draw A Quad |
| 54 | + glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left |
| 55 | + glVertex3f(1.0f, 1.0f, 0.0f); // Top Right |
| 56 | + glVertex3f(1.0f, -1.0f, 0.0f); // Bottom Right |
| 57 | + glVertex3f(-1.0f, -1.0f, 0.0f); // Bottom Left |
| 58 | + glEnd(); // Done Drawing The Quad |
| 59 | + glPopMatrix(); |
| 60 | + rtri += 0.2f; // Increase The Rotation Variable For The Triangle ( NEW ) |
| 61 | + rquad -= 0.15f; // Decrease The Rotation Variable For The Quad ( NEW ) |
| 62 | + |
| 63 | + |
| 64 | + glutSwapBuffers(); |
| 65 | + // Swap The Buffers To Not Be Left With A Clear Screen |
| 66 | +} |
| 67 | + |
| 68 | +void reshape(int w, int h) // Create The Reshape Function (the viewport) |
| 69 | +{ |
| 70 | + glViewport(0, 0, w, h); |
| 71 | + glMatrixMode(GL_PROJECTION); // Select The Projection Matrix |
| 72 | + glLoadIdentity(); // Reset The Projection Matrix |
| 73 | + if (h == 0) // Calculate The Aspect Ratio Of The Window |
| 74 | + gluPerspective(80, (float)w, 1.0, 5000.0); |
| 75 | + else |
| 76 | + gluPerspective(80, (float)w / (float)h, 1.0, 5000.0); |
| 77 | + glMatrixMode(GL_MODELVIEW); // Select The Model View Matrix |
| 78 | + glLoadIdentity(); // Reset The Model View Matrix |
| 79 | +} |
| 80 | + |
| 81 | +int main(int argc, char **argv) // Create Main Function For Bringing It All Together |
| 82 | +{ |
| 83 | + glutInit(&argc, argv); // Erm Just Write It =) |
| 84 | + init(); |
| 85 | + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); // Display Mode |
| 86 | + glutInitWindowSize(640, 448); // If glutFullScreen wasn't called this is the window size |
| 87 | + glutCreateWindow("NeHe's OpenGL Framework"); // Window Title (argv[0] for current directory as title) |
| 88 | + glutDisplayFunc(display); // Matching Earlier Functions To Their Counterparts |
| 89 | + glutReshapeFunc(reshape); |
| 90 | + glutIdleFunc(display); |
| 91 | + glutMainLoop(); // Initialize The Main Loop |
| 92 | + |
| 93 | + return 0; |
| 94 | +} |
0 commit comments