gischenjun04
gischenjun04
采纳率0%
2016-01-29 06:34 阅读 1.8k

请教渲染到缓冲区为什么结果都是1呢

我尝试创建一个1024*728大小的浮点纹理,初始值为0。通过Initialize函数初始化环境;
    通过Timer定时执行Tick。Tick函数实现绑定到缓冲区并绘制。
      Initialize函数为:
        STDMETHODIMP CRunoffModel::Initialize(IGpuModelViewer*pViewer)

{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
if(frontTexture!=0) glDeleteTextures(1,&frontTexture);
if(backTexture!=0) glDeleteTextures(1,&backTexture);
if(FramebufferName!=0) glDeleteFramebuffers(1,&FramebufferName);
if(DepthBufferName!=0) glDeleteRenderbuffers(1,&DepthBufferName);
frontTexture=0;
backTexture=0;
FramebufferName=0;
glGenTextures(1,&frontTexture);
glGenTextures(1,&backTexture);
glGenFramebuffers(1, &FramebufferName);
glGenRenderbuffers(1, &DepthBufferName);
glBindTexture(GL_TEXTURE_2D, frontTexture);
float*textData=(float*)psa->pvData;

glTexImage2D(GL_TEXTURE_2D,0,GL_R32F,cols, rows,0,GL_RED,GL_FLOAT,textData);

glBindTexture(GL_TEXTURE_2D, backTexture);
glTexImage2D(GL_TEXTURE_2D,0,GL_R32F,cols, rows,0,GL_RED,GL_FLOAT,textData);

glBindTexture(GL_TEXTURE_2D, backTexture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);

glBindTexture(GL_TEXTURE_2D, frontTexture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);

pShader.CompileShader(IDR_PASS_VP,IDR_PASS_FP, 2, GLT_ATTRIBUTE_VERTEX, "vVertex",GLT_ATTRIBUTE_TEXTURE0,"vTexCoords");

CString uniforms[2]={"mvpMatrix","m_texture"};
pShader.FindUniforms(uniforms,2);
return S_OK;
}
Tick函数为:
STDMETHODIMP CRunoffModel::Tick(IGpuModelViewer*pViewer)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, FramebufferName);
glBindRenderbuffer(GL_RENDERBUFFER, DepthBufferName);

glBindFramebuffer(GL_DRAW_FRAMEBUFFER,FramebufferName);
glBindRenderbuffer(GL_RENDERBUFFER, DepthBufferName);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT32, cols, rows);

glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,frontTexture,0);  
glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER,DepthBufferName);

M3DMatrix44f proj;
m3dMakeOrthographicMatrix(proj,0,cols,0,rows,-1,0);
M3DMatrix44f matProj;
m3dLoadIdentity44(matProj);
M3DMatrix44f matViewProj;
m3dMatrixMultiply44(matViewProj,proj,matProj);
//GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0_EXT};
//glDrawBuffers(1,DrawBuffers);
glDrawBuffer(GL_COLOR_ATTACHMENT0);
glPolygonMode(GL_FRONT,GL_FILL);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) 
{
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    glBindRenderbuffer(GL_RENDERBUFFER, 0);
    //设置为0后,就退出纹理渲染状态
    return S_OK;
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,backTexture);
//纹理绘制过程
glUseProgram(pShader.GetShaderID());
glUniform1i(pShader.GetUniformLocation("m_texture"), 0);
glUniformMatrix4fv(pShader.GetUniformLocation("mvpMatrix"), 1, GL_FALSE,matProj);
glBegin(GL_QUADS);
glVertex3f(0.0,0.0,0.0);
glTexCoord2f(0.0,0.0);
glVertex3f(cols, 0.0,0.0);
glTexCoord2f(1.0,0.0);
glVertex3f(cols,rows,0.0) ;
glTexCoord2f(1.0,1.0);
glVertex3f(0.0,rows,0.0) ;
glTexCoord2f(0.0,1.0);
glEnd();
glFinish();
if(tickCount%1==0)
{
    float*textData=(float*)psa->pvData;
    glReadBuffer(GL_COLOR_ATTACHMENT0);  
    glReadPixels(0,0,cols,rows,GL_RED,GL_FLOAT,textData);  
    //读取数据
}
//交换纹理
GLuint tempText=frontTexture;
frontTexture=backTexture;
backTexture=tempText;
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
tickCount++;
return S_OK;

}
无论我怎样设置片段着色器,Tick函数glReadPixels结果始终为1。请求大神帮助

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • gischenjun04 gischenjun04 2016-01-29 12:51

    STDMETHODIMP CRunoffModel::Initialize(IGpuModelViewer*pViewer)
    {
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    if(frontTexture!=0) glDeleteTextures(1,&frontTexture);
    if(backTexture!=0) glDeleteTextures(1,&backTexture);
    if(FramebufferName!=0) glDeleteFramebuffers(1,&FramebufferName);
    if(DepthBufferName!=0) glDeleteRenderbuffers(1,&DepthBufferName);
    frontTexture=0;
    backTexture=0;
    FramebufferName=0;
    glGenTextures(1,&frontTexture);
    glGenTextures(1,&backTexture);
    glBindTexture(GL_TEXTURE_2D, frontTexture);
    float*textData=(float*)psa->pvData;

    glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,cols, rows,0,GL_RGBA,GL_FLOAT,textData);

    glBindTexture(GL_TEXTURE_2D, backTexture);
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,cols, rows,0,GL_RGBA,GL_FLOAT,textData);

    glBindTexture(GL_TEXTURE_2D, backTexture);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);

    glBindTexture(GL_TEXTURE_2D, frontTexture);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);

    computeShader.CompileShader(IDR_PASS_VP,IDR_PASS_FP, 2, GLT_ATTRIBUTE_VERTEX, "vVertex",GLT_ATTRIBUTE_TEXTURE0,"vTexCoords");

    CString uniforms[2]={"mvpMatrix","m_texture"};
    computeShader.FindUniforms(uniforms,2);
    showShader.CompileShader(IDR_PASS_VP,IDR_PASSS_FP, 2, GLT_ATTRIBUTE_VERTEX, "vVertex",GLT_ATTRIBUTE_TEXTURE0,"vTexCoords");

    showShader.FindUniforms(uniforms,2);
    glGenFramebuffers(1, &FramebufferName);
    glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
    glGenRenderbuffers(1, &DepthBufferName);
    glBindRenderbuffer(GL_RENDERBUFFER, DepthBufferName);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, cols, rows);
    glBindRenderbuffer(GL_RENDERBUFFER, 0);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    return S_OK;
    }
    STDMETHODIMP CRunoffModel::Tick(IGpuModelViewer*pViewer)
    {
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
    glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,GL_TEXTURE_2D,frontTexture,0);

    glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER,DepthBufferName);
    glClearColor(1, 1, 1, 1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glViewport(0,0,cols,rows);
    M3DMatrix44f proj;
    m3dMakeOrthographicMatrix(proj,0,cols,0,rows,-1,0);
    M3DMatrix44f matProj;
    m3dLoadIdentity44(matProj);
    M3DMatrix44f matViewProj;
    m3dMatrixMultiply44(matViewProj,proj,matProj);
    /*GLenum DrawBuffers[1] = {GL_COLOR_ATTACHMENT0_EXT};
    glDrawBuffers(1,DrawBuffers);*/
    glDrawBuffer(GL_COLOR_ATTACHMENT0);
    glPolygonMode(GL_FRONT,GL_FILL);
    if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
    {
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    glDrawBuffer(GL_BACK_LEFT);
    //设置为0后,就退出纹理渲染状态
    return S_OK;
    }
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D,backTexture);
    //纹理绘制过程
    glUseProgram(computeShader.GetShaderID());
    glUniform1i(computeShader.GetUniformLocation("m_texture"), 0);
    glUniformMatrix4fv(computeShader.GetUniformLocation("mvpMatrix"), 1, GL_FALSE,matProj);
    glBegin(GL_QUADS);
    glVertex3f(-1,-1,0.0);
    glTexCoord2f(0.0,0.0);
    glVertex3f(1, -1,0.0);
    glTexCoord2f(1.0,0.0);
    glVertex3f(1,1,0.0) ;
    glTexCoord2f(1.0,1.0);
    glVertex3f(-1,1,0.0) ;
    glTexCoord2f(0.0,1.0);
    glEnd();
    glFinish();
    if(tickCount%1==0)
    {
    float*textData=(float*)psa->pvData;
    glReadBuffer(GL_COLOR_ATTACHMENT0);

    glReadPixels(0,0,cols,rows,GL_RGBA,GL_FLOAT,textData);

    //读取数据
    }
    //交换纹理
    GLuint tempText=frontTexture;
    frontTexture=backTexture;
    backTexture=tempText;
    glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D,0);
    glDrawBuffer(GL_BACK_LEFT);
    tickCount++;
    return S_OK;
    }

    点赞 评论 复制链接分享

相关推荐