//
// This code was created by Jeff Molofee '99 (ported to
// Linux/GLUT by Richard Campbell '99)
// If you've found this code userful, please let me know.
// Visit me at www.demonews.com/hosted/nehe
// (email Richard Camepbell at ulmont@bellsouth.net)
//
#include <GLUT/GLUT.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/* ascii code fo the escape key */
#define ESCAPE 27
/* The number of our GLUT window */
int window;
/* A general OpenGL initialization function. Sets all of the initial parameters */
// We call this right after our OpenGL window is created.
void initGL(int width, int height) {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This will clear the background color to black
glClearDepth(1.0); // Enables Clearing of the depth buffer
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Reset the projection matrix
// Calculate the aspect ratio of the window
gluPerspective(45.0f, (GLfloat) width/ (GLfloat) height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
}
/* The function called when our window is resized (which shouldn't happend,
because we're fullscreen) */
void resizeGLScene(int width, int height) {
if (height == 0) // prevent a deivce by zero if the window is too small
height = 1;
glViewport(0, 0, width, height); // Reset the curent viewport and perspective transformation
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat) width / (GLfloat) height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
}
/* The main drawing functions. */
void drawGLScene(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen and the depth buffer
glLoadIdentity(); // Reset the view
// since this is double buffered, swap the buffers to display what just got drawn.
glutSwapBuffers();
}
/* The function called whenever a key is pressed. */
void keyPressed(unsigned char key, int x, int y) {
/* avoid threashing this procedure */
usleep(100);
/* If escape is pressed, kill everthing. */
if (key == ESCAPE) {
glutDestroyWindow(window);
/* exit the program ... normal termination. */
exit(0);
}
}
int main(int argc, char **argv) {
/* Initialize GLUT state -glut will take any command line argumetns taht pertain to it or
X Windows - look at its documentation at http://reality.sgi.com/mjk/spec3/spec3.html */
glutInit(&argc, argv);
/* Select type of Display mode:
Double buffer
RGBA color
Alpha components supported
Depth buffer */
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
/* GET A 640 X 480 window */
glutInitWindowSize(640, 480);
/* the window starts at the upper left corner of the screen */
glutInitWindowPosition(0, 0);
glutCreateWindow("Jeff molofee's GL Code Tutorial .. NEH3 '99'");
/* Register the function to do all our OpenGL drawing. */
glutDisplayFunc(&drawGLScene);
/* Go fullscreen. This is as soon as possible. */
//glutFullScreen();
/* Even if there are not events, redraw our gl scene. */
glutIdleFunc(&drawGLScene);
/* Register the function scalled when our window is resized. */
glutReshapeFunc(&resizeGLScene);
/* Register the function called when the keyboard is pressed. */
glutKeyboardFunc(&keyPressed);
/* Initialize our window */
initGL(640, 480);
/* Start event processing engine */
glutMainLoop();
}
测试在Mac OS 运行OK
gcc -o lession lession1.c -Wno-deprecated -framework OpenGL -framework GLUT