1

I have an image 500x500 pixels which I converted as Texture2D to GLSL and return back the raw data to C++/OSG. I have faced problems with texture coordinates (the coordinates on GLSL goes from 0 to 1). Can someone help me with this point?

C++ code:

cv::Mat test = cv::Mat::zeros(512, 512, CV_8UC3);
test(cv::Rect(  0,   0, 255, 255)).setTo(cv::Scalar(255,0,0));
test(cv::Rect(256,   0, 255, 255)).setTo(cv::Scalar(0,255,0));
test(cv::Rect(  0, 256, 255, 255)).setTo(cv::Scalar(0,0,255));
test(cv::Rect(256, 256, 255, 255)).setTo(cv::Scalar(255,255,255));

osg::ref_ptr<osg::Image> image = new osg::Image;
image->setImage(512, 512, 3, GL_RGB, GL_BGR, GL_UNSIGNED_BYTE, test.data, osg::Image::NO_DELETE, 1);

osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D;
texture->setTextureSize(512, 512);
texture->setImage(image);

// Pass the texture to GLSL as uniform
osg::StateSet* ss = scene->getOrCreateStateSet();

osg::Uniform* samUniform = new osg::Uniform(osg::Uniform::SAMPLER_2D, "vertexMap");
samUniform->set(0);
ss->addUniform(samUniform);
ss->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);

Vertex code:

#version 130

void main() {
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

    gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
}

Fragment code:

#version 130

uniform sampler2D vertexMap;
out vec4 out_data;

void main() {
    vec3 value = texture2D(vertexMap, gl_TexCoord[0].st).xyz;
    out_data = vec4(value, 1);
}

This is my input data:

Input data

Output data from shader:

enter image description here

  • I'm not familiar with openscenegraph, but don't you have to set some sort of viewport dimension that matches with your surface? – StarShine Mar 10 '18 at 20:59
  • Looks good at a glance, but I suspect your model's texture coordinates, since I don't see the code that creates them. Can you show that code as well? Also, try putting in a vertex shader that simply displays the texcoords as R and G to verify that the texcoords really are what you expect. The behavior you demonstrate here is consistent with texcoords that vary on one axis but are constant on the other. – XenonofArcticus Mar 12 '18 at 00:33
  • Hi @XenonofArcticus, I'm not creating the texture coordinates. How should I do this? – Rômulo Cerqueira Mar 12 '18 at 21:55
  • Then you need to show the rest of your program and where/how texture coordinates for your geometry are being created. I feel like you got it working without really solving the problem, which doesn't bode well. – XenonofArcticus Mar 14 '18 at 03:24
  • I'm not confortable with my solution, because the texture size matches with the screen resolution. – Rômulo Cerqueira Mar 14 '18 at 20:32

1 Answers1

1

I solved by replacing texture2D by texelFetch on fragment shader. The difference between these two functions can be found here: https://gamedev.stackexchange.com/questions/66448/how-do-opengls-texelfetch-and-texture-differ

#version 130

uniform sampler2D vertexMap;

out vec4 out_data;

void main() {
    vec3 value = texelFetch(vertexMap, ivec2(gl_FragCoord.xy), 0).xyz;
    out_data = vec4(value, 1);
}