Thursday, 17 January 2013

First Graphics Programming


This my first time playing with DirectX and graphics. I had really wanted to learn this the first time I made my first 3D game: Alien Escape (Mentis). Having understanding on graphics was important for game engineering. This was also the reason I sat on several art classes.


For the first assignment on Game Graphics class, we have to create our own mesh file input, play around with the vertex and fragment shader.

For my first mesh input, I decided to use a simple format:
{        // start of a triangle list
x position, y position           // first vertex
x position, y position           // second vertex
x position, y position           // third vertex
}        // end of triangle list

In the beginning, I wanted to use XML-like format or some standard format but I wasn’t sure how this mesh-file would grow. Therefore I decided using a simple one and then grow it as the assignment requirement or even changed it as it is fit.
I scaled and rotate my triangles in my vertex shader by time. First I scale it by time (0 to 1):
float o_position_x = i_position.x * cos( g_secondsElapsed );
float o_position_y = i_position.y * cos( g_secondsElapsed );
Then I rotate it:
o_position_x = o_position_x * cos(g_secondsElapsed) - o_position_y * sin(g_secondsElapsed);
o_position_y = o_position_x * sin(g_secondsElapsed) + o_position_y * cos(g_secondsElapsed);

Finally, I sent it to fragment shader:
o_position = float4( o_position_x, o_position_y, 0.0f, 1.0f );

PIX: vertex shader

In addition I also sent colour information to fragment shader. The colour I gave to it is randomly generated by time:      
float red = ( sin( g_secondsElapsed ) * 0.5 ) + 0.5;
float green = ( cos( 5.0 * g_secondsElapsed ) * 0.5 ) + 0.5f;
float blue = 1.0 - red;

Finally, in my fragment shader, I switched the red, green, and blue value:
float red = i_color.z;
float green = i_color.x;
float blue = i_color.y;

PIX: fragment shader

Frankly, I wanted to play with the alpha value but it did not make any changes yet and I could not figure the reason yet. This is how I changed the alpha by time:
float alpha = cos( g_secondsElapsed );
And how I sent it:
o_color = float4( red, green, blue, alpha );

While working on this assignment, I had a big question in mind: why we use triangle list instead of triangle strip? I had searched for a while but still couldn't find the best answer that satisfy me. Hmm... I will ask on the next class. Also I was wondering why the alpha was not working.

Source Code

1 comment:

  1. The answer for my question from John:
    1. we use triangle list instead of triangle strip because it's simpler. For the purpose of learning, it's better to use a simple method
    2. because we disable the alpha in our current renderer framework program ^__^

    ReplyDelete