View difference between Paste ID: LCKmVJ8v and
SHOW: | | - or go back to the newest paste.
1-
1+
2
// This code was created by Jeff Molofee '99 (ported to Linux/GLUT by Richard Campbell '99)
3
//
4
// If you've found this code useful, please let me know.
5
//
6
// Visit me at www.demonews.com/hosted/nehe 
7
// (email Richard Campbell at ulmont@bellsouth.net)
8
//
9
#include <GL/glut.h>    // Header File For The GLUT Library 
10
#include <GL/gl.h>	// Header File For The OpenGL32 Library
11
#include <GL/glu.h>	// Header File For The GLu32 Library
12
#include <unistd.h>     // Header file for sleeping.
13
14
/* ascii code for the escape key */
15
#define ESCAPE 27
16
17
/* The number of our GLUT window */
18
int window; 
19
20
/* A general OpenGL initialization function.  Sets all of the initial parameters. */
21
void InitGL(int Width, int Height)	        // We call this right after our OpenGL window is created.
22
{
23
  glClearColor(0.0f, 0.0f, 0.0f, 0.0f);		// This Will Clear The Background Color To Black
24
  glClearDepth(1.0);				// Enables Clearing Of The Depth Buffer
25
  glDepthFunc(GL_LESS);				// The Type Of Depth Test To Do
26
  glEnable(GL_DEPTH_TEST);			// Enables Depth Testing
27
  glShadeModel(GL_SMOOTH);			// Enables Smooth Color Shading
28
29
  glMatrixMode(GL_PROJECTION);
30
  glLoadIdentity();				// Reset The Projection Matrix
31
32
  gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);	// Calculate The Aspect Ratio Of The Window
33
34
  glMatrixMode(GL_MODELVIEW);
35
}
36
37
/* The function called when our window is resized (which shouldn't happen, because we're fullscreen) */
38
void ReSizeGLScene(int Width, int Height)
39
{
40
  if (Height==0)				// Prevent A Divide By Zero If The Window Is Too Small
41
    Height=1;
42
43
  glViewport(0, 0, Width, Height);		// Reset The Current Viewport And Perspective Transformation
44
45
  glMatrixMode(GL_PROJECTION);
46
  glLoadIdentity();
47
48
  gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
49
  glMatrixMode(GL_MODELVIEW);
50
}
51
52
/* The main drawing function. */
53
void DrawGLScene()
54
{
55
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// Clear The Screen And The Depth Buffer
56
  glLoadIdentity();				// Reset The View
57
58
  // since this is double buffered, swap the buffers to display what just got drawn.
59
  glutSwapBuffers();
60
}
61
62
/* The function called whenever a key is pressed. */
63
void keyPressed(unsigned char key, int x, int y) 
64
{
65
    /* avoid thrashing this procedure */
66
    usleep(100);
67
68
    /* If escape is pressed, kill everything. */
69
    if (key == ESCAPE) 
70
    { 
71
	/* shut down our window */
72
	glutDestroyWindow(window); 
73
	
74
	/* exit the program...normal termination. */
75
	exit(0);                   
76
    }
77
}
78
79
int main(int argc, char **argv) 
80
{  
81
  /* Initialize GLUT state - glut will take any command line arguments that pertain to it or 
82
     X Windows - look at its documentation at http://reality.sgi.com/mjk/spec3/spec3.html */  
83
  glutInit(&argc, argv);  
84
85
  /* Select type of Display mode:   
86
     Double buffer 
87
     RGBA color
88
     Alpha components supported 
89
     Depth buffer */  
90
  glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);  
91
92
  /* get a 640 x 480 window */
93
  glutInitWindowSize(640, 480);  
94
95
  /* the window starts at the upper left corner of the screen */
96
  glutInitWindowPosition(0, 0);  
97
98
  /* Open a window */  
99
  window = glutCreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe '99");  
100
101
  /* Register the function to do all our OpenGL drawing. */
102
  glutDisplayFunc(&DrawGLScene);  
103
104
  /* Go fullscreen.  This is as soon as possible. */
105
  glutFullScreen();
106
107
  /* Even if there are no events, redraw our gl scene. */
108
  glutIdleFunc(&DrawGLScene);
109
110
  /* Register the function called when our window is resized. */
111
  glutReshapeFunc(&ReSizeGLScene);
112
113
  /* Register the function called when the keyboard is pressed. */
114
  glutKeyboardFunc(&keyPressed);
115
116
  /* Initialize our window. */
117
  InitGL(640, 480);
118
  
119
  /* Start Event Processing Engine */  
120
  glutMainLoop();  
121
122
  return 1;
123
}