gpt4 book ai didi

c++ - 纹理坐标 OSG GLSL

转载 作者:行者123 更新时间:2023-11-30 05:03:52 25 4
gpt4 key购买 nike

我有一个 500x500 像素的图像,我将其作为 Texture2D 转换为 GLSL,并将原始数据返回给 C++/OSG。我遇到了纹理坐标问题(GLSL 上的坐标从 0 到 1)。有人可以帮我解决这一点吗?

C++代码:

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);

顶点代码:

#version 130

void main() {
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

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

片段代码:

#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);
}

这是我的输入数据:

Input data

着色器的输出数据:

enter image description here

最佳答案

我通过在片段着色器上用 texelFetch 替换 texture2D 解决了这个问题。可以在此处找到这两个函数之间的区别: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);
}

关于c++ - 纹理坐标 OSG GLSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49213140/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com