-1

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();             
    }
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
aqeel
  • 7
  • 1
  • You shouldn't use the old and deprecated fixed-function pipeline functions. – vallentin Dec 08 '13 at 07:34
  • A wireframe is just lines. Hidden surface removal is the only thing I can think of you'd want to consider. E.g do you want to see the back-facing 1/2 of a sphere or not. – enhzflep Dec 08 '13 at 10:19
  • This should help: http://stackoverflow.com/questions/137629/how-do-you-render-primitives-as-wireframes-in-opengl – Full Frontal Nudity Dec 08 '13 at 15:22
  • @FullFrontalNudity You do know that, `glPolygonMode()` is deprecated, right? – vallentin Dec 08 '13 at 19:53
  • @Vallentin: glPolygonMode() is NOT deprecated. Learn your ropes; only GL_FRONT/GL_BACK PolygonModes are deprecated, GL_FRONT_AND_BACK calls are still present in Khronos OpenGL 4 without any sign of deprecation http://www.opengl.org/sdk/docs/man4/xhtml/glPolygonMode.xml –  Jan 04 '14 at 02:36

1 Answers1

3

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 X-Y-Z coordinates that should be connected in sequence.

Jherico
  • 28,584
  • 8
  • 61
  • 87
  • Dear Jherico, Totally agree with ur answer and already used GL_LINE_STRIP and the result was separated lines (connected all points), what I want actually is the output to be wireframe or mesh not only lines. Thanks – aqeel Dec 09 '13 at 01:44
  • If you want a mesh of connected lines, then you need to know the indices of the individual connections, i.e. the edges. You problably need to post more information in the question about the format of the file and how the geometry is designed. – Jherico Dec 09 '13 at 01:51
  • The file format is like this, I read an image and extract the pixel information position and intensity as XYZ, x and y the pixel position and the z is the intensity value of the pixel, then save the xyz in text file. xyz file read using vc++ and OpenGL to visualize it as 3d cloud of points, lines, .... This the whole idea, and I have tried to save the text file as rows then columns after that visualize both as a mesh its work, but I think its not correct way. – aqeel Dec 10 '13 at 02:53