Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6599511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:30:02+00:00 2026-05-25T18:30:02+00:00

I am trying to make a very simple MFC OpenGL tutorial. There is a

  • 0

I am trying to make a very simple MFC OpenGL tutorial.
There is a sphere. And the spere is the source of light(like sun). And there is two triangle. According to movement of the spere, I want to have the triangles lightend.
But When I move the sphere, the surface of the triangle changed only for the first time.
Next movement of the sphere don’t change the surface the the triangle.

How to keep trace of the light position?
This is the keyborad handler function.

void CView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    if (nChar == VK_UP)
    {
        m.x += 0.1f;
    }
    else if (nChar == VK_DOWN)
    {
        m.y -= 0.1f;
    }
    else if (nChar == VK_LEFT)
    {
        m.x -= 0.1f;
    }
    else if (nChar == VK_RIGHT)
    {
        m.x += 0.1f;
    }
    setGL();
    DrawGL();
    CView::OnKeyDown(nChar, nRepCnt, nFlags);
}  

And these are the functions which called by keyboard function.

void CView::setGL(GLvoid)           
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);       
    glClearDepth(1.0f); 
    glEnable(GL_DEPTH_TEST);    
    glDepthFunc(GL_LEQUAL);             
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

    // light source configuration ##################
    glMatrixMode(GL_MODELVIEW);
    gluLookAt(0.f,0.f,1.f, 0.f,0.f,0.f, 0.f,1.f,0.f);

    GLfloat light0_diffuse[] = {0.5, 0.5, 1.0};
    GLfloat light0_specular[] = {1.0, 1.0, 1.0, 1.0};
    GLfloat light0_spot_direction[] = {1,1,1,0};

    GLfloat light1_diffuse[] = {1.0f, 0.3f, 0.3f};
    GLfloat light1_specular[] = {1.0f, 0.8f, 0.8f, 1.0f};
    GLfloat light1_shiniess[] = {50.0};
    GLfloat light1_pos[] = {m.x, m.y, m.z};

    glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
    glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light0_spot_direction);

    glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
    glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
    glLightfv(GL_LIGHT1, GL_SHININESS, light1_shiniess);
    glLightfv(GL_LIGHT1, GL_POSITION, light1_pos);

    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHT1);
}

void CView::DrawGL(void)                
{
    // clear screen and depth buffer
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glLoadIdentity();
    // camera view configuration 
    gluLookAt(0.0f,0.0f,1.0f, 0.0f,0.0f,0.0f, 0.0f,1.0f,0.0f);

    // draw 
    //glColor3f(1.f, 1.f, 1.f);
    glPushMatrix();
    glTranslatef(m.x, m.y, m.z);
    glutSolidSphere(0.25f, 120, 120);

    glPopMatrix();
    glBegin(GL_TRIANGLES);
    glVertex3f(-0.5f, 0.5f, -0.5f);
    glVertex3f(0.0f, 0.0f, -0.5f);
    glVertex3f(-1.0f, 0.0f, -0.5f);

    m.calculateNormal(-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -0.5f, -1.0f, 0.0f, -0.5f);
    glNormal3f(m.normalX, m.normalY, m.normalZ);

    glBegin(GL_TRIANGLES);
    glVertex3f(0.5f, 0.5f, -1.0f);
    glVertex3f(0.0f, 0.0f, -1.0f);
    glVertex3f(1.0f, 0.0f, -1.0f);

    m.calculateNormal(0.5f, 0.5f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, -1.0f);
    glNormal3f(m.normalX, m.normalY, m.normalZ);

    glEnd();

    // swap buffer
    SwapBuffers(m_hDC);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:30:03+00:00Added an answer on May 25, 2026 at 6:30 pm

    You should never place drawing calls in a input event handler. That’s your main problem. The other problem is – so it seems – that you confuse OpenGL for a scene graph.

    OpenGL is a drawing API with no recollection of a scene, whatsoever. In your input handler you set some variables, issue a redraw event and then in the drawing event handler render from those variables. OpenGL being a state machine drawing API also means, that there is no explicit initialization phase. A function like setGL makes no sense whatsoever. What you do in setGL actually belongs into the drawing function. This includes setting the viewport and the projection matrix!

    void CView::drawGL(GLvoid)           
    {
        glClearColor(0.0f, 0.0f, 0.0f, 0.5f);       
        glClearDepth(1.0f); 
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    
        glViewport(…);
        glMatrixMode(GL_PROJECTION);
            set_projection_matrix(…);    
    
        // light source configuration ##################
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity(); // gluLookAt expects to work from a identity matrix.
        gluLookAt(0.f,0.f,1.f, 0.f,0.f,0.f, 0.f,1.f,0.f);
    
        GLfloat light0_diffuse[] = {0.5, 0.5, 1.0};
        GLfloat light0_specular[] = {1.0, 1.0, 1.0, 1.0};
        GLfloat light0_spot_direction[] = {1,1,1,0};
    
        GLfloat light1_diffuse[] = {1.0f, 0.3f, 0.3f};
        GLfloat light1_specular[] = {1.0f, 0.8f, 0.8f, 1.0f};
        GLfloat light1_shiniess[] = {50.0};
        GLfloat light1_pos[] = {m.x, m.y, m.z};
    
        glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
        glLightfv(GL_LIGHT0, GL_SPECULAR, light0_specular);
        glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light0_spot_direction);
    
        glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
        glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
        glLightfv(GL_LIGHT1, GL_SHININESS, light1_shiniess);
        glLightfv(GL_LIGHT1, GL_POSITION, light1_pos);
    
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        glEnable(GL_LIGHT1);
    
        glEnable(GL_DEPTH_TEST);
        glDepthFunc(GL_LEQUAL);
    
        // draw 
        glColor3f(1.f, 1.f, 1.f);
        glPushMatrix();
        glTranslatef(m.x, m.y, m.z);
        glutSolidSphere(0.25f, 120, 120);
        glPopMatrix();
    
        glBegin(GL_TRIANGLES);
        glVertex3f(-0.5f, 0.5f, -0.5f);
        glVertex3f(0.0f, 0.0f, -0.5f);
        glVertex3f(-1.0f, 0.0f, -0.5f);
    
        m.calculateNormal(-0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -0.5f, -1.0f, 0.0f, -0.5f);
        glNormal3f(m.normalX, m.normalY, m.normalZ);
    
        glBegin(GL_TRIANGLES);
        glVertex3f(0.5f, 0.5f, -1.0f);
        glVertex3f(0.0f, 0.0f, -1.0f);
        glVertex3f(1.0f, 0.0f, -1.0f);
    
        m.calculateNormal(0.5f, 0.5f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, -1.0f);
        glNormal3f(m.normalX, m.normalY, m.normalZ);
        glEnd();
    
        // swap buffer
        SwapBuffers(m_hDC);
    }
    

    The calls to glLight… happen in the context of the currently set state (most importantly for the lights is the modelview matrix). The light positions are multiplied by the modelview matrix. If using gluLookAt, which you do, the calls to glLightfv(GL_LIGHT<n>, GL_POSITION, …) must happen after gluLookAt and before drawing the illuminated geometry.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a very simple object rotate around a fixed point
I am trying to make a very simple web-service which does the following: The
I am trying to make a (very) simple Data Binding test, but it doesn't
Basically what I'm trying to do is make a very simple vertical bullet projectile
I am just playing around and trying to make a very simple CMS. Right
Trying to make a call and retrieve a very simple, one line, JSON file.
I was trying to make a tail-recursive version of this very simple SML function:
Very simple thing I'm trying to do here. I would like to have 2
I am trying to make a very simple application. With a starting menu as
I'm trying to make a very simple iphone app that displays the real-time camera

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.