G
G
gibsonen2018-03-17 22:33:21
Java
gibsonen, 2018-03-17 22:33:21

OPEN GL model not fully displayed?

Hello. I display the obj model on the screen, but for some reason it is not displayed completely, so-called "holes" appear on the figure. What is the problem?
Below is the render class:

public SceneRenderer(ObjModel objModel, List<ObjTexture> objTextureList) throws Exception {
        glEnable(GL_DEPTH_TEST);
        glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        glPointSize(10);
        String fragmentSource = IOUtils.toString(getClass().getResourceAsStream("shader.frag"));
        String vertexSource = IOUtils.toString(getClass().getResourceAsStream("shader.vert"));
        shaderProgram = Util.createShaderProgram(vertexSource, fragmentSource);
        for (ObjTexture objTexture : objTextureList) {
            int vaoTriangles = glGenVertexArrays();
            List<MultiIndex> multiIndices = objModel.getFaces(objTexture.getNameTexture());
            List<Polygon> polygons = objModel.getPolygons(objTexture.getNameTexture());
            vbao.add(new ArrayIdTriangle(vaoTriangles, multiIndices.size(), objTexture));
            glBindVertexArray(vaoTriangles);
            int vertexBuffer = glGenBuffers();
            glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
            glBufferData(GL_ARRAY_BUFFER, Geometry.getVertexCoord(objModel.getVertices(), multiIndices).rewind(), GL_STATIC_DRAW);
            glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
            glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
            int normalBuffer = glGenBuffers();
            glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
            glBufferData(GL_ARRAY_BUFFER, Geometry.getNormals(polygons, multiIndices).rewind(), GL_STATIC_DRAW);
            glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
            glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0);
            int textureBuffer = glGenBuffers();
            glBindBuffer(GL_ARRAY_BUFFER, textureBuffer);
            glBufferData(GL_ARRAY_BUFFER, Geometry.getTexture(objModel.getTexCoords(), multiIndices).rewind(), GL_STATIC_DRAW);
            glBindBuffer(GL_ARRAY_BUFFER, textureBuffer);
            glVertexAttribPointer(textureLocation, 2, GL_FLOAT, false, 0, 0);
        }
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindVertexArray(0);
    }
    void render() throws Exception {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
        FloatBuffer vMatrix = BufferUtils.createFloatBuffer(16);
        new Matrix4f()
                .lookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z,
                        0.0f, 0.0f, 0.0f,
                        0.0f, 1.0f, 0.0f).get(vMatrix);

        FloatBuffer mMatrix = BufferUtils.createFloatBuffer(16);
        new Matrix4f().translate(modelPosition)
                .rotateX(modelRotation.x)
                .rotateY(modelRotation.y)
                .rotateZ(modelRotation.z)
                .scale(modelScale)
                .get(mMatrix);
        glUseProgram(shaderProgram);
        glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "P"), false, pMatrix);
        glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "V"), false, vMatrix);
        glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "M"), false, mMatrix);
        glUniform3f(glGetUniformLocation(shaderProgram, "light_worldspace"), lightPosition.x, lightPosition.y, lightPosition.z);
        for (ArrayIdTriangle aVbao : vbao) {
            if (aVbao.getTexture() == null) {
                aVbao.setTexture(new Texture(ConstPath.FILE_TEXTURE.filePath + aVbao.getObjTexture().getTexturePath()));
            }
            glActiveTexture(GL_TEXTURE0);
            glBindTexture(GL_TEXTURE_2D, aVbao.getTexture().getId());
            glBindVertexArray(aVbao.getId());
            glEnableVertexAttribArray(vertexLocation);
            glEnableVertexAttribArray(textureLocation);
            glEnableVertexAttribArray(normalLocation);
            glDrawArrays(GL_TRIANGLES, 0, aVbao.getSize());
        }
    }

I read the model from the obj file and divide it into groups of triangles according to the same textures.
ArrayIdTriangle- contains the texture, the path to the texture and the name of the texture in the obj file
multiIndices- contains the indexes of the i-th group of triangles with a common texture.
Geometry.getVertexCoord(objModel.getVertices(), multiIndices)
- finds all vertices for which the texture is used

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Larin, 2018-03-18
@engine9

Alas, not a programmer (but since they called) perhaps the model itself has "inverted" normals, looking from the camera and discarded during rendering.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question