openGL的第一课
1 #include <stdio.h> 2 #include <GLTools.h> 3 4 #include <GLShaderManager.h> 5 6 7 8 #ifdef __APPLE__ 9 10 #include <GLUT/glut.h> 11 12 #else 13 14 #define FREEGLUT_STATIC 15 16 #include <GL/glut.h> 17 18 #endif 19 20 21 22 GLBatch triangleBatch; 23 24 GLShaderManager shaderManager; 25 26 27 28 void changeSize( int W, int H ) 29 30 { 31 32 glViewport(0, 0, W, H); 33 34 } 35 36 37 38 void SetupRc() 39 40 { 41 42 glClearColor(1.0f, 1.0f, 1.0f, 1.0f); 43 44 45 46 shaderManager.InitializeStockShaders(); 47 48 49 50 GLfloat vVerts[] = { -0.5f, 0.0f, 0.0f, 51 52 0.5f, 0.0f, 0.0f, 53 54 0.0f, 0.5f, 0.0f}; 55 56 57 58 triangleBatch.Begin(GL_TRIANGLES, 3); 59 60 triangleBatch.CopyVertexData3f(vVerts); 61 62 triangleBatch.End(); 63 64 } 65 66 67 68 void RenderScene() 69 70 { 71 72 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT ); 73 74 75 76 GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f }; 77 78 shaderManager.UseStockShader( GLT_SHADER_IDENTITY,vRed ); 79 80 triangleBatch.Draw(); 81 82 83 84 glutSwapBuffers(); 85 86 } 87 88 89 90 int main( int argc, char* argv[] ) 91 92 { 93 94 //设置当前工作目录 95 96 gltSetWorkingDirectory(argv[0]); 97 98 99 100 //初始化glut 101 102 glutInit(&argc, argv); 103 104 105 106 //设置显示模式 107 108 glutInitDisplayMode( GLUT_DEPTH|GLUT_RGBA|GLUT_STENCIL|GLUT_DOUBLE ); 109 110 111 112 //初始化窗口 113 114 glutInitWindowSize(800, 600); 115 116 117 118 //创建窗口,并且命名 119 120 glutCreateWindow( "Triangle" ); 121 122 123 124 125 126 glutReshapeFunc(changeSize); 127 128 129 130 glutDisplayFunc(RenderScene); 131 132 133 134 GLenum err = glewInit(); 135 136 if (err !=GLEW_OK ) 137 138 { 139 140 fprintf(stderr, "GLEW Error: %s\n",glewGetErrorString(err) ); 141 142 return 1; 143 144 } 145 146 147 148 SetupRc(); 149 150 151 152 //开启OpenGL主循环 153 154 glutMainLoop(); 155 156 157 158 159 160 return 0; 161 162 } 163 164
最近开始对OpenGL进行了一下恶补,不得不说,蓝宝书,真的是本好书!