使用VBO在Java中绘制的多维数据集与几何不匹配

时间:2014-07-28 18:43:20

标签: java opengl game-engine lwjgl vbo

我在java(使用lwjgl)和opengl中渲染测试多维数据集。立方体本身是一个简单的10x10x10,使用以下顶点数据绘制:

float[] vertexData =
    {length/2, length/2, length/2,
    -length/2, length/2, length/2,
    -length/2, length/2, -length/2,
     length/2, length/2, -length/2,
     length/2, -length/2, length/2,
    -length/2, -length/2, length/2,
    -length/2, -length/2, -length/2,
     length/2, -length/2, -length/2};

short[] indexData = {0, 4, 1,
    1, 4, 5,
    1, 5, 2,
    2, 5, 6,
    2, 6, 3,
    3, 7, 6,
    3, 7, 0,
    0, 7, 4,
    3, 0, 2,
    2, 0, 1,
    4, 7, 6,
    5, 4, 6};

可变长度设置为10.(为简单起见,我省略了我的颜色数据,但是你也看到它只包含1,0,0重复)。

我的问题是:在绘制而不是10x10x10时,我得到大约4.7x4.7x4.7。我已经运行了几个测试(例如:使用glBegin绘制几何图形以及放置两个立方体,中心有10个像素,也可以看看是否触摸它们没有)

我将VBO打包在我自己创建的一个类中,其内容如下:

 public class BasicVBO {

 IntBuffer ib = BufferUtils.createIntBuffer(3);

 int vHandle = ib.get(0);
 int cHandle = ib.get(1);
 int iHandle = ib.get(2);

 float[] vertexData;
 float[] colorData;
 short[] indexData;


public BasicVBO(float[] vertexArray, float[] textArray, short[] indexArray){

 vertexData = vertexArray;
 colorData = textArray;
 indexData = indexArray; 


FloatBuffer vBuffer = BufferUtils.createFloatBuffer(vertexArray.length);
FloatBuffer cBuffer = BufferUtils.createFloatBuffer(colorData.length);
ShortBuffer iBuffer = BufferUtils.createShortBuffer(indexArray.length);

glGenBuffers(ib);
  vHandle = ib.get(0);
  cHandle = ib.get(1);
  iHandle = ib.get(2);



    vBuffer.put(vertexData);
    vBuffer.flip();


    cBuffer.put(colorData);
    cBuffer.flip();


    iBuffer.put(indexData);
    iBuffer.flip();

  glEnableClientState(GL_VERTEX_ARRAY);
  glEnableClientState(GL_COLOR_ARRAY);

  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, iHandle);
  glBufferData(GL_ELEMENT_ARRAY_BUFFER_ARB, iBuffer, GL_DYNAMIC_DRAW_ARB);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);

  glBindBuffer(GL_ARRAY_BUFFER_ARB, vHandle);
  glBufferData(GL_ARRAY_BUFFER_ARB, vBuffer, GL_DYNAMIC_DRAW_ARB);
  glBindBuffer(GL_ARRAY_BUFFER_ARB, 0);


  glBindBuffer(GL_ARRAY_BUFFER_ARB, cHandle);
  glBufferData(GL_ARRAY_BUFFER_ARB, cBuffer, GL_DYNAMIC_DRAW_ARB);
  glBindBuffer(GL_ARRAY_BUFFER_ARB, 0);



}



public void Draw(float x,float y,float z,float a,float b,float c,float d){

   glPushMatrix();

    glTranslatef(x, y, z);
    glRotatef((float)(Math.toDegrees(a)),b,c,d);


  glEnableClientState(GL_VERTEX_ARRAY);
  glEnableClientState(GL_COLOR_ARRAY);

  glBindBuffer(GL_ARRAY_BUFFER, vHandle);
  glVertexPointer(3, GL_FLOAT,  3 << 2 , 0L);
  glBindBuffer(GL_ARRAY_BUFFER, 0);

  glBindBuffer(GL_ARRAY_BUFFER, cHandle);
  glColorPointer(3, GL_FLOAT,  3 << 2  , 0L);
  glBindBuffer(GL_ARRAY_BUFFER, 0);

  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iHandle);

  glDrawElements(GL_TRIANGLES,indexData.length  , GL_UNSIGNED_SHORT, 0L);

  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

  glDisableClientState(GL_VERTEX_ARRAY);
  glDisableClientState(GL_COLOR_ARRAY);

   glPopMatrix(); 

}



}

它在我的游戏循环中以下列方式使用:

       //////////
       //render//
       //////////

       switch (gamemode) {
           case Intro:

               break;
           case MainMenu:
               switch (menupage) {

               }
               break;
           case Game:
               switch (submode) {
                   case fp:
                       //setting fp settings



                       // camera for fp
                       MyCamera.ConvertToVectorAndDraw(cameraPosition); **//unrelated camera function**





                       //drawing for fp//

                //3d here

   glPushMatrix();


                     World.Render(); **//This is the draw function for the VBO**

                     //Draw plans to check sizing

                     glBegin(GL_QUADS);
                     glColor3f(0,1,0);
                     glVertex3f(5,-5,(float)(9.5));
                     glVertex3f(-5,-5,(float)(9.5));
                     glVertex3f(-5,5,(float)(9.5));
                     glVertex3f(5,5,(float)(9.5));
                     glEnd();



                    **//The above draws a 10x10 square at the correct scale**





    glPopMatrix();




   //  Set Up for 2D 

                       glMatrixMode(GL_PROJECTION);
                       glPushMatrix();
                       glLoadIdentity();
                       glOrtho(0.0, Display.getWidth(), Display.getHeight(), 0.0, -1.0, 10.0);
                       glMatrixMode(GL_MODELVIEW);
                       glLoadIdentity();
                       glDisable(GL_CULL_FACE);

                       glClear(GL_DEPTH_BUFFER_BIT);



                //2D here




      //  Reprep for 3D 

                       glPopMatrix();
                       glMatrixMode(GL_PROJECTION);
                       glPopMatrix();
                       glMatrixMode(GL_MODELVIEW);





               }
               break;
       }

简而言之,我将上面的顶点和索引数据传递给我的vbo类的初始化函数,然后在我的渲染循环中使用它的draw函数。图像绘制,而不是在需要的顶点位置(如上所述,它绘制了4.7x4.7x4.7立方体的纵横比)。

我一直在渲染部分和vbo类中使用各种函数,并且无法使对象正确绘制。

更新:

很抱歉这需要很长时间才能回答,但我忘了我发布了这个问题。问题是glRotate。我传递给它的数据是未旋转的,但也有0,0,0作为旋转的向量。这给出了一个未定义的结果,并在我的原因中调整了对象的大小。 所以,你去,我的数学而不是我的编码。

0 个答案:

没有答案