PDA

View Full Version : How to set the texture coordinates?



SagoO
04-09-2011, 11:01 PM
Hi,

Let's say I have a texture with dimension 512x512.

If I do not set the coordinates of the texture, I just define the color of each pixels and output to a window with size 512x512. Will the color I set before displayed in order?

Eg: I use 8-bit RGBA format. Max value =255. I set some 1st row to white and 2nd row to black and so on.

So, I should get this:

1st row 255 255 ...........................255 (512x4)
2nd row 0 0 ........................... 0 (512x4)
.
.
.
512 row 255 255 ........................... 255 (512x4)

This is what I expect. But somehow, the values I get are rubbish. The values seems like to mix up. I dun even know here is the problem. My understanding correct or not from this example?

Alfonse Reinheart
04-09-2011, 11:14 PM
If I do not set the coordinates of the texture, I just define the color of each pixels and output to a window with size 512x512. Will the color I set before displayed in order?

What you said here does not make sense.

A texture is nothing more than a fancy array. You cannot draw an array; you can only draw the elements in an array. To get an element from an array, you must index that array. For textures, the index is called a "texture coordinate".

If you're using shaders, then you will need to provide a texture coordinate to the texture function. Whether this coordinate is interpolated from a vertex shader output, or generated on the fly in the fragment shader is irrelevant. What matters is that you need a texture coordinate.

If you're using the fixed-function pipeline, you will need to provide texture coordinates as vertex attributes.

Also, how do you "define the color of each pixels"? The color that is written to the framebuffer is what you output from the fragment shader (blending and other post-shader operations notwithstanding).

SagoO
04-09-2011, 11:34 PM
If I do not set the coordinates of the texture, I just define the color of each pixels and output to a window with size 512x512. Will the color I set before displayed in order?

What you said here does not make sense.

A texture is nothing more than a fancy array. You cannot draw an array; you can only draw the elements in an array. To get an element from an array, you must index that array. For textures, the index is called a "texture coordinate".

What I means here is how set the texture coordinates exactly the same on screen. If my texture is 512x512, I want to draw the elements inside the texture to a screen with 512x512 also. And, I want the elements in the texture to be exactly the same on the screen without distortion.

May be I should explain in this way:

coord(x,y) is the coordinates on screen. TexCoord(x,y) is the coordinates of the texture.

I want to draw the elements of TexCoord(0,0) to coord(0,0), TexCoord(1,1) to coord(1,1) and so on.



If you're using shaders, then you will need to provide a texture coordinate to the texture function. Whether this coordinate is interpolated from a vertex shader output, or generated on the fly in the fragment shader is irrelevant. What matters is that you need a texture coordinate.

If you're using the fixed-function pipeline, you will need to provide texture coordinates as vertex attributes.

Also, how do you "define the color of each pixels"? The color that is written to the framebuffer is what you output from the fragment shader (blending and other post-shader operations notwithstanding).

Ya. I use fragment to output the color to frame buffer using gl_FragColor. I will get the elements in TexCoord(0,0) and output the color to coord(0,0). But I am not sure how to set the coordinates on the screen. My values all screw up. In my program, it seems like to take element in TexCoord(0,0) and output to coord(1,1) instead of coord(0,0) as I want for example.

Alfonse Reinheart
04-09-2011, 11:45 PM
I use fragment to output the color to frame buffer using gl_FragColor. I will get the elements in TexCoord(0,0) and output the color to coord(0,0). But I am not sure how to set the coordinates on the screen. My values all screw up. In my program, it seems like to take element in TexCoord(0,0) and output to coord(1,1) instead of coord(0,0) as I want for example.

Fragment shaders can not dictate which pixel/sample location that they write to. The built-in variable (http://www.opengl.org/wiki/GLSL_Predefined_Variables#Fragment_shader_inputs) gl_FragCoord has the window-space coordinates of the current fragment. However, as a shader input, it cannot be changed. And even if you could, changing it wouldn't matter.

You can use gl_FragCoord to generate your texture coordinates if you want.