我正在尝试解决Anton的OpenGL 4教程中的实验。在第一章的实验中,它要求创建第二个VAO来绘制2个形状而不是一个但我不知道如何做到这一点,如何同时显示第二个?
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
int main () {
// start GL context and O/S window using the GLFW helper library
if (!glfwInit ()) {
fprintf (stderr, "ERROR: could not start GLFW3\n");
return 1;
}
GLFWwindow* window = glfwCreateWindow (640, 480, "Hello Triangle", NULL, NULL);
if (!window) {
fprintf (stderr, "ERROR: could not open window with GLFW3\n");
glfwTerminate();
return 1;
}
glfwMakeContextCurrent (window);
// start GLEW extension handler
glewExperimental = GL_TRUE;
glewInit ();
// get version info
const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
const GLubyte* version = glGetString (GL_VERSION); // version as a string
printf ("Renderer: %s\n", renderer);
printf ("OpenGL version supported %s\n", version);
// tell GL to only draw onto a pixel if the shape is closer to the viewer
glEnable (GL_DEPTH_TEST); // enable depth-testing
glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"
GLfloat points[] = {
0.0f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f
};
GLuint vbo = 0;
glGenBuffers (1, &vbo);
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glBufferData (GL_ARRAY_BUFFER, 18 * sizeof (float), points, GL_STATIC_DRAW);
GLuint vao = 0;
glGenVertexArrays (1, &vao);
glBindVertexArray (vao);
glEnableVertexAttribArray (0);
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
//second vao
GLuint vao_two = 0;
glGenVertexArrays(1, &vao_two);
glBindVertexArray(vao_two);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
const char* vertex_shader =
"#version 400\n"
"in vec3 vp;"
"in vec3 vp2;"
"void main() {"
" gl_Position = vec4(vp.x, vp.y + 0.2, vp.z, 1.0);"
" gl_Position2 = vec4(vp2.x, vp2.y - 0.9, vp2.z, 1.0);"
"}";
const char* fragment_shader =
"#version 400\n"
"out vec4 frag_colour;"
"void main () {"
" frag_colour = vec4 (0.0, 1.0, 0.0, 1.0);"
"}";
GLuint vs = glCreateShader (GL_VERTEX_SHADER);
glShaderSource (vs, 1, &vertex_shader, NULL);
glCompileShader (vs);
GLuint fs = glCreateShader (GL_FRAGMENT_SHADER);
glShaderSource (fs, 1, &fragment_shader, NULL);
glCompileShader (fs);
GLuint shader_programme = glCreateProgram();
glAttachShader (shader_programme, fs);
glAttachShader (shader_programme, vs);
glLinkProgram(shader_programme);
glClearColor(0.6f, 0.6f, 0.6f, 0.0f);
while (!glfwWindowShouldClose (window)) {
// wipe the drawing surface clear
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram (shader_programme);
glBindVertexArray (vao);
// draw points 0-3 from the currently bound VAO with current in-use shader
glDrawArrays (GL_TRIANGLES, 0, 6);
glBindVertexArray (vao_two);
glDrawArrays (GL_TRIANGLES, 0, 6);
// update other events like input handling
glfwPollEvents ();
// put the stuff we've been drawing onto the display
glfwSwapBuffers (window);
}
// close GL context and any other GLFW resources
glfwTerminate();
return 0;
}
答案 0 :(得分:1)
实际上,您正在绘制两种不同的形状,但是,因为在两个顶点属性对象(vao和vao_two)中使用相同的顶点缓冲对象,它们具有相同的坐标并且它们重叠。 尝试制作具有不同坐标的2个GLfloat点阵列,并将每个点归属于不同的顶点缓冲区对象,然后将每个顶点缓冲区对象归属到不同的顶点属性对象,然后您的形状将是不同的。
答案 1 :(得分:0)
首先绑定缓冲区,然后全部绘制它们:
glBindVertexArray(vao);
glBindVertexArray(vao2);
glDrawArrays(GL_TRIANGLES, 0, 6);
或者,您可以先绘制第一个形状,然后绘制第二个形状:
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(vao2);
glDrawArrays(GL_TRIANGLES, 3, 3);