Get a data from shader

Started by
2 comments, last by V-man 12 years, 7 months ago
Greetings,

Sorry for my english.

I have a texture on quad with specific sizes. I need to get accurate pixel coordinates on mouse click and draw new texture with cross in this place. I heard it can be implement with additional shader like this:

Main shader:



VERTEX_SHADER = compileShader("""
void main() {
gl_TexCoord[0] = (gl_Vertex+1.0)*0.5;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

}""", GL_VERTEX_SHADER)
FRAGMENT_SHADER = compileShader("""
uniform sampler2D color;


void main() {


vec3 tcolor = texture2D(color, gl_TexCoord[0].st).xyz;

gl_FragColor = vec4(tcolor, 0.0);


}""", GL_FRAGMENT_SHADER)




Additional shader I create on mouse click:


VERTEX_SHADER = compileShader("""
void main() {
gl_TexCoord[0] = (gl_Vertex+1.0)*0.5;
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}""", GL_VERTEX_SHADER)
FRAGMENT_SHADER = compileShader("""

uniform sampler2D color;
void main() {
vec3 tcolor = texture2D(color, gl_TexCoord[0].st).xyz;
gl_FragColor = vec4(gl_TexCoord[0].st, 0.0, 0.0);
}""", GL_FRAGMENT_SHADER)



So as far as I know coordinates of mouse should be in back buffer after that. I tried to read data from back buffer with glReadPixels but got pixels values instead of needed coordinates.
Please help to implement this.

Thank you in advance.
Advertisement
You want to write the tex coord values to the framebuffer?
That is possible but you have to pay attention to the format of the framebuffer. The default framebuffer is typically a BGRA8 format.
Tex coords can be any float value. Perhaps you should render to a RenderBuffer with a floating point format.

The Wiki page about FBO code GL_EXT_framebuffer_object
http://www.opengl.org/wiki/GL_EXT_framebuffer_object#Quick_example.2C_render_to_buffer_.28p-buffer_replacement.29

but instead of
glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_RGBA8, 256, 256); you would use

GL_RGBA32F_ARB (which requires http://www.opengl.org/registry/specs/ARB/texture_float.txt)
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);
Actually I don't know where better to write values. Is it the best way to implement it? And how do the values from shader put to RenderBuffer.

Thank you.
Write it to a floating point format render buffer and read it back with glReadPixels.

Is it the best way to implement it?[/quote]

The alternative way is to do it yourself. Look for barycentric coordinates and you'll get a few web pages about it.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

This topic is closed to new replies.

Advertisement