PDA

View Full Version : Use fragment shader to set olny one color channel instead of 3



tkostas
11-10-2015, 02:23 AM
Hi,

I would like to set colors firstly with my color_buffer_


glColorPointer(3, GL_FLOAT, 0, color_buffer_);
glVertexPointer(3,GL_FLOAT, 0, faces_);// Vertex Pointer to triangle array
glNormalPointer(GL_FLOAT, 0, normale_); // Normal pointer to normal array
glDrawArrays(GL_TRIANGLES, 0, totalconnectedtriangles_);

and then my fragment shader only to change the red channel


#version 410
layout(location = 0) out vec3 color_out;
precision highp float;


void main()
{
float z = 0.3f;
color_out = vec3(z, green_value_from_color_pointer(NO CHANGE) , blue_value_from_color_pointer(NO CHANGE))
}


Actually my fragment shader is able to set all the colors but I want to set only the red one.
Any help will appreciated if you have an idea :)

GClements
11-10-2015, 03:57 AM
It's not entirely clear what you're asking.

If you want rendering to leave the green and blue components in the framebuffer unchanged, use glColorMask(). It's not possible to achieve the same result using a fragment shader, as the fragment shader can't control which components are written, and can't read from the framebuffer directly (although you could make a copy of the framebuffer as a texture and have the fragment shader read from that).

tkostas
11-10-2015, 05:21 AM
It's not entirely clear what you're asking.

Ok so i'll try to be more precise.
Actually the aim of the programm I'm developping is to transmit Z buffer from Opengl to Cuda

To transmit it I comput it into my fragment shader(the calculation code is missing in the given code) and dump it into the Red color channel of my texture (The z value).

As I have many triangulations my idea was to make a differentiation between them into cuda thanks to values i set in G and B color channels with a color_buffer.

that's why I want my fragment shader to change only red color channel, and keep the color channels G and B setted by my color_buffer.

I tried also this so get the value setted by my color_buffer


#version 330
layout(location = 0) out vec3 color_out;
precision highp float;
uniform sampler2D tex;



void main()
{
float z = ...; //my_z_calculation
highp vec4 currentColor = texture(tex, gl_PointCoord);
color_out = vec3(z, currentColor.g , currentColor.b);
}

But it appear that currentColor.g , currentColor.b are always equal to 0.

Is it more clear ? if you have another strategy to suggest me feel free to tell me :)

Alfonse Reinheart
11-10-2015, 06:41 AM
As two people have told you thus far, color masking (https://www.opengl.org/wiki/Write_Mask#Color_Mask) is the way to do this. It's not a fragment shader property; it's state you set when rendering.

tkostas
11-10-2015, 06:56 AM
I'm sorry, I'm sure I'm missing something. I used color masking this allow only my fragment shader to update red color wich is what I want but glDrawArray can't set Green and Blue from my color buffer when I do that.



glColorPointer(3, GL_FLOAT, 0, color_buffer_);
glVertexPointer(3,GL_FLOAT, 0, faces_);// Vertex Pointer to triangle array
glNormalPointer(GL_FLOAT, 0, normale_); // Normal pointer to normal array
glDrawArrays(GL_TRIANGLES, 0, totalconnectedtriangles_);


How can i specify color masking for olny my fragment shader attached ?

Alfonse Reinheart
11-10-2015, 10:10 AM
When you bind the program you want to only write red, you also turn on color masking. When you bind some other program, you turn off color masking. This is not a hard problem.

tkostas
11-11-2015, 12:55 PM
When you bind the program you want to only write red, you also turn on color masking.
Do you mean when I make make a call to glUseProgram(...) before launching glutmainloop ?
Something like this:


glColorMask(true, false, false, false);
glUseProgram(program_id);
glColorMask(true, true, true, true);


I don't think you mean that because this obviously doesn't work.
Could you show me some code to help me to understand ?

Alfonse Reinheart
11-11-2015, 04:56 PM
OK, at some point in your render loop, you bind the program for use (with glUseProgram). Following this call, you make some rendering calls. And during those rendering calls, you want to write only to the red color. At some later point in your render loop, you bind a different program. Then you make other rendering calls. And during those calls, you want to write to all color channels.

So you do that. When you bind the program that you only want to write to the red color channel, you set the color mask to write only red. When you bind a different program, you change the color mask back.