|
| 1 | +#include <GL/gl.h> |
| 2 | +#include <GL/glut.h> |
| 3 | +#include <math.h> |
| 4 | + |
| 5 | +// Simple replacement for gluPerspective (GLU not available on PS2) |
| 6 | +void gluPerspective(GLdouble fovy, GLdouble aspect, GLdouble zNear, GLdouble zFar) |
| 7 | +{ |
| 8 | + GLdouble xmin, xmax, ymin, ymax; |
| 9 | + |
| 10 | + ymax = zNear * tan(fovy * M_PI / 360.0f); |
| 11 | + ymin = -ymax; |
| 12 | + xmin = ymin * aspect; |
| 13 | + xmax = ymax * aspect; |
| 14 | + |
| 15 | + glFrustum(xmin, xmax, ymin, ymax, zNear, zFar); |
| 16 | +} |
| 17 | + |
| 18 | +// Global Variables |
| 19 | +bool g_gamemode = false; |
| 20 | +bool g_fullscreen = false; |
| 21 | + |
| 22 | +// Our GL Specific Initializations |
| 23 | +bool init(void) |
| 24 | +{ |
| 25 | + glShadeModel(GL_SMOOTH); // Enable Smooth Shading |
| 26 | + glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background |
| 27 | + glClearDepth(1.0f); // Depth Buffer Setup |
| 28 | + glEnable(GL_DEPTH_TEST); // Enables Depth Testing |
| 29 | + glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do |
| 30 | + glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Nice perspective |
| 31 | + return true; |
| 32 | +} |
| 33 | + |
| 34 | +// Our Rendering Is Done Here |
| 35 | +void render(void) |
| 36 | +{ |
| 37 | + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer |
| 38 | + glLoadIdentity(); // Reset The Current Modelview Matrix |
| 39 | + |
| 40 | + // Swap The Buffers To Become Our Rendering Visible |
| 41 | + glutSwapBuffers(); |
| 42 | +} |
| 43 | + |
| 44 | +// Our Reshaping Handler (Required Even In Fullscreen-Only Modes) |
| 45 | +void reshape(int w, int h) |
| 46 | +{ |
| 47 | + glViewport(0, 0, w, h); |
| 48 | + glMatrixMode(GL_PROJECTION); // Select The Projection Matrix |
| 49 | + glLoadIdentity(); // Reset The Projection Matrix |
| 50 | + if (h == 0) h = 1; |
| 51 | + gluPerspective(80, (float)w/(float)h, 1.0, 5000.0); |
| 52 | + glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix |
| 53 | + glLoadIdentity(); // Reset The Modelview Matrix |
| 54 | +} |
| 55 | + |
| 56 | +// Our Keyboard Handler (Normal Keys) |
| 57 | +void keyboard(unsigned char key, int x, int y) |
| 58 | +{ |
| 59 | + switch (key) { |
| 60 | + case 27: // When Escape Is Pressed... |
| 61 | + exit(0); // Exit The Program |
| 62 | + break; // Ready For Next Case |
| 63 | + default: // Now Wrap It Up |
| 64 | + break; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// Our Keyboard Handler For Special Keys (Like Arrow Keys And Function Keys) |
| 69 | +void special_keys(int a_keys, int x, int y) |
| 70 | +{ |
| 71 | + // PS2 GLUT does not support the original F1/fullscreen logic from NeHe. |
| 72 | + // Keep special keys no-op for compatibility with ps2glut's limited mapping. |
| 73 | + (void)a_keys; (void)x; (void)y; |
| 74 | +} |
| 75 | + |
| 76 | +int main(int argc, char** argv) |
| 77 | +{ |
| 78 | + // Note: original NeHe uses a Windows MessageBox to ask for gamemode. |
| 79 | + // For PS2 we default to windowed mode. |
| 80 | + glutInit(&argc, argv); // GLUT Initializtion |
| 81 | + glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); // Display Mode (Rgb And Double Buffered) |
| 82 | + glutInitWindowSize(640, 448); // If glutFullScreen wasn't called this is the window size |
| 83 | + glutCreateWindow("NeHe's OpenGL Framework - Lesson 01"); // Window Title |
| 84 | + init(); // Our Initialization |
| 85 | + glutDisplayFunc(render); // Register The Display Function |
| 86 | + glutReshapeFunc(reshape); // Register The Reshape Handler |
| 87 | + glutKeyboardFunc(keyboard); // Register The Keyboard Handler |
| 88 | + glutSpecialFunc(special_keys); // Register Special Keys Handler |
| 89 | + glutMainLoop(); // Go To GLUT Main Loop |
| 90 | + return 0; |
| 91 | +} |
0 commit comments