Question

I am writing a program that just needs to read a .obj file and render it in wire frame mode. I had already read the .obj file (correctly - i believe) that i want to render. But i am having some problems... It is suppose to be in wire frame but instead... (image bellow)

my output

Here is the code:

    public void render(GL gl){
    float xMiddle = (m.getXVertexMax() + m.getXVertexMin())/2;
    float yMiddle = (m.getYVertexMax() + m.getYVertexMin())/2;
    float zMiddle = (m.getZVertexMax() + m.getZVertexMin())/2;

    gl.glScalef(1/(m.getXVertexMax() - m.getXVertexMin()), 1, 1);
    gl.glScalef(1, 1/(m.getYVertexMax() - m.getYVertexMin()), 1);
    gl.glScalef(1, 1, 1/(m.getZVertexMax() - m.getZVertexMin()));

    gl.glBegin(GL.GL_TRIANGLES);
    {
        gl.glEnable(GL.GL_DEPTH_TEST);
        gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINES);
        for(int i = 0; i < m.faces.size(); i++ ){
            Vector3f n1 = m.normals.get(m.faces.get(i).getNormalIndices()[0] - 1); 
            Vector3f v1 = m.vertices.get(m.faces.get(i).getVertexIndices()[0] - 1);
            gl.glVertex3f(v1.x - xMiddle, v1.y - yMiddle, v1.z - zMiddle);
            gl.glNormal3f(n1.x, n1.y, n1.z);


            Vector3f n2 = m.normals.get(m.faces.get(i).getNormalIndices()[1] - 1); 
            Vector3f v2 = m.vertices.get(m.faces.get(i).getVertexIndices()[1] - 1);
            gl.glVertex3f(v2.x - xMiddle, v2.y - yMiddle, v2.z - zMiddle);
            gl.glNormal3f(n2.x, n2.y, n2.z);




            Vector3f n3 = m.normals.get(m.faces.get(i).getNormalIndices()[2] - 1); 
            Vector3f v3 = m.vertices.get(m.faces.get(i).getVertexIndices()[2] - 1);
            gl.glVertex3f(v3.x - xMiddle, v3.y - yMiddle, v3.z - zMiddle);
            gl.glNormal3f(n3.x, n3.y, n3.z);

        }
    }
    gl.glEnd(); 
}

NOTE: Vector3f in the code, is a data structure that i made.

I have tried everything i could find but still, it wont render the image as wire frame! :-/

Can anyone give a hand?

Était-ce utile?

La solution

gl.glBegin(GL.GL_TRIANGLES);
{
    gl.glEnable(GL.GL_DEPTH_TEST);
    gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINES);
    ...

Very few GL commands are valid inside a glBegin()/glEnd() block:

Only a subset of GL commands can be used between glBegin and glEnd. The commands are glVertex, glColor, glSecondaryColor, glIndex, glNormal, glFogCoord, glTexCoord, glMultiTexCoord, glVertexAttrib, glEvalCoord, glEvalPoint, glArrayElement, glMaterial, and glEdgeFlag. Also, it is acceptable to use glCallList or glCallLists to execute display lists that include only the preceding commands. If any other GL command is executed between glBegin and glEnd, the error flag is set and the command is ignored.

glEnable() and glPolygonMode() are not on that list.

Move them outside your glBegin() block.


    gl.glVertex3f(v1.x - xMiddle, v1.y - yMiddle, v1.z - zMiddle);
    gl.glNormal3f(n1.x, n1.y, n1.z);

Wrong way around. You want normal, then vertex:

    gl.glNormal3f(n1.x, n1.y, n1.z);
    gl.glVertex3f(v1.x - xMiddle, v1.y - yMiddle, v1.z - zMiddle);

glNormal() only sets the current normal, glVertex() is what actually sends that down the pipeline.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top