Advertisement

Help drawing parametic surfaces?

Started by
9 comments, last by joshualimm 18 years, 11 months ago
hi. I need to draw a parameteric surface in opengl. I've managed to generate all the vertices correctly, but I've no idea how to correctly use them to draw a surface. Here's a snippet of my code:

float** calcShape2(int numVerts)
{
    float **vertices = (float**)malloc(sizeof(float*)*numVerts*numVerts);

    int i=0;
    float du = (M_PI*2)/numVerts;
    float dv = (M_PI*2)/numVerts;
    double x,y,z;
    int u,v=0;
    for (v=0; v<numVerts; v++)
    {
        for (u=0; u<numVerts; u++)
        {
            vertices = (float*)malloc(sizeof(float)*3);
            vertices[0] = (u*du)*cos(v*dv)/(2*M_PI);
            vertices[1] = (u*du)*sin(v*dv)/(2*M_PI);
            vertices[2] = (u*du)*sin(v*dv)*sin(u*du)/(2*M_PI);
            i++;
        }
    }
    return vertices;
}


and to draw it using quads:

for (i=0; i< pow(numVerts,2)-4; i+=4)
{
    glBegin(GL_QUADS);
        glVertex3fv(vert);
        glVertex3fv(vert[i+1]);
        glVertex3fv(vert[i+2]);
        glVertex3fv(vert[i+3]);
    glEnd();
}


but somehow, the surface is totally messed up. Can anyone help? Need to draw a smooth surface using the generate vertices. Thank you! [Edited by - joshualimm on September 18, 2005 11:47:57 PM]
Could you post a screenshot of what happens?
Advertisement



Anyway, it seems that the order of the generated vertices are wrong, and now, I've no idea how to get the quads/triangles/etc to work properly.

[edit]
Btw, I've tried reversing the order of the 2 for loops. But I still couldn't get the surface to appear correctly. Please help!!!

[edit 2]
ok, I've added another pic of the same formula used, only that it's drawn with points. What I need is to create a surface using these vertices (which I've stored in an array of array :))



[Edited by - joshualimm on September 18, 2005 10:42:03 AM]
I haven't tried running your code, but what shape are you trying to produce there? I can't work out if you're trying to draw a disc or a ring (or is it something else?)

In any case it may be because when you're drawing the vertices for each quad, they are being provided in different directions each time (i.e. some are clockwise, some are anti-clockwise) - first off try disabling face culling and see if the surface appears like it should.

If it does, before you draw each quad check it's surface normals and use this vector to find the direction they are pointing in. If the vector is pointing the wrong way, draw the vertices in a different order, otherwise draw them as you've already done.

Otherwise something's going wrong with the vertex calculations

use standard html <img> tags to attach your image inline btw
"I must not fear. Fear is the mindkiller. Fear is the little death that brings total obliteration. I will face my fear. I will permit it to pass over me and through me. And when it has gone past me I will turn to see fear's path. Where the fear has gone there will be nothing. Only I will remain." ~Frank Herbert, DuneMy slice of the web
finally, a reply!!! Thanks for taking your time to answer my post. Anyway, I'm not really trying to draw anything. I need to come up with a function that is robust enough that when given a parametric function (that will generate x,y,z position), will generate a list of vertices that, when joined together, form a surface.

My parametric function is working (the snippet is slightly outdated, but it does generate the correct position of each vertices), but I've no idea what order I should put them in my glBegin and glEnd, as well as what type I should use (triangles/quads/polygon).

I wouldn't be worried about culling. The shape is correct, but the surface is wrong, because of the order the vertices are stored and used.

Ok, lemme ask for this instead:

Quote:
can someone please show me how to generate a surface, given that the formula for it is:

x = cos(u)
y = cos(v)*sin(u)
z = sin(v)*sin(u)

or

x = u*cos(v)/2*PI
y = u*sin(v)/2*PI
z = u*sin(v)*sin(u)/2*PI


given:
0<= u <=PI
0<= v <=2*PI

thanks!
Quote: Original post by joshualimm
finally, a reply!!! Thanks for taking your time to answer my post.


Lol, taking my sweet time or taking the time? J/K ;) sorry couldn't resist..

Ah ok that second pic made it much clearer. By the looks of things, the vertices are ordered incorrectly as you say - for example during two iterations of the loop it may first calculate a point on one side of the surface, and the next iteration would produce a point on the other side. The two are therefore not connected, but are adjacent to each other in the vertex array.

A simple way to debug that would be to colour the points differently depending on what is drawn first, say starting at white and gradually changing to red.

I've been thinking about how to do this for a couple of hours now & I have to say this is actually a bit out of my league I'm afraid. I think a potential solution would be to build the surface up from scratch from say top left to bottom right - if you compare this to creating a sphere where you draw a strip at a pole, then move to the next subdivision (the radius & height increasing by defined amounts) and draw this as a strip, and so on:

kind of like this:

 \______/  <- second strip  \____/   <- first strip


So you would rearrange the loop to create the vertices from the furthest point (from either the center or the point at the other extreme), second furthest points.. etc.

Sorry I can't suggest something more concrete :/ I hope it makes sense though, I'll try and explain better if needed.
"I must not fear. Fear is the mindkiller. Fear is the little death that brings total obliteration. I will face my fear. I will permit it to pass over me and through me. And when it has gone past me I will turn to see fear's path. Where the fear has gone there will be nothing. Only I will remain." ~Frank Herbert, DuneMy slice of the web
Advertisement
heya, and thanks again. Anyway, that's what I've been trying to ask about: How do I reorder my many vertices so that I can generate the surface correctly, simply by looping through them

(actually, my question was to ask how to draw parametric surfaces... oh well)

By the way, by using 2 for loops to generate the vertices, I'm still stuck with either:

1)Slices from top to bottom, or
2)Slices from front to back

Either way, even if I manage to link them up together, i'll get planes, instead of a surface around it.

Oh, and here's a different shape generated with another parametric function. Looks pretty (i know, i know, thank you), got colors, but it kinda supports the "slices-of-planes" idea.

With GL_POINTS


With GL_QUADS (the code I'm using to generate these vertices are still the same as posted above)


So, is there perhaps, a gl/glu/glut function that allows me to
1)just specify the parameters, and generate a surface for me, or
2) Reorder all my vertices so that I can just loop through them all and generate my surface without a sweat?

Please help!!! Thanks
If you need help with OpenGL-specific stuff, take it to the OpenGL forum; try and keep this thread focused on API-independent ways of drawing the surfaces.
I've sent a request for this thread to be moved before your reply. Wouldn't want to start a double post. So no worries. :P

By the way, up till now, my codes HAVE been platform independent. (Thanks you guys for not straying off!), no wait... as for api independence, I think both gl/glu should be, since they are often used together. As for glut, well, it IS platform independent...oh well.

Did you find out how to do this? I can't find the other thread any more.
"I must not fear. Fear is the mindkiller. Fear is the little death that brings total obliteration. I will face my fear. I will permit it to pass over me and through me. And when it has gone past me I will turn to see fear's path. Where the fear has gone there will be nothing. Only I will remain." ~Frank Herbert, DuneMy slice of the web

This topic is closed to new replies.

Advertisement