Game Development Reference
In-Depth Information
3.
Before we can use the canvas, we have to construct it. Note FRect is created as a
fullscreen quad:
clCanvas::clCanvas()
{
FRect = clGeomServ::CreateRect2D( 0.0f, 0.0f,
1.0f, 1.0f, 0.0f, false, 1 );
FRectVA = new clGLVertexArray();
FRectVA->SetVertexAttribs( FRect );
FRectSP = new clGLSLShaderProgram( RectvShaderStr,
RectfShaderStr );
FTexRectSP = new clGLSLShaderProgram( RectvShaderStr,
TexRectfShaderStr );
}
4.
We remap the coordinates of FRect in the following vertex shader, so that they
match the user-speciied dimensions:
uniform vec4 u_RectSize;
in vec4 in_Vertex;
in vec2 in_TexCoord;
out vec2 Coords;
void main()
{
Coords = in_TexCoord;
float X1 = u_RectSize.x;
float Y1 = u_RectSize.y;
float X2 = u_RectSize.z;
float Y2 = u_RectSize.w;
float Width = X2 - X1;
float Height = Y2 - Y1;
vec4 VertexPos = vec4( X1 + in_Vertex.x * Width,
Y1 + in_Vertex.y * Height,
in_Vertex.z, in_Vertex.w ) *
vec4( 2.0, -2.0, 1.0, 1.0 ) +
vec4( -1.0, 1.0, 0.0, 0.0 );
gl_Position = VertexPos;
}
5. The actual dimensions, speciied as the top-left and bottom-right corners of the
rectangle, are passed as xyzw components of the u_RectSize uniform. A simple
arithmetic does the rest. The fragment shader is very simple. Indeed, we need to
apply just a solid color from the uniform:
uniform vec4 u_Color;
out vec4 out_FragColor;
in vec2 Coords;
void main()
{
out_FragColor = u_Color;
}
 
Search WWH ::




Custom Search