LWJGL纹理的VBO [英] LWJGL Textures in VBO

查看:195
本文介绍了LWJGL纹理的VBO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,以及我做了使用VBO一个简单的3D立方体,而我想纹理加载到它。唯一的问题是,材质都搞砸了,这是我的code:

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?

是的,我翻我的缓冲区,是的我的图片是2的幂。

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

推荐答案

您实施 renderCube(...)是错误的:

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);
}

您需要立即调用之前做的是绑定VBO为您顶点的位置是什么的glVertexPointer(3,GL_FLOAT,0,0)和VBO的纹理立即坐标打电话之前 glTexCoordPointer(2 GL_FLOAT,0,0)

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).

这些功能建立指针相内存通过任何VBO有义务在你给他们打电话(或实际的系统内存的指针,如果你不使用驻国际中心组织)时所定义的地址空间。

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);

这篇关于LWJGL纹理的VBO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆