使用LWJGL和VBO进行简单纹理不起作用

时间:2013-09-14 11:24:26

标签: java opengl textures lwjgl vbo

我用VBO编写了一个小程序,但是纹理失败了。加载它们很好,但它们不会显示。因此,我为VBO纹理编写了一个小的纹理渲染器,并采用了本主题中的内容:VBO with texture in LWJGL。 但仍然没有任何渲染。 帮我吧,让这段代码显示一个纹理;)

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;

import java.io.IOException;
import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;

public class VBOTextureDemo {

private static Texture texture;

public static void main(String[] args) {
    try {
        Display.setDisplayMode(new DisplayMode(500, 500));
        Display.setTitle("Texture");
        Display.create();
    } catch (LWJGLException e) {
        e.printStackTrace();
        Display.destroy();
        System.exit(1);
    }

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(1, -1, 1, -1, 1, 1);
    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_TEXTURE_2D);
    glLoadIdentity();


    try {
        texture = TextureLoader.getTexture("PNG",
                ResourceLoader.getResourceAsStream("res/images/grass.png"));
    } catch (IOException e) {
        e.printStackTrace();
    }

    final int amountOfVertices = 6;
    final int vertexSize = 3;
    final int texSize = 2;

    FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertices
            * vertexSize);
    vertexData.put(new float[] { -10f, 10f, 0f, }); // Vertex
    vertexData.put(new float[] { 10f, 10f, 0f, }); // Vertex
    vertexData.put(new float[] { -10f, -10f, 0f, }); // Vertex

    vertexData.put(new float[] { 10f, -10f, 0f, }); // Vertex
    vertexData.put(new float[] { -10f, -10f, 0f, }); // Vertex
    vertexData.put(new float[] { 10f, 10f, 0f, }); // Vertex;
    vertexData.flip();

    FloatBuffer textureData = BufferUtils
            .createFloatBuffer(amountOfVertices * texSize);
    textureData.put(new float[] { 0f, 1f, }); // Texture Coordinate
    textureData.put(new float[] { 1f, 1f, }); // Texture Coordinate
    textureData.put(new float[] { 0f, 0f, }); // Texture Coordinate

    textureData.put(new float[] { 1f, 0f, }); // Texture Coordinate
    textureData.put(new float[] { 0f, 0f, }); // Texture Coordinate
    textureData.put(new float[] { 1f, 1f, }); // Texture Coordinate
    textureData.flip();

    glBindTexture(GL_TEXTURE_2D, texture.getTextureID());

    texture.bind();

    int vboVertexHandle = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
    glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);


    int vboTexCoordHandle = texture.getTextureID();
    glBindBuffer(GL_ARRAY_BUFFER, vboTexCoordHandle);
    glBufferData(GL_ARRAY_BUFFER, textureData, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glClearColor(0.5f, 0.1f, 0f, 1f);



    while (!Display.isCloseRequested()) {
        glClear(GL_COLOR_BUFFER_BIT);

        glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
        glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);

        glBindBuffer(GL_ARRAY_BUFFER, vboTexCoordHandle);
        glTexCoordPointer(2, GL_FLOAT, 0, 0);

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glDrawArrays(GL_TRIANGLES, 0, amountOfVertices);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
        glDisableClientState(GL_VERTEX_ARRAY);

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

    glDeleteBuffers(vboVertexHandle);
    glDeleteBuffers(vboTexCoordHandle);
    texture.release();


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

}

1 个答案:

答案 0 :(得分:2)

我没有对你的代码进行测试,但我认为有两件事可能会出现问题!

  1. 顶点的顺序错误!
  2. 纹理没有被加载,因此在调用绑定时,没有任何东西被绑定!
  3. 简单纹理测试

    仅用于测试尝试使用已弃用的方法

    • glBegin(GL_TRIANGLES);
    • glVertex3f(x, y, z);
    • glTexCoord2f(u, v);

    记住当然要绑定纹理,但如果使用不推荐使用的方法渲染三角形,那么它肯定不是纹理问题。虽然如果没有任何东西出现,它可能是一个纹理程序(或顶点的顺序错误)。

    简单纹理测试渲染|代码

    texture.bind();
    
    glBegin(GL_TRIANGLES);
    
        glTexCoord2f(0f, 1f);
        glVertex3f(-1f, 1f, 0f);
        glTexCoord2f(1f, 1f);
        glVertex3f(1f, 1f, 0f);
        glTexCoord2f(0f, 0f);
        glVertex3f(-1f, -1f, 0f);
    
        glTexCoord2f(1f, 0f);
        glVertex3f(1f, -1f, 0f);
        glTexCoord2f(0f, 0f);
        glVertex3f(-1f, -1f, 0f);
        glTexCoord2f(1f, 1f);
        glVertex3f(1f, 1f, 0f);
    
    glEnd();
    
    texture.unbind();
    

    VBO测试

    因此,如果它没有显示的原因是由于顶点的顺序错误,那么请尝试使用以下代码来创建和渲染纹理VBO。

    创建VBO |代码

    int vertices = 6;
    
    int vertex_size = 3; // X, Y, Z,
    int texture_size = 2; // U, V,
    
    FloatBuffer vertex_data = BufferUtils.createFloatBuffer(vertices * vertex_size);
    vertex_data.put(new float[] { -1f, 1f, 0f, }); // Vertex
    vertex_data.put(new float[] { 1f, 1f, 0f, }); // Vertex
    vertex_data.put(new float[] { -1f, -1f, 0f, }); // Vertex
    
    vertex_data.put(new float[] { 1f, -1f, 0f, }); // Vertex
    vertex_data.put(new float[] { -1f, -1f, 0f, }); // Vertex
    vertex_data.put(new float[] { 1f, 1f, 0f, }); // Vertex
    
    FloatBuffer texture_data = BufferUtils.createFloatBuffer(vertices * texture_size);
    texture_data.put(new float[] { 0f, 1f, }); // Texture Coordinate
    texture_data.put(new float[] { 1f, 1f, }); // Texture Coordinate
    texture_data.put(new float[] { 0f, 0f, }); // Texture Coordinate
    
    texture_data.put(new float[] { 1f, 0f, }); // Texture Coordinate
    texture_data.put(new float[] { 0f, 0f, }); // Texture Coordinate
    texture_data.put(new float[] { 1f, 1f, }); // Texture Coordinate
    
    vertex_data.flip();
    texture_data.flip();
    
    int vbo_vertex_handle = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
    glBufferData(GL_ARRAY_BUFFER, vertex_data, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    
    int vbo_texture_handle = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, vbo_texture_handle);
    glBufferData(GL_ARRAY_BUFFER, texture_data, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    

    渲染VBO |代码

    还尝试实际绑定和解除绑定纹理!实际上,这也可能是一种奇怪的问题。

    texture.bind();
    
    glBindBuffer(GL_ARRAY_BUFFER, vbo_vertex_handle);
    glVertexPointer(vertex_size, GL_FLOAT, 0, 0l);
    
    glBindBuffer(GL_ARRAY_BUFFER, vbo_texture_handle);
    glTexCoordPointer(texture_size, GL_FLOAT, 0, 0l);
    
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    
    glDrawArrays(GL_TRIANGLES, 0, vertices); // The vertices is of course the max vertices count, in this case 6
    
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisableClientState(GL_VERTEX_ARRAY);
    
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    
    texture.unbind();
    

    当然使用纹理时,请记得调用glEnable(GL_TEXTURE_2D);

    修改

    我在代码中发现了错误,实际上是在说。

    int vboTexCoordHandle = texture.getTextureID();
    glBindBuffer(GL_ARRAY_BUFFER, vboTexCoordHandle);
    glBufferData(GL_ARRAY_BUFFER, textureData, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    

    但你需要说。

    int vboTexCoordHandle = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, vboTexCoordHandle);
    glBufferData(GL_ARRAY_BUFFER, textureData, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);