PDA

View Full Version : Draw a triangle and his borders too??



Vincent Dumont
04-11-2006, 05:34 PM
Hello, I am new to OpenGL and I'd like to draw triangles but also to draw their borders.

The standard way draws solid/plain triangles.
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE) draws triangles' borders.

I would like both, is that possible? Thank you! :)

04-11-2006, 06:54 PM
you mean draw a triangle in both wire frame and solid? what's stopping you?

Vincent Dumont
04-11-2006, 07:28 PM
Well I don't know how


if I do for example

glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glEnd(); This will do solid triangles

And

glPolygonMode(GL_FRONT_AND_BACK,GL_LINE)
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glEnd();This will do wire frame


But how can I do wireframe and solid at the same time? Do I have to draw them separatly?

04-11-2006, 07:52 PM
it would help if you could detail what effect you're after. if this is some sort of selection outline for a solid in an editor, i've never really come up with anything satisfactory. i've played with stippled lines, texmapped lines, plain old lines, wireframe solid lines, green eggs and lines... nothing quite suits me. i like the texmapped lines the best, i think, for selection anyway.

04-11-2006, 08:13 PM
You should be able to do both, just draw with one mode then with the other.

// Filled
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
glBegin(GL_TRIANGLES);
glVertex....
glEnd();

// Wireframe
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
glBegin(GL_TRIANGLES);
glVertex....
glEnd();

Vincent Dumont
04-11-2006, 08:13 PM
I will post a screenshot of what I'm looking for tomorrow morning (don't have the models here)

Vincent Dumont
04-11-2006, 08:22 PM
I tried this and it's nearly what I'm looking for. My problem is that the lines lack width and they look stippled

http://www.magwa.ca/vincent/borders.gif


I use glPolygonMode(GL_FRONT_AND_BACK, GL_FILL) for the solid ones, and GL_LINE for the wire frame.

04-11-2006, 08:30 PM
yeah, that's z-fighting, and it's considered by many the worst thing imaginable. you could try glPolygonOffset to slide the solid part "into" screen a bit, but this isn't perfect either.

if you don't care about the lines being visible through the object you could of course disable the depth test or you could try playing with the stencil buffer to mask the parts that are backfacing...

04-11-2006, 10:13 PM
it's been a while, but i just did a quick test with a cube and it worked pretty well. you can go 2 routes: push the solid geometry in, or pull the lines out.

push solids in:
glPolygonOffset(1,1);
glEnable(GL_POLYGON_OFFSET_FILL);
draw solids...
glDisable(GL_POLYGON_OFFSET_FILL);
draw lines normally

pull lines out:
draw solids normally...
glPolygonOffset(-1,-1);
glEnable(GL_POLYGON_OFFSET_LINE);
draw lines...
glDisable(GL_POLYGON_OFFSET_LINE);

maybe the 2nd approach is better, but who really knows for sure...

Vincent Dumont
04-12-2006, 07:22 AM
This improved the drawing a lot, but still there are some small glitches with it.

See the following:

http://www.magwa.ca/vincent/glitch.GIF

Relic
04-12-2006, 08:42 AM
This is an indication for depth buffer imprecision.

Make sure your pixelformat has 24 bits of depth.
glGetIntegerv(GL_DEPTH_BITS, &numDepthBits).
If not, change your pixelformat selection code. 24 bits of depth is the max. what today's hardware offers mainly.

Then check your viewing frustum zNear and zFar settings and make the ratio zFar/zNear as small as possible without clipping your geometry, that is push zNear out and draw zFar in to increase the depth buffer precision.

Vincent Dumont
04-12-2006, 09:35 AM
Hi Relic, thank you for the answer.

glGetIntegerv(GL_DEPTH_BITS, &numDepthBits) returned 16 bits

How can I set the pixel format to 24? (Sorry I've only been doing OpenGL for 3 days now)

I am using wxWidgets (don't know if that means something for the pixel format)

Also, for the zNear and zFar settings, are you talking about something like this --->

This is what I have:


GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);

const float aspectRatio = (float)(viewport[2]) / (float)(viewport[3]);
gluPerspective(45, aspectRatio, 0.1, 200);

04-12-2006, 10:56 AM
u try nehe tuts? they helped me! :)

Vincent Dumont
04-12-2006, 11:53 AM
Yeha but they set it related with Windows API

04-12-2006, 12:04 PM
be careful with the value you use with polygonoffset. the factor and units need to be in proportion to your depth range. the last image suggests to me that your values are out of whack. try something smaller... experiment.

i dunno about the wxWidget stuff. don't they have a forum or tutorials or documents? the windows setup stuff is tedious but very straightforward, and you need only do it once.

Vincent Dumont
04-12-2006, 12:25 PM
Setting the PolygonOffset to -0,485 is way better, but not perfect. I still need to figure out how to set the pixel format to 24 bits.

Sorry I am quite new with OpenGL.

- What do you mean by "depth range"? Are you talking about this: gluPerspective(45, aspectRatio, 0.1, 200); ???

- I've found nothing in wxWidget's doc but I'll ask on forums

- How can I enlarge the width of the GL_LINES?

04-12-2006, 12:32 PM
http://pyopengl.sourceforge.net/documentation/manual/gluPerspective.3G.html

and

http://www.rush3d.com/reference/opengl-redbook-1.1/

and give that wiki a go... looks interesting ;)

04-12-2006, 01:02 PM
uh, you're welcome, i guess. gees, this is a tough crowd!

:D

Vincent Dumont
04-12-2006, 01:19 PM
Thank you very much indeed :) I'll read some more.

songho
04-12-2006, 01:45 PM
Originally posted by Vincent Dumont:
- What do you mean by "depth range"? Are you talking about this: gluPerspective(45, aspectRatio, 0.1, 200); ???
Relic meant that increase the near value AND decrease the far value, so the depth range gets narrower and the depth precision is increased as a result.
==song==

earthquad
04-12-2006, 01:48 PM
http://www.opengl.org/wiki/index.php/General_OpenGL:_The_Depth_Buffer

Vincent Dumont
04-12-2006, 02:25 PM
I was able to set the Depth buffer size to 24 bits but it didn't do it, I guess the problem is with the depth range.

I'll read more on the depth buffer and get back to you guys.

earthquad
04-12-2006, 02:51 PM
that's great, vincent. let us know how it works out.

Overmind
04-12-2006, 04:16 PM
When you modify the depth range, first put out the near plane as far as possible. For your values, changing the near plane to 0.2 would have the same effect as changing the far plane to 100 ;) .

Just try some other near values, like 0.5 or even 1.0, this should dramatically increase depth precision.

Vincent Dumont
04-13-2006, 12:54 PM
I tried playing with the near/far values but id didn't change except sometimes it would clip parts of the drawing...

I'll upload a video sample soon ;)

Vincent Dumont
04-13-2006, 12:58 PM
Here it is!! Video sample (http://www.magwa.ca/vincent/sample3.avi) Video sample 2 (http://www.magwa.ca/vincent/sample4.avi)

Vincent Dumont
04-13-2006, 01:20 PM
If I understand correctly, the depth buffer precision is affected by zNear and zFar, which are in the line


gluPerspective(45, aspectRatio, 1.0, 200); 1.0 being the "Near" value, 200 being the "far" one.

(I actually thought this was used for clipping purposes only)

To get a better buffer precision I have to push the NEAR to the biggest number possible and lower the FAR to the lowest number possible?

I tried setting it to 3.0 and 150 and it didn't change much, in fact I think it made it worse.

earthquad
04-13-2006, 08:36 PM
yep. Looks like you've got a handle on it.

I see you've still got some glitches. Looks like maybe your polygonoffset and/or near/far values need some more tweaking. Like I said, this isn't perfect, but it shouldn't be nearly that bad, not in such a small range.

KreK
04-15-2006, 05:59 AM
You should really get rid of that polygon offset. It's unreliable and scene-setup specific.

Have you tried a simple two pass approach:

- enable depth buffer
- setup depth function to less than or equal to

- enable depth write
- draw solid mesh

- disable depth write but keep the depth testing enabled (this might even be optional)
- draw wireframe mesh

< edit: terminology changed >