2 gischenjun04 gischenjun04 于 2016.01.29 14:34 提问

请教渲染到缓冲区为什么结果都是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 20: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;
}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
工具栏测试
传上来请教大神的。。。 用完会删掉 为什么一定要20个字呢 不合理啊 不合理
类不显示的问题
点击Query按钮,为什么没结果呢?
OpenGL中的缓冲区
OpenGL中的缓冲区 颜色缓冲区          OpenGL在绘制图元时,先是在一个缓冲区中完成渲染,然后再把渲染结果交换到屏幕上。我们把这两个缓冲区称为前颜色缓冲区(屏幕)和后颜色缓冲区。在默认情况下,OpenGL命令是在后颜色缓冲区进行渲染的。当然,也可以直接在前颜色缓冲区中进行渲染。          若要在前颜色缓冲区中进行渲染,第一种方法是直接告诉OpenGL希望在前
前缓冲区渲染
Android中标准的OpenGLES实现需要三倍于surface的缓冲区。三倍的缓冲区为了增加平滑度而增加了延时。对于大部分App来说,这是一个有争议的折衷。但是对于VR来说,这毫无疑问是很烂的选择。一个消耗大量GPU资源及从来不强制同步的APP会有超过50ms的延时,从eglSwapBuffers()调用到开始更新屏幕的像素,即使它的运行帧率是60Fps。          Android很
iOS OpenGL ES FBO 帧缓存区 渲染缓存区详解
参考文章:http://www.jianshu.com/p/c516e899e606 原文地址: https://developer.apple.com/library/content/documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/WorkingwithEAGLContexts/WorkingwithEAGLConte
PD教程PD教程PD教程
为什么下不了呢?为什么下不了呢?为什么下不了呢?为什么下不了呢?为什么下不了呢?为什么下不了呢?为什么下不了呢?为什么下不了呢?为什么下不了呢?
连续数的公倍数 为什么1小时有60分钟,而不是100分钟呢?这是历史上的习惯导致。事实上,它是1至6的每个数字的倍数。即1,2,3,4,5,6都是可以除尽60。
/* 连续数的公倍数 为什么1小时有60分钟,而不是100分钟呢?这是历史上的习惯导致。 但也并非纯粹的偶然:60是个优秀的数字,它的因子比较多。 事实上,它是1至6的每个数字的倍数。即1,2,3,4,5,6都是可以除尽60。 我们希望寻找到能除尽1至n的的每个数字的最小整数。 不要小看这个数字,它可能十分大,比如n=100, 则该数为: 697203752297124771645338089
深入理解缓冲区(一)
1          什么是缓冲区 百度上讲:http://baike.baidu.com/view/266782.htm[1] 缓冲器为暂时置放输出或输入资料的内存。   缓冲器内资料自储存设备(如硬盘)来,放置在缓冲器中,须待机送至CPU或其他运算设备。 缓冲区(b
在路由器后面,traceroute返回一系列*原因分析
首先:traceroute是如何工作的?Traceroute transmits packets with small TTL (Time To Live) values. The TTL is an IP header field that is used to prevent packets from running into endless loops. When a router that handles the packet subtracts one from the packe
以悲伤的姿态而存在,这是习惯
以悲伤的姿态而存在,这是习惯 — 以悲伤的姿态而存在,这是习惯 有人问我,我一直在想,能写出那样忧伤文字的女孩是怎样的。 紧握着空空的手心,再一次抬头望向天空的时候,我才发现,天空的云朵早已不知什么时候就已散开。 轻轻的放着那些音乐,一遍遍的看着本子上一页页的歌词。 什么时候我丢失了自己的心,什么时候需要用一段空白来形容自己。 心不在了,所有与我相关的事情都变成了一场意外,因为我没