简体   繁体   English

glsl着色器传递和渲染纹理

[英]glsl shader passing and rendering texture

I'm trying to pass and use a texture to shader... but every time I try to use it in even the easiest way I get black box, sometimes white - depends on the texture I put him.. somehow.... 我试图通过并使用纹理来着色器...但是每次我尝试以最简单的方式使用它时,我都会得到黑匣子,有时是白色,这取决于我放他的纹理..

Here's code fragment witch should work ( i've removed my wrappers to make it easier): 这是女巫应该工作的代码片段(为了方便起见,我删除了包装器):

fbo->bind();
fbo->drawBuffer(0);
fbo->setClearColor( 1.0, 1.0, 1.0, 1.0);
fbo->cleanCurrentTexture();

GLuint location;
location = glGetUniformLocation( Program->getID(), "sampler");
glActiveTexture( GL_TEXTURE0 );
glBindTexture(GL_TEXTURE_2D, fbo2->getTexture(0) );
glUniform1i(location, 0);
Program->begin();
glEnable(GL_TEXTURE_2D);

glBegin(GL_QUADS);
    glVertex3f( -1.0f, -1.0f, 0.0f );
    glVertex3f( -1.0f, 1.0f, 0.0f );
    glVertex3f( 1.0f, 1.0f, 0.0f );
    glVertex3f( 1.0f, -1.0f, 0.0f );
glEnd();

Program->end();

fbo->unbind();

glColor3f(1.0,1.0,1.0);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, fbo->getTexture(0) );
    glBegin(GL_QUADS);
         glTexCoord2d( 0.0f, 0.0f);
        glVertex3f( -1.0f, -1.0f, 0.0f );
         glTexCoord2d( 0.0f, 1.0f);
        glVertex3f( -1.0f, 1.0f, 0.0f );
         glTexCoord2d( 1.0f, 1.0f);
        glVertex3f( 1.0f, 1.0f, 0.0f );
         glTexCoord2d( 1.0f, 0.0f);
        glVertex3f( 1.0f, -1.0f, 0.0f );
    glEnd();
glDisable( GL_TEXTURE_2D);

Vertex shader: 顶点着色器:

#version 120

void main()
{
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
    gl_TexCoord[0] = gl_MultiTexCoord0;
}

Fragment shader: 片段着色器:

#version 120

uniform sampler2D sampler;

void main()
{   
    gl_FragColor = texture2D(sampler, gl_TexCoord[0].xy);
}

Ofcourse the texture I'm trying to bind exists and its complete. 当然,我要绑定的纹理存在且完整。 Here are it's parameters: 这是参数:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

Just as Plow says in his comment, I believe this has to do with texture coordinates not being set. 正如Plow在他的评论中所说,我认为这与未设置纹理坐标有关。 You are only setting texture coordinates for the second quad. 您仅设置第二个四边形的纹理坐标。 The second quad is not being drawn with the shader program either, and it is being drawn in the exact same coordinates as the first, which means it should be either completely white (because glColor3f(1.0,1.0,1.0) ), or it should not be drawn at all (depending on your camera position and whether or not depth testing is enabled). 也不使用着色器程序绘制第二个四边形,并且以与第一个四边形完全相同的坐标绘制它,这意味着它应该是完全白色的(因为glColor3f(1.0,1.0,1.0) ),或者应该根本不会绘制(取决于您的相机位置以及是否启用深度测试)。

Try drawing only the first quad, with texture coordinates properly set up. 尝试仅绘制第一个四边形,并正确设置纹理坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM