我執行與GL_TRIANGLE_STRIPS和LWJGL OpenGL的Java綁定,一個高度圖。當我使用'GL_BEGIN/GL_END'繪製'直接'時,它可以很好地工作,但是當高度圖大時,它的運行速度會變慢。OpenGL的頂點數組和GL_TRIANGLE_STRIP繪製錯誤
正如我希望使用移動VBO的,我現在正在學習如何使用頂點數組的。這是我遇到問題的地方,繪圖是錯誤的。看起來,該帶的最後一個三角形返回到第一個。 Perheps圖片更好:
好圖紙:
壞頂點數組繪製:
我的代碼是正常繪製如下:
public void renderDirect() {
//adapt the camera to the map
float scale = 5.0f/Math.max(w - 1, l - 1);
GL11.glScalef(scale, scale, scale);
GL11.glTranslatef(-(float) (w - 1)/2, 0.0f, -(float) (l - 1)/2);
//choose map color
GL11.glColor3f(0.3f, 0.9f, 0.0f);
for (int z = 0; z < l - 1; z++) {
//Makes OpenGL draw a triangle at every three consecutive vertices
GL11.glBegin(GL11.GL_TRIANGLE_STRIP);
for (int x = 0; x < w; x++) {
Vector3f normal = getNormal(x, z);
GL11.glNormal3f(normal.getX(), normal.getY(), normal.getZ());
GL11.glVertex3f(x, getHeight(x, z), z);
normal = getNormal(x, z + 1);
GL11.glNormal3f(normal.getX(), normal.getY(), normal.getZ());
GL11.glVertex3f(x, getHeight(x, z + 1), z + 1);
}
glEnd();
}
}
我的代碼是以下爲頂點陣列圖:
private void loadArrays(){
//calculate the length of the buffers
bLength = (l-1) * w * 6;
//create the normal and vertex buffer array's
dataBuffer = BufferUtils.createFloatBuffer(bLength*2);
cBuffer = BufferUtils.createFloatBuffer(bLength);
for (int z = 0; z < l - 1; z++) {
//Fill up the buffers
for (int x = 0; x < w; x++) {
Vector3f normal = getNormal(x, z);
dataBuffer.put(x).put(getHeight(x,z)).put(z);
dataBuffer.put(normal.getX()).put(normal.getY()).put(normal.getZ());
normal = getNormal(x, z + 1);
dataBuffer.put(x).put(getHeight(x,z+1)).put(z+1);
dataBuffer.put(normal.getX()).put(normal.getY()).put(normal.getZ());
}
}
}
int stride = 6*4;
public void renderDirect() {
//adapt the camera to the map
float scale = 5.0f/Math.max(w - 1, l - 1);
GL11.glScalef(scale, scale, scale);
GL11.glTranslatef(-(float) (w - 1)/2, 0.0f, -(float) (l - 1)/2);
//choose map color
GL11.glColor3f(0.3f, 0.9f, 0.0f);
//Draw the vertex arrays
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
dataBuffer.position(0);
glVertexPointer(3, stride, dataBuffer);
dataBuffer.position(3);
glNormalPointer(stride,dataBuffer);
glDrawArrays(GL_TRIANGLE_STRIP, 0, bLength/3);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
我在做什麼錯?如你預期
@OP想想吧,你需要一個'glDrawArrays'呼籲每個'在glBegin/glEnd'的方式這樣做您以前的代碼塊。在這種情況下,考慮索引原語(因此'glDrawElements'而不是'glDrawArrays')可能是一個好主意,因爲每個頂點基本上都是兩個條的一部分。 – 2012-02-02 16:02:12