Jump to content
  • Advertisement
Sign in to follow this  
lumx

OpenGL Problem binding normals to vertex

This topic is 1424 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello to all,

 

I have started learnign openGL. It is frustrating sometimes but it is fun and i learned a lot. I have commed acros a problem that i have hard time figuring out what to do.

 

I am loading OBJ objects with tinyobjloader. It works and i can bind correctlly vertexes and indexes. I would like to bind normals so taht i will be able to start working with lights and learn them  and shaders. I can bind it but if i bind the normals i get nothing on screen. I will post the code below.

 

Thanks for help.

 

Binding data code

void initData(){
	GLenum errorCode = 0;
 
	// Copy data to GPU
	// Vertex
	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 \n", 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); <====== this is where the normal buffer is commented
	//glBindBuffer(GL_ARRAY_BUFFER, 0);
	//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
	glBindVertexArray(0);
 
	uniform_mvp = glGetUniformLocation(shader_program, "MVP");
	uniform_lightPosition = glGetUniformLocation(shader_program, "lightPosition");
 
	errorCode = glGetError();
	if (errorCode != 0)
	{
		fprintf(stderr, "Error data: %s, code %d\n", glewGetErrorString(errorCode), errorCode);
	}
}

Render Code

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, 1, glm::value_ptr(glm::vec4(100.0f, 10.0f, 0.0f, 0.0f) * cam.calculateMVP()));
	//glUniform3fv(uniform_lightPosition, 1, glm::value_ptr((glm::vec4(cam.cameraPosition, 0.0f) - glm::vec4(cam.cameraPosition, 0.0f)) * cam.calculateMVP()));
 
	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\n", 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

#version 330
 
uniform mat4 MVP;
uniform vec3 lightPosition;
 
vec3 lightIntensity = vec3(1.0f, 1.0f, 1.0f);
vec3 diffuseColor = vec3(1.0f, 1.0f, 1.0f);
 
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 normals;
 
smooth out vec3 Color;
 
void main() {
 
	gl_Position = MVP * vec4(position, 1.0);
	// wrong i guess
	float cosAngIncidence = dot(normals,  lightPosition);
	cosAngIncidence = clamp(cosAngIncidence, 0, 1);
 
	Color = lightIntensity * diffuseColor * cosAngIncidence;
}

The problems that i have when i render is that thw whole object is iluminated when i turn the camea. I know untill im sure i have fixed normals i cant do much.

Please look at the pictures that i have posted at opengl forum since i am not allowed to use image extension.

http://www.opengl.org/discussion_boards/showthread.php/184836-After-binding-the-normal-buffer-OpenGL-does-not-draw-anymore?p=1261845#post1261845

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!