开发者

Can I use fixed glEnable(GL_LIGHTING) alongside fragment/vertex shaders?

I have browsed many online articles and adding lighting seems very straight forward but I'm not able to get a result. I'm using OpenGL 3.0 and I use a vertex and fragment shaders for my animating model. Btw, I'm new to OpenGL.

Disregarding the lighting I have everything working fine with the 3D char running around the environment.

    glEnable(GL_DEPTH_TEST);
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    *snip*

    glEnable(GL_LIGHTING);
    glEnable (GL_LIGHT0);

    float ambientLight0[] = { 0.2f, 0.2f, 0.2f, 1.0f };
    float diffuseLight0[] = { 0.8f, 0.8f, 0.8f, 1.0f };
    float specularLight0[] = { 0.5f, 0.5f, 0.5f, 1.0f };
    float position0[] = { -2.5f, 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_POSITION, position0);

    //glEnable(GL_COLOR_MATERIAL);
    //float mcolor[] = { 1.0f, 0.0f, 0.0f, 1.0f };
    //glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, mcolor);

    m_Beast.onRender();

Here's how m_Beast.onRender() looks:

    glPushMatrix();

    Vector3 pos = getPosition();
    glTranslatef(pos.x, pos.y, pos.z);

    glBindTexture(GL_TEXTURE_2D, m_beastTextureID);

    static float modelviewMatrix[16];
    static float projectionMatrix[16];

    glGetFloatv(GL_MODELVIEW_MATRIX, modelviewMatrix);
    glGetFloatv(GL_PROJECTION_MATRIX, projectionMatrix);

    m_shaderProgram->bindShader();
    m_shaderProgram->sendUniform4x4("modelview_matrix", modelviewMatrix);
    m_shaderProgram->sendUniform4x4开发者_Python百科("projection_matrix", projectionMatrix);
    m_shaderProgram->sendUniformTexture("texture0", 0);

    glEnableVertexAttribArray(0);
    glEnableVertexAttribArray(1);

    glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
    glVertexAttribPointer((GLint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glBindBuffer(GL_ARRAY_BUFFER, m_texCoordBuffer);
    glVertexAttribPointer((GLint)1, 2, GL_FLOAT, GL_FALSE, 0, 0);

    glDrawArrays(GL_TRIANGLES, 0, m_interpolatedFrame.vertices.size());

    glDisableVertexAttribArray(1);
    glDisableVertexAttribArray(0);

    glPopMatrix();

I just want a quick way to get some directional lighting on my model. I'm really not interesting in writing a lighting shader at this point.


As soon as you use vertex shaders, you must implement the lighting calculations yourself. In older OpenGL one could still use the fixed function vertex pipeline and program only the fragment stage. But with OpenGL-3 and onwards you're required to do it all.

However you shouldn't feel too bad about this. In the end you'll write less, because the huge plethora of function calls required to configure fixed function, can be replaced by much shorter and concise shader code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜