View Full Version : how to render an object as wireframe

bananeeek

04-10-2013, 07:47 AM

I managed to render an 3d egg object using GL_POINTS mode, I`ll post the code beneath, but now I need to render that egg not as a cloud of points but as a linked net of points, one of which has to be triangular net and the other square net. All I know is that I need to link those points in a specific order but I have no clue how to guess which points should I link.

Here's the function that renders an egg:

void Egg(void)

{

double N = 80.0;

double x,y,z;

for(double u=0; u<1;)

{

for(double v=0; v<1;)

{

x = ax(u,v);

y = ay(u,v);

z = az(u,v);

glBegin( GL_POINTS );

glVertex3f(x,y,z);

glEnd();

v += 1/N;

}

u += 1/N;

}

glFlush();

}

and the functions that calculate next coordinates

double const Pi = 3.14159265359;

double ax(double u, double v)

{

return (( ((-90.0)*(pow(u,5))) + (225.0*(pow(u,4))) - (270.0*(pow(u,3))) + (180.0*(pow(u,2))) - (45.0*u) ) * (cos(Pi*v)) );

}

double ay(double u, double v)

{

return ( (160.0*(pow(u,4))) - (320.0*(pow(u,3))) + (160.0*(pow(u,2))) );

}

double az(double u, double v)

{

return ( ( ((-90.0)*(pow(u,5))) + (225.0*(pow(u,4))) - (270.0*(pow(u,3))) + (180.0*(pow(u,2))) - (45*u)) * (sin(Pi*v)) );

}

same but not in a c++ code

x(u, v) = (−90u^5 + 225u^4 − 270u^3 + 180u^2 − 45u) cos(Pi*v)

y(u, v) = 160u^4 − 320u^3 + 160u^2

z(u, v) = (−90u^5 + 225u^4 − 270u^3 + 180u^2 − 45u) sin(Pi*v)

where 0 <= u <= 1 and 0 <= v <= 1

Carmine

04-10-2013, 04:08 PM

Read the docs on glBegin.

Try changing GL_POINTS to GL_LINES just to see what happens.

Hold v constant at 0.5 and let u do its loop.

Hold U constant at 0.5 and let v do its loop.

This should give you a feel for the topology of your points.

You can also accomplish your goal using GL_TRIANGLES and GL_QUADS,

or using GL_POLYGONS inside of glBegin.

bananeeek

04-13-2013, 08:37 AM

I can't simply change GL_POINTS to GL_LINES, because it requires two points to make a line, and the best I've achieved this way, was linking every two points, like a dash line. Here's a printscreen imageshack.us/photo/my-images/585/wireegg.png

Linking every 4 points or other combinations just gave me a mess of lines, that's why I asked for help...

And by doing those two loops separately where I hold one constant and let the other increase, it drew me just one vertical slice, not a whole egg. And when I tried to change the value of u constant (from 0.5, which drew me only a single dot) it drew me an additional half of vertical slice.

Carmine

04-13-2013, 01:57 PM

And by doing those two loops separately where I hold one constant and let the other increase, it drew me just one vertical slice, not a whole egg. And when I tried to change the value of u constant (from 0.5, which drew me only a single dot) it drew me an additional half of vertical slice. Now you are on the right track - as long as you ONLY want a wireframe object. If you can draw one vertical slice, you can draw ALL the vertical slices. Then do all the horizontal slices and you'll have a wireframe egg. Remember to use glBegin and glEnd to encapsulate each slice (i.e. just before and just after the inner loop is executed). Do all the vertical slices with your current code. Then write another piece of code with the inner and outer loop variables switched to get the horizontal slices.

jmetzgar

04-13-2013, 02:28 PM

I can't simply change GL_POINTS to GL_LINES, because it requires two points to make a line, and the best I've achieved this way, was linking every two points, like a dash line. Here's a printscreen imageshack.us/photo/my-images/585/wireegg.png

Linking every 4 points or other combinations just gave me a mess of lines, that's why I asked for help...

And by doing those two loops separately where I hold one constant and let the other increase, it drew me just one vertical slice, not a whole egg. And when I tried to change the value of u constant (from 0.5, which drew me only a single dot) it drew me an additional half of vertical slice.

Well, the simple answer is to figure out what you are drawing lines of. You said triangles and rectangles. Each of these is 3 sides and 4 sides. I'm supposing that you are actually wanting to draw two triangles to represent the rectangle? That means that your drawing code will look something like this:

glBegin(GL_TRIANGLES);

// Triangle 1

glVertex3f(V1);

glVertex3f(V2);

glVertex3f(V3);

// Triangle 2

glVertex3f(V4);

glVertex3f(V5);

glVertex3f(V6);

glEnd();

AND/OR

glBegin(GL_QUADS);

// Draw clockwise (counter-clockwise = BACK FACING)

glVertex3f(V1);

glVertex3f(V2);

glVertex3f(V4);

glVertex3f(V3);

glEnd();

Ok, so that'll draw the geometry for you. We're assuming the V1 and V2 are the top left and top right of the rectangle and V3 and V4 are the bottom left and bottom right of the rectangle. The triangle drawing code could be rewritten as:

glBegin(GL_TRIANGLES);

// Triangle 1 [drawing clockwise]

glVertex3f(V1);

glVertex3f(V2);

glVertex3f(V3);

// Triangle 2 [drawing clockwise]

glVertex3f(V2);

glVertex3f(V4);

glVertex3f(V3);

glEnd();

Ok, now the easiest way to draw lines is just to use GL_LINE_LOOP or GL_LINE_STRIP. GL_LINES works if you want to specify the first and second vertices for each edge. GL_LINE_LOOP and GL_LINE_STRIP let you specify those vertices once.

glBegin(GL_LINE_LOOP);

// Triangle 1 [drawing clockwise]

glVertex3f(V1);

glVertex3f(V2);

glVertex3f(V3);

glEnd(); // done with first triangle

glBegin(GL_LINE_LOOP)

// Triangle 2 [drawing clockwise]

glVertex3f(V2);

glVertex3f(V4);

glVertex3f(V3);

glEnd(); // done with second triangle

This works the same way for rectangles:

glBegin(GL_LINE_LOOP);

// Draw clockwise (counterclock wise = BACK FACING)

glVertex3f(V1);

glVertex3f(V2);

glVertex3f(V4);

glVertex3f(V3);

glEnd();

Whew! Now we know how to draw outlines of triangles and rectangles. Now we need to figure out what the vertices we need for drawing. This thankfully is pretty easy. You said we're drawing a shape given (U,V) where 0 <= U <= 1 and 0 <= V <= 1 with N segments. Let du = 1/N and dv = 1/N. Ok, so (ignoring the seam you need to mend), the vertices are going to be:

V1 = f(u,v);

V2 = f(u+du,v);

V3 = f(u, v+dv);

V4 = f(u+du, v+dv);

The seam will be left as an exercise for you to figure out. But let's just say that you already know the coordinates of those (just "connect" them).

bananeeek

04-14-2013, 06:32 AM

woohoo! Got it! Got it all : ) It took me an hour, but results are satisfying enough : )

Here's the code:

void Egg(int model)

{

double N = 35.0;

double x,y,z;

double du=1/N, dv=1/N;

double xdu=du/2;

if(model == 1)

{

for(double u=0; u<1; )

{

for(double v=0; v<1; )

{

glBegin( GL_POINTS );

glVertex3f(ax(u,v),ay(u,v),az(u,v));

glEnd();

v += 1/N;

}

u += 1/N;

}

}

if(model == 2)

{

for(double v=0; v<1; )

{

for(double u=0; u<1; )

{

glBegin( GL_LINE_LOOP );

glVertex3f(ax(u,v),ay(u,v),az(u,v));

glVertex3f(ax(u,v+dv),ay(u,v+dv),az(u,v+dv));

glEnd();

glBegin( GL_LINE_LOOP );

glVertex3f(ax(u,v+dv),ay(u,v+dv),az(u,v+dv));

glVertex3f(ax(u+du,v+dv),ay(u+du,v+dv),az(u+du,v+d v));

glEnd();

u+=1/N;

}

v+=1/N;

}

}

if(model == 3)

{

for(double v=0; v<1; )

{

for(double u=0; u<1; )

{

//horizontal circles

glBegin( GL_LINE_LOOP );

glVertex3f(ax(u,v),ay(u,v),az(u,v));

glVertex3f(ax(u,v+dv),ay(u,v+dv),az(u,v+dv));

glEnd();

//for multiplying vertical circles by 2, w/o it it's a net made of rhombs,

//additional circles cut rhombs exactly in half, thus making triangles

u += xdu;

glBegin( GL_LINE_LOOP );

glVertex3f(ax(u+du,v),ay(u+du,v),az(u+du,v));

glVertex3f(ax(u+du,v+dv),ay(u+du,v+dv),az(u+du,v+d v));

glEnd();

u -= xdu;

//vertical lines twisted right

glBegin( GL_LINE_LOOP );

glVertex3f(ax(u,v),ay(u,v),az(u,v));

glVertex3f(ax(u+du,v+dv),ay(u+du,v+dv),az(u+du,v+d v));

glEnd();

//vertical lines twisted left

glBegin( GL_LINE_LOOP );

glVertex3f(ax(u+du,v),ay(u+du,v),az(u+du,v));

glVertex3f(ax(u,v+dv),ay(u,v+dv),az(u,v+dv));

glEnd();

u += 1/N;

}

v += 1/N;

}

}

}

and shots of each model

points : imageshack.us/photo/my-images/692/pointegg.png/

wire : imageshack.us/photo/my-images/546/wiredegg.png/

triangles : imageshack.us/photo/my-images/854/triangularegg.png/

Thanks pals : )

jmetzgar

04-14-2013, 07:24 AM

If you are just drawing lines with two endpoints, just use GL_LINES

glBegin(GL_LINES);

// Line 1

glVertex3f(V1);

glVertex3f(V2);

// Line 2

glVertex3f(V3);

glVertex3f(V4);

// ... Line N

glVertex3f(...);

glVertex3f(...);

glEnd();

GL_LINE_LOOP with two vertices V1 and V2 draws a line from V1 to V2 and then back to V1 again.

GL_LINE_STRIP with two vertices draws a line from V1 to V2 and then stops

GL_LINES takes pairs of vertices at a time and draws LINES for each pair

GL_LINE_LOOP with 4 vertices V1, V2, V3, and V4 looks like this for GL_LINES

glBegin(GL_LINES);

glVertex(V1);

glVertex(V2);

glVertex(V2);

glVertex(V3);

glVertex(V3);

glVertex(V4);

glVertex(V4);

glVertex(V1);

glEnd();

And the same vertices with GL_LINE_STRIP

glBegin(GL_LINES);

glVertex(V1);

glVertex(V2);

glVertex(V2);

glVertex(V3);

glVertex(V3);

glVertex(V4);

glVertex(V4);

glEnd();

Powered by vBulletin® Version 4.2.3 Copyright © 2018 vBulletin Solutions, Inc. All rights reserved.