Hello,
I am trying to learn lights in Opengl. It seems that i have a problem when i and the third buffer for normals to VAO. When i enable the normal buffer (glBindBuffer(GL_ARRAY_BUFFER, normals_buffer) I dont get anything in rendered even if leave the code as it is.
I have checked the number of vertexes and normals. they are the same. i did export other obj files from blender with normals I still get the same problem.
The good thing is that im learning even thought it can be frustrating sometimes
Binding data code
size_t normals_buffer_size = 0;
size_t vertex_buffer_size = 0;
size_t index_buffer_size = 0;
for (size_t i = 0; i < shapes.size(); i++) {
vertex_buffer_size += sizeof(float)* shapes[i].mesh.positions.size();
normals_buffer_size += sizeof(float)* shapes[i].mesh.normals.size();
index_buffer_size += sizeof(unsigned int)* shapes[i].mesh.indices.size();
}
glGenBuffers(1, &vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, vertex_buffer_size, NULL, GL_STATIC_DRAW);
vertex_buffer_size = 0;
for (size_t i = 0; i < shapes.size(); i++) {
glBufferSubData(GL_ARRAY_BUFFER, vertex_buffer_size, sizeof(float)* shapes[i].mesh.positions.size(), &shapes[i].mesh.positions[0]);
vertex_buffer_size += sizeof(float)* shapes[i].mesh.positions.size();
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Index
/*size_t index_buffer_size = 0;
for (size_t i = 0; i < shapes.size(); i++) {
index_buffer_size += sizeof(unsigned int)* shapes[i].mesh.indices.size();
}*/
glGenBuffers(1, &index_buffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, index_buffer_size, NULL, GL_STATIC_DRAW);
index_buffer_size = 0;
for (size_t i = 0; i < shapes.size(); i++) {
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, index_buffer_size, sizeof(unsigned int)* shapes[i].mesh.indices.size(), &shapes[i].mesh.indices[0]);
index_buffer_size += sizeof(unsigned int)* shapes[i].mesh.indices.size();
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// Normals
glGenBuffers(1, &normals_buffer);
glBindBuffer(GL_ARRAY_BUFFER, normals_buffer);
glBufferData(GL_ARRAY_BUFFER, normals_buffer_size, NULL, GL_STATIC_DRAW);
normals_buffer_size = 0;
for (size_t i = 0; i < shapes.size(); i++) {
glBufferSubData(GL_ARRAY_BUFFER, normals_buffer_size, sizeof(float)* shapes[i].mesh.normals.size(), &shapes[i].mesh.normals[0]);
normals_buffer_size += sizeof(float)* shapes[i].mesh.normals.size();
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
//fprintf(stderr, "normals_buffer_size %d vertex_buffer_size %d index_buffer_size %d
", normals_buffer_size, vertex_buffer_size, index_buffer_size);
// draw multiple objects with one draw call
glGenVertexArrays(1, &vertex_array_object);
glBindVertexArray(vertex_array_object);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
//glBindBuffer(GL_ARRAY_BUFFER, normals_buffer); <===== if I un-comment this i wont get anything rendered
//glBindBuffer(GL_ARRAY_BUFFER, 0);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
//MVP matrix
uniform_mvp = glGetUniformLocation(shader_program, "MVP");
//light position
uniform_lightPosition = glGetUniformLocation(shader_program, "lightPosition");
errorCode = glGetError();
if (errorCode != 0)
{
fprintf(stderr, "Error data: %s, code %d
", glewGetErrorString(errorCode), errorCode);
}
the draw function
void display(){
GLenum errorCode = 0;
// Clear the screen to black
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Use our shader
glUseProgram(shader_program);
// Send our transformation to the currently bound shader, in the "MVP" uniform
glUniformMatrix4fv(uniform_mvp, 1, GL_FALSE, glm::value_ptr(cam.calculateMVP()));
glUniform3fv(uniform_lightPosition, 3, glm::value_ptr(glm::vec4(cam.calculateMVP() * glm::vec4(cam.cameraPosition,1.0f))) );
glBindVertexArray(vertex_array_object);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
size_t vertex_buffer_size = 0;
size_t index_buffer_size = 0;
size_t normals_buffer_size = 0;
for (size_t i = 0; i < shapes.size(); i++) {
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)vertex_buffer_size);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)normals_buffer_size);
glDrawElements(GL_TRIANGLES, shapes[i].mesh.indices.size(), GL_UNSIGNED_INT, (void*)index_buffer_size);
vertex_buffer_size += sizeof(float) * shapes[i].mesh.positions.size();
index_buffer_size += sizeof(unsigned int) *shapes[i].mesh.indices.size();
normals_buffer_size += sizeof(float) * shapes[i].mesh.normals.size();
if (errorCode != 0)
{
fprintf(stderr, "Error rendering shape[%d].name = %s. Error name: %s. Error code code %d
", i, shapes[i].name.c_str(), glewGetErrorString(errorCode), errorCode);
}
}
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindVertexArray(0);
glUseProgram(0);
// Swap buffers
SDL_GL_SwapWindow(window);
}
Vertex shader code. I deleted few comments and few lines oc code that i am usign jsut to make something render so i might have few errors in this code. Sorry
#version 330
uniform mat4 MVP;
uniform vec3 lightPosition;
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 normals;
vec3 lightIntensity = vec3(1.0f, 1.0f, 1.0f); // for now this value for testing
vec3 diffuseColor = vec3(0.0f, 0.9f, 0.9f); // for now this value for testing
vec3 direction;
out vec3 Color;
void main() {
gl_Position = MVP * vec4(position, 1.0);
vec3 normalsMVP = vec3(MVP * vec4(normals,1.0f));
float cosAngIncidence = dot(normalsMVP, lightPos);
//cosAngIncidence = clamp(cosAngIncidence, 0, 1);
cosAngIncidence = clamp(cosAngIncidence, 0.1, 1);
Color = vec3(1.0f, 1.0f, 1.0f) *lightIntensity * diffuseColor * cosAngIncidence;
//Color = lightIntensity * diffuseColor * cosAngIncidence;
//Color = vec3(1.0f, 1.0f, 1.0f) * cosAngIncidence;
//Color = vec3(1.0f, 1.0f, 1.0f);
}
Edit:: I did change the vertex shared a bit and i am getting these results. I am not sure why the walls are transparent before going black. Thanks.
New shader code
#version 330
uniform mat4 MVP;
uniform vec3 lightPosition;
vec3 lightIntensity = vec3(1.0f, 1.0f, 1.0f);
vec3 lightPos = vec3(10.0f, 120.0f, 100.0f);
vec3 diffuseColor = vec3(0.0f, 0.9f, 0.9f);
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 normals;
vec3 direction;
out vec3 Color;
void main() {
gl_Position = MVP * vec4(position, 1.0);
vec4 normals = MVP * vec4(lightPos, 1.0);
direction = vec3(gl_Position - normals);
float cosAngIncidence = dot(direction, lightPos);
cosAngIncidence = clamp(cosAngIncidence, 0, 1);
//Color = vec3(1.0f, 1.0f, 1.0f) * lightIntensity * diffuseColor;
Color = vec3(1.0f, 1.0f, 1.0f) *lightIntensity * diffuseColor * cosAngIncidence;
//Color = lightIntensity * diffuseColor * cosAngIncidence;
//Color = vec3(1.0f, 1.0f, 1.0f) * cosAngIncidence;
}
Fixed the issue with rendering. I had culling enabled the wrong way.
[ATTACH=CONFIG]751[/ATTACH]
No im have to fix this problem with binding normals and i believe I am set for light tutorials and experiments