gpt4 book ai didi

c++ - opengl 多纹理 glsl - 一次只有一个纹理

转载 作者:太空宇宙 更新时间:2023-11-04 14:10:47 25 4
gpt4 key购买 nike

好像我使用了一次texture(texture0,textCoord),然后我尝试再次将它与另一个纹理一起使用texture(texture1,textCoord); 它返回 (0,0,0,0);我只是不知道为什么。

在我的程序中,这似乎是一个工作顺序,如果该部门比其他部门更有效,如果我不使用深度而不是仅使用颜色等等。(Depht、Color、Normal、Pos、Shine 是顺序)

this is the fragment shader :

uniform sampler2D tex0;
uniform sampler2D tex1;
uniform sampler2D tex2;
uniform sampler2D tex3;
uniform sampler2D depht;

void main()
{
float UNIT = 1./640. ;

//vec4 depht = texture(depht,gl_TexCoord[0].st);
vec4 color = texture(tex0,gl_TexCoord[0].st);
vec4 norma = texture(tex1,gl_TexCoord[0].st);
vec4 posit = texture(tex2,gl_TexCoord[0].st);
vec4 shine = texture(tex3,gl_TexCoord[0].st);


if(false){
gl_FragColor=( texture2D(tex0,gl_TexCoord[0].st +vec2(0,0))+
texture2D(tex0,gl_TexCoord[0].st +vec2(0,-UNIT))+
texture2D(tex0,gl_TexCoord[0].st +vec2(UNIT,-UNIT))+
texture2D(tex0,gl_TexCoord[0].st +vec2(UNIT,0))+
texture2D(tex0,gl_TexCoord[0].st +vec2(UNIT,UNIT))+
texture2D(tex0,gl_TexCoord[0].st +vec2(0,UNIT))+
texture2D(tex0,gl_TexCoord[0].st +vec2(-UNIT,UNIT))+
texture2D(tex0,gl_TexCoord[0].st +vec2(-UNIT,0))+
texture2D(tex0,gl_TexCoord[0].st +vec2(-UNIT,-UNIT)))/9.;
}else{
gl_FragColor=(norma+color);
}
}

这是绑定(bind)部分:

    fbotest.drawFBO();
DEFERRED.UseNow();
DrawGLScene();

fbotest.readFBO();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

DEFERRED2.UseNow();

//glBlendFunc (GL_ONE, GL_ONE);
glEnable( GL_BLEND );

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, fbotest.Color);
glEnable(GL_TEXTURE_2D);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, fbotest.Normal);
glEnable(GL_TEXTURE_2D);

glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, fbotest.Pos);
glEnable(GL_TEXTURE_2D);

glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, fbotest.Shine);
glEnable(GL_TEXTURE_2D);

glActiveTexture(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, fbotest.Depht);
glEnable(GL_TEXTURE_2D);

glUniform1i(glGetUniformLocation(DEFERRED2.PR,"tex0"),0);
glUniform1i(glGetUniformLocation(DEFERRED2.PR,"tex1"),1);
glUniform1i(glGetUniformLocation(DEFERRED2.PR,"tex2"),2);
glUniform1i(glGetUniformLocation(DEFERRED2.PR,"tex3"),3);
glUniform1i(glGetUniformLocation(DEFERRED2.PR,"depht"),4);

glLoadIdentity();
glBegin(GL_QUADS);
glMultiTexCoord2f(GL_TEXTURE0, 1.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE1, 1.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE2, 1.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE3, 1.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE4, 1.0, 0.0);
glVertex3f( 1,-1,-3);
glMultiTexCoord2f(GL_TEXTURE0, 1.0, 1.0);
glMultiTexCoord2f(GL_TEXTURE1, 1.0, 1.0);
glMultiTexCoord2f(GL_TEXTURE2, 1.0, 1.0);
glMultiTexCoord2f(GL_TEXTURE3, 1.0, 1.0);
glMultiTexCoord2f(GL_TEXTURE4, 1.0, 1.0);
glVertex3f( 1,1 ,-3);
glMultiTexCoord2f(GL_TEXTURE0, 0.0, 1.0);
glMultiTexCoord2f(GL_TEXTURE1, 0.0, 1.0);
glMultiTexCoord2f(GL_TEXTURE2, 0.0, 1.0);
glMultiTexCoord2f(GL_TEXTURE3, 0.0, 1.0);
glMultiTexCoord2f(GL_TEXTURE4, 0.0, 1.0);
glVertex3f( -1,1,-3);
glMultiTexCoord2f(GL_TEXTURE0, 0.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE1, 0.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE2, 0.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE3, 0.0, 0.0);
glMultiTexCoord2f(GL_TEXTURE4, 0.0, 0.0);
glVertex3f(-1,-1,-3);
glEnd();

glActiveTextureARB(GL_TEXTURE4);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTextureARB(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTextureARB(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTextureARB(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, 0);
glActiveTextureARB(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
glDisable( GL_BLEND );

这是可能的 FBO 类:

    class deferred{
public :
unsigned int FBO;
unsigned int Depht;
unsigned int Color;
unsigned int Normal;
unsigned int Pos;
unsigned int Shine;

deferred();
void initFBO();
void readFBO();
void drawFBO();

};

void deferred::readFBO(){
glBindFramebuffer(GL_DRAW_FRAMEBUFFER,0);
glBindFramebuffer(GL_READ_FRAMEBUFFER,FBO);
}

void deferred::drawFBO(){
glBindFramebuffer(GL_READ_FRAMEBUFFER,0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER,FBO);
}

deferred::deferred(){
FBO =0;
Depht =0;
Color =0;
Normal =0;
Pos =0;
Shine =0;
}

void deferred::initFBO(){
glGenFramebuffers(1, &FBO);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, FBO);


glGenTextures(1,&Depht );
glGenTextures(1,&Color );
glGenTextures(1,&Normal);
glGenTextures(1,&Pos );
glGenTextures(1,&Shine );

int WindowWidth = Ww;
int WindowHeight= Wh;

glBindTexture(GL_TEXTURE_2D, Color);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB32F, WindowWidth, WindowHeight, 0, GL_RGB, GL_FLOAT, NULL);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, Color, 0);

glBindTexture(GL_TEXTURE_2D, Normal);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB32F, WindowWidth, WindowHeight, 0, GL_RGB, GL_FLOAT, NULL);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, Normal, 0);

glBindTexture(GL_TEXTURE_2D, Pos);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB32F, WindowWidth, WindowHeight, 0, GL_RGB, GL_FLOAT, NULL);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, Pos, 0);

glBindTexture(GL_TEXTURE_2D, Shine);
glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB32F, WindowWidth, WindowHeight, 0, GL_RGB, GL_FLOAT, NULL);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT3, GL_TEXTURE_2D, Shine, 0);

glBindTexture(GL_TEXTURE_2D, Depht);
glTexImage2D (GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, WindowWidth, WindowHeight, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, Depht, 0);

GLenum DrawBuffers[] = { GL_COLOR_ATTACHMENT0,
GL_COLOR_ATTACHMENT1,
GL_COLOR_ATTACHMENT2,
GL_COLOR_ATTACHMENT3};

glDrawBuffers(4, DrawBuffers);

GLenum Status = glCheckFramebufferStatus(GL_FRAMEBUFFER);

if (Status != GL_FRAMEBUFFER_COMPLETE) {
cout <<"Frame Buffer ERROR : "<< Status << "\n" ;
}

glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
}

最佳答案

glEnable(GL_TEXTURE_2D);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
glUniform1i(glGetUniformLocation(DEFERRED2.PR,"tex0"),0);

尝试将它们更改为如下所示。绑定(bind)时应启用 GL_TEXTURE_2D。此外,glBindTexture 肩部末尾的 ID 上升 - 0 1 2 3 4 5。我不确定您的值返回了什么。然后每次使用 glUinform1i 发送该数据,这将绑定(bind)绑定(bind)在 0 处的纹理。

关于c++ - opengl 多纹理 glsl - 一次只有一个纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14497634/

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