Search code examples
ios3dopengl-es-1.1

OpenGL ES 1 Scaling without changing lighting on object?


I am implementing a 3d obj viewer like app and when I did a scaling on my object in the app (in OpenGL ES 1.x) it became lighter(scale down) and darker (when I scale up).

Is there a way for me to prevent this "changing of lighting" to happen? i.e. the same brightness uniformly through out.

I guess I have to do something to the lighting normals?

My Render method is as below:

void RenderingEngine::Render(const vector<Visual>& visuals, ivec2 screenSize) const
{
glClearColor(0.5f, 0.5f, 0.5f, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

vector<Visual>::const_iterator visual = visuals.begin();
for (int visualIndex = 0; visual != visuals.end(); ++visual, ++visualIndex) {

    // Set the viewport transform.
    ivec2 size = visual->ViewportSize;
    ivec2 lowerLeft = visual->LowerLeft;
    glViewport(lowerLeft.x, lowerLeft.y, size.x, size.y);

    // Set the light position.
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    vec4 lightPosition(0.25, 0.25, 1, 0);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPosition.Pointer());

    // Set the model-view transform.
    mat4 rotation = visual->Orientation.ToMatrix();
    mat4 translation = visual->Translate;
    mat4 scale;
    scale = scale.Scale(visual->Scale);
    rotation = rotation * scale;
    //mat4 modelview = rotation * m_translation;
    mat4 modelview = rotation * m_translation * translation;

    glLoadMatrixf(modelview.Pointer());

    // Set the projection transform.
    float h = 4.0f * size.y / size.x;
    mat4 projection = mat4::Frustum(-2, 2, -h / 2, h / 2, 5, 50);
    glMatrixMode(GL_PROJECTION);
    glLoadMatrixf(projection.Pointer());

    // Set the diffuse color.
    vec3 color = visual->Color * 0.75f;
    vec4 diffuse(color.x, color.y, color.z, 1);
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse.Pointer());

    // Draw the surface.
    int stride = 2 * sizeof(vec3);
    const GLvoid* normalOffset = (const GLvoid*) sizeof(vec3);
    const Drawable& drawable = m_drawables[visualIndex];
    glBindBuffer(GL_ARRAY_BUFFER, drawable.VertexBuffer);
    glVertexPointer(3, GL_FLOAT, stride, 0);
    glNormalPointer(GL_FLOAT, stride, normalOffset);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, drawable.IndexBuffer);
    glDrawElements(GL_TRIANGLES, drawable.IndexCount, GL_UNSIGNED_SHORT, 0);
}
}

Solution

  • This solution works for me. So I'll be sharing this for any newbies to the subject who run into the same problem as me.

    Use:

    glEnable(GL_NORMALIZE);
    

    to always normalize the surface normals to unit length.