NeHe OpenGL lession 2

Source: Internet
Author: User

NeHe OpenGL lession 2

 

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
// Header file for sleeping/* ASCII code for 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 oclor to blackglClearDepth (1.0 ); // Enable clearing of the depth buffer glDepthFunc (GL_LESS); // The type depth test to doglEnable (GL_DEPTH_TEST); // Enables depth testingglShadeModel (GL_SMOOTH ); // Enables smooth color shadingglMatrixMode (GL_PROJECTION); glLoadIdentity (); // Reset the projection matrix // Calculate the aspect ratio of the windowgluPerspective (45.0f, (GLfloat) width/(GLfloat) height, 0.1f, 1001_f); glMatrixMode (GL_MODELVIEW);}/* The function called when our window is resized (which shouldn't happend, because we're re fullscreen) */void resizeGLScene (int width, int height) {if (height = 0) // Prevent A devide by zero if the window is too smallheight = 1; glViewport (0, 0, width, height); // Reset teh current viewport and perspective transformationglMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective (45.0f, (GLfloat) width/(GLfloat) height, 0.1f, 1001_f); glMatrixMode (GL_MODELVIEW);}/* The main drawing function. */void drawGLScene () {glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen and the depth. glLoadIdentity (); // Reset the viewglTranslatef (-1.5f, 0.0f,-6.0f); // Move left 1.5 units and into the screen 6.0 // Draw a triangleglBegin (GL_POLYGON ); glVertex3f (0.0f, 1.0f, 0.0f); // TopglVertex3f (1.0f,-1.0f, 0.0f); // Bottom RightglVertex3f (-1.0f,-1.0f, 0.0f ); // Bottom LeftglEnd (); // We're doen with the polygonglTranslatef (3.0f, 0.0f, 0.0f); // Move Right 3 units (Move the following quadrilateral to the Right) // Draw a square (quadrilateral) glBegin (GL_QUADS); // Start drawing a polygon (4 sided) glVertex3f (-1.0f, 1.0f, 0.0f); // top leftglVertex3f (1.0f, 1.0f, 1.0f, 0.0f); // top rightglVertex3f (1.0f,-1.0f, 0.0f); // bottom rightglVertex3f (-1.0f,-1.0f, 0.0f); // Bottom leftglEnd (); // done with the polygon // swap buffers to display, since we're doouble buffered. gluswapbuffers ();}/* the function claled whever a key is pressed. */void keyPressed (unsigned char key, int x, int y) {/* avoid thrashing this procedure * // * If escape is pressed. kill everything. */if (key = ESCAPE) {/* shut down our window */FIG (window);/* exit teh program... normal termination. */exit (0) ;}} int main (int argc, char ** argv) {gluinit (& argc, argv);/* Select type of Display mode: double bufferRGBA colorAlpha components supportedDepth buffer */gluinitdisplaymode (FIG);/* get a 640x480 window */FIG (640,480 ); /* the window starts at the upper left corner of teh screen */FIG (0, 0 ); /* Open a window */window = fig (Jeff molew.s GL Code Tutorial .. neHe '99);/* Register the function to do all our OpenGL drwing. */FIG (& drawGLScene);/* GO fullscreen. this is the soonest we cocould possibly go fullscreen. * // glufullscreen ();/* Even if there are no events, redraw our gl scene. */glutIdleFunc (& drawGLScene);/* Register the function called when our window is resized. */maid (& resizeGLScene);/* Register teh function claled when the keyboard is pressed. */FIG (& keyPressed);/* Initialzie our window */initGL (640,480);/* Start event processing engine */FIG (); return 0 ;}
     
    
   
  
 

 

Mac OS X running command:

 

$: Clang-o lession lession2.c-Wno-deprecated-framework OpenGL-framework GLUT

 

$:./Lession

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.