java - 我没有告诉它 VBO 渲染顶点

标签 java opengl lwjgl vbo

在 opengl 中以即时模式渲染世界后,我开始学习 VBO。一切都运行良好,除了分散在世界各地的这些随机顶点,我确信我没有指定。然而,当我将 UV 坐标从 1 更改为 0.5 时,创建的形状会变小。我已经检查了我的 UV 坐标好几次了,但我似乎找不到任何问题。就像我说的,我在使用 VBO 方面有点菜鸟,所以任何帮助我将这些形状从我的世界中剔除的人将不胜感激。这是我的源代码(纹理的 ss=S 坐标开始和 se = S 坐标结束):

public class MyProject {

int width=1,depth=1,height=1,w=854,h=480;
float size= .50f;
Camera cam = new Camera();
Block[][][] blocks = new Block[height][width][depth]; 
int textureId,vboHandle,vtoHandle;
FloatBuffer vbo,vto;

public static void main(String[] args){
    MyProject mp = new MyProject();
    mp.start();
}

public void start(){
    initDisplay();
    initGL();
    initWorld();
    run();
}

public void initWorld(){
    for(int x = 0; x<blocks[0].length;x++){
        for(int y = 0; y<blocks.length;y++){
            for(int z = 0; z<blocks[0][0].length;z++){
                blocks[y][x][z] = new Block(x,y,z);
                blocks[y][x][z].rendertype=GL11.GL_QUADS;
            }
        }
    }
}

public void initDisplay(){
    try {
        boolean set=false;
        DisplayMode[] displays = Display.getAvailableDisplayModes();
        for(int i=0;i<displays.length;i++){
            DisplayMode d = displays[i];
            if(d.getWidth()==w&&d.getHeight()==h){
                Display.setDisplayMode(d);
                set=true;
                break;
            }
        }
        if(!set)Display.setDisplayMode(new DisplayMode(w,h));
        Display.create();
    } catch (LWJGLException e) {
        e.printStackTrace();
    }
}

public void initGL(){
    GL11.glEnable(GL11.GL_TEXTURE_2D);
    GL11.glEnable(GL11.GL_CULL_FACE);
    GL11.glShadeModel(GL11.GL_SMOOTH);
    GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    GL11.glClearDepth(1.0);
    GL11.glEnable(GL11.GL_DEPTH_TEST);
    GL11.glDepthFunc(GL11.GL_LEQUAL);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();

    GLU.gluPerspective(45.0f, (float)w/(float)h, .001f, 100f);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    textureId = Texture.loadTexture(Texture.loadImage("/imgs/texture.png"));
    vboHandle = GL15.glGenBuffers();
    vtoHandle = GL15.glGenBuffers();

}

public void run(){
    while(!Display.isCloseRequested()){
        Input.tick();
        cam.tick();
        render();
        Display.update();
        Display.sync(60);
    }
    Display.destroy();
    GL15.glDeleteBuffers(vboHandle);
    GL15.glDeleteBuffers(vtoHandle);
    GL11.glDeleteTextures(textureId);
}

public Block getBlock(int x, int y, int z){
    if(x<0||x>=width-1||y<0||y>=height-1||z<0||z>=depth-1){
        return null;
    }
    else return blocks[y][x][z];
}

public boolean checkSurroundings(int x, int y, int z){
    if(getBlock(x,y-1,z)!=null&&getBlock(x,y+1,z)!=null&&
            getBlock(x,y,z-1)!=null&&getBlock(x,y,z+1)!=null&&
                    getBlock(x-1,y,z)!=null&&getBlock(x+1,y,z)!=null){
        return true;
    }
    return false;
}

public void render(){
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    GL11.glLoadIdentity();
    GL11.glRotatef(cam.pitchrot, 1, 0, 0);
    GL11.glRotatef(cam.yawrot, 0, 1, 0);
    GL11.glRotatef(cam.rollrot, 0, 0, 1);
    GL11.glTranslatef(cam.x, cam.y, cam.z);
    GL11.glColor3f(1.0f, 1.0f, 1.0f);
    vbo = BufferUtils.createFloatBuffer(width*height*depth*24*3);
    vto = BufferUtils.createFloatBuffer(width*height*depth*24*2);
    for(int x = 0; x<blocks[0].length;x++){
        for(int y = 0; y<blocks.length;y++){
            for(int z = 0; z<blocks[0][0].length;z++){
                Block b = blocks[y][x][z];
                if(b!=null)
                    renderCubeVBO(b);
            }
        }
    }
    vbo.flip();
    vto.flip();

    glBindBuffer(GL_ARRAY_BUFFER, vboHandle);
    glBufferData(GL_ARRAY_BUFFER, vbo, GL_STATIC_DRAW);
    glVertexPointer(3, GL_FLOAT, 0, 0L);
    glBindBuffer(GL_ARRAY_BUFFER, vtoHandle);
    glBufferData(GL_ARRAY_BUFFER, vto, GL_STATIC_DRAW);
    glTexCoordPointer(2, GL_FLOAT, 0, 0);

    glDrawArrays(GL_QUADS, 0, width*height*depth*24*3);
}

public void renderCubeVBO(Block b){
    vbo.put(new float[]{
        //top
        b.x+size, b.y+size, b.z-size,
        b.x-size, b.y+size, b.z-size,
        b.x-size, b.y+size, b.z+size,
        b.x+size, b.y+size, b.z+size,
        //bottom
        b.x+size,b.y-size, b.z+size,
        b.x-size,b.y-size, b.z+size,
        b.x-size,b.y-size, b.z-size,
        b.x+size,b.y-size, b.z-size,
        //front
        b.x+size, b.y+size, b.z+size,
        b.x-size, b.y+size, b.z+size,
        b.x-size, b.y-size, b.z+size,
        b.x+size, b.y-size, b.z+size,
        //back
        b.x-size, b.y +size, b.z-size,
        b.x+size, b.y +size, b.z-size,
        b.x+size, b.y-size, b.z-size,
        b.x-size, b.y-size, b.z-size,
        //left
        b.x-size, b.y+size, b.z+size,
        b.x-size, b.y+size, b.z-size,
        b.x-size,b.y-size, b.z-size,
        b.x-size,b.y-size, b.z+size,
        //right
        b.x+size, b.y+size, b.z-size,
        b.x+size, b.y+size, b.z +size,
        b.x+size, b.y-size, b.z+size,
        b.x+size, b.y-size, b.z-size,
    });

    vto.put(new float[]{
        //top
        b.se, b.ts, b.ss, b.ts, b.ss, b.te, b.se, b.te,
        //bottom
        b.se, b.ts, b.ss, b.ts, b.ss, b.te, b.se, b.te,
        //front
        b.se, b.ts, b.ss, b.ts, b.ss, b.te, b.se, b.te,
        //back
        b.se, b.ts, b.ss, b.ts, b.ss, b.te, b.se, b.te,
        //left
        b.se, b.ts, b.ss, b.ts, b.ss, b.te, b.se, b.te,
        //right
        b.se, b.ts, b.ss, b.ts, b.ss, b.te, b.se, b.te,
    });
}

}

以下是我所讨论内容的一些屏幕截图: /image/yFtdY.jpg

最佳答案

glDrawArrays 获取要渲染的顶点数,而不是附加缓冲区的大小。您在那里得到了一些神奇的数字 (24 * 3),因此您很可能混淆了这些数字。

关于java - 我没有告诉它 VBO 渲染顶点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18337791/

相关文章:

c++ - 隐藏光标/扭曲鼠标 (SDL 1.3)

java - JOGL glArrayElement点提供0,0,0

java - 使用着色器在 Windows 中工作但在 Linux 中不工作的 OpenGL 渲染

java - Lwjgl 启动时出现错误代码 0x502

java - Spring session + Spring Web 套接字。根据 session ID 向特定客户端发送消息

java.lang.IllegalStateException : Could not find a method?

c++ - 分层窗口中的 OpenGL 立体

javascript - OpenGL方法数值

java.security AES 加密 key 长度

java - 我的构造函数不工作 谁能帮我解决这个问题?