Image processing 如何在目标纹理中填充reategle内的纹理和reategle外的(0.0,0.0,0.0)纹理。?
有两种纹理 目标纹理-7201080 源纹理-300300 所需帮助: 我们需要将源纹理放置到目标纹理的给定位置(x中心、y中心、高度和宽度),并用0,0填充剩余的目标纹理 我们面临的挑战是,源纹理正在扩大到目标纹理的大小 我在着色器编码器中尝试了如下操作Image processing 如何在目标纹理中填充reategle内的纹理和reategle外的(0.0,0.0,0.0)纹理。?,image-processing,opengl,opengl-es,Image Processing,Opengl,Opengl Es,有两种纹理 目标纹理-7201080 源纹理-300300 所需帮助: 我们需要将源纹理放置到目标纹理的给定位置(x中心、y中心、高度和宽度),并用0,0填充剩余的目标纹理 我们面临的挑战是,源纹理正在扩大到目标纹理的大小 我在着色器编码器中尝试了如下操作 // Texture Coordinates static const GLfloat square_vertices[] = { -1.0f, -1.0f, // bottom left 1.0f, -1.0
// Texture Coordinates
static const GLfloat square_vertices[] = {
-1.0f, -1.0f, // bottom left
1.0f, -1.0f, // bottom right
-1.0f, 1.0f, // top left
1.0f, 1.0f, // top right
};
static const GLfloat texture_vertices[] = {
0.0f, 0.0f, // bottom left
1.0f, 0.0f, // bottom right
0.0f, 1.0f, // top left
1.0f, 1.0f, // top right
};
// program
glUseProgram(upsample_program_);
// vertex storage
GLuint vbo[2];
glGenBuffers(2, vbo);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
// vbo 0
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, 4 * 2 * sizeof(GLfloat), square_vertices,
GL_STATIC_DRAW);
glEnableVertexAttribArray(ATTRIB_VERTEX);
glVertexAttribPointer(ATTRIB_VERTEX, 2, GL_FLOAT, 0, 0, nullptr);
// vbo 1
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glBufferData(GL_ARRAY_BUFFER, 4 * 2 * sizeof(GLfloat), texture_vertices,
GL_STATIC_DRAW);
glEnableVertexAttribArray(ATTRIB_TEXTURE_POSITION);
glVertexAttribPointer(ATTRIB_TEXTURE_POSITION, 2, GL_FLOAT, 0, 0, nullptr);
// draw
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// cleanup
glDisableVertexAttribArray(ATTRIB_VERTEX);
glDisableVertexAttribArray(ATTRIB_TEXTURE_POSITION);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glDeleteVertexArrays(1, &vao);
glDeleteBuffers(2, vbo);
// Frame drawing
output_width=720;
output_height=1080;
// Upsample small mask into output.
GlTexture output_texture = CreateDestinationTexture(
output_width, output_height,GpuBufferFormat::kBGRA32);
{
gpu_helper_.BindFramebuffer(output_texture); // GL_TEXTURE0
glActiveTexture(GL_TEXTURE1);
//small_mask_texture is the texture need to render inside the react.
glBindTexture(GL_TEXTURE_2D, small_mask_texture.id());
GlRender();
glBindTexture(GL_TEXTURE_2D, 0);
glUniform2f(glGetUniformLocation(upsample_program_, "rcenters"),normx_center,normy_center);
glUniform2f(glGetUniformLocation(upsample_program_, "rhw"),normalized_width,normalized_height);
glFlush();
}
这是我的着色器代码
#if __VERSION__ < 130
#define in varying
#endif // __VERSION__ < 130
#ifdef GL_ES
#define fragColor gl_FragColor
precision highp float;
#else
#define lowp
#define mediump
#define highp
#define texture2D texture
out vec4 fragColor;
#endif // defined(GL_ES)
in vec2 sample_coordinate;
uniform sampler2D input_data;
uniform vec2 rcenters;
uniform vec2 rhw;
void main() {
vec4 pix = texture2D(input_data, sample_coordinate);
float xcenter = rcenters.s;
float ycenter = rcenters.t;
float rwidth = rhw.s;
float rheight = rhw.t;
if(rwidth != 0.0 && rheight != 0.0){
float cox = sample_coordinate.s;
float coy = sample_coordinate.t;
float xmin = xcenter-(rwidth/2.0);
float ymin = ycenter-(rheight/2.0);
float xmax = xcenter+(rwidth/2.0);
float ymax = ycenter+(rheight/2.0);
if((xmin<cox && xmax>cox) && (ymin<coy && ymax>coy)){
fragColor = pix;
}
else{
fragColor = vec4(0.0,0.0,0.0,1.0);
}
}
else{
fragColor = vec4(0.0,0.0,0.0,1.0);
}
}
如果版本<130
#以不同的方式定义
#endif/\uuuu版本<130
#国际财务报告准则
#定义fragColor gl_fragColor
高精度浮点;
#否则
#定义下限
#定义mediump
#定义高点
#定义纹理2D纹理
out vec4 fragColor;
#endif//已定义(总账)
在vec2样本坐标中;
统一的二维输入数据采样器;
均匀矢量中心;
均匀vec2-rhw;
void main(){
vec4 pix=纹理2D(输入数据、样本坐标);
浮动xcenter=rcenters.s;
浮点数中心=rcenters.t;
浮点数rwidth=rhw.s;
浮动rheight=rhw.t;
if(rwidth!=0.0&&rheight!=0.0){
float cox=样本坐标s;
float coy=样本坐标t;
浮点xmin=xcenter-(rwidth/2.0);
浮动ymin=中心-(rheight/2.0);
浮点xmax=xcenter+(rwidth/2.0);
浮动ymax=ycenter+(rheight/2.0);
如果((xmincox)&(ymincoy)){
fragColor=pix;
}
否则{
fragColor=vec4(0.0,0.0,0.0,1.0);
}
}
否则{
fragColor=vec4(0.0,0.0,0.0,1.0);
}
}
使用这个解决方案,我得到的源纹理放大等于目标纹理。我知道我需要在顶点中使用纹理坐标,但不知道该做什么。这个应用程序会变得更复杂吗?通常,将矩阵传递给顶点着色器,以乘以顶点位置,将它们投影到帧缓冲区上的正确位置。如果你永远不会做更多的事情,你可以作弊并手动定位。这个应用会变得更复杂吗?通常,将矩阵传递给顶点着色器,以乘以顶点位置,将它们投影到帧缓冲区上的正确位置。如果你永远不会做更多的事情,你可以作弊并手动定位它们。