How to have multiple independently moving objects in OpenGL - c++

I am trying to do some simple graphics processing with OpenGL, but I am having trouble having 2 objects where one of them is static and the other moves. The objects are a simple cube and a square that represents the floor. I want the cube to move down until it touches the floor (as if it were moving). I can render the falling cube on its own, and I can get the floor on its own. But when I want to have them both in the same scene I am having issues as they either both fall down (the cubes behaviour), or both stay in the same place (the floors behaviour). Which one of these two options occurs is due to whether I push and pop my model matrix - when I do pop and push, they stay static, when I don't, they fall down (I guess this makes sense as I draw the floor then the cube.
This is my code in the draw phase of the program:
//Clear the screen to the colour specified earlier, as well as the depth
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(programID); // Use our shader
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
//RENDER THE FLOOR
pushMat(Model); //PUSH - WHEN ACTIVE, CUBE AND FLOOR FALL. WHEN COMMENTED OUT, BOTH FLOOR AND CUBE ARE STATIC
MVP = Projection * View * Model; //These are all matrices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, floorVertexBuffer);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
//FLOOR COLOURS
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer2);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_TRIANGLES, 0, 2*3); //2 triangles, of 3 vertices each
Model = popMat(); //POP - WHEN ACTIVE, CUBE AND FLOOR FALL. WHEN COMMENTED OUT, BOTH FLOOR AND CUBE ARE STATIC
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
//RENDER THE CUBE
pushMat(Model); //PUSH - WHEN ACTIVE, CUBE AND FLOOR FALL. WHEN COMMENTED OUT, BOTH FLOOR AND CUBE ARE STATIC
Model = translate(Model, vec3(0.0f, deltaY, 0.0f)); //deltaY is the change in the y position of the cube, it is calculated earlier in this draw loop
MVP = Projection * View * Model;
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, cubeVertexBuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
//CUBE COLOURS
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
//DRAW CUBE
glDrawArrays(GL_TRIANGLES, 0, 12 * 3); //12 triangle, of 3 vertices each
Model = popMat(); //FINAL POP - WHEN ACTIVE, CUBE AND FLOOR FALL. WHEN COMMENTED OUT, BOTH FLOOR AND CUBE ARE STATIC
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
Code for my push and pop functions:
stack<mat4> modelViewStack; //This is initialised with the identity matrix in the main function
void pushMat(mat4 m)
{
modelViewStack.push(m);
}
mat4 popMat()
{
mat4 temp = modelViewStack.top();
modelViewStack.pop();
return temp;
}
Any clues as to how I get it so the floor stays in one place and the cube moves down? I'm happy to help explain any code, provide more of my code, or answer any questions in general. Thanks for any help.

glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
This call needs to appear before each call to glDrawArrays. Right now, it's only being called once, before you render everything, which means both objects are receiving the same MVP matrix.
Also, I would reconsider the logic of implementing this logic using a Matrix Stack. That was how it worked in Legacy OpenGL (because everything depended on Global State, and some other reasons) but it's not obvious that this is the best solution today, when we can simply associate Matrices with individual objects and simply bind them as needed.

Related

Rendering each object into the space independently and rendering 2D scene over 3D scene

Spoiler : this problem is long so please be patient and read until the end.
Problem number 1:
I'm making a pool project and I want to render each object separately because of the independent movement of each object.
As for right now, i'm stacking it into one buffer and rendering it in one chunk.
(side note : Each Ball object and Table object includes in it vertices and normals.)
Problem number 2:
I'm trying to render 2D bar (that represents striking power) and it won't show up. I'm calling it after im rendering the 3d scene
I've watched this video a few times and didn't understand what I did wrong.
What i'm doing right now (after stacking the objects into vertexbuffer and normalbuffer): (original source)
do {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(programID);
glUniform3f(LightID, 0, 6, 4);
computeMatricesFromInputs();
ProjectionMatrix = getProjectionMatrix();
ModelMatrix = glm::mat4(1.0);
MVP = ProjectionMatrix * viewMatrix * ModelMatrix;
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
glUniformMatrix4fv(ModelMatrixID, 1, GL_FALSE, &ModelMatrix[0][0]);
glUniformMatrix4fv(ViewMatrixID, 1, GL_FALSE, &viewMatrix[0][0]);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
glEnableVertexAttribArray(2);
glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
glVertexAttribPointer(
2, // attribute
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
DrawHUD();
// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
} // Check if the ESC key was pressed or the window was closed
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0);
What I want it to be:
do{
for (int i = 0; i < BALL_NUMBER; i++)
{
balls[i].draw();
}
} // Check if the ESC key was pressed or the window was closed
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0);
what should draw contain and how do I make it work?
Your top-level loop should:
Clear the color and depth buffers with glClear
Set scene-wide uniforms such as the light ID, projection and view matrix
Render each ball separately: (ie the Ball::draw method)
Set the model matrix uniform to rotate and/or reposition the ball in world space. This might need recomputing the MVP uniform, depending on your shader code
(optionally) Identify the ball somehow (different color via uniform or bind a different texture)
Render the shared ball mesh (optionally bind the VAO for balls and then call glDrawArrays)
Render the HUD
Swap the buffer
All the setup code (loading meshes, creating array buffers and the VAO, setting up the uniform-attribute bindings) should be done up front.

OpenGL multiple draw calls with unique shaders gives blank screen

I am trying to render two different vertex collections on top of one another. Right now, my main loop renders one correctly when it's by itself, and the other correctly when it's by itself, but when I call both of my draw functions, I see a blank window. Why might this be happening?
The first draw call uses one shader, while the second draw call uses a different one. I don't clear the screen in between.
If it makes the code more clear, my shader programs are stored as class variables, as are the texture IDs after they're loaded elsehwere in my program.
This is my main loop:
while (true)
{
// Clear the colorbuffer
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawModel1(); // This works when drawModel2() is commented out
drawModel2(); // This works when drawModel1() is commented out
// Unbind buffer
glBindBuffer(GL_ARRAY_BUFFER, 0);
// Swap the screen buffers
glfwSwapBuffers(_window);
}
My drawModel1() function renders points:
void drawModel1()
{
// Use the image shader
_img_shader.use();
// Feed the position data to the shader
glBindBuffer(GL_ARRAY_BUFFER, _img_pos_VBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
// Feed the color data to the shader
glBindBuffer(GL_ARRAY_BUFFER, _img_color_VBO);
glVertexAttribPointer(1, 3, GL_UNSIGNED_BYTE, GL_TRUE, 3 * sizeof(GLubyte), (GLvoid*)0);
glEnableVertexAttribArray(1);
// Set the projection matrix in the vertex shader
GLuint projM = glGetUniformLocation(_img_shader.program(), "proj");
glm::mat4 proj = _ndc * _persMat;
glUniformMatrix4fv(projM, 1, GL_TRUE, glm::value_ptr(proj));
// Set the view matrix in the vertex shader
GLuint viewM = glGetUniformLocation(_img_shader.program(), "view");
glUniformMatrix4fv(viewM, 1, GL_TRUE, glm::value_ptr(_viewMat));
// Draw the points
glBindVertexArray(_img_VAO);
glDrawArrays(GL_POINTS, 0, _numImageVertices);
// Disable attributes
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
}
And my drawModel2() function renders indexed triangles:
void drawModel2()
{
_model_shader.use();
// Load the mesh texture
GLuint texID = _loaded_textures.at(mesh.tex_file());
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texID);
glUniform1i(glGetUniformLocation(_model_shader.program(), "texture_img"), 0);
// Set the proj matrix in the vertex shader
GLuint nvpmM = glGetUniformLocation(_model_shader.program(), "npvm");
glm::mat4 npvm = _ndc * _persMat * _viewMat * mat;
glUniformMatrix4fv(nvpmM, 1, GL_FALSE, glm::value_ptr(npvm));
// Feed the position data to the shader
glBindBuffer(GL_ARRAY_BUFFER, mesh.pos_VBO());
GLuint pos_att = glGetAttribLocation(_model_shader.program(), "position");
glEnableVertexAttribArray(pos_att);
glVertexAttribPointer(pos_att, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
// Feed the texture coordinate data to the shader
glBindBuffer(GL_ARRAY_BUFFER, mesh.tex_VBO());
GLuint tex_coord_att = glGetAttribLocation(_model_shader.program(), "texCoords");
glEnableVertexAttribArray(tex_coord_att);
glVertexAttribPointer(tex_coord_att, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);
// Draw mesh
glBindVertexArray(mesh.VAO());
glDrawElements(GL_TRIANGLES, mesh.numIndices(), GL_UNSIGNED_SHORT, (void*)0);
// Disable attributes
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
// Release resources
glBindTexture(GL_TEXTURE_2D, 0);
}
You need to bind your vertex arrays at the start of your function, not right before the draw call itself. The Vertex Array is responsible for maintaining the state associated with a given object[-type] and any calls made that will setup state (like glVertexAttribPointer or glEnableVertexAttribArray) will be maintained on that Vertex Array. What you were essentially doing with your old code is that you were setting up state for your object, then switching to an entirely different VAO, then drawing, which meant model1 was using model2's bindings and setup, and vice-versa. Unless they have identical rules and setups, it's extremely unlikely that they'll both draw.
Incidentally, because VAO's store state, the only things that need to be in your draw calls are the draw call itself, and any data that changed that frame. So you'll want to consider spending some time refactoring your code, as it looks like most of those settings (like buffer binding) don't change on a frame-by-frame basis.

Open GL ES 2.0 multiple drawElements and draw order

I implemented simple OBJ-parser and using parallelepiped as example model. I added rotation feature based on quaternions. Next goal - adding light. I parsed normals and decided drawing normals as "debug" feature (for further better understanding light). But I stuck after that:
Here my parallelepiped with small rotation.
Look at the right further bottom vertice and normal. I can't understand why it rendered through my parallelepiped. It should be hidden.
I use depth buffer (because without it parallelepiped looking weird while I rotate it). So I initialize it:
glGenRenderbuffers(1, &_depthRenderbuffer);
glBindRenderbuffer(GL_RENDERBUFFER, _depthRenderbuffer);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, _frameBufferWidth, _frameBufferHeight);
and enable it:
glEnable(GL_DEPTH_TEST);
I generate 4 VBOs: vertex and index buffers for parallelepiped, vertex and index buffers for lines(normals).
I use one simple shader for both models(if it will be needed - I can add code later but I think everything is ok with it).
At first I draw parallelepiped, after that - normals.
Here my code:
// _field variable - parallelepiped
glClearColor(0.3, 0.3, 0.4, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int vertexSize = Vertex::size();
int colorSize = Color::size();
int normalSize = Normal::size();
int totalSize = vertexSize + colorSize + normalSize;
GLvoid *offset = (GLvoid *)(sizeof(Vertex));
glBindBuffer(GL_ARRAY_BUFFER, _geomBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indicesBufferID);
glVertexAttribPointer(_shaderAtributePosition, vertexSize, GL_FLOAT, GL_FALSE, sizeof(Vertex::oneElement()) * totalSize, 0);
glVertexAttribPointer(_shaderAttributeColor, colorSize, GL_FLOAT, GL_FALSE, sizeof(Color::oneElement()) * totalSize, offset);
glDrawElements(GL_TRIANGLES, _field->getIndicesCount(), GL_UNSIGNED_SHORT, 0);
#ifdef NORMALS_DEBUG_DRAWING
glBindBuffer(GL_ARRAY_BUFFER, _normalGeomBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _normalIndexBufferID);
totalSize = vertexSize + colorSize;
glVertexAttribPointer(_shaderAtributePosition, vertexSize, GL_FLOAT, GL_FALSE, sizeof(Vertex::oneElement()) * totalSize, 0);
glVertexAttribPointer(_shaderAttributeColor, colorSize, GL_FLOAT, GL_FALSE, sizeof(Color::oneElement()) * totalSize, offset);
glDrawElements(GL_LINES, 2 * _field->getVertexCount(), GL_UNSIGNED_SHORT, 0);
#endif
I understand for example if I merge this two draw calls in one (And use same VBOs for parallelepiped and normals - everything will be fine).
But it will be uncomfortable because I use lines and triangles.
There are should be another way for fixing Z order. I can't believe that complex scene (for example sky, land and buildings) draws via one draw call.
So, what I am missing?
Thanks in advance.
If you are rendering into a window surface you need to request depth as part of your EGL configuration request. The depth renderbuffer you have allocated is only useful if you attach it to a Framebuffer Object (FBO) for off-screen rendering.

(OpenGL) How to transform an object while keeping an identical object stationary?

So I had a quick question about a homework assignment I was assigned for my OpenGL class. In class, we made two triangles transform (get bigger and smaller) as they follow a sine wave, and our homework is to make it so that only one transforms while the other stays static, all while only using one vertex shader, one fragment shader, and uniform variables. When I tried it, my mindset was to somehow return the value of 1 to a Scale variable to keep one of the triangles from moving. I was able to make it so that one of the triangles was stationary, but no shader was attached to it, so it was solid white. I have a feeling I have to use some OOP to create another instance of the triangle, but I really can't wrap my head around how I'm supposed to distinguish between the two triangles while only using one vertex shader. Can anyone shed some light onto this? This is the hardest class of the curriculum, so even a hint would be lovely! Thank you! I can post my code if you all want, but I made some changes to it that made my program cry, so I'll try to retrace my steps and post it tomorrow! Thank you all!
[EDIT] Okay here's my code. Trying to make it so that only the red triangle is static. I have a feeling the answer is in the provided class in the form of an "if statement", though I could be wrong about the complexity of this problem. Thank you all again for helping me.
#include "Triangle.h"
Triangle::Triangle(vec3 points[], vec4 color[], GLuint pID)
{
ProgramID = pID;
memcpy(Points, points, sizeof(Points));
memcpy(Colors, color, sizeof(Colors));
glUseProgram(ProgramID);
glGenVertexArrays(1, &VBO);
glBindVertexArray(VBO);
glGenBuffers(1, &VB);
glBindBuffer(GL_ARRAY_BUFFER, VB);
glBufferData(GL_ARRAY_BUFFER, sizeof(Points), Points, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glGenBuffers(1, &CB);
glBindBuffer(GL_ARRAY_BUFFER, CB);
glBufferData(GL_ARRAY_BUFFER, sizeof(Colors), Colors, GL_STATIC_DRAW);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(1);
Scale = 90.0f;
gScaleLocation = glGetUniformLocation(ProgramID, "Scale");
}
void Triangle::Draw()
{
glUseProgram(ProgramID);
glBindVertexArray(VBO);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
if ()
{
Scale += 0.0f;
}
else
{
Scale += 0.01f;
}
glUniform1f(gScaleLocation, sinf(Scale));
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
}
Rather than putting the animation logic into the Triangle class, move it to the caller. So first extract the scaling factor as a parameter:
void Triangle::Draw(double scale)
{
glUseProgram(ProgramID);
glBindVertexArray(VBO);
glUniform1f(gScaleLocation, scale);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
Next call Draw with different parameters on different triangle instances:
// your triangles. I assume you have two instances based on the little code you provided.
Triangle tri0, tri1;
// add this to some context, initialized to 0
int frame;
// Your rendering function, you didn't post it.
void render()
{
tri0.Draw(1); // triangle 0 is not animating
tri1.Draw(sin(frame*0.01)); // triangle 1 follows a sine wave
frame++;
}

OpenGL loading OBJ model, texture distortion

I decided to import Wavefront .OBJ format to a test-scene that I'm working on. I get the model (vertices) to be in the right place and it displays fine. When I then apply a texture a lot of things looks distorted. I checked my Maya scene (there it looks good), and the object has many more uv-coordinates than vertex positions (this is what makes the scene looks weird in OpenGL, is my guess).
How would I go about loading a scene like that. Do I need to duplicate vertices and how do I store it in the vertex-buffer object?
You are right that you have to duplicate the vertices.
In addition to that you have to sort them in draw order, meaning that you have to order the vertices with the same offsets as the texture coordinates and normals.
basically you'll need this kind of structure:
float *verts = {v1_x,v1_y,v1_z,v1_w,v2_x,v2_y,v2_z,v2_w,...};
float *normals = {n1_x,n1_y,n1_z,n2_x,n2_y,n2_z,...};
float *texcoords = {t1_u,t1_v,t1_w,t2_u,t2_v,t2_w,...};
This however would mean that you have at least 108bytes per Triangle.
3(vert,norm,tex)
*3(xyz/uvw)
*3(points in tri)
*4(bytes in a float))
-----------------------
= 108
You can significantly reduce that number by only duplicating the vertices that actually are duplicate (have identical texture coordinate,vertices and normals meaning: smoothed normals and no UV borders) and using an Index Buffer Object to set the draw order.
I faced the same problem recently in a small project and I just split the models along the hard-edges and UV-Shell borders therefore creating only the necessary duplicate Vertices. Then I used the glm.h and glm.cpp from Nate Robins and copied/sorted the normals and texture coordinates in the same order as the vertices.
Then setup the VBO and IBO:
//this is for Data that does not change dynamically
//GL_DYNAMIC_DRAW and others are available
GLuint mDrawMode = GL_STATIC_DRAW;
//////////////////////////////////////////////////////////
//Setup the VBO
//////////////////////////////////////////////////////////
GLuint mId;
glGenBuffers(1, &mId);
glBindBuffer(GL_ARRAY_BUFFER, mId);
glBufferData(GL_ARRAY_BUFFER,
mMaxNumberOfVertices * (mVertexBlockSize + mNormalBlockSize + mColorBlockSize + mTexCoordBlockSize),
0,
mDrawMode);
glBufferSubData(GL_ARRAY_BUFFER, mVertexOffset, numberOfVertsToStore * mVertexBlockSize, vertices);
glBufferSubData(GL_ARRAY_BUFFER, mNormalOffset, numberOfVertsToStore * mNormalBlockSize, normals);
glBufferSubData(GL_ARRAY_BUFFER, mColorOffset, numberOfVertsToStore * mColorBlockSize, colors);
glBufferSubData(GL_ARRAY_BUFFER, mTexCoordOffset, numberOfVertsToStore * mTexCoordBlockSize, texCoords);
//////////////////////////////////////////////////////////
//Setup the IBO
//////////////////////////////////////////////////////////
GLuint IBOId;
glGenBuffers(1, &IBOId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBOId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, mMaxNumberOfIndices * sizeof(GLuint), 0, mDrawMode);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, numberOfIndicesToStore * sizeof(GLuint), indices);
//////////////////////////////////////////////////////////
//This is how to draw the object
//////////////////////////////////////////////////////////
glBindBuffer(GL_ARRAY_BUFFER, mId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBOId);
//Enables and Disables are only necessary each draw
//when they change between objects
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(mVertexComponents, GL_FLOAT, 0, (void*)mVertexOffset);
if(mNormalBlockSize){
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, 0, (void*)mNormalOffset);
}
if(mColorBlockSize){
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(mColorComponents, GL_FLOAT, 0, (void*)mColorOffset);
}
if(mTexCoordBlockSize){
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(mTexCoordComponents, GL_FLOAT, 0, (void*)mTexCoordOffset);
}
glDrawRangeElements(primMode,
idFirstVertex,
idLastVertex,
idLastVertex - idFirstVertex + 1,
mAttachedIndexBuffer->getDataType(),
0);
if(mTexCoordBlockSize)
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
if(mColorBlockSize)
glDisableClientState(GL_COLOR_ARRAY);
if(mNormalBlockSize)
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);