Jump to content
  • Advertisement
Sign in to follow this  
Dries Lamberechts

OpenGL Rendering multiple 2D objects

This topic is 1416 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

I'm trying to render multiple objects in my OpenGL based scene. I got one working with the following code:

This first piece of code is in the Initialize part of the object so it's only run once.

// Create Vertex Array Object
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

glGenBuffers(1, &vbo); // Generate 1 buffer
glBindBuffer(GL_ARRAY_BUFFER, vbo);	// Set vbo as active
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);	// load vertex data to vbo

GLuint ebo;
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);

m_Program.CompileShaders("Assets/Shaders/Colorshading.vert", "Assets/Shaders/ColorShading.frag");
m_Program.LinkShaders();
glUseProgram(m_Program.GetID());

glBindFragDataLocation(m_Program.GetID(), 0, "outColor");

GLint posAttrib = glGetAttribLocation(m_Program.GetID(), "position");
glEnableVertexAttribArray(posAttrib);
glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), 0);

GLint colAttrib = glGetAttribLocation(m_Program.GetID(), "color");
glEnableVertexAttribArray(colAttrib);
glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));

GLint texAttrib = glGetAttribLocation(m_Program.GetID(), "texcoord");
glEnableVertexAttribArray(texAttrib);
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));

GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);

int width, height;
unsigned char* image = SOIL_load_image("Sample.png", &width, &height, 0, SOIL_LOAD_RGB);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
SOIL_free_image_data(image);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

After that, this is in the Draw method of the object which is run every tick:

// Render a test background
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);

// Bind the VAO
glBindVertexArray(vao);

//glUseProgram(m_Program.GetID());
// Draw the triangle !
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // Starting from vertex 0; 3 vertices total -> 1 triangle

// CLear VAO binding
glBindVertexArray(0);

Now, I've done this in a second object, which is also drawn second because of the way the engine works and if I start the game I can only see the second object. I have no idea what I'm doing wrong. Am I binding the vbo for the object wrong? Am I doing something wrong in the drawing?

 

Also, a question paired to this one, how would I be able to change the layering of the seperate objects? Can I just change the z-value? That is what I have now but of course I can't see it.

Share this post


Link to post
Share on other sites
Advertisement
If Draw clears the screen every time, and you're calling it for each object, of course you can only see the second one. You cleared the rendering of the first one when you drew the second.

Share this post


Link to post
Share on other sites

You need to bind the correct texture(s) before your draw call just like you bind the VAO and VBO.

Share this post


Link to post
Share on other sites
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!