OpenGL太可怕了。。。必需得把学的记下来,不然绝壁忘。
首先贴出代码,然后分析创建一个OpenGL程序都需要什么
1 #include <cstdio> 2 #include <cstdlib> 3 4 #include <GL/glew.h> 5 6 #include <GLFW/glfw3.h> 7 8 #include <glm/glm.hpp> 9 #include <glm/gtc/matrix_transform.hpp> 10 using namespace glm; 11 using namespace std; 12 13 #include <shader.hpp> 14 #include <shader.cpp> 15 16 static const GLfloat g_vertex_buffer_data[] = { 17 -1.0f,-1.0f,-1.0f, // triangle 1 : begin 18 -1.0f,-1.0f, 1.0f, 19 -1.0f, 1.0f, 1.0f, // triangle 1 : end 20 1.0f, 1.0f,-1.0f, // triangle 2 : begin 21 -1.0f,-1.0f,-1.0f, 22 -1.0f, 1.0f,-1.0f, // triangle 2 : end 23 1.0f,-1.0f, 1.0f, 24 -1.0f,-1.0f,-1.0f, 25 1.0f,-1.0f,-1.0f, 26 1.0f, 1.0f,-1.0f, 27 1.0f,-1.0f,-1.0f, 28 -1.0f,-1.0f,-1.0f, 29 -1.0f,-1.0f,-1.0f, 30 -1.0f, 1.0f, 1.0f, 31 -1.0f, 1.0f,-1.0f, 32 1.0f,-1.0f, 1.0f, 33 -1.0f,-1.0f, 1.0f, 34 -1.0f,-1.0f,-1.0f, 35 -1.0f, 1.0f, 1.0f, 36 -1.0f,-1.0f, 1.0f, 37 1.0f,-1.0f, 1.0f, 38 1.0f, 1.0f, 1.0f, 39 1.0f,-1.0f,-1.0f, 40 1.0f, 1.0f,-1.0f, 41 1.0f,-1.0f,-1.0f, 42 1.0f, 1.0f, 1.0f, 43 1.0f,-1.0f, 1.0f, 44 1.0f, 1.0f, 1.0f, 45 1.0f, 1.0f,-1.0f, 46 -1.0f, 1.0f,-1.0f, 47 1.0f, 1.0f, 1.0f, 48 -1.0f, 1.0f,-1.0f, 49 -1.0f, 1.0f, 1.0f, 50 1.0f, 1.0f, 1.0f, 51 -1.0f, 1.0f, 1.0f, 52 1.0f,-1.0f, 1.0f 53 }; 54 55 //每个顶点一个颜色 56 static const GLfloat g_color_buffer_data[] = { 57 0.583f, 0.771f, 0.014f, 58 0.609f, 0.115f, 0.436f, 59 0.327f, 0.483f, 0.844f, 60 0.822f, 0.569f, 0.201f, 61 0.435f, 0.602f, 0.223f, 62 0.310f, 0.747f, 0.185f, 63 0.597f, 0.770f, 0.761f, 64 0.559f, 0.436f, 0.730f, 65 0.359f, 0.583f, 0.152f, 66 0.483f, 0.596f, 0.789f, 67 0.559f, 0.861f, 0.639f, 68 0.195f, 0.548f, 0.859f, 69 0.014f, 0.184f, 0.576f, 70 0.771f, 0.328f, 0.970f, 71 0.406f, 0.615f, 0.116f, 72 0.676f, 0.977f, 0.133f, 73 0.971f, 0.572f, 0.833f, 74 0.140f, 0.616f, 0.489f, 75 0.997f, 0.513f, 0.064f, 76 0.945f, 0.719f, 0.592f, 77 0.543f, 0.021f, 0.978f, 78 0.279f, 0.317f, 0.505f, 79 0.167f, 0.620f, 0.077f, 80 0.347f, 0.857f, 0.137f, 81 0.055f, 0.953f, 0.042f, 82 0.714f, 0.505f, 0.345f, 83 0.783f, 0.290f, 0.734f, 84 0.722f, 0.645f, 0.174f, 85 0.302f, 0.455f, 0.848f, 86 0.225f, 0.587f, 0.040f, 87 0.517f, 0.713f, 0.338f, 88 0.053f, 0.959f, 0.120f, 89 0.393f, 0.621f, 0.362f, 90 0.673f, 0.211f, 0.457f, 91 0.820f, 0.883f, 0.371f, 92 0.982f, 0.099f, 0.879f 93 }; 94 95 int main() { 96 if(!glfwInit()) { 97 fprintf(stderr, "Failed To init OpenGL\n"); 98 } 99 100 // We want OpenGL 3.3 101 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 102 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); 103 // To make MacOS happy; should not be needed 104 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); 105 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don‘t want the old OpenGL 106 107 // Open a window and create its OpenGL context 108 // (In the accompanying source code, this variable is global) 109 GLFWwindow* window; 110 window = glfwCreateWindow(800, 600, "Tutorial 01", NULL, NULL); 111 if( window == NULL ){ 112 fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" ); 113 glfwTerminate(); 114 return -1; 115 } 116 glfwMakeContextCurrent(window); // Initialize GLEW 117 glewExperimental = true; // Needed in core profile 118 119 //z-buffer 120 glEnable(GL_DEPTH_TEST); 121 glDepthFunc(GL_LEFT); 122 123 if (glewInit() != GLEW_OK) { 124 fprintf(stderr, "Failed to initialize GLEW\n"); 125 return -1; 126 } 127 128 //背景颜色 129 glClearColor(0.0f, 0.0f, 0.4f, 0.0f); 130 131 //shader 132 GLuint programID = LoadShaders( "/Users/jeff/IDEProjects/xcode_projects/openGL/openGL/vertex.shader", "/Users/jeff/IDEProjects/xcode_projects/openGL/openGL/fragment.shader" ); 133 134 GLuint VertexArrayID; 135 glGenVertexArrays(1, &VertexArrayID); 136 glBindVertexArray(VertexArrayID); 137 138 //vertex buffer 139 GLuint vertexbuffer; 140 glGenBuffers(1, &vertexbuffer); 141 glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); 142 glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW); 143 144 //color buffer 145 GLuint colorbuffer; 146 glGenBuffers(1, &colorbuffer); 147 glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); 148 glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW); 149 150 mat4 Projection = perspective(radians(70.0f), (float)4/3, 0.1f, 1000.f); 151 152 mat4 View = lookAt(vec3(3, 4, -5), vec3(0,0,0), vec3(0,1,0)); 153 154 mat4 Model = mat4(1.0f); 155 156 //模型到投影转换 157 mat4 mvp = Projection * View * Model; 158 159 GLuint MatrixID = glGetUniformLocation(programID, "mvp"); 160 161 while(glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS && !glfwWindowShouldClose(window)) { 162 //每次开始时清空画布 163 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 164 165 //使用shader 166 glUseProgram(programID); 167 168 //把变换矩阵送进shader 169 glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &mvp[0][0]); 170 171 //画三角形 172 glEnableVertexAttribArray(0); 173 glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); 174 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); 175 176 //三角形颜色 177 glEnableVertexAttribArray(1); 178 glBindBuffer(GL_ARRAY_BUFFER, colorbuffer); 179 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); 180 181 //draw call 182 glDrawArrays(GL_TRIANGLES, 0, 12 * 3); 183 184 glDisableVertexAttribArray(0); 185 glDisableVertexAttribArray(1); 186 187 glfwSwapBuffers(window); 188 glfwPollEvents(); 189 } 190 glfwTerminate(); 191 return 0; 192 }
Code
时间: 2024-12-27 02:58:49