简体   繁体   中英

OpenGL wireframe from x,y, and z text file

I have a file with 3 coordinates and I can render it as points, lines, triangles , or any primitive. I want to construct a wireframe model of this file, what should I change or add to view it as a wireframe

sample:

void draw()
{   
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glOrtho(-50.0,50.0,-50.0,50.0,-50.0,50.0);  
    glColor4f(1.0f,1.0f,1.0f,1.0f);         
    glPointSize(3); 
    glLineWidth(3);
    glColor3f(1.0f,1.0f,1.0f);
    for(int i=0; i<points; i++)
    {   
        glBegin(GL_LINES);
        glNormal3f(0.0f, 0.0f, 1.0f);
        glVertex3d(vList[i][0],vList[i][1],vList[i][2]);
        glEnd();             
    }
}

This will never produce any output:

for(int i=0; i<points; i++)
{   
    glBegin(GL_LINES);
    glNormal3f(0.0f, 0.0f, 1.0f);
    glVertex3d(vList[i][0],vList[i][1],vList[i][2]);
    glEnd();             
}

GL_LINES requires two vertices per line, and you're only providing one between your glBegin and glEnd calls.

glBegin and glEnd should bookend particular pieces of geometry , not individual vertices.

However, simply moving the calls out of the for loop won't fix your problem:

glBegin(GL_LINES);
for(int i=0; i<points; i++)
{   
    glNormal3f(0.0f, 0.0f, 1.0f);
    glVertex3d(vList[i][0],vList[i][1],vList[i][2]);
}
glEnd();             

This would almost produce what you want, but will actually show every OTHER line, because it's treating each pair you send in as one line. So it will draw a line between point 1 and 2, and then between 3 and 4. This is because GL_LINES means "interpret each pair I send in as a completely new line, unrelated to the previous vertices.

What you really want is this:

glBegin(GL_LINE_STRIP);
for(int i=0; i<points; i++)
{   
    glNormal3f(0.0f, 0.0f, 1.0f);
    glVertex3d(vList[i][0],vList[i][1],vList[i][2]);
}
glEnd();             

Using GL_LINE_STRIP instructs OpenGL that it should take the first two vertices and draw a line, and then for each new vertex, draw another line from the end of the last line.

Caveat

All this assumes your file is actually designed to produce lines like this. Most 3D file formats include both vertices and indices. The vertices tell you the 3D positions, but the indices tell you which points should be connected to which. However, since this looks like a sort of homework assignment, I'm going to assume that the file is as described, a simple list of XYZ coordinates that should be connected in sequence.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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