Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 8 of 8

Thread: how can I do shading

  1. #1
    Junior Member Regular Contributor
    Join Date
    Aug 2009
    Posts
    111

    how can I do shading

    Dear Friends
    How can I implement shading for my 3D model. I am calling
    glShadeModel (GL_SMOOTH);
    In my DrawScene() but its not shading properly.
    Please give me some hints about shading. Thanks a lot in advance...Sujan

  2. #2
    Super Moderator OpenGL Guru dorbie's Avatar
    Join Date
    Jul 2000
    Location
    Bay Area, CA, USA
    Posts
    4,388

    Re: how can I do shading

    You need to have a color result that is different per vertex for GL_SMOOTH to have an effect.

    This could be the specified per vertex color attribute of the result of a lighting calculation where surface normals per vertex are averaged with neighbors.

    Basically DrawScene() could be doing anything and GL_SMOOTH just interpolates per-vertex color results (from lighting or whatever) across triangles, what those per-vertex color results are is a result of many other factors that are in your hands.

  3. #3
    Junior Member Regular Contributor tksuoran's Avatar
    Join Date
    Mar 2008
    Location
    United Kingdom
    Posts
    201

    Re: how can I do shading

    What kind of shading you have in mind? Is this fixed function or with shaders?

    If you mean lighting, then you need to set vertex normals and you need to enable lighting and lights, and set light positions/directions and material properties accordingly. With fixed function you would use glLight*, glMaterial* functions.

  4. #4
    Junior Member Regular Contributor
    Join Date
    Aug 2009
    Posts
    111

    Re: how can I do shading

    I want to have an effect of the color intensity based on the points position with respect to 0,0 axis. Do I need to use lighting for that. specifying colors for each vertex would so much tedious, is there very simple way that I can achieve shading. Thanks for the reply.

  5. #5
    Junior Member Regular Contributor
    Join Date
    Aug 2009
    Posts
    111

    Re: how can I do shading

    I am doing like this

    glShadeModel(GL_SMOOTH);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHT1);
    float position0[]= { 0.0f, 0.0f, 0.0f, 1.0f };
    // Assign createdcomponents to GL_LIGHT0
    glLightfv(GL_LIGHT0, GL_POSITION, position0);
    float position1[]= { 0.0f, 0.0f, 0.0f, 1.0f };
    // Assign createdcomponents to GL_LIGHT1
    glLightfv(GL_LIGHT1, GL_POSITION, position1);

    But with this the result is not satisfactory. In fact I am loosing my actual model color, i.e, red.

    After shading this way model color becomes white and dark shade is coming, in blackbackground the model is not visible at all.

    Please give me some suggestions.

  6. #6
    Member Regular Contributor
    Join Date
    Oct 2010
    Location
    France
    Posts
    466

    Re: how can I do shading

    Color has nothing to do with lighting. You need materials.

    I highly suggest you to learn OpenGL among other things. You simply can't do a thing if you know absolutely nothing about it. So, you can read for example this tutorial:

    http://glprogramming.com/red/chapter05.html

  7. #7
    Advanced Member Frequent Contributor
    Join Date
    Oct 2009
    Posts
    595

    Re: how can I do shading

    Perhaps rather to and search for opengl:

    http://library.nu

  8. #8
    Junior Member Regular Contributor
    Join Date
    Aug 2009
    Posts
    111

    Re: how can I do shading

    I am currently shading my model like this. But with this the model has dark shade, black shadow is coming, moreover the shading is not realistic. Can u help someone to modify my existing to get a better shading, Thanks a lot for any help. Sujan
    Code snippet.

    Code :
    void CRevolutionProjView::OnViewShade()
    {
    	// TODO: Add your command handler code here
    		// TODO: Add your command handler code here
     
    		CDC* pDC = GetDC();
    		wglMakeCurrent(pDC->m_hDC, m_hrc);
     
    	   glClearDepth(1.0f);
           glEnable(GL_DEPTH_TEST);
           glDepthFunc(GL_LEQUAL);
           glShadeModel(GL_SMOOTH);
           glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_FASTEST);
           // Enable light and setup 2 light sources (GL_LIGHT0 and GL_LIGHT1)
           glEnable(GL_LIGHTING);
           glEnable(GL_LIGHT0);
           glEnable(GL_LIGHT1);
           // We're setting up twolight sources. One of them is located
           // on the left side ofthe model (x = -1.5f) and emits white light. The
           // second light sourceis located on the right side of the model (x = 1.5f)
           // emitting red light.
           // GL_LIGHT0: the whitelight emitting light source
           // Create lightcomponents for GL_LIGHT0
           float ambientLight0[] = { 1.0f, 0.1f, 0.1f, 1.0f };
           float diffuseLight0[] = { 1.0f, 0.1f, 0.1f, 1.0f };
           float specularLight0[] = { 1.0f, 0.1f, 0.1f, 1.0f };
           float position0[]= { 0.0f, 0.0f, -4.0f, 1.0f };      
           // Assign createdcomponents to GL_LIGHT0
           glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight0);
           glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight0);
           glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight0);
           glLightfv(GL_LIGHT0, GL_POSITION, position0);
           // GL_LIGHT1: the redlight emitting light source
           // Create lightcomponents for GL_LIGHT1
           float ambientLight1[] = { 1.0f, 0.1f, 0.1f, 1.0f };
           float diffuseLight1[] = { 1.0f, 0.1f, 0.1f, 1.0f };
           float specularLight1[] = { 1.0f, 0.1f, 0.1f, 1.0f };
           float position1[]= { 0.0f, 0.0f, -4.0f, 1.0f };       
           // Assign createdcomponents to GL_LIGHT1
           glLightfv(GL_LIGHT1, GL_AMBIENT, ambientLight1);
           glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuseLight1);
           glLightfv(GL_LIGHT1, GL_SPECULAR, specularLight1);
           glLightfv(GL_LIGHT1, GL_POSITION, position1);
          //---------------------------------
    	   glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
    	   Invalidate();
    	   wglMakeCurrent(NULL,NULL);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •