[英]LWJGL (OpenGL) VBO model matrix not rendering correctly
I have two VBOs I am trying to render, both of them should have two different positions on the screen. 我有两个要渲染的VBO,它们在屏幕上应该有两个不同的位置。 When I try to modify the position of one of the VBOs, it gets transferred to the other.
当我尝试修改其中一个VBO的位置时,它会转移到另一个。
Example- I change the y position of object 2, both object 1 and object 2 now exist at that y position. 示例-我更改了对象2的y位置,现在对象1和对象2都位于该y位置。
My code for transforming the VBOs: 我的转换VBO的代码:
Matrix4f.scale(scale, modelMatrix, modelMatrix);
Matrix4f.translate(position, modelMatrix, modelMatrix);
Matrix4f.rotate(Toolkit.degToRad(rotation.x), new Vector3f(1f,0f,0f), modelMatrix, modelMatrix);
Matrix4f.rotate(Toolkit.degToRad(rotation.y), new Vector3f(0f,1f,0f), modelMatrix, modelMatrix);
Matrix4f.rotate(Toolkit.degToRad(rotation.z), new Vector3f(0f,0f,1f), modelMatrix, modelMatrix);
Please note that position, rotation, and scale are all Vector3fs, and modelMatrix is well, the model matrix. 请注意,位置,旋转和比例都是Vector3fs,而modelMatrix是model矩阵。
Also, Toolkit.degToRad is similar to a Math.toRadians() type method. 另外,Toolkit.degToRad与Math.toRadians()类型的方法相似。
My code for passing info to the shaders: 我的将信息传递到着色器的代码:
//Apply Transformations
camera.reset();
camera.transform();
glUseProgram(ss.pId);
//Projection Matrix
camera.projectionMatrix.store(camera.matrixBuffer);
camera.matrixBuffer.flip();
projection.Matrix4(camera.matrixBuffer);
//View Matrix
camera.viewMatrix.store(camera.matrixBuffer);
camera.matrixBuffer.flip();
view.Matrix4(camera.matrixBuffer);
glUseProgram(0);
//Apply Transformations
obj.reset();
obj.transform();
glUseProgram(ss.pId);
//Object 1
obj.modelMatrix.store(camera.matrixBuffer);
camera.matrixBuffer.flip();
model.Matrix4(camera.matrixBuffer);
glUseProgram(0);
//Apply Transformations
obj2.reset();
obj2.transform();
glUseProgram(ss.pId);
obj2.modelMatrix.store(camera.matrixBuffer);
camera.matrixBuffer.flip();
model.Matrix4(camera.matrixBuffer);
glUseProgram(0);
obj and obj2 are VBOs, model is a shader uniform, camera is the camera, and ss is the shader program and ss.pId is the program id. obj和obj2是VBO,模型是统一的着色器,camera是摄像头,ss是着色器程序,而ss.pId是程序ID。
My code for drawing the VBOs: Note that this is found in obj and obj2 and is used like this 我绘制VBO的代码:请注意,这是在obj和obj2中找到的,并且像这样使用
obj.draw();
obj2.draw();
And here is the drawing method 这是绘制方法
GL20.glUseProgram(ss.pId);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, t.id);
GL30.glBindVertexArray(g.vaoId);
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
GL20.glEnableVertexAttribArray(2);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, g.vboiId);
GL11.glDrawElements(GL11.GL_TRIANGLES, g.indices, GL11.GL_UNSIGNED_BYTE, 0);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1);
GL20.glDisableVertexAttribArray(2);
GL20.glUseProgram(0);
GL20.glUseProgram(ss.pId);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, t.id);
GL30.glBindVertexArray(g.vaoId);
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
GL20.glEnableVertexAttribArray(2);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, g.vboiId);
GL11.glDrawElements(GL11.GL_TRIANGLES, g.indices, GL11.GL_UNSIGNED_BYTE, 0);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1);
GL20.glDisableVertexAttribArray(2);
GL20.glUseProgram(0);
g is the geometry class, g是几何类别,
g contains indices buffer id, and vertices buffer id. g包含索引缓冲区ID和顶点缓冲区ID。 t contains texture and id.
t包含纹理和ID。
Thanks for your help. 谢谢你的帮助。
Your whole program structure looks fairly unusual, and I believe this is part of what is tripping you up. 您的整个程序结构看起来很不正常,我相信这是使您绊倒的一部分。 For example, while I'm all for encapsulation, wrapping a single uniform in an object, like you appear to be doing with your
model
variable, is pushing it too far IMHO. 例如,当我全力以赴封装时,将一个统一的对象包装在一个对象中,就像您似乎对
model
变量所做的那样,将其推向恕我直言。 A uniform value is really an attribute of a shader program, not an independent object. 统一值实际上是着色器程序的属性,而不是独立的对象。
Anyway, without going too deep into design aspects, I believe your main problem is in this code sequence (with parts omitted): 无论如何,在不深入设计方面的情况下,我相信您的主要问题在于此代码序列(省略了部分):
// calculate camera.matrixBuffer for object 1
model.Matrix4(camera.matrixBuffer);
...
// calculate camera.matrixBuffer for object 2
model.Matrix4(camera.matrixBuffer);
The second of these calls will overwrite the value written in the first one, without the first one ever being used. 这些调用中的第二个将覆盖在第一个调用中写入的值,而不会使用第一个。 When you later render object 1 and object 2, they will both use the second value for the uniform.
稍后渲染对象1和对象2时,它们都将使用第二个值作为制服。
As long as you use the same shader program for both objects (which is a good thing, unless they really need different shaders), you will have to set the uniform value before you draw each of the objects. 只要对两个对象都使用相同的着色器程序(这是一件好事,除非它们确实需要不同的着色器),否则在绘制每个对象之前,必须设置统一的值。
So the calls to set the uniforms should go into the draw function, where the structure will look roughly like this: 因此,设置制服的调用应进入draw函数,该函数的结构大致如下所示:
// calculate camera.matrixBuffer for object 1
model.Matrix4(camera.matrixBuffer);
obj1.draw();
...
// calculate camera.matrixBuffer for object 2
model.Matrix4(camera.matrixBuffer);
obj2.draw();
Matrix transforms are applied to everything, not just a single VBO. 矩阵转换可应用于所有内容,而不仅仅是单个VBO。
My suggestion would be to transform, draw, then do a reverse transformation for the next draw. 我的建议是变换,绘制,然后对下一个绘制进行反向变换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.