# how to render complex shapes

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-12-2013, 10:11 AM
asda333
how to render complex shapes
Hi

I want to know how do you render a complex shape.

lets say i have a pyramid and i have vertex point for each corner in the pyramid and i want to render the surface of the object only.

i tried to filter through points, which have no other corners above or to the side but i just can't seem to get it to render.

e.g.

vertexpoints (vector filled with all points of the surface of the complex shape stored

now i want to render these points to form a solid 3d model but i can't do it
i tried triangle_strip
but it just shows layers of triangles on top of each other and not the model.

thanks
• 08-12-2013, 10:44 AM
carsten neumann
Just throwing a bunch of points at OpenGL will not give you the surface of an object, the points need to be specified in a careful order. For example, if you use GL_TRIANGLES as your primitive type you need to specify a multiple of three many points where each triple describes exactly one triangle (in either clockwise or counter-clockwise order). If you are not using indexed draw commands (glDrawElements() and friends) that may require that you specify certain points of your model multiple times (because that point is shared by multiple triangles) - that can even be necessary when using an index and multiple vertex attributes, but that is a bit more advanced topic.
If you are having trouble with this, check out one of the tutorials linked from the wiki, most will cover at some point how models are made up of triangles.
• 08-13-2013, 07:04 AM
asda333
so for example

lets say i have a list of vertices out of the list i keep on feeding opengl three points.

so i loop through the vectors

glBegin(GL_TRIANGLE_STRIP)
for(int i=0<i<vertexvector;i++)
{
if(vertexvector.z)==0||vertexvector==vertexvector. size()-1)
glvertexpoint3f(vertexvector.x,vertexvector.y+1,ve rtexvector.z); //top of triangle
glvertexpoint3f(vertexvector.x-1,vertexvector.y-1,vertexvector.z); //bottom left
glvertexpoint3f(vertexvector.x+1,vertexvector.y-1,vertexvector.z); //bottom right
if(vertexvector.z)>0&&vertexvector.x>0||vertextvec tor<vertexvector.size()-1&&vertexvector.y >0 and < vertexvector.size()-1
glvertexpoint3f(vertexvector.x,vertexvector.y+1,ve rtexvector.z); //top of triangle
glvertexpoint3f(vertexvector.x-1,vertexvector.y-1,vertexvector.z-1); //bottom left
glvertexpoint3f(vertexvector.x+1,vertexvector.y-1,vertexvector.z-1); //bottom right

//also if statement to check if x is 0 or end of vector and same with y then only draw in the z direction
if(vertexvector.x)==0||vertexvector.y)==0
glvertexpoint3f(vertexvector.x+1,vertexvector.y+1, vertexvector.z); //top of triangle
glvertexpoint3f(vertexvector.x,vertexvector.y,vert exvector.z-1); //bottom left
glvertexpoint3f(vertexvector.x+1,vertexvector.y,ve rtexvector.z-1); //bottom right

}
glEnd();

so the next one would be connected to the bottom left and draw it counterclockwise. until if i has reached the end of the vertexvector

something like that
• 08-13-2013, 07:28 AM
carsten neumann
Please use [code][/code] around source snippets. Without indention I have trouble making sense of the conditionals and what they control.

That being said, you should simply have a list of triangle corners and to draw them you just iterate through the list and pass each element to glVertex3f. You should also read up on how GL_TRIANGLE_STRIP specifies triangles: all but the first triangle are specified by a single new vertex (and reuse two previous ones).
• 08-14-2013, 03:25 PM
asda333
Quote:

Originally Posted by carsten neumann
Please use [code][/code] around source snippets. Without indention I have trouble making sense of the conditionals and what they control.

That being said, you should simply have a list of triangle corners and to draw them you just iterate through the list and pass each element to glVertex3f. You should also read up on how GL_TRIANGLE_STRIP specifies triangles: all but the first triangle are specified by a single new vertex (and reuse two previous ones).

that's the hard part, i only have points i don't know how to get them into triangle points unless i just take any 3 points.
so what you are saying for the first triangle you use the first three points then the second triangle uses the the previous two and the current one.

so something like this (just pseudocode)

loop through list of points.
get first point
point 1 = x value
point 2 = y value
point 3 = y+1 value
for second and upto last
point 1 = x-1
point 2 = y-1
point 3 = x

but then how would i do it for the z direction do i then

i want to implement marching cubes algorithm using these voxel points, that's why the triangles.

i was thinking would using gl_lines connect all the points and would make the model appear like a wireframe mesh

thanks
• 08-14-2013, 03:38 PM
carsten neumann
If your data is just a bunch of points and you currently have no information which ones form triangles you can only visualize them with GL_POINTS. Everything else requires that there is some implicit structure to the points, for example they are ordered such that points 0-2 form a triangle, points 3-5 form another triangle, etc. (for use with GL_TRIANGLES). If your points don't have that kind of structure, but you know that they are all on the surface of an object (e.g. they were generated by a 3D scanner) then you can use marching cubes to reconstruct a triangle approximation of that surface.
• 08-14-2013, 07:17 PM
GClements
Quote:

Originally Posted by asda333
i want to implement marching cubes algorithm using these voxel points, that's why the triangles.

Marching cubes generates the topology (which vertices form each triangle) as well as the vertex coordinates.

In order to render lines or polygons, you need two pieces of data:

1. The vertex coordinates
2. The topology

The first is typically an array which is passed to glVertexPointer(). The second is an array of integers which is passed as the indices parameter to glDrawElements(). In some specific cases, it is possible to order the vertices so that the index array is just [0,1,2,...], in which case you can use glDrawArrays() instead, but this is rarely possible for closed surfaces.

The key point is that you need to know the topology, i.e. which vertices form each triangle. It can't be deduced from the vertex coordinates.
• 08-15-2013, 05:10 AM
asda333
is there like an algorithm to deduce the topology from a set of points
there is the long way of doing it by by rendering cubes for each point.
then obtain the corners and then from that obtain which parts for the triangle and which ones don't using the 15 combinations.
the only problem i had with that is when i try to check the corner it says it is part of the shape so i will have to check the neighbouring voxel.
so my function will have a lot of checks about 15+ for each combination and the location of the voxels.
then this way i can get the topology.

so all that i will be doing is replacing voxels with triangles.

also would it work if you had something like

if(triangle)

else
glbegin(gl_triangle)

glend();

or should i have the loop outside the glbegin

if(triangle)
glbegin(triangle)
glend
else
glend

what im trying to say is do i have to give all my points in one gl begin or can i have them in two separate glbegins and it will still draw both together and not make them separate.

will see how that goes.
• 08-15-2013, 06:11 AM
GClements
Quote:

Originally Posted by asda333
is there like an algorithm to deduce the topology from a set of points

No. For any given set of vertices, there are usually multiple valid topologies. An algorithm cannot possibly know which one you want.

If you're using marching cubes, you should already have the topology.

Quote:

Originally Posted by asda333
do i have to give all my points in one gl begin or can i have them in two separate glbegins and it will still draw both together and not make them separate.

If you're using GL_TRIANGLES or GL_QUADS, you can split them into multiple blocks. If you're using fans or strips, then you'll need to copy over either the first vertex (for a fan) or the last two vertices (for a strip).
• 08-15-2013, 10:06 AM
Carmine
Quote:

... i have a pyramid and i have vertex point for each corner in the pyramid
For this example I would use glBegin(GL_POINTS) to render the points only (without trying to connect them). THEN, I would loop through the vertex array again using the coordinates to position labels for each point. So, first point in the array would be labeled '1', next point '2', etc. With each point labeled it would be easy to figure out how to connect them to make triangles.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last