0

I'm having problems with my shading.

I exported my model from SolidWorks to .wrl file. I read this file and show it with glShadeModel(GL_SMOOTH), result is not smooth object joust like in GL_FLAT mode.

RESULT: http://shrani.si/f/2y/Ah/E8gXzGH/model.png

Its funny beacuse when i draw glutSolidSphere(1,10,10) the sphere is smooth. Im wondering what im doing wrong? Do i have to calculate shading colour for every vertex at the time or i have to change something globaly in my program? It should be easy fix right?

Code for initializing OpenGL :

glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(31, float(screen_px[0])/screen_px[1],0.1, 1000)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
gluLookAt(0,0,0,0,0,-1,0,1,0)
glEnable(GL_DEPTH_TEST)
glEnable(GL_COLOR_MATERIAL)
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glDepthFunc(GL_LESS)
glShadeModel(GL_SMOOTH)
glClearColor(0.5, 0.5, 1, 1)
glMaterial(GL_FRONT, GL_AMBIENT,  (0.5, 0.5, 0.5, 1))
glMaterial(GL_FRONT, GL_DIFFUSE,  (0.8,0.8,0.8,0.8))
glMaterial(GL_FRONT, GL_SPECULAR, (0.4, 0.4, 0.4, 1))
glMaterial(GL_FRONT, GL_SHININESS, (128))
glLight(GL_LIGHT0, GL_POSITION,  (100, 100, 0, 1))
glLight(GL_LIGHT0, GL_AMBIENT,  (0.4,0.4,0.4,0.4))
glLight(GL_LIGHT0, GL_DIFFUSE,  (0.4,0.4,0.4,0.4))
glLight(GL_LIGHT0, GL_SPECULAR, (0.4,0.4,0.4,0.4))

Code for drawing

  • model[0]=[[vertex0],[vertex1], ...]
  • model[[1]]=index order for model[0]
  • model[2]=normal vectors for model[0]

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glLight(GL_LIGHT0, GL_POSITION,(10, 10,10, 1))
    glBegin(GL_TRIANGLES)
    j=0
    for i in range(0,len(model[2]),1):
        glNormal3f(model[2][i][0],model[2][i][1],model[2][i][2])
        k=model[1][j]
        glVertex3f(model[0][k][0],model[0][k][1],model[0][k][2])
        j=j+1
        k=model[1][j]
        glVertex3f(model[0][k][0],model[0][k][1],model[0][k][2])
        j=j+1
        k=model[1][j]
        glVertex3f(model[0][k][0],model[0][k][1],model[0][k][2])
        j=j+1
    glEnd()
    
    pygame.display.flip()
    
genpfault
  • 51,148
  • 11
  • 85
  • 139

1 Answers1

2

single glNormal() call per face

If you want smooth shading you have to provide a different normal for each vertex of a triangle.

EDIT: If all you have are face normals you can average the face normals for a vertex to get smoother per-vertex normals.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • EH i was thinking OpenGL calculate this automatic :S. I know how to calculate normals for vertexes but is there an algoritem for calculating normals that is optimized? I will search on google... –  Jul 26 '13 at 17:18
  • OOOOOOh even better, some model files should provide a normal vectors for every vertex. Does enyone know this? –  Jul 26 '13 at 17:35
  • @user2081554: OpenGL won't do any work for you, you can do yourself better than OpenGL. Since precomputation always beats online computation, OpenGL expects from you to do this task. Also calculating per vertex normals is easy. For each vertex sum up the contributing face normals and normalize to unit length. – datenwolf Jul 26 '13 at 19:50