OpenGL 顶点缓冲区未绘制 (LWJGL)
我一直在尝试实现一种更好的绘制方式,并尝试使用顶点缓冲区。 现在,我一直在关注 LWJGL Wiki [使用顶点缓冲区] 中的教程对象(VBO)],但它对我不起作用,它根本不绘制(据我所知)。 如果我使用 glVertex3d()
进行绘制,则一切正常。
它看起来是这样的:
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL12.*;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.ARBVertexBufferObject;
import org.lwjgl.opengl.GLContext;
public abstract class Shape {
public void render()
{
if(vertexBufferID > 0 && indexBufferID > 0)
{
glEnableClientState(GL_VERTEX_ARRAY);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, vertexBufferID);
glVertexPointer(3, GL_FLOAT, 0, 0);
if(colorBufferID > 0)
{
glEnableClientState(GL_COLOR_ARRAY);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, colorBufferID);
glColorPointer(4, GL_FLOAT, 0, 0);
}
if(textureCoordBufferID > 0)
{
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, textureCoordBufferID);
glColorPointer(2, GL_FLOAT, 0, 0);
}
if(normalBufferID > 0)
{
glEnableClientState(GL_NORMAL_ARRAY);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, normalBufferID);
glNormalPointer(GL_FLOAT, 0, 0);
}
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, indexBufferID);
glDrawRangeElements(drawMode, 0, verticesNumber, verticesNumber,
GL_UNSIGNED_INT, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
}
public static int createVBOID()
{
if (GLContext.getCapabilities().GL_ARB_vertex_buffer_object) {
IntBuffer buffer = BufferUtils.createIntBuffer(1);
ARBVertexBufferObject.glGenBuffersARB(buffer);
return buffer.get(0);
}
return 0;
}
public static void bufferData(int id, FloatBuffer buffer)
{
if (GLContext.getCapabilities().GL_ARB_vertex_buffer_object)
{
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, id);
ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, buffer, ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
}
}
public static void bufferElementData(int id, IntBuffer buffer)
{
if (GLContext.getCapabilities().GL_ARB_vertex_buffer_object)
{
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, id);
ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, buffer, ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
}
}
@Override
protected void finalize() throws Throwable
{
if(vertexBufferID > 0) ARBVertexBufferObject.glDeleteBuffersARB(vertexBufferID);
if(colorBufferID > 0) ARBVertexBufferObject.glDeleteBuffersARB(colorBufferID);
if(textureCoordBufferID > 0) ARBVertexBufferObject.glDeleteBuffersARB(textureCoordBufferID);
if(indexBufferID > 0) ARBVertexBufferObject.glDeleteBuffersARB(indexBufferID);
super.finalize();
}
protected int verticesNumber;
protected int drawMode;
protected int vertexBufferID;
protected int colorBufferID;
protected int textureCoordBufferID;
protected int normalBufferID;
protected int indexBufferID;
}
子类实际上填充了内容:
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
public class ShapeCube extends Shape{
public ShapeCube()
{
drawMode = GL11.GL_QUADS;
verticesNumber = 6 * 4;
FloatBuffer vertices = BufferUtils.createFloatBuffer(verticesNumber * 3);
IntBuffer indices = BufferUtils.createIntBuffer(verticesNumber);
for(float x = -0.5F; x < 1; x++) for(float y = -0.5F; y < 1; y++) for(float z = -0.5F; z < 1; z++)
{
vertices.put(new float[]{x, y, z});
}
indices.put(new int[]{0, 1, 2, 3, 4, 5, 6, 7});
indices.put(new int[]{0, 1, 4, 5, 2, 3, 6, 7});
indices.put(new int[]{0, 2, 4, 6, 1, 3, 5, 7});
vertexBufferID = createVBOID();
bufferData(vertexBufferID, vertices);
indexBufferID = createVBOID();
bufferElementData(indexBufferID, indices);
}
}
正如我所说,这段代码不会为我绘制任何内容(我可以看到)。我确信我在正确的位置调用了正确的代码,它应该绘制一个简单的彩色立方体。 我很确定我一定错过了一些简单的东西,但我对顶点缓冲区对象没有任何经验。
I've been trying to implement a better way of drawing stuff, and tried using Vertex Buffers.
Now, I've been following the Tutorial from the LWJGL Wiki [Using Vertex Buffer Objects (VBO)], but it does not work for me, it does not draw at all (as far as I can see).
If I draw with glVertex3d()
instead, all works fine.
This is how it looks like:
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL12.*;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.ARBVertexBufferObject;
import org.lwjgl.opengl.GLContext;
public abstract class Shape {
public void render()
{
if(vertexBufferID > 0 && indexBufferID > 0)
{
glEnableClientState(GL_VERTEX_ARRAY);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, vertexBufferID);
glVertexPointer(3, GL_FLOAT, 0, 0);
if(colorBufferID > 0)
{
glEnableClientState(GL_COLOR_ARRAY);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, colorBufferID);
glColorPointer(4, GL_FLOAT, 0, 0);
}
if(textureCoordBufferID > 0)
{
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, textureCoordBufferID);
glColorPointer(2, GL_FLOAT, 0, 0);
}
if(normalBufferID > 0)
{
glEnableClientState(GL_NORMAL_ARRAY);
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, normalBufferID);
glNormalPointer(GL_FLOAT, 0, 0);
}
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, indexBufferID);
glDrawRangeElements(drawMode, 0, verticesNumber, verticesNumber,
GL_UNSIGNED_INT, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
}
public static int createVBOID()
{
if (GLContext.getCapabilities().GL_ARB_vertex_buffer_object) {
IntBuffer buffer = BufferUtils.createIntBuffer(1);
ARBVertexBufferObject.glGenBuffersARB(buffer);
return buffer.get(0);
}
return 0;
}
public static void bufferData(int id, FloatBuffer buffer)
{
if (GLContext.getCapabilities().GL_ARB_vertex_buffer_object)
{
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, id);
ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, buffer, ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
}
}
public static void bufferElementData(int id, IntBuffer buffer)
{
if (GLContext.getCapabilities().GL_ARB_vertex_buffer_object)
{
ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, id);
ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, buffer, ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
}
}
@Override
protected void finalize() throws Throwable
{
if(vertexBufferID > 0) ARBVertexBufferObject.glDeleteBuffersARB(vertexBufferID);
if(colorBufferID > 0) ARBVertexBufferObject.glDeleteBuffersARB(colorBufferID);
if(textureCoordBufferID > 0) ARBVertexBufferObject.glDeleteBuffersARB(textureCoordBufferID);
if(indexBufferID > 0) ARBVertexBufferObject.glDeleteBuffersARB(indexBufferID);
super.finalize();
}
protected int verticesNumber;
protected int drawMode;
protected int vertexBufferID;
protected int colorBufferID;
protected int textureCoordBufferID;
protected int normalBufferID;
protected int indexBufferID;
}
And the subclass, actually filling the Content in:
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
public class ShapeCube extends Shape{
public ShapeCube()
{
drawMode = GL11.GL_QUADS;
verticesNumber = 6 * 4;
FloatBuffer vertices = BufferUtils.createFloatBuffer(verticesNumber * 3);
IntBuffer indices = BufferUtils.createIntBuffer(verticesNumber);
for(float x = -0.5F; x < 1; x++) for(float y = -0.5F; y < 1; y++) for(float z = -0.5F; z < 1; z++)
{
vertices.put(new float[]{x, y, z});
}
indices.put(new int[]{0, 1, 2, 3, 4, 5, 6, 7});
indices.put(new int[]{0, 1, 4, 5, 2, 3, 6, 7});
indices.put(new int[]{0, 2, 4, 6, 1, 3, 5, 7});
vertexBufferID = createVBOID();
bufferData(vertexBufferID, vertices);
indexBufferID = createVBOID();
bufferElementData(indexBufferID, indices);
}
}
As I said, this code doesn't draw anything (I can see) for me. I'm sure I'm invoking the right code at the right spot, and it should draw a simple colored cube.
I'm pretty sure I must be missing something easy, but I don't have any experience with Vertex Buffer objects.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我现在自己找到了答案。对于每个人来说,我一直使用的缓冲区的位置必须设置为零。然后就可以完美运行了:)
I've found the answer myself now. For everyone wondering, the position of the Buffers I've been using must be set to Zero. Then it works perfectly :)