// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind glBindBuffer(GL_ARRAY_BUFFER, 0);
// remember: do NOT unbind the EBO while a VAO is active as the bound element buffer object IS stored in the VAO; keep the EBO bound. //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other // VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary. glBindVertexArray(0);
std::string line; std::stringstream ss[2]; ShaderType type = ShaderType::NONE;
while (getline(stream, line)) {//string头文件中的getline方法 if (line.find("#shader") != std::string::npos) { //std::string::npos,表示字符串末尾(无效字符串) if (line.find("vertex") != std::string::npos) { // set mode to vertex type = ShaderType::VERTEX; } elseif (line.find("fragment") != std::string::npos) { // set mode to fragment type = ShaderType::FRAGMENT; } } else { //把代码添加到vs或fs的字符串流中 ss[(int)type] << line << "\n"; } }
/* Create a windowed mode window and its OpenGL context */ window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL); if (!window) { glfwTerminate(); return-1; } /* Make the window's context current */ glfwMakeContextCurrent(window); glfwSwapInterval(1);
if (glewInit() != GLEW_OK) std::cout << "GlewInit fail!" << std::endl;
//解除绑定 //glUseProgram(0); glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); /* Loop until the user closes the window */ while (!glfwWindowShouldClose(window)) { /* Render here */
if (r > 1.0f) increment = -0.05f; elseif (r < 0.0f) increment = 0.05f; r += increment; /* Swap front and back buffers */ glfwSwapBuffers(window); /* Poll for and process events */ glfwPollEvents(); } //glDeleteProgram(shader); glfwTerminate(); return0; }
std::string line; std::stringstream ss[2]; ShaderType type = ShaderType::NONE;
while (getline(stream, line)) {//string头文件中的getline方法 if (line.find("#shader") != std::string::npos) { //std::string::npos,表示字符串末尾(无效字符串) if (line.find("vertex") != std::string::npos) { // set mode to vertex type = ShaderType::VERTEX; } elseif (line.find("fragment") != std::string::npos) { // set mode to fragment type = ShaderType::FRAGMENT; } } else { //把代码添加到vs或fs的字符串流中 ss[(int)type] << line << "\n"; } }
glDrawArrays(GL_TRIANGLES,0,3);//我们没有index buffer,可以这样做 //Mode,First(starting index),Count(number of indices)
另一种方式是
1 2 3 4 5
glDrawElements(GL_TRIANGLS,3,GL_UNSIGNED_INT,indices)//和idnex buffer一起使用 //Mode //count //type:Must be one of GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, or GL_UNSIGNED_INT //indices
voidglShaderSource(GLuint , shader GLsizei , count const GLchar **, string const GLint *length); //shader //Specifies the handle of the shader object whose source code is to be replaced.
//count //Specifies the number of elements in the string and length arrays.字符串的数量
//string //Specifies an array of pointers to strings containing the source code to be loaded into the shader. //需要提供一个双指针
//length //Specifies an array of string lengths. //null表示以null终止
/* Loop until the user closes the window */ while (!glfwWindowShouldClose(window)) { /* Render here */ glDrawArrays(GL_TRIANGLES, 0, 3); /* Swap front and back buffers */ glfwSwapBuffers(window); /* Poll for and process events */ glfwPollEvents(); } glDeleteShader(shader);//清理着色器 glfwTerminate(); return0; }
for draw in renderPass: for primitive in draw: for vertex in primitive: execute_vertex_shader(vertex) if primitive not culled: for fragment in primitive: execute_fragment_shader(fragment)
# Pass one for draw in renderPass: for primitive in draw: for vertex in primitive: execute_vertex_shader(vertex) if primitive not culled: append_tile_list(primitive) # Pass two for tile in renderPass: for primitive in tile: for fragment in primitive: execute_fragment_shader(fragment)