PDA

View Full Version : texture to texture copy, running through a shader?



zassGL
07-19-2011, 03:16 PM
Hello everyone,

I've got a very basic shader that simply rotates blue->red->green colors. So a blue rectangle running through this shader would output as a red rectangle.

I've got texture A. I would like to create texture B, that is a copy of texture A run through the shader (thus colors rotated).

The way I'm trying to do this is by creating an FBO, attaching texture B to it, and then rendering texture A to that FBO.


This sort of works, but not really. Texture B ends up having a smaller version of texture A.. I've probably got the viewport and/or projection matrix settings off.. I keep tweaking them but it never looks quite right.

Rather than trying goldilocks programming (mess with it until it looks just right), I was hoping that someone here could aid me. I'm sure this is a problem that's been solved before. Does anyone have a short code snippet of how to do this?

Thank you so much!

_arts_
07-20-2011, 01:19 AM
Simply do it inside a shader with something like this (not tested):



// fragment shader
uniform sampler2D tex;
varying vec3 col;

void main()
{
col = texture2D(tex, texture_coordinates);

gl_FragColor = vec4(col.b, col.r, col.g, 1.0);
}

Alfonse Reinheart
07-20-2011, 02:19 AM
You can't set a varying variable in a fragment shader. Perhaps you meant this:



uniform sampler2D tex;
in vec2 texture_coordinates;

void main()
{
vec3 col = texture2D(tex, texture_coordinates);

gl_FragColor = vec4(col.b, col.r, col.g, 1.0);
}

Y-tension
07-20-2011, 03:18 AM
Hi zassGL, set projection and modelview matrix to identity, viewport must match the dimensions of your second (write-to) texture. Then just render a full screen quad (1, 1),(-1, 1),(-1, -1),(1, -1) - z coordinate is 0 in all vertices - with appropriate texture coordinates (1, 1),(0, 1),(0, 0),(1, 0), and using a shader like the one above things should work. If texture A and B don't have the same dimensions you will get distortion but I thought from your description that they must be the same.

_arts_
07-20-2011, 03:29 AM
You can't set a varying variable in a fragment shader.

I have varying variables in most of my fragment shaders and that always works like a charm... I think I have to learn a newer version of GLSL, or maybe is it a big mistake from my part and varying could never be used (even with GLSL 1.0) in fragment shaders ?
Anyway, I would prefer to stuck with GLSL 1.0 for some reasons.

zassGL
07-20-2011, 02:04 PM
Y-Tension, thank you for the reply. That worked (also I had to set my projection to orthographic).

Thank you also arts and alphonse for the replies.