Hi guys! A few months ago I asked this question, yet it was suggested to me to use a Texture Atlas. This time round however, (also part of my new year’s resolution ) I’m trying to learn shader language properly, and I decided to tackle this problem again seeing as I’m trying to re-create my sprite engine from scratch. In short, I’m using GLSL to draw sprites from a sprite-sheet.
The most important part of drawing a sprite from a sprite sheet in this case is to draw only a subset/range of pixels, for example the range from (100, 0) to (200, 100). In the following test case sprite-sheet, and using the previous bounds, only the green part of the sprite-sheet would be drawn.
This is what I have so far:
Definition:
[java]MaterialDef Solid Color {
//This is the list of user-defined variables to be used in the shader
MaterialParameters {
Vector4 Color
Texture2D ColorMap
}
Technique {
VertexShader GLSL100: Shaders/tc_s1.vert
FragmentShader GLSL100: Shaders/tc_s1.frag
WorldParameters {
WorldViewProjectionMatrix
}
}
}[/java]
.vert file:
[java]uniform mat4 g_WorldViewProjectionMatrix;
attribute vec3 inPosition;
attribute vec4 inTexCoord;
varying vec4 texture_coordinate;
void main(){
gl_Position = g_WorldViewProjectionMatrix * vec4(inPosition, 1.0);
texture_coordinate = vec4(inTexCoord);
}[/java]
.frag:
[java]uniform vec4 m_Color;
uniform sampler2D m_ColorMap;
varying vec4 texture_coordinate;
void main(){
vec4 color = vec4(m_Color);
vec4 tex = texture2D(m_ColorMap, texture_coordinate);
color *= tex;
gl_FragColor = color;
}[/java]
As you can see, I tried to give the texture_coordinate a vec4 type, rather than a vec2, so as to be able to reference its p and q values (texture_coordinate.p and texture_coordinate.q). Modifying them only resulted in different hues.
m_Color refers to the color, inputted by the user, and serves the purpose of altering the hue. In this case, it should be disregarded.
So far, the shader works as expected and the texture displays correctly.
I’ve been using resources and tutorials from NeHe (NeHe Productions: GLSL: An Introduction) and Lighthouse3D (http://www.lighthouse3d.com/tutorials/glsl-tutorial/simple-texture/).
I would appreciate any help, especially if it points me in the right direction as to which functions/values I should alter to get the desired effect of displaying only part of the texture. Thanks in advance!