VBO渲染顶点我没告诉它

时间:2013-08-20 14:34:58

标签: java opengl lwjgl vbo

在opengl中以立即模式渲染世界后,我开始学习VBO。一切运作良好,除了分散在世界各地,有这些随机顶点,我相信我没有指定。但是,当我将UV坐标从1更改为.5时,创建的形状会变小。我已经多次检查过我的紫外线坐标,但我似乎找不到任何错误。就像我说的那样,我是一个使用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,
    });
}

}

以下是我正在谈论的一些截图: http://imgur.com/a/YQ9NU

1 个答案:

答案 0 :(得分:2)

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

相关问题