JOGL glArrayElement点提供0,0,0

发布于 2024-10-07 16:44:46 字数 1631 浏览 1 评论 0原文

我有一个 JOGL opengl 问题,我尝试使用顶点数组,但每当我使用 glArrayElement (注意:glDrawElements 也不起作用。)时,它给出点 0,0,0。重要代码。我假设窗口已初始化并指定了重塑函数。

...
public void display(GLDrawable glDrawable) {
 final GL gl = glDrawable.getGL();
 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
 gl.glLoadIdentity();
 gl.glTranslatef(0, 0, -6);
 gl.glBegin(GL.GL_TRIANGLES);
  gl.glColor3f(1.0f, 0.0f, 0.0f);
  gl.glArrayElement(4);
  /*gl.glArrayElement(5); // These are what I'm trying to use, but they seem to return the point 0,0,0.
  gl.glArrayElement(6);
  gl.glArrayElement(5);
  gl.glArrayElement(6);
  gl.glArrayElement(7);*/
  //gl.glVertex3f(1, 1, -1); // Replaced with uncommented glArrayElement above.
  gl.glColor3f(0.0f, 1.0f, 0.0f);
  gl.glVertex3f(-1, 1, -1);
  gl.glVertex3f(1, -1, -1);
  gl.glColor3f(0.0f, 0.0f, 1.0f);
  gl.glVertex3f(-1, 1, -1);
  gl.glVertex3f(1, -1, -1);
  gl.glVertex3f(-1, -1, -1);
 gl.glEnd();
}
...
protected final static float[] mesh = {1,1,1, -1,1,1, 1,-1,1, -1,-1,1, 

1,1,-1, -1,1,-1, 1,-1,-1, -1,-1,-1};
protected static ByteBuffer stdMesh;
...
public void init(GLDrawable glDrawable) {
 final GL gl = glDrawable.getGL();
 gl.glShadeModel(GL.GL_SMOOTH);
 gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
 gl.glClearDepth(1.0f);
 gl.glEnable(GL.GL_DEPTH_TEST);
 gl.glDepthFunc(GL.GL_LEQUAL);
 gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
 gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
 stdMesh = ByteBuffer.allocateDirect(mesh.length * 4);
 stdMesh.asFloatBuffer().put(mesh);
 gl.glVertexPointer(3, GL.GL_FLOAT, 0, stdMesh);
}
...

是否还需要调用其他初始化函数/绘制函数,或者这是另一个问题? 任何帮助表示赞赏。

I have a JOGL opengl problem, I'm trying to use Vertex Arrays, but whenever I use glArrayElement (Note: glDrawElements doesn't work either.), it gives the point 0,0,0. Important code. I'm assuming a window is initialized and a reshape function is specified.

...
public void display(GLDrawable glDrawable) {
 final GL gl = glDrawable.getGL();
 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
 gl.glLoadIdentity();
 gl.glTranslatef(0, 0, -6);
 gl.glBegin(GL.GL_TRIANGLES);
  gl.glColor3f(1.0f, 0.0f, 0.0f);
  gl.glArrayElement(4);
  /*gl.glArrayElement(5); // These are what I'm trying to use, but they seem to return the point 0,0,0.
  gl.glArrayElement(6);
  gl.glArrayElement(5);
  gl.glArrayElement(6);
  gl.glArrayElement(7);*/
  //gl.glVertex3f(1, 1, -1); // Replaced with uncommented glArrayElement above.
  gl.glColor3f(0.0f, 1.0f, 0.0f);
  gl.glVertex3f(-1, 1, -1);
  gl.glVertex3f(1, -1, -1);
  gl.glColor3f(0.0f, 0.0f, 1.0f);
  gl.glVertex3f(-1, 1, -1);
  gl.glVertex3f(1, -1, -1);
  gl.glVertex3f(-1, -1, -1);
 gl.glEnd();
}
...
protected final static float[] mesh = {1,1,1, -1,1,1, 1,-1,1, -1,-1,1, 

1,1,-1, -1,1,-1, 1,-1,-1, -1,-1,-1};
protected static ByteBuffer stdMesh;
...
public void init(GLDrawable glDrawable) {
 final GL gl = glDrawable.getGL();
 gl.glShadeModel(GL.GL_SMOOTH);
 gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
 gl.glClearDepth(1.0f);
 gl.glEnable(GL.GL_DEPTH_TEST);
 gl.glDepthFunc(GL.GL_LEQUAL);
 gl.glHint(GL.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
 gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
 stdMesh = ByteBuffer.allocateDirect(mesh.length * 4);
 stdMesh.asFloatBuffer().put(mesh);
 gl.glVertexPointer(3, GL.GL_FLOAT, 0, stdMesh);
}
...

Are there other initalization functions/draw functions I also need to call, or is it another problem?
Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聚集的泪 2024-10-14 16:44:46

您需要用数组值正确填充缓冲区。您这样做的方式返回一个新的 FloatBuffer 但您正在丢弃结果而不是存储它。

而不是

...
protected static ByteBuffer stdMesh;
...
 stdMesh = ByteBuffer.allocateDirect(mesh.length * 4);
 stdMesh.asFloatBuffer().put(mesh);
...

Do

...
protected static FloatBuffer stdMesh;
...
stdMesh = BufferUtil.newFloatBuffer(mesh.length * 3);
for (int i = 0; i < mesh.length; i++){
    stdMesh.put(mesh[i]);
}
stdMesh.flip();
...

确保调用

You need to properly populate your buffer with your array values. The way you are doing it returns a new FloatBuffer but you are discarding the result instead of storing it.

Instead of

...
protected static ByteBuffer stdMesh;
...
 stdMesh = ByteBuffer.allocateDirect(mesh.length * 4);
 stdMesh.asFloatBuffer().put(mesh);
...

Do

...
protected static FloatBuffer stdMesh;
...
stdMesh = BufferUtil.newFloatBuffer(mesh.length * 3);
for (int i = 0; i < mesh.length; i++){
    stdMesh.put(mesh[i]);
}
stdMesh.flip();
...

Make sure to call flip() on the buffer before using it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文