首页 > 代码库 > neHe OpenGL lession 3
neHe OpenGL lession 3
#include <GLUT/GLUT.h> #include <OpenGL/OpenGL.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> /* ASCII code for the escape key. */ #define ESCAPE 27 /* The number of our GLUT window */ int window; /* A general OpenGL initializtion function. Sets all of teh 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 Clearting Of the depth buffer glDepthFunc(GL_LESS); // The type of depth test to do glEnable(GL_DEPTH_TEST); // Enable Depth Testing. glShadeModel(GL_SMOOTH); // Enable smooth color shading glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Reset the projection matrix // @see: http://hi.baidu.com/korndorben/item/76c5020453f18212cc34ea90 gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 100.0f); // Calcuate the aspect ratio of teh window 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 divide by zero if the window is too small height = 1; glViewport(0, 0, width, height); // Reset the current 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 function */ void drawGLScene() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the screen and // the dpeth buffer glLoadIdentity(); // Reset the view glTranslatef(-1.5f, 0.0f, -6.0f); // Move Left 1.5 units And into the screen 6.0 // draw a triangle (in smooth colring mode) glBegin(GL_POLYGON); // start drawing a polygon glColor3f(1.0f, 0.0f, 0.0f); // set the color to red glVertex3f(0.0f, 1.0f, 0.0f); // top glColor3f(0.0f, 1.0f, 0.0f); // Set the Color to green. glVertex3f(1.0f, -1.0f, 0.0f); // bottom right glColor3f(0.0f, 0.0f, 1.0f); // set the color to blue. glVertex3f(-1.0f, -1.0f, 0.0f); // bottom left glEnd(); // We're down with the polygon (smooth color interpolation); glTranslatef(3.0f, 0.0f, 0.0f); // draw a square (quadrilateral) glColor3f(0.5f, 0.5f, 1.0f); // set color to a blue shade. glBegin(GL_QUADS); glVertex3f(-1.0f, 1.0f, 0.0f); // Top left glVertex3f( 1.0f, 1.0f, 0.0f); // top right glVertex3f( 1.0f, -1.0f, 0.0f); // bottom right glVertex3f(-1.0f, -1.0f, 0.0f); // bottom left glEnd(); // Done with the polygon // we need ot swap the buffer to display our drawing. glutSwapBuffers(); } /* The function called whenever a key is pressed. */ void keyPressed(unsigned char key, int x, int y) { usleep(100); // sleep to avoid thrashing this procedure */ /* If escape is pressd, kill everything. */ if (key == ESCAPE) { /* shut down our window. */ glutDestroyWindow(window); /* exit the program...normal termination. */ exit(0); } } int main(int argc, char **argv) { 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 360 window */ glutInitWindowSize(640, 480); /* the window starts at the upper left corner of the screen. */ glutInitWindowPosition(0, 0); /* Open a window */ window = glutCreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe 'oo"); /* Register the function to do all our OpenGL drawing. */ glutDisplayFunc(&drawGLScene); /* Go fullscree. Tis is as soon as possible. */ //glutFullScreen(); /* Even if there are no events, redraw our gl scene. */ glutIdleFunc(&drawGLScene); /* Register the function called 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 procesing engine */ glutMainLoop(); return 1; }
Mac OS X 运行:
$: clang -o lession lession3.c -Wno-deprecated -framework OpenGL -framework GLUT
$: ./lession
结果:一个三角形。一个正方形。
neHe OpenGL lession 3
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。