Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Graphics
  4. [OPENGL] render mesh/lighting

[OPENGL] render mesh/lighting

Scheduled Pinned Locked Moved Graphics
graphicsgame-dev
3 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yakovm3
    wrote on last edited by
    #1

    Hello, I need to render the mesh.It is given by set of its surfaces each surface is presented by its vertexes(x,y,z) The camera and light source has to be placed at the same place. I use this code:

    //Projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    		GLdouble diam = m\_allModels.getDiam();
    		GLdouble left = m\_allModels.getCenter()\[0\] - diam;
    		GLdouble right = m\_allModels.getCenter()\[0\] + diam;
    		GLdouble bottom = m\_allModels.getCenter()\[1\] - diam;
    		GLdouble top = m\_allModels.getCenter()\[1\] + diam;
    
            
    		glFrustum(left,right,bottom,top,diam,diam\*3.5);            
    

    //Model
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt (0.0, 0.0,2*m_allModels.getDiam(),
    m_allModels.getCenter()[0],m_allModels.getCenter()[1],m_allModels.getCenter()[2],
    0.0, 1.0, 0.0);

    //Lighting
    GLfloat light_position[] = {0.0, 0.0,0.0, 1.0 };
    GLfloat white_light[] = { 0.7, 0.3, 0.2, 1.0 };
    glEnable(GL_DEPTH_TEST);

    		glEnable(GL\_LIGHTING);
    		glLightfv(GL\_LIGHT1, GL\_POSITION, light\_position);
    		glLightfv(GL\_LIGHT1, GL\_DIFFUSE, white\_light);
    		glEnable(GL\_LIGHT1);
                        glShadeModel(GL\_FLAT);
    

    //Draw
    glEnable(GL_NORMALIZE);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    //render here
    for(unsigned int modelIdx = 0;modelIdx < m_allModels.zise();++modelIdx)
    {
    model curModel = m_allModels.getModel(modelIdx);
    for(unsigned int modelSurfIdx = 0;modelSurfIdx <curModel.surfNum();
    ++modelSurfIdx)
    {
    surface curSurf = curModel.getSurface(modelSurfIdx);
    glLineWidth(2);
    glBegin( GL_POLYGON );

    				glNormal3f(curSurf.getN\_x(),curSurf.getN\_y(),curSurf.getN\_z());
    				for(unsigned int vertIdx = 0;vertIdx< curSurf.size();++vertIdx)
    				{
    					unsigned int curVertIdx = curSurf.getSurfVertices()\[vertIdx\];
    					vertex curVert = curModel.getVertex(curVertIdx);
    					glVertex3f((GLfloat)curVert.getX(),(GLfloat)curVert.getY(),(GLfloat)curVert.getZ());
    				}	
    				glEnd();
    
    			}
    		}
    

    And I get this http://img17.imageshack.us/img17/1103/64347046.png[^] And I don't understand why

    L E 2 Replies Last reply
    0
    • Y yakovm3

      Hello, I need to render the mesh.It is given by set of its surfaces each surface is presented by its vertexes(x,y,z) The camera and light source has to be placed at the same place. I use this code:

      //Projection
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();

      		GLdouble diam = m\_allModels.getDiam();
      		GLdouble left = m\_allModels.getCenter()\[0\] - diam;
      		GLdouble right = m\_allModels.getCenter()\[0\] + diam;
      		GLdouble bottom = m\_allModels.getCenter()\[1\] - diam;
      		GLdouble top = m\_allModels.getCenter()\[1\] + diam;
      
              
      		glFrustum(left,right,bottom,top,diam,diam\*3.5);            
      

      //Model
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      gluLookAt (0.0, 0.0,2*m_allModels.getDiam(),
      m_allModels.getCenter()[0],m_allModels.getCenter()[1],m_allModels.getCenter()[2],
      0.0, 1.0, 0.0);

      //Lighting
      GLfloat light_position[] = {0.0, 0.0,0.0, 1.0 };
      GLfloat white_light[] = { 0.7, 0.3, 0.2, 1.0 };
      glEnable(GL_DEPTH_TEST);

      		glEnable(GL\_LIGHTING);
      		glLightfv(GL\_LIGHT1, GL\_POSITION, light\_position);
      		glLightfv(GL\_LIGHT1, GL\_DIFFUSE, white\_light);
      		glEnable(GL\_LIGHT1);
                          glShadeModel(GL\_FLAT);
      

      //Draw
      glEnable(GL_NORMALIZE);
      glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
      //render here
      for(unsigned int modelIdx = 0;modelIdx < m_allModels.zise();++modelIdx)
      {
      model curModel = m_allModels.getModel(modelIdx);
      for(unsigned int modelSurfIdx = 0;modelSurfIdx <curModel.surfNum();
      ++modelSurfIdx)
      {
      surface curSurf = curModel.getSurface(modelSurfIdx);
      glLineWidth(2);
      glBegin( GL_POLYGON );

      				glNormal3f(curSurf.getN\_x(),curSurf.getN\_y(),curSurf.getN\_z());
      				for(unsigned int vertIdx = 0;vertIdx< curSurf.size();++vertIdx)
      				{
      					unsigned int curVertIdx = curSurf.getSurfVertices()\[vertIdx\];
      					vertex curVert = curModel.getVertex(curVertIdx);
      					glVertex3f((GLfloat)curVert.getX(),(GLfloat)curVert.getY(),(GLfloat)curVert.getZ());
      				}	
      				glEnd();
      
      			}
      		}
      

      And I get this http://img17.imageshack.us/img17/1103/64347046.png[^] And I don't understand why

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      So what is it supposed to look like? Can't read minds, you know... I'll take a wild guess and say that, as is often the case with 3rd party models, the winding direction of the polygons is probably inconsistent. If you want to see the entire model, either enable double-sided rendering, or run the model through PoseRay (or some other model cleaner) and fix the polygon winding.

      L u n a t i c F r i n g e

      1 Reply Last reply
      0
      • Y yakovm3

        Hello, I need to render the mesh.It is given by set of its surfaces each surface is presented by its vertexes(x,y,z) The camera and light source has to be placed at the same place. I use this code:

        //Projection
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();

        		GLdouble diam = m\_allModels.getDiam();
        		GLdouble left = m\_allModels.getCenter()\[0\] - diam;
        		GLdouble right = m\_allModels.getCenter()\[0\] + diam;
        		GLdouble bottom = m\_allModels.getCenter()\[1\] - diam;
        		GLdouble top = m\_allModels.getCenter()\[1\] + diam;
        
                
        		glFrustum(left,right,bottom,top,diam,diam\*3.5);            
        

        //Model
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt (0.0, 0.0,2*m_allModels.getDiam(),
        m_allModels.getCenter()[0],m_allModels.getCenter()[1],m_allModels.getCenter()[2],
        0.0, 1.0, 0.0);

        //Lighting
        GLfloat light_position[] = {0.0, 0.0,0.0, 1.0 };
        GLfloat white_light[] = { 0.7, 0.3, 0.2, 1.0 };
        glEnable(GL_DEPTH_TEST);

        		glEnable(GL\_LIGHTING);
        		glLightfv(GL\_LIGHT1, GL\_POSITION, light\_position);
        		glLightfv(GL\_LIGHT1, GL\_DIFFUSE, white\_light);
        		glEnable(GL\_LIGHT1);
                            glShadeModel(GL\_FLAT);
        

        //Draw
        glEnable(GL_NORMALIZE);
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
        //render here
        for(unsigned int modelIdx = 0;modelIdx < m_allModels.zise();++modelIdx)
        {
        model curModel = m_allModels.getModel(modelIdx);
        for(unsigned int modelSurfIdx = 0;modelSurfIdx <curModel.surfNum();
        ++modelSurfIdx)
        {
        surface curSurf = curModel.getSurface(modelSurfIdx);
        glLineWidth(2);
        glBegin( GL_POLYGON );

        				glNormal3f(curSurf.getN\_x(),curSurf.getN\_y(),curSurf.getN\_z());
        				for(unsigned int vertIdx = 0;vertIdx< curSurf.size();++vertIdx)
        				{
        					unsigned int curVertIdx = curSurf.getSurfVertices()\[vertIdx\];
        					vertex curVert = curModel.getVertex(curVertIdx);
        					glVertex3f((GLfloat)curVert.getX(),(GLfloat)curVert.getY(),(GLfloat)curVert.getZ());
        				}	
        				glEnd();
        
        			}
        		}
        

        And I get this http://img17.imageshack.us/img17/1103/64347046.png[^] And I don't understand why

        E Offline
        E Offline
        ely_bob
        wrote on last edited by
        #3

        it appears that your normals are flip flopped, at least that's what causes my some of my renders to go astray...

        I'd blame it on the Brain farts.. But lets be honest, it really is more like a Methane factory between my ears some days then it is anything else...

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups