Skip to content

Commit e96a253

Browse files
committed
Add nehe lessons
1 parent 881fbae commit e96a253

2 files changed

Lines changed: 92 additions & 0 deletions

File tree

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ add_custom_command(TARGET tricked_out POST_BUILD
172172
)
173173

174174
# NeHe tutorials
175+
add_ps2gl_example(nehe_lesson01 nehe/lesson01/lesson1.cpp)
175176
add_ps2gl_example(nehe_lesson02 nehe/lesson02/lesson2.cpp)
176177
add_ps2gl_example(nehe_lesson03 nehe/lesson03/lesson3.cpp)
177178
add_ps2gl_example(nehe_lesson04 nehe/lesson04/lesson4.cpp)

examples/nehe/lesson01/lesson1.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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

Comments
 (0)