Jump to content
  • Advertisement
Sign in to follow this  
alexblacko

Get a data from shader

This topic is 2533 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!