简体   繁体   English

LWJGL为什么有时我的几何图形渲染不正确?

[英]LWJGL Why does my geometry sometimes render incorrectly?

I've made a simple opengl application in java, using LWJGL and GLFW. 我使用LWJGL和GLFW在Java中制作了一个简单的opengl应用程序。 I created a class for rendering a textured box. 我创建了一个用于渲染纹理框的类。 It creates the vertex positions, texture coordinates, and uses a VBO. 它创建顶点位置,纹理坐标并使用VBO。 The box is rendered using GL_TRIANGLES . 该框使用GL_TRIANGLES渲染。 When I start the program to test it, sometimes: - The output is correct, I see the box rendered correctly. 当我启动程序进行测试时,有时:-输出正确,我看到正确显示的框。 - There's very big, weird (white/gray) triangle sticking out of the box. -盒子外面有一个很大的,怪异的(白色/灰色)三角形。 - The box is totally deformed, like there are a lot of triangles totally random. -盒子完全变形,就像有很多完全随机的三角形一样。

When I don't change any code, the result is still different every time. 当我不更改任何代码时,每次的结果仍然是不同的。 But sometimes the box is rendered correctly. 但是有时盒子正确显示。

I'm quite sure my vertices and texture coordinates are correct. 我非常确定我的顶点纹理坐标正确。 It's hard to find what's wrong. 很难找到问题所在。 I recreated the box class so it renders it using GL_QUADS (and of course rethought the vertex/texture coordinates). 我重新创建了box类,以便它使用GL_QUADS进行GL_QUADS (当然GL_QUADS重新考虑顶点/纹理坐标)。 But then I still get the same weird behaviour. 但是后来我仍然得到相同的怪异行为。

The hard thing is that the result is different every time the program runs, while the code isn't edited. 困难的是,每次程序运行时结果都是不同的,而代码没有被编辑。

Here some pictures, one of a run where it's all correct: 这是一些图片,都是正确的运行之一: 正确渲染框

And now I run the program again, and I get this: 现在,我再次运行该程序,我得到了: 在此处输入图片说明

And so on... 等等...

Does someone know what could be happening? 有人知道会发生什么吗? This is how I render the box: 这就是我渲染盒子的方式:

texture.bind();

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnable(GL_TEXTURE_2D);

glBindBuffer(GL_ARRAY_BUFFER, vboId);
glVertexPointer(3, GL_FLOAT, 0, 0);

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

glDrawArrays(GL_TRIANGLES, 0, vertices.length);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);

Texture.unbind();

It already could help if someone knows what causes the changing result. 如果有人知道导致更改结果的原因,它已经可以提供帮助。

The code of my box class: 我的盒子类的代码:

package shapes;

import static org.lwjgl.opengl.GL11.GL_FLOAT;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D;
import static org.lwjgl.opengl.GL11.GL_TEXTURE_COORD_ARRAY;
import static org.lwjgl.opengl.GL11.GL_TRIANGLES;
import static org.lwjgl.opengl.GL11.GL_VERTEX_ARRAY;
import static org.lwjgl.opengl.GL11.glDisable;
import static org.lwjgl.opengl.GL11.glDisableClientState;
import static org.lwjgl.opengl.GL11.glDrawArrays;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glEnableClientState;
import static org.lwjgl.opengl.GL11.glTexCoordPointer;
import static org.lwjgl.opengl.GL11.glVertexPointer;
import static org.lwjgl.opengl.GL15.GL_ARRAY_BUFFER;
import static org.lwjgl.opengl.GL15.GL_STATIC_DRAW;
import static org.lwjgl.opengl.GL15.glBindBuffer;
import static org.lwjgl.opengl.GL15.glBufferData;
import static org.lwjgl.opengl.GL15.glGenBuffers;

import java.nio.FloatBuffer;

import main.Geometry;

import org.lwjgl.BufferUtils;

import textures.Texture;

public class TriangleBox extends Geometry {

    private float sizeX;
    private float sizeY;
    private float sizeZ;

    private short[] indices;
    private float[] texCoords;

    private int vboId;
    private int texId;
    private int indId;

    private Texture texture;

    /**
     * Creates a beam figure (a bar)
     * 
     * @param widthX
     * @param heightY
     * @param depthZ
     */
    public TriangleBox(float widthX, float heightY, float depthZ, Texture tex) {

        this.sizeX = widthX;
        this.sizeY = heightY;
        this.sizeZ = depthZ;

        this.setTexture(tex);
        this.createVertices();
        this.createVBO();
    }

    public void setTexture(Texture tex) {

        this.texture = tex;
    }

    protected void createVertices() {

        vertices = new float[] { 
                //Bottom
                0, 0, 0,    
                1, 0, 0,    
                0, 0, 1,    

                0, 0, 1,    
                1, 0, 0,    
                1, 0, 1,    

                //Top
                0, 1, 0,    
                1, 1, 0,    
                0, 1, 1,    

                0, 1, 1,    
                1, 1, 0,    
                1, 1, 1,    

                //Front
                0, 1, 1,    
                1, 1, 1,    
                0, 0, 1,    

                0, 0, 1,    
                1, 1, 1,    
                1, 0, 1,    

                //Back
                1, 1, 0,    
                0, 1, 0,    
                1, 0, 0,    

                1, 0, 0,    
                0, 1, 0,    
                0, 0, 0,    

                //Left
                0, 1, 0,    
                0, 1, 1,    
                0, 0, 0,    

                0, 0, 0,    
                0, 1, 1,
                0, 0, 1,

                //Right
                1, 1, 1,
                1, 1, 0,
                1, 0, 1,

                1, 0, 1,
                1, 1, 0,
                1, 0, 0,
        };

        texCoords = new float[] { 
                // Bottom
                0f,     .33f,
                .25f,   .33f,
                0f,     .66f,

                0f,     .66f,
                .25f,   .33f,
                .25f,   .66f,

                // Top
                .75f,   .33f,
                .50f,   .33f,
                .75f,   .66f,

                .75f,   .66f,
                .50f,   .33f,
                .50f,   .66f,

                // Front  
                0f,     1f  ,
                .25f,   1f  ,
                0f,     .66f,

                0f,     .66f,
                .25f,   1f  ,
                .25f,   .66f,

                // Back
                .25f,   0f  ,
                0f,     0f  ,
                .25f,   .33f,

                .25f,   .33f,
                0f,     0f  ,
                0f,     .33f,

                // Left
                .75f,   .33f,
                .75f,   .66f,
                1f,     .33f,

                1f,     .33f,
                .75f,   .66f,
                1f,     .66f,

                // Right
                .50f,   .66f,
                .50f,   .33f,
                .25f,   .66f,

                .25f,   .66f,
                .50f,   .33f,
                .25f,   .33f
        };
    }

    public void createVBO() {

        FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(vertices.length);
        vertexBuffer.put(vertices);
        vertexBuffer.flip();

        FloatBuffer texCoordBuffer = BufferUtils.createFloatBuffer(texCoords.length);
        texCoordBuffer.put(texCoords);
        texCoordBuffer.flip();

        vboId = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboId);
        glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);

        texId = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, texId);
        glBufferData(GL_ARRAY_BUFFER, texCoordBuffer, GL_STATIC_DRAW);

        glBindBuffer(GL_ARRAY_BUFFER, 0);
    }

    public void render() {

        texture.bind();

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
        glEnable(GL_TEXTURE_2D);

        glBindBuffer(GL_ARRAY_BUFFER, vboId);
        glVertexPointer(3, GL_FLOAT, 0, 0);

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

        glDrawArrays(GL_TRIANGLES, 0, vertices.length);

        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
        glDisable(GL_TEXTURE_2D);

        Texture.unbind();
    }
}

Vertex shader: 顶点着色器:

#version 120

attribute vec3 position;

void main() {

    gl_Position = vec4(position.x, position.y, position.z, 1.0);
}

Fragment shader 片段着色器

#version 120

void main() {

    gl_FragColor = gl_Color; //vec4(0.1, 1.0, 1.0, 1.0);
}

Since in the pictures you show the box from different angles, my best guess would still be, there is something wrong with the vertices. 由于在图片中您从不同角度显示了方框,所以我最好的猜测仍然是,顶点存在问题。 So maybe you dont get different results everytime you run it, but actually only see the same result from different angles, which makes the object appear differently because of faceculling or other reasons. 因此,也许您每次运行时都不会得到不同的结果,但实际上只能从不同的角度看到相同的结果,这使得对象由于面部剔除或其他原因而出现不同的外观。

Try rendering a quad (easier to determin right vertices) and see if you still get the same result. 尝试渲染一个四边形(更容易确定右顶点),看看是否仍然得到相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM