我试图绘制两个立方体:一个代表光源,另一个代表物体。但是,我的代码仅显示黑屏,而我无法查明我要去哪里。
const char* vertex_shader =
"#version 150\n"
"in vec3 vp;"
"uniform mat4 model;"
"uniform mat4 view;"
"uniform mat4 projection;"
"void main() {"
" gl_Position = projection * view * model * vec4(vp, 1.0);"
"}";
const char* fragment_shader =
"#version 150\n"
"out vec4 frag_colour;"
"uniform vec3 objColor;"
"uniform vec3 lightColor;"
"void main() {"
" frag_colour = vec4(objColor * lightColor, 1.0);"
"}";
//second set of shaders for light source
const char* light_vertex_shader =
"#version 150\n"
"in vec3 vp;"
"uniform mat4 model;"
"uniform mat4 view;"
"uniform mat4 projection;"
"void main() {"
" gl_Position = projection * view * model * vec4(vp, 1.0);"
"}";
const char* light_fragment_shader =
"#version 150\n"
"out vec4 frag_colour;"
"void main() {"
" frag_colour = vec4(1.0);"
"}";
//window size
const int WIDTH = 640;
const int HEIGHT = 480;
// camera
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
float lastX = WIDTH / 2.0f;
float lastY = HEIGHT / 2.0f;
//position of light source
glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
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;
}
// uncomment these lines if on Apple OS X
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "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"
/////////////////////////////////////////////////
float points[] = {
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
};
//cube
GLuint vbo = 0;//
glGenBuffers(1, &vbo); //
glBindBuffer(GL_ARRAY_BUFFER, vbo);//
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);//
//vao for cube object
GLuint vao = 0;//
glGenVertexArrays(1, &vao); //
glBindVertexArray(vao); //
glEnableVertexAttribArray(0);//
glBindBuffer(GL_ARRAY_BUFFER, vbo);//
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), NULL);//
//vao for light
GLuint lightVao;//
glGenVertexArrays(1, &lightVao);//
glBindVertexArray(lightVao);//
glBindBuffer(GL_ARRAY_BUFFER, vbo);//
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);//
glEnableVertexAttribArray(0);//
//creating and compiling OBJECT shaders
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_program = glCreateProgram(); //creating joint OBJECT shader program
glAttachShader(shader_program, fs);
glAttachShader(shader_program, vs);
glLinkProgram(shader_program);
//creating and compiling LIGHT SOURCE shaders
GLuint vsLight = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vsLight, 1, &light_vertex_shader, NULL);
glCompileShader(vsLight);
GLuint fsLight = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fsLight, 1, &light_fragment_shader, NULL);
glCompileShader(fsLight);
GLuint light_shader_program = glCreateProgram(); //creating joint LIGHT SOURCE shader program
glAttachShader(light_shader_program, fsLight);
glAttachShader(light_shader_program, vsLight);
glLinkProgram(light_shader_program);
//starting loop
while(!glfwWindowShouldClose(window)) {
// wipe the drawing surface clear
glClearColor(0.1f,0.1f,0.1f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//activate OBJECT shader and set values
glUseProgram(shader_program);
glUniform3f(glGetUniformLocation(shader_program, "objColor"), 1.0f, 0.5f, 0.31f);
glUniform3f(glGetUniformLocation(shader_program, "lightColor"), 1.0f, 1.0f, 1.0f);
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)WIDTH / (float)HEIGHT, 0.1f, 100.0f);
glm::mat4 view = camera.GetViewMatrix();
glUniformMatrix4fv(glGetUniformLocation(shader_program,"projection"), 1, GL_FALSE, &projection[0][0]);
glUniformMatrix4fv(glGetUniformLocation(shader_program,"view"), 1, GL_FALSE, &view[0][0]);
glm::mat4 model = glm::mat4(1.0f);
glUniformMatrix4fv(glGetUniformLocation(shader_program,"model"), 1, GL_FALSE, &model[0][0]);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 36); // draw w/ current in-use shader
//activate LIGHT SOURCE shader and set values
glUseProgram(light_shader_program);
glUniformMatrix4fv(glGetUniformLocation(light_shader_program,"projection"), 1, GL_FALSE, &projection[0][0]);
glUniformMatrix4fv(glGetUniformLocation(light_shader_program,"view"), 1, GL_FALSE, &view[0][0]);
model = glm::mat4(1.0f);
model = glm::translate(model, lightPos);
model = glm:: scale(model, glm::vec3(0.2f));
glUniformMatrix4fv(glGetUniformLocation(light_shader_program,"model"), 1, GL_FALSE, &model[0][0]);
glBindVertexArray(lightVao);
// draw points 0-3 from the currently bound VAO with current in-use shader
glDrawArrays(GL_TRIANGLES, 0, 36);
// 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;
}
因此,到目前为止,我已经尝试过再次阅读本教程,以了解我在哪里出错,但是我不确定。 任何帮助将不胜感激!
编辑:编辑以反映建议的更改(x2)