Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 10 of 11

Thread: how to render complex shapes

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Aug 2013
    Posts
    6

    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
    quad_strip
    but it just shows layers of triangles on top of each other and not the model.

    thanks

  2. #2
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    749
    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.

  3. #3
    Junior Member Newbie
    Join Date
    Aug 2013
    Posts
    6
    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
    Last edited by asda333; 08-13-2013 at 07:13 AM.

  4. #4
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    749
    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).

  5. #5
    Junior Member Newbie
    Join Date
    Aug 2013
    Posts
    6
    Quote Originally Posted by carsten neumann View Post
    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

  6. #6
    Advanced Member Frequent Contributor
    Join Date
    Apr 2010
    Posts
    749
    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.

  7. #7
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    490
    Quote Originally Posted by asda333 View Post
    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.

  8. #8
    Junior Member Regular Contributor
    Join Date
    Apr 2012
    Location
    Los Angeles
    Posts
    187
    ... 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.

  9. #9
    Junior Member Newbie
    Join Date
    Aug 2013
    Posts
    6
    I guess so, but if you have a lot of labels, it becomes confusing which three shall i use then which three shall i use after as you have to connect it and the surface does become slanted at some point towards the z axes.

    that's why i prefer the marching cubes algorithm. maybe if i can mix this with marching cubes then it might work but i would only like to label points which are on the outside so i need to figure out an algorithm to test if the voxel is a surface voxel or inside shape voxel.

    thanks everybody
    will work on it to see if i get anything
    i can probably get it to render if i render each point by extracting 8 corners and draw the cube for each point.
    but will have to try and get the marching cube algorithm to work

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •