PDA

View Full Version : GL_FLAT/GL_SMOOTH, switching front and back issue



LaureH
01-11-2010, 08:32 AM
Hi!
I have started learning OpenGL last week and I have some issue with the lighting and the winding, I guess.
I have written a small program which draw 8 triangles to represent something like mountains (my final goal is to draw a DEM). It worked just fine with
glShadeModel(GL_FLAT), but now I want to use the lighting and when I switch to smooth, all my windings are switch, front faces become back faces and vice versa.
Now what troubles me most is that even if I switch the winding like this:
glFrontFace(GL_CW), nothing changes.

I've added my code below. I'm sorry, I have to work in Fortran, but the OpenGL part looks the same.

I would be really glad if you could help me.
Thanks



!================================================= ==============!

subroutine RenderScene !Called to draw scene !

!---------------------------------------------------------------!
! SPECIFICATIONS !
!---------------------------------------------------------------!

! DECLARATIONS !
!Used to flag alternating colors !
integer :: iPivot = 1 !

!---------------------------------------------------------------!

! CORPS !
!
!Clear the window and the depth buffer
call glClear(GL_COLOR_BUFFER_BIT + GL_DEPTH_BUFFER_BIT)

!Save matrix state and do the rotation
call glPushMatrix()
call glRotatef(xRot, 1.0_GLfloat, 0.0_GLfloat, 0.0_GLfloat)
call glRotatef(yRot, 0.0_GLfloat, 1.0_GLfloat, 0.0_GLfloat)

!Begin a triangle strip (first column)
call glBegin(GL_TRIANGLE_STRIP)

call glColor3f(0.0_GLfloat, 1.0_GLfloat, 0.0_GLfloat)
call glVertex3f(-50.0_GLfloat, 0.0_GLfloat, -50.0_GLfloat)!V0
call glColor3f(1.0_GLfloat, 0.0_GLfloat, 0.0_GLfloat)
call glVertex3f(0.0_GLfloat, 12.5_GLfloat, -50.0_GLfloat)!V1
call glColor3f(0.0_GLfloat, 1.0_GLfloat, 0.0_GLfloat)
call glVertex3f(-50.0_GLfloat, -12.5_GLfloat, 0.0_GLfloat)!V2
call glColor3f(1.0_GLfloat, 0.0_GLfloat, 0.0_GLfloat)
call glVertex3f(0.0_GLfloat, -25.0_GLfloat, 0.0_GLfloat)!V3
call glColor3f(0.0_GLfloat, 1.0_GLfloat, 0.0_GLfloat)
call glVertex3f(-50.0_GLfloat, 12.5_GLfloat, 50.0_GLfloat)!V4
call glColor3f(1.0_GLfloat, 0.0_GLfloat, 0.0_GLfloat)
call glVertex3f(0.0_GLfloat, 0.0_GLfloat, 50.0_GLfloat)!V5

!Done drawing
call glEnd()

!Begin another triangle strip (second column)
call glBegin(GL_TRIANGLE_STRIP)

call glColor3f(1.0_GLfloat, 0.0_GLfloat, 0.0_GLfloat)
call glVertex3f(0.0_GLfloat, 12.5_GLfloat, -50.0_GLfloat)!V6
call glColor3f(0.0_GLfloat, 1.0_GLfloat, 0.0_GLfloat)
call glVertex3f(50.0_GLfloat, -12.5_GLfloat, -50.0_GLfloat)!V7
call glColor3f(1.0_GLfloat, 0.0_GLfloat, 0.0_GLfloat)
call glVertex3f(0.0_GLfloat, -25.0_GLfloat, 0.0_GLfloat)!V8
call glColor3f(0.0_GLfloat, 1.0_GLfloat, 0.0_GLfloat)
call glVertex3f(50.0_GLfloat, 25.0_GLfloat, 0.0_GLfloat)!V9
call glColor3f(1.0_GLfloat, 0.0_GLfloat, 0.0_GLfloat)
call glVertex3f(0.0_GLfloat, 0.0_GLfloat, 50.0_GLfloat)!V10
call glColor3f(0.0_GLfloat, 1.0_GLfloat, 0.0_GLfloat)
call glVertex3f(50.0_GLfloat, 12.5_GLfloat, 50.0_GLfloat)!V11

!Done drawing
call glEnd()

!Restore transformations
call glPopMatrix()

!Flush drawing commands
call glutSwapBuffers()
!

!---------------------------------------------------------------!

end subroutine RenderScene !

!================================================= ==============!

!================================================= ==============!

subroutine SetupRC !
!This function does any needed initialization on the rendering !
!context !

!---------------------------------------------------------------!

! SPECIFICATIONS !
!---------------------------------------------------------------!

! DECLARATIONS !
!Light values !
!Bright white light !
real(kind=GLfloat), dimension(1:4) :: ambientLight = & !
[1.0_GLfloat, 1.0_GLfloat, 1.0_GLfloat, 1.0_GLfloat] !

!---------------------------------------------------------------!

! CORPS !
!
!Black background
call glClearColor(0.0_GLfloat, 0.0_GLfloat, &
0.0_GLfloat, 1.0_GLfloat )

!Turn culling on/off
!(Draw on screen only the elements front facing)
!call glEnable(GL_CULL_FACE)
call glDisable(GL_CULL_FACE)

!Enable/Disable depth testing
call glEnable(GL_DEPTH_TEST)!If two elements share the same position on the screen,
!draw on screen only the element closest to the camera
!and not the last element created in time.
!call glDisable(GL_DEPTH_TEST)

!Draw the back side as a wireframe only/filled
call glPolygonMode(GL_BACK,GL_LINE)
!call glPolygonMode(GL_BACK,GL_FILL)

!Set color shading model to flat/smooth
call glShadeModel(GL_FLAT)!------------------------------------------------------------ can't use lighting
!call glShadeModel(GL_SMOOTH)!--------------------------------------------------------- error front/back

!(Counter)Clockwise-wound polygons are front facing
!call glFrontFace(GL_CW)

!Lighting stuff
!call glEnable(GL_LIGHTING) !Enable lighting

!Set light model to use ambient light specified by ambientLight
!call glLightModelfv(GL_LIGHT_MODEL_AMBIENT,ambientLight )

!Enable material color tracking
!call glEnable(GL_COLOR_MATERIAL)

!Front material ambient and diffuse colors track glColor
!call glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE)
!

!---------------------------------------------------------------!

end subroutine SetupRC !

!================================================= ==============!

Dark Photon
01-11-2010, 06:31 PM
It worked just fine with
glShadeModel(GL_FLAT), but now I want to use the lighting and when I switch to smooth, all my windings are switch, front faces become back faces and vice versa.
What makes you think so?

And are you saying you just changed GL_FLAT to GL_SMOOTH, or are you saying you [i]also enabled GL_LIGHTING?

Also, for LIGHTING, you need vertex normals, and you're not specifying any here.

LaureH
01-12-2010, 02:41 AM
I put all the lighting lines in comments, so that the only line I changed is glShadeModel, but I still need it to be GL_SMOOTH to use the lighting afterwards.
To tell the difference between front and back I used glDisable(GL_CULL_FACE), glEnable(GL_DEPTH_TEST) and glPolygonMode(GL_BACK,GL_LINE).

Thank you for the hint about vertex normals, I'll add them later.

Dark Photon
01-12-2010, 05:33 AM
Thank you for the hint about vertex normals, I'll add them later.
Well, until you add them, disable GL_LIGHTING. It's pretty nonsensical without that, unless your active glNormal setting is applicable for every vertex in your mesh.