LWJGL Textures in VBO

非 Y 不嫁゛ 提交于 2019-12-13 04:37:23

问题


Ok, well I made a simple 3D cube using VBO, and I wanted to load textures onto it. Only problem is that the textures are all messed up, here is my code:

public void create() {
    setup();
    render();
}

private void makeCube(float x, float y, float z) {
    int cube = glGenBuffers();
    int texture = glGenBuffers();
    FloatBuffer cubeBuffer;
    FloatBuffer textureBuffer;

    float highX = x + tileSize;
    float highY = y + tileSize;
    float highZ = z + tileSize;

    float[] textureData = new float[]{
        0,0,
        1,0,
        1,1,
        0,1
    };

    textureBuffer = asFloatBuffer(textureData);
    glBindBuffer(GL_ARRAY_BUFFER, texture);
    glBufferData(GL_ARRAY_BUFFER, textureBuffer, GL_DYNAMIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    float[] cubeData = new float[]{
        /*Front Face*/
        x, y, z,
        highX, y, z,
        highX, highY, z,
        x, highY, z,
        /*Back Face*/
        x, y, highZ,
        highX, y, highZ,
        highX, highY, highZ,
        x, highY, highZ,
        /*Left Face*/
        x, y, z,
        x, y, highZ,
        x, highY, highZ,
        x, highY, z,
        /*Right Face*/
        highX, y, z,
        highX, y, highZ,
        highX, highY, highZ,
        highX, highY, z,
        /*Bottom Face*/
        x, y, z,
        x, y, highZ,
        highX, y, highZ,
        highX, y, z,
        /*Top Face*/
        x, highY, z,
        x, highY, highZ,
        highX, highY, highZ,
        highX, highY, z,};

    cubeBuffer = asFloatBuffer(cubeData);

    glBindBuffer(GL_ARRAY_BUFFER, cube);
    glBufferData(GL_ARRAY_BUFFER, cubeBuffer, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glBindBuffer(GL_ARRAY_BUFFER, cube);

}

private void renderCube() {
    textures.get(0).bind();
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, 0);
    glTexCoordPointer(2, GL_FLOAT, 0, 0);
    glDrawArrays(GL_QUADS, 0, 22);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

private void render() {
    while (!Display.isCloseRequested()) {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();
        camera();

        renderCube();

        Display.update();
        Display.sync(30);
    }

    Display.destroy();
    System.exit(0);
}

private void setup() {
    try {
        Display.setDisplayMode(new DisplayMode(frameWidth, frameHeight));
        Display.setTitle("3D Project");
        Display.setVSyncEnabled(vSync);
        Display.create();
    } catch (LWJGLException ex) {
        Logger.getLogger(Camera.class.getName()).log(Level.SEVERE, null, ex);
    }

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(fov, (float) Display.getWidth() / (float) Display.getHeight(), zNear, zFar);
    //glOrtho(0, Display.getWidth(), Display.getHeight(), 0, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);

    glLoadIdentity();

    loadTextures();

    makeCube(0, 0, -1);

}

The only thing I think is wrong with this is my texture coordinate array, if so, can anyone give me a correct order?

Yes I flip my buffers, and yes my images are powers of 2.


回答1:


Your implementation of renderCube (...) is faulty:

private void renderCube() {
  textures.get(0).bind();
  glEnableClientState(GL_VERTEX_ARRAY);
  glEnableClientState(GL_TEXTURE_COORD_ARRAY);

  /* These two pointers reference the same memory! */
  glVertexPointer(3, GL_FLOAT, 0, 0);
  glTexCoordPointer(2, GL_FLOAT, 0, 0);

  glDrawArrays(GL_QUADS, 0, 22);
  glDisableClientState(GL_VERTEX_ARRAY);
  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}

What you need to do is bind the VBO for your vertex position immediately before calling glVertexPointer(3, GL_FLOAT, 0, 0) and the VBO for your texture coordinates immediately before calling glTexCoordPointer(2, GL_FLOAT, 0, 0).

These functions establish pointers to memory relative to the address space defined by whatever VBO is bound at the time you call them (or actual system memory pointers if you are not using VBOs).

/* Setup Position Pointer */
glBindBuffer    (GL_ARRAY_BUFFER, cube);
glVertexPointer (3, GL_FLOAT, 0, 0);

/* Setup Texture Coordinate Pointer */
glBindBuffer      (GL_ARRAY_BUFFER, texture);
glTexCoordPointer (2, GL_FLOAT, 0, 0);


来源:https://stackoverflow.com/questions/20979673/lwjgl-textures-in-vbo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!