PDA

View Full Version : Transmit image data to shader sampler2D



Bezarius
09-12-2015, 06:33 AM
Hello. I'm trying add to ofx plugin shader.
Please tell me how to transfer the image to the shader.


// ! shader id
GLuint Program;

GLuint TEXTURE;

GLint Resolution;

GLuint TDiffuse;


// ! shader log
void shaderLog(unsigned int shader)
{
int infologLen = 0;
int charsWritten = 0;
char *infoLog;

glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infologLen);

if (infologLen > 1)
{
infoLog = new char[infologLen];
if (infoLog == NULL)
{
std::cout << "ERROR: Could not allocate InfoLog buffer\n";
exit(1);
}
glGetShaderInfoLog(shader, infologLen, &charsWritten, infoLog);
std::cout << "InfoLog: " << infoLog << "\n\n\n";
delete[] infoLog;
}
}

// ! init ogl
void initGL()
{
glClearColor(0, 0, 0, 0);
}

#define STR(x) #x
#define STRINGIFY(x) STR(x)

// ! shaders init
void initShader()
{
const char* msaaSource =
STRINGIFY(
uniform sampler2D tDiffuse;
uniform vec2 resolution;

varying vec2 vUv;

const float FXAA_REDUCE_MIN = 1.0 / 128.0;
const float FXAA_REDUCE_MUL = 1.0 / 8.0;
const float FXAA_SPAN_MAX = 8.0;

void main() {

vec3 rgbNW = texture2D(tDiffuse, (gl_FragCoord.xy + vec2(-1.0, -1.0)) * resolution).xyz;
vec3 rgbNE = texture2D(tDiffuse, (gl_FragCoord.xy + vec2(1.0, -1.0)) * resolution).xyz;
vec3 rgbSW = texture2D(tDiffuse, (gl_FragCoord.xy + vec2(-1.0, 1.0)) * resolution).xyz;
vec3 rgbSE = texture2D(tDiffuse, (gl_FragCoord.xy + vec2(1.0, 1.0)) * resolution).xyz;
vec4 rgbaM = texture2D(tDiffuse, gl_FragCoord.xy * resolution);
vec3 rgbM = rgbaM.xyz;
float opacity = rgbaM.w;

vec3 luma = vec3(0.299, 0.587, 0.114);

float lumaNW = dot(rgbNW, luma);
float lumaNE = dot(rgbNE, luma);
float lumaSW = dot(rgbSW, luma);
float lumaSE = dot(rgbSE, luma);
float lumaM = dot(rgbM, luma);
float lumaMin = min(lumaM, min(min(lumaNW, lumaNE), min(lumaSW, lumaSE)));
float lumaMax = max(lumaM, max(max(lumaNW, lumaNE), max(lumaSW, lumaSE)));

vec2 dir;
dir.x = -((lumaNW + lumaNE) - (lumaSW + lumaSE));
dir.y = ((lumaNW + lumaSW) - (lumaNE + lumaSE));

float dirReduce = max((lumaNW + lumaNE + lumaSW + lumaSE) * (0.25 * FXAA_REDUCE_MUL), FXAA_REDUCE_MIN);

float rcpDirMin = 1.0 / (min(abs(dir.x), abs(dir.y)) + dirReduce);
dir = min(vec2(FXAA_SPAN_MAX, FXAA_SPAN_MAX),
max(vec2(-FXAA_SPAN_MAX, -FXAA_SPAN_MAX),
dir * rcpDirMin)) * resolution;

vec3 rgbA = 0.5 * (
texture2D(tDiffuse, gl_FragCoord.xy * resolution + dir * (1.0 / 3.0 - 0.5)).xyz +
texture2D(tDiffuse, gl_FragCoord.xy * resolution + dir * (2.0 / 3.0 - 0.5)).xyz);

vec3 rgbB = rgbA * 0.5 + 0.25 * (
texture2D(tDiffuse, gl_FragCoord.xy * resolution + dir * -0.5).xyz +
texture2D(tDiffuse, gl_FragCoord.xy * resolution + dir * 0.5).xyz);

float lumaB = dot(rgbB, luma);

if ((lumaB < lumaMin) || (lumaB > lumaMax)) {

gl_FragColor = vec4(rgbA, opacity);

}
else {

gl_FragColor = vec4(rgbB, opacity);

}

}
);
// ! var for shaers ids
GLuint msaaShader;


// ! create fragment shader
msaaShader = glCreateShader(GL_FRAGMENT_SHADER); //GL_SAMPLER_2D
// ! transmit shader source
glShaderSource(msaaShader, 1, &msaaSource, NULL);
// ! complie shader
glCompileShader(msaaShader);

std::cout << "sampler2d shader \n";
shaderLog(msaaShader);

// ! create program and attach shaders
Program = glCreateProgram();
glAttachShader(Program, msaaShader);

// ! link shader program
glLinkProgram(Program);

// ! check link
int link_ok;
glGetProgramiv(Program, GL_LINK_STATUS, &link_ok);
if (!link_ok)
{
std::cout << "error attach shaders \n";
return;
}

// just for check
int total = -1;
glGetProgramiv(Program, GL_ACTIVE_UNIFORMS, &total);
std::cout << "total: " << total;
for (int i = 0; i < total; ++i) {
int name_len = -1, num = -1;
GLenum type = GL_ZERO;
char name[100];
glGetActiveUniform(Program, GLuint(i), sizeof(name) - 1,
&name_len, &num, &type, name);
name[name_len] = 0;
GLuint location = glGetUniformLocation(Program, name);
}

// get params ids

const char* tDiffuse_name = "tDiffuse";
TDiffuse = glGetUniformLocation(Program, tDiffuse_name);
if (TDiffuse == -1)
{
std::cout << "could not bind tDiffuse " << tDiffuse_name << std::endl;
return;
}

const char* resolution_name = "resolution";
Resolution = glGetUniformLocation(Program, resolution_name);
if (Resolution == -1)
{
std::cout << "could not bind resolution " << resolution_name << std::endl;
return;
}

checkOpenGLerror();
}


//! render
void render()
{
if (!flag)
{
flag = true;
char fakeParam[] = "fake";
char *fakeargv[] = { fakeParam, NULL };
int fakeargc = 1;
glutInit(&fakeargc, fakeargv);
glewInit();
//initGL();
initShader();
}

glClear(GL_COLOR_BUFFER_BIT);

glUseProgram(Program);

// set resolution
glUniform2f(Resolution, 1.f, 1.f);

//glActiveTexture(GL_TEXTURE0);
//glBindTexture(GL_TEXTURE_2D, texture0);
//glUniform1i(TDiffuse, 0);

//glUniform1i(TDiffuse, 0); /// <== ?????

// ! turn off shader program
glUseProgram(0);

checkOpenGLerror();
}


a place where I need help:



...

glUseProgram(Program);

// set resolution
glUniform2f(Resolution, 1.f, 1.f);

// set sampler2D
//glActiveTexture(GL_TEXTURE0);
//glBindTexture(GL_TEXTURE_2D, texture0);
//glUniform1i(TDiffuse, 0);

//glUniform1i(TDiffuse, 0); /// <== ?????

// ! turn off shader program
glUseProgram(0);

checkOpenGLerror();


I testet simple shaders like this:


const char* vsSource =
"attribute vec2 coord;\n"
"void main() {\n"
" gl_Position = vec4(coord, 0.0, 1.0);\n"
"}\n";
const char* fsSource =
"uniform vec4 color;\n"
"void main() {\n"
" gl_FragColor = color;\n"
"}\n";

it's work ok.

for render i use this code:


glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(Program);
static float red[4] = {1.0f, 0.0f, 0.0f, 1.0f};
glUniform4fv(Unif_color, 1, red);
glEnableVertexAttribArray(Attrib_vertex);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(Attrib_vertex, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDrawArrays(GL_TRIANGLES, 0, sizeof (vertex));
glDisableVertexAttribArray(Attrib_vertex);
glUseProgram(0);
checkOpenGLerror();


if needed, can provide full plugin code

Dark Photon
09-12-2015, 08:50 AM
Hello. I'm trying add to ofx plugin shader.
Please tell me how to transfer the image to the shader.

It appears you're binding the texture to texture unit 0 before you render -- good.

You also need to poke the texture unit number (0) into the sampler2D uniform in your shader (which you don't have yet) with glUniform1i().

To add it, add a "uniform sampler2D texture;" reference in your shader and use it in your shading logic.

See any GLSL tutorial on texturing for ready-made copy/paste code.

Bezarius
09-12-2015, 01:47 PM
It appears you're binding the texture to texture unit 0 before you render -- good.

You also need to poke the texture unit number (0) into the sampler2D uniform in your shader (which you don't have yet) with glUniform1i().

To add it, add a "uniform sampler2D texture;" reference in your shader and use it in your shading logic.

See any GLSL tutorial on texturing for ready-made copy/paste code.

Thanks for the answer. Go in reading tutorials :)

Bezarius
09-13-2015, 11:02 AM
I read a couple of tutorials, did not understand: \
They are generally considered independent apps. In my case, using the plugin.
I guess I need to get the existing buffer and convert it to a texture.

Bezarius
09-14-2015, 09:08 AM
i found solution. Texture can be obtained using ofx.