View Full Version : GL_TRIANGLES

08-14-2002, 08:40 PM
I am calling glBegin (GL_TRIANGLES) with quite a number of points, but when I view my object there appears to be a couple square on screen. Is there any chance that GL:TRIANGLES is optimizing the unnecessary triangles? (i.e. if two triangles form a rectangle, does opengl display the two triangles, or the rectangle?)

If opengl doesn't optimize, any one have any ideas what my error is?? is it possible to make squares with no diagonal line with gl:triangles?


08-15-2002, 12:12 AM
This may or may not be an error depending on what are the points that you are providing to the card. what glBegin(GL_TRIANGLES) does is that it takes every 3 points that you provide and form a triangle from them. ie if you have 6 points in the glBegin()...glEnd(), you have 2 triangles. and yes, it is possible to make a quad with no diagonal lines. (disclaimer: i am not a pro at this so there is a chance that i might be wrong)

08-15-2002, 01:43 AM
First of all, unless you have lighting enabled, all triangles will be the same colour and therefore any pair of adjacent triangles might look like a quad (square in geekspeak). Even with lighting a pair of adjacent triangles might look like a quad if they are coplanar. But maybe I misunderstood your question, is your object displaying properly at all?

08-15-2002, 07:34 PM
My object is not displaying properly at all right now, but before I put the lighting stuff in, and played around with the frustrum, most of my object would render except for 2 cases. I guess a small explanation of what i'm doing in my program might help: i'm creating triangles between two contours, so that I can render the surface between them . . kind of like a crumpled circular ribbon. the two cases that seemed to fail, were either the beginning or the end of the ribbon (which is probably algorithmic) and when two triangles formed what's called a triangle strip, my program seemed to render only a square when I viewed it as a triangular mesh.

Since I used only GL:TRIANGLE to render my points, and this was before any lighting or depth buffering, i'm confused how a square got onto my screen.

[This message has been edited by jenny (edited 08-15-2002).]

08-16-2002, 06:36 PM
Post some code...

08-16-2002, 11:58 PM
I would post code, except for the couple facts that it's a big section of code, and it's written in LISP, so i could be very wrong here, but since OpenGl is normally a C/C++ language (i'm calling opengl as foreign functions in lisp) and instead of using GLUT, i'm using a package that was specifically written for the program i'm adding to plus CLX . . . .well . . i don't know if posting code will help to solve the problem that much . .. . . in addition, since it isn't my program, i'm not exactly sure that i should be posting code to it since I don't own it. . .

08-17-2002, 12:29 AM
I'm still not sure what your problem is. Are you viewing your mesh in wire frame and two adjacent triangles forming a strip are being rendered as a square? I.e there's no diagonal separating these two triangles? That sounds like your shared vertices do not match up exactly, try using indexed primitives.

08-17-2002, 12:29 AM
if this helps .. . my code is approximately:

;somewhere before this i set a variable called contour list to
(setf contour-list ((((x y z)(x y z)(x y z))((x y z)(x y z)(x y z))etc...) etc . . . )

-->in lisp the parenthesis signify a linked list. the important thing to understanding lisp, is that it doesn't matter how it's stored in memory (in lisp, the computer takes care of memory management, not the human), what's important is that the following is a linked list: '(1 9 'a 'b 4.5 x y z "string" #'my-function), and mixed varaible types are allowed, and you can even store functions in a linked list if you want. ta da. poof! its a linked list. just because i wrote the paranthesis. (it's a whole lot easier then C isn't it?)

then I have:
(gl:glBegin gl:GL_TRIANGLES)
(dolist (contour contour-list)
(dolist (triangle contour)
(dolist (pt triangle)
(eval`(gl:glVertex3d ,@pt)))))(gl:glEnd)

-> what this means: the dolist is a way of parsing off the outermost parantheis and then scrolling through all the elements. the first argument after dolist is the variable that is being set in each loop, the second argument is the list. so contour-list is a list of contours. contours is a list of points that are supposed to be interpreted that every three points are a triangle. pt is a list of x y z coordinates. ,@ means to remove the elements from a list and use each element as individual arguments. more or less, I have that structure because it isn't any harder for me to write a list of lists of lists, then it is to write one list, and it's easier for me to read the points and know whats going on with the lists of lists. it's a linked-list so i don't have to worry about size, and in lisp making a linked list is as easy as writting paranthesis around what u want a list of.

in C/C++ this is basicly a cute way of:

for(x = 0; x++; x >= size of array)
glVertex (array[x 0], array[x 1], array[x 2]);

But, it doesn't really matter what the points are, how they got there, or what my code looks like. To my understanding GL_TRIANGLES can only make triangles . . . . this is the only time when glBegin is called in any of the 50,000 lines of this program .. . . . so how come a see squares sometimes??? this was before there was any lighting .. . there was perpective. . .but to my understanding perspective makes things look smaller in the distance (i.e. more triangular). . .not squarish. my code is supposed to put what would normally be called with triangle-strips and triangle-fans on the screen (but i don't want to write the code to sort out which sets of points are fans and which are triangles. . .thus i'm using glTriangle).

In summation, my question is: Where I should see triangle-strips like objects, I see squares. Why?

[This message has been edited by jenny (edited 08-17-2002).]

08-17-2002, 03:10 AM
"GL_TRIANGLES can only make triangles"

wtf is going on here... jenny, a square IS two coplanar triangles. so if you have triangles that are coplanar and lined up right, you might get something like this:

| \ |
| \ |

that's a crappy drawing, but the point is you can get squares when you're drawing triangles. of course, if you're drawing in wireframe mode then you'll actually see two triangles, but if you're drawing in filled mode then you'll see a square.

but i can't imagine that this is what you're talking about. you must be phrasing the issue incorrectly or something.

lisp? man, i feel bad for you.

edit: had to fix the crappy drawing.

[This message has been edited by SThomas (edited 08-17-2002).]

08-17-2002, 07:51 AM
i get that it's supposed to look like that. the problem is that it does NOT look like that -- in wireframe mode, the above drawing does not render the diagnol line that's obviously supposed to be there. i'm trying to make the above picture, but its not showing correctly

and since my program is supposed to be creating a triangular mesh . . .missing that diagonal means it's not a triangular mesh . . .

What are indexed primitives?

[This message has been edited by jenny (edited 08-17-2002).]

08-17-2002, 09:16 AM
A triangle strip like the one in the drawing above has two triangles and four vertices. Some of these vertices are shared, i.e. they are a part of two triangles, not just one. For a strip like this you are sending 6 vertices, you send the shared vertices twice. If the values for these vertices don't match up exactly you might end up with slightly overlapping triangles which could cause the effect you're describin. Have you got polygon smoothing on? Turn it off if you do. Anyway, using indexed primitives you point OpenGL to an array of vertices and then you just supply indices into this array instead of actual vertices. This let's you take advantage of shared vertices. If you're sure your vertex data is correct then there should be a visible diagonal (no squares). Otherwise your graphics card is breaking OpenGL's rasterization rules or you have some weird state on that causes this. If you change the view, do the offending triangles flicker? Maybe you should try pushing your near plane (specified with gluPersopective probably) out somewhat and see if that helps.

08-17-2002, 12:02 PM
Well I'll see what I can do. . . I don't think that I can use an array of vertices, because I don't belive that its supported in the LISP wrapper library for opengl that i'm using. Thanks. I'm going to try pushing my near plane out somewhat though.

08-17-2002, 05:18 PM
If I were you I would try to manually draw a few (two or three) triangles to test is my stuff is drawing properly...

08-17-2002, 09:34 PM
my apologies jenny, from your posts it seemed to me that you were drawing in filled mode.

but you're getting no diagonal on some of your triangles in wireframe mode? yeah, that's pretty bizarre.