CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2007
    Location
    Bangalore,India
    Posts
    164

    Smile model, shade, looksRealistic OpenGL

    Dear Friends
    I am successfully able to generate a 3D surface by revolving the profiles by 360 degree. Now my 3d model is in red color so the color is so shining I am not able to distinguish various parts in the model. For this I am trying to use some lighting but I am not successful. Can u help me someone what kind of lighting should I use so that my model looks realistics and I would be able to visualize various parts on the model . Check my snippet I am trying to use this kind of lighting but finding black shadows on the sides not able to visualize properly. Thanks a lot for any help.

    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);
      	   glColor3f(1.0f, 0.0f, 0.0f); 
    	   // 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
    	   GLfloat emissionLight0[] = {0.2f, 0.0f, 0.0f, 1.0f};
    	   GLfloat ambientLight0[] = { 0.2f, 0.2f, 0.2f, 1.0f };
    	   GLfloat diffuseLight0[] = { 0.8f, 0.8f, 0.8, 1.0f };
    	   GLfloat specularLight0[] = { 0.5f, 0.5f, 0.5f, 1.0f };
    	   GLfloat position0[] = { -1.5f, 1.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_EMISSION, emissionLight0);
           glLightfv(GL_LIGHT0, GL_POSITION, position0);
           // GL_LIGHT1: the redlight emitting light source
           // Create lightcomponents for GL_LIGHT1
           float ambientLight1[] = { 0.2f, 0.2f, 0.2f, 1.0f };
           float diffuseLight1[] = { 0.8f, 0.8f, 0.8f, 1.0f };
           float specularLight1[] = { 0.5f, 0.5f, 0.5f, 1.0f };
           float emissionLight1[] = { 0.2f, 0.0f, 0.0f, 1.0f };
    	   float position1[]= { -1.5f, 1.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_EMISSION, emissionLight1);
           glLightfv(GL_LIGHT1, GL_POSITION, position1);
           //---------------------------------
    	   // enable color tracking
    	   glEnable(GL_COLOR_MATERIAL);
    	   // set material properties which will be assigned by glColor
    	   glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
    	   // ...Somewhere in the main rendering loop of your program...
    	   // Draw a polygon with material properties set by glColor
    	   float specReflection[] = { 0.8f, 0.8f, 0.8f, 1.0f };
    	   glMaterialfv(GL_FRONT, GL_SPECULAR, specReflection);
    	   glMateriali(GL_FRONT, GL_SHININESS, 96);
    	   glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
    	   Invalidate();
    	   wglMakeCurrent(NULL,NULL);
    }

  2. #2
    Join Date
    Oct 2011
    Posts
    5

    Re: model, shade, looksRealistic OpenGL

    Use glMaterial (not glColor) to set your model's appearance and be sure that you're assigning surface normals. This will improve the look of your model a lot.

  3. #3
    Join Date
    Oct 2011
    Posts
    5

    Re: model, shade, looksRealistic OpenGL

    Ok, just saw that you are using glColorMaterial....

    Are you using surface normals?

  4. #4
    Join Date
    Sep 2007
    Location
    Bangalore,India
    Posts
    164

    Re: model, shade, looksRealistic OpenGL

    no i am not using surface normals. Can u give some hint how can use surface normals ? Thanks Sujan

  5. #5
    Join Date
    Sep 2007
    Location
    Bangalore,India
    Posts
    164

    Re: model, shade, looksRealistic OpenGL

    Dear Friends
    Now I am calculating normals and assigning a normal using glNormal3f() for each QUAD......When rendered it's coming fine but some quads near the edges are flickering. How can I resolve this. ? Please help me. Thanks Sujan

  6. #6
    Join Date
    Sep 2007
    Location
    Bangalore,India
    Posts
    164

    Re: model, shade, looksRealistic OpenGL

    Dear Friends
    I am able to implement shading but I dont know why when I am rotating zooming out near the edges its flickering. I am calculating normals for each QUQD like this ...V0,V1,V2,V3 are 4 vertiuces of the quad.
    Code:
    				vec1.x = v1[0] - v0[0];
    				vec1.y = v1[1] - v0[1];
    				vec1.z = v1[2] - v0[2];
    
    				vec2.x = v3[0] - v0[0];
    				vec2.y = v3[1] - v0[1];
    				vec2.z = v3[2] - v0[2];
    
    				normal1[0] = ((vec1.y*vec2.z)-(vec1.z * vec2.y));
    				normal1[1] = ((vec1.z * vec2.x)-(vec1.x * vec2.z));
    				normal1[2] = ((vec1.x * vec2.y)-(vec1.y * vec2.x));
    
    				GLfloat len = sqrt((normal1[0]*normal1[0]) + (normal1[1]*normal1[1]) + (normal1[2]*normal1[2]));
    				
    				normal1[0] /= len;
    				normal1[1] /= len;
    				normal1[2] /= len;
    				
    				glBegin(GL_QUADS);      // draw a cube with 6 quads
    				glNormal3f(normal1[0],normal1[1],normal1[2]);
    				glVertex3fv(v0);    // front face
    				glVertex3fv(v1);
    				glVertex3fv(v2);
    				glVertex3fv(v3);

  7. #7
    Join Date
    Sep 2007
    Location
    Bangalore,India
    Posts
    164

    Re: model, shade, looksRealistic OpenGL

    my shade algorithms is like this ..
    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);
           glShadeModel(GL_SMOOTH);
           glEnable(GL_DEPTH_TEST);
    	   glEnable(GL_LIGHTING);
           glEnable(GL_LIGHT0);
    	   glEnable(GL_COLOR_MATERIAL);
    	   GLfloat ambientLight0[] = { 0.3f, 0.3f, 0.3f, 1.0f };
    	   GLfloat diffuseLight0[] = { 1.0f, 0.5f, 0.5f, 1.0f };
    	   GLfloat specularLight0[] = { 0.0f, 0.0f, 1.0f, 1.0f };
    	   GLfloat emissionLight0[] = {0.0f, 0.0f, 0.0f, 1.0f};
    	   GLfloat position0[] = {1.0f,1.0f,1.0f,1.0f};
           glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight0);
           glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight0);
           glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight0);
           glLightfv(GL_LIGHT0, GL_EMISSION, emissionLight0);
    	   glLightfv(GL_LIGHT0, GL_POSITION, position0);
    	   glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
    	   glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
    	   Invalidate();
    	   wglMakeCurrent(NULL,NULL);
    }
    Its flickering near the edges... while zooming out its minimizing and clickering more.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured