Jjang
Jjang

Reputation: 11444

Matrices for Computer Graphics operations

What I'm trying to do

I'm implementing my own viewing pipeline with Java AWT only, imitating the behaviour of OpenGL.

In the bottom line, I'm able to transform 3d polygons in World Coordinate System through the whole pipleline process and output them on the 2D screen in Viewport Coordinate System.

I also have the ability to perform scaling, rotation or translation on my world either.

Optimizations - Body of question

I've read that OpenGL holds internally matrices for each transformation (world to viewing, viewing to projection, projection to device/viewport - I have no model coordinates, I'm starting everything from world coordinates - all 3d polygons are on same world coordinates already, so please ignore the model-world transformations).

Now if I want to imitate that programatically, how do I achieve that?

If I simply hold the 3 matrices and every time a scaling/rotation/transformation is applied to the world, I have to:

  1. loop through the world coordinates and for each coordinate:

    1.1 multiply coordinate with the world-view matrix

    1.2 multiply coordinate with projection matrix (to 2D)

    1.3 multiply coordinate with viewport matrix

  2. final points are in screen coordinates, simply print to screen using Java AWT

Is it the appropriate approach?

Can I multiply the world-view matrix, projection matrix and projection-viewport matrix ahead, to a single matrix M, and just multiple each point with this M? I guess I can't because first matrix is 4x4, second 4x4 and third is 3x3 (no Z values anymore).

Upvotes: 1

Views: 233

Answers (1)

Xirema
Xirema

Reputation: 20396

Bear in mind that "Looping through each coordinate and multiplying them by each of the respective Matrices is:

  1. What OpenGL does in its Fixed Function Pipeline
  2. What most Vertex Shaders do in the Programmable Pipeline

You can, in fact, premultiply the matrices together ahead of time to cut down on calculations—some Vertex Shaders simply take a "MVP" matrix which is Projection, View, and Model premultiplied together. The viewport matrix should be reconstructable if you simply set the 'z' components to 0 and construct a 4x4 matrix, thus allowing it to be multiplied with the other matrices.

If I were you though, attempting to recreate the functionality of OpenGL on a software level, I'd focus on how OpenGL's Programmable Pipeline functions, i.e. taking a source or binary for shaders and parsing them into functions which are applied on the data, rather than having predefined matrices that you use. OpenGL's immediate mode does maintain the matrices you're thinking of, but Immediate Mode is more than two decades old, and if you want the stuff you're learning to be meaningful, you should probably build your pipeline around modern OpenGL, not 20-years-ago OpenGL.

Upvotes: 0

Related Questions