Draw a triangle and his borders too??

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! :slight_smile:

you mean draw a triangle in both wire frame and solid? what’s stopping you?

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?

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.

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();

I will post a screenshot of what I’m looking for tomorrow morning (don’t have the models here)

I tried this and it’s nearly what I’m looking for. My problem is that the lines lack width and they look stippled

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

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…

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…

This improved the drawing a lot, but still there are some small glitches with it.

See the following:

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.

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); 

u try nehe tuts? they helped me! :slight_smile:

Yeha but they set it related with Windows API

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.

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?

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 :wink:

uh, you’re welcome, i guess. gees, this is a tough crowd!

:smiley:

Thank you very much indeed :slight_smile: I’ll read some more.

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==