0
\$\begingroup\$

I want to add somebasic point lights to my OpenGL application. I read here that I have to caluclate the light in a pre-projection space:

Lighting can be done in any pre-projection space (e.g., world coordinates or eye coordinates), as long as all the objects and lights are all in that same space.

And here is my problem. My Renderer currently looks something like this:

public void onDrawFrame(GL10 unused)
{
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
    Matrix.multiplyMM(camProjection, 0, projection, 0, camera, 0);

    for(int i=0; i < sceneObjects.size(); i++)
    {
        SceneObject object = sceneObjects.get(i);
        object.draw(camProjection);
    }

    // ...
}

As you can see I multiply my projectionMatrix with the viewMatrix and I think that is a good solution because I only have to do this once. To draw I give the PVMatrix to all objects and multiply it with their modelMatrix and then I push the finall MVPMatrix to the shader. This is a problem because I can not calculate the light direction in eye space in the shader because I do not have any pre-projection matrix here. Do I really have to push a MVMatrix to the shader and calculate the normals and so on with it? I think this is really inefficient because I have to do much more matrix multiplications, isn't it? What would you suggest to calculate this efficiently?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Sending MVMatrix to the GPU and performing multiplication is not a problem. The GPU is very good at matrix multiplication that most of it is better moved from CPU to GPU, actually the GPU is designed as a vector processor to handle these operations. Newer GPUs can handle Giga Flops so don't worry about performance for simple light calculations. If your application took a performance hit, you first need to identify the bottleneck and optimize accordingly.

Sending both Projection and Model View matrix to the GPU is very common to perform light calculations, so don't worry.

\$\endgroup\$
4
  • \$\begingroup\$ Okay thank you for this explanation. I come from normal programming to OpenGL. There I looked a lot after performance that's why I allways worry about it. So you mean I should send all three matrix (view, projection and model) to the shader and multiply them only there? Or should I calculate the MVMatrix in my Java code (CPU) and send it as one matrix to the shader? \$\endgroup\$
    – Cilenco
    Feb 4, 2015 at 13:38
  • \$\begingroup\$ @Cilenco Usually you should send all three matrices and multiply them in the shader. No need to perform the operation on the CPU side unless there is a need for it. \$\endgroup\$
    – concept3d
    Feb 4, 2015 at 14:13
  • \$\begingroup\$ Okay thank you. One last question: The shader programm, does it run vor each vertex? So let's say I render a cube for which I need 8 vertecies. Does the shader run the whole programm 8 times or only one time for the whole object? \$\endgroup\$
    – Cilenco
    Feb 4, 2015 at 18:13
  • \$\begingroup\$ @Cilenco it runs for each vertex. but don't worry it's not like a program that runs on CPU the GPU can handle millions of vertices. \$\endgroup\$
    – concept3d
    Feb 4, 2015 at 18:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .