C ++ Opengl Lighting不起作用

时间:2015-02-01 09:47:11

标签: c++ opengl lighting

我已经搜索了解决方案,但都没有效果。 我已经参与了照明,除了物体之外的所有物体都没有点亮。

当我下载他们工作的例子,但当我尝试启用它的照明时 不起作用。

以下是代码:

#include <GLFW/glfw3.h>
#include <glm\gtx\transform.hpp>
#include <iostream>
#include <string>
#include <vector>
#include <GL\glut.h>

#include "Camera.h"

#define windowWidth 1024
#define windowHeight 620
#define CAMERA_SPEED 0.1

GLFWwindow* Window;
CCamera Camera;

bool aPressed = false;
bool dPressed = false;
bool wPressed = false;
bool sPressed = false;
bool ePressed = false;
bool qPressed = false;
bool spacePressed = false;

static void keyEvent(GLFWwindow* window_, int key, int scancode, int action, int mods)
{
    if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(Window, GL_TRUE);

    if(key == GLFW_KEY_A && action == GLFW_PRESS)
        aPressed=true;
    if(key == GLFW_KEY_D && action == GLFW_PRESS)
        dPressed=true;
    if(key == GLFW_KEY_W && action == GLFW_PRESS)
        wPressed=true;
    if(key == GLFW_KEY_S && action == GLFW_PRESS)
        sPressed=true;
    if(key == GLFW_KEY_E && action == GLFW_PRESS)
        ePressed=true;
    if(key == GLFW_KEY_Q && action == GLFW_PRESS)
        qPressed=true;
    if(key == GLFW_KEY_SPACE && action == GLFW_PRESS)
        spacePressed=true;

    if(key == GLFW_KEY_A && action == GLFW_RELEASE)
        aPressed=false;
    if(key == GLFW_KEY_D && action == GLFW_RELEASE)
        dPressed=false;
    if(key == GLFW_KEY_W && action == GLFW_RELEASE)
        wPressed=false;
    if(key == GLFW_KEY_S && action == GLFW_RELEASE)
        sPressed=false;
    if(key == GLFW_KEY_E && action == GLFW_RELEASE)
        ePressed=false;
    if(key == GLFW_KEY_Q && action == GLFW_RELEASE)
        qPressed=false;
    if(key == GLFW_KEY_SPACE && action == GLFW_RELEASE)
        spacePressed=false;
}

int main()
{
    if(!glfwInit())
    {
        exit(EXIT_FAILURE);
    }

    Window = glfwCreateWindow(windowWidth, windowHeight, "3D 2.0", NULL, NULL);

    if(!Window)
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    glClearColor (0.0, 0.0, 0.0, 0.0);

    GLfloat light_ambient[] =
    {0.0, 0.0, 0.0, 1.0};
    GLfloat light_diffuse[] =
    {1.0, 0.0, 0.0, 1.0};
    GLfloat light_specular[] =
    {1.0, 1.0, 1.0, 1.0};
/* light_position is NOT default value */
    GLfloat light_position[] =
    {1.0, 1.0, 1.0, 0.0};

    glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv(GL_LIGHT0, GL_POSITION, light_position);

    glEnable(GL_LIGHT0);
    glDepthFunc(GL_LESS);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);

    glfwMakeContextCurrent(Window);
    glfwSwapInterval(1);

    glfwSetKeyCallback(Window, keyEvent);

    while(!glfwWindowShouldClose(Window))
    {
        glViewport(0, 0, (double)windowWidth, (double)windowHeight);

        //glClearColor(0.0,0.5,1.0,1.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glMatrixMode(GL_PROJECTION_MATRIX);
        glLoadIdentity();


        gluPerspective( 90, (double)windowWidth / (double)windowHeight, 0.1, 100 );
        gluLookAt(0,0,5,0,0,-0.1,0.0,1.0,0.0);



        glMatrixMode(GL_MODELVIEW_MATRIX);


        if(aPressed)
            Camera.StrafeRight(-CAMERA_SPEED);
        if(dPressed)
            Camera.StrafeRight(CAMERA_SPEED);
        if(wPressed)
            Camera.MoveForward(-CAMERA_SPEED);
        if(sPressed)
            Camera.MoveForward(CAMERA_SPEED);
        if(ePressed)
            Camera.MoveUpward(CAMERA_SPEED);
        if(qPressed)
            Camera.MoveUpward(-CAMERA_SPEED);

        /* Draw Here */

        Camera.Render();

        /* Draw Cube */

        glPushMatrix();

        glTranslated(0.0,0.0,0.0);

        glColor3f(0.0,0.0,0.0);
        glLineWidth(4);
        //glutWireCube(1);
        glColor3f(0.0,0.3,1.0);
        glNormal3f(0.0,0.0,1.0);
        glutSolidCube(1);

        glPopMatrix();

        /* Draw Teapot */

        glPushMatrix();

        glTranslated(3.0,0.0,0.0);

        glColor3f(0.0,0.0,0.0);
        glLineWidth(4);
        glutWireTeapot(1);
        glColor3f(0.6,0.3,0.0);
        glutSolidTeapot(1);

        glPopMatrix();

        /* Draw Torus */

        glPushMatrix();

        glTranslated(-3.0,0.0,0.0);

        glColor3f(0.0,0.0,0.0);
        glLineWidth(4);
        glutWireTorus(0.25, 1, 16, 16);
        glColor3f(0.6,0.3,0.3);
        glutSolidTorus(0.25, 1, 16, 16);

        glPopMatrix();


        glfwSwapBuffers(Window);
        glfwPollEvents();
    }
    glfwDestroyWindow(Window);

    glfwTerminate();
    exit(EXIT_SUCCESS);
}

2 个答案:

答案 0 :(得分:1)

您正在使用glut功能,而无需先致电glutInit

main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    ...

在任何OpenGL函数之前还必须调用glfwMakeContextCurrent(Window);

为了正确照明,请考虑使用实际视图矩阵调整光线方向(-1,-1,-1,0)会更好。

答案 1 :(得分:1)

gluLookAt()用于构建视图矩阵。因此,当您处于GL_MODELVIEW矩阵模式时,必须指定它。初始转换设置的调用顺序应为:

glMatrixMode(GL_PROJECTION_MATRIX);
glLoadIdentity();
gluPerspective(90.0, (double)windowWidth / (double)windowHeight, 0.1, 100.0);

glMatrixMode(GL_MODELVIEW_MATRIX);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, -0.1, 0.0, 1.0, 0.0);

您实际上可以在事件循环之外移动此调用序列。没有必要为每一帧重复它。好吧,gluPerspective()调用取决于窗口大小,因此您应该在窗口大小更改时重复它。但看起来你的代码中的窗口大小是硬连线的。

您可能还想查看使用glMaterialfv()设置材料属性。启用照明后,您用于设置颜色的glColor3f()个调用将无法获得所需的效果。

当然,正如另一个答案中已经指出的那样,您需要在进行任何OpenGL调用之前致电glfwMakeContextCurrent()

我希望您知道您正在使用的大多数OpenGL功能已被弃用,并且在最新版本的OpenGL(Core Profile以及OpenGL ES 2.0及更高版本)中不再可用。