我正在尝试在一个立方体对象上实现环境+漫射照明。我有2个多维数据集(光源和多维数据集对象)。我的环境光正常工作,但是当我添加漫反射时,立方体对象停止一起显示。这是我的代码(部分省略了长度):
着色器:
const char* vertex_shader =
"#version 150\n"
"in vec3 vp;"
"in vec3 aNormal;"
"out vec3 normal;"
"out vec3 fragPos;"
"uniform mat4 model;"
"uniform mat4 view;"
"uniform mat4 projection;"
"void main() {"
" fragPos = vec3(model * vec4(vp, 1.0));"
" normal = aNormal;"
" gl_Position = projection * view * model * vec4(vp, 1.0);"
"}";
const char* fragment_shader =
"#version 150\n"
"out vec4 frag_colour;"
"in vec3 normal;"
"in vec3 fragPos;"
"uniform vec3 objColor;"
"uniform vec3 lightColor;"
"uniform vec3 lightPos;"
"void main() {"
" //diffuse"
" vec3 norm = normalize(normal);"
" vec3 lightDir = normalize(lightPos - fragPos);"
" float diff = max(dot(norm, lightDir), 0.0);"
" vec3 diffuse = diff * lightColor;"
" //ambient"
" float ambientVal = 0.1;"
" vec3 ambient = ambientVal * lightColor;"
" vec3 result = (ambient + diffuse) * objColor;"
" frag_colour = vec4(1.0,1.0,1.0, 1.0);"
"}";
主要功能:
int main() {
/... setup
/////////////////////////////////////////////////
float points[] = {
/... position and normal vectors
};
//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); // position vectors
glBindBuffer(GL_ARRAY_BUFFER, vbo);//
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), NULL);//
glEnableVertexAttribArray(1); // normal vectors
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
//vao for light
/... drawing light source
//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]);
//glUniform3f(glGetUniformLocation(shader_program, "lightPos"), lightPos.x, lightPos.y, lightPos.z);
glUniform3fv(glGetUniformLocation(shader_program, "lightPos"), 1, &lightPos[0]);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 36); // draw w/ current in-use shader
//activate LIGHT SOURCE shader and set values
/... same for light source
// 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;
}
我相信这是因为多维数据集对象着色器未编译,但我不确定。任何帮助将不胜感激!