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

问题描述

请教渲染到缓冲区为什么结果都是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。请求大神帮助

解决方案

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;
}

时间: 2024-10-29 15:30:25

opengl-请教渲染到缓冲区为什么结果都是1呢的相关文章

多线程-如何在OpenGL中渲染多个视频

问题描述 如何在OpenGL中渲染多个视频 程序使用win32开发,视频使用ffmpeg进行帧的截取,然后贴到OpenGL纹理中,渲染一个视频的话不用使用到多线程,处理能力足够快,但是现在需要渲染多个视频,不可能在主线程完成,两三个视频基本上窗口就卡死了.后来改用多线程,建立了两个OpenGL的上下文环境,一个线程负责更新纹理,主线程负责渲染,但是更新纹理的线程依然是照着视频一个个照顺序截,截完再通知主线程画出来,本来以为这样的结构应该没问题,结果渲染速度依然达不到要求,现在想问问有什么办法做到

[求助]请教GIS中缓冲区分析和区域合并的算法:

问题描述 [求助]请教GIS中缓冲区分析和区域合并的算法:小弟我正在做一个GIS系统中的"缓冲区分析"和"区域合并":不知道那位大哥有这方面的经验,谢谢!

java基于OpenGL ES实现渲染实例_java

本文实例讲述了java基于OpenGL ES实现渲染的方法.分享给大家供大家参考.具体如下: 1. Run.java文件: package net.obviam.opengl; import android.app.Activity; import android.opengl.GLSurfaceView; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; public

(译)OpenGL ES2.0 – Iphone开发指引

纯的不能再纯的转载,原译者说别人是牛人,不过在我看来,他也挺牛的了. 刚接触OpenGL ES时,这篇文章不知看了多少遍了,只能照本宣科,不知其然与其所以然. 后决定放弃这种不负责任的工作方式,虽然不是我的方式,但我又必须按要求做事! 按自已的想法,找到罗朝辉博客上最全面的那篇,把例子下载下来,准备花长些时间,耐下心来,一点一点地看. 到目前,从几何顶点,到灯光,到着色器,再到模型加载,最后是纹理,中间回到几何顶点几次,轻轻地,仅是轻轻地扒了一小层皮儿而已,现在再回头看这篇,感觉明白了许多. 不

OpenGL ES简介

概述 在聊Android的View渲染流程中,通常会有一个比较核心的步骤:通过OpeGL ES接口调用GPU接口通知GPU绘制图形.其完整的流程:UI对象---->CPU处理为多维图形,纹理 -----通过OpeGL ES接口调用GPU----> GPU对图进行光栅化(Frame Rate ) ---->硬件时钟(Refresh Rate)----垂直同步---->投射到屏幕. 详解的绘制原理,后面会慢慢讲的. 渲染的基础知识 使用OpenGL ES,一般包括如下几个步骤: (1)

最简单的视音频播放示例6:OpenGL播放YUV420P(通过Texture,使用Shader)

本文记录OpenGL播放视频的技术.上一篇文章中,介绍了一种简单的使用OpenGL显示视频的方式.但是那还不是OpenGL显示视频技术的精髓.和Direct3D一样,OpenGL更好的显示视频的方式也是通过纹理(Texture).本文介绍OpenGL通过纹理的方式显示视频的技术. OpenGL中坐标和Direct3D坐标的不同 OpenGL中的纹理的坐标和Direct3D中的坐标是不一样的. 在Direct3D中.纹理坐标如下图所示.取值是0到1.坐标系原点在左上角. 物体表面坐标如下图所示.取

通过 Autostereograms 案例学习 OpenGL 和 OpenCL 的互操作性 【已翻译100%】(3/3)

注意到所有平台都可以遍历确保正确的context 被创建.下面的代码演示了OpenCL context 的创建. cl_int lError = CL_SUCCESS; std::string lBuffer; // // Generic OpenCL creation. // // Get platforms. cl_uint lNbPlatformId = 0; clGetPlatformIDs( 0 , 0 , &lNbPlatformId ); if ( lNbPlatformId ==

Android开发 OpenGL ES绘制3D 图形实例详解_Android

OpenGL ES是 OpenGL三维图形API 的子集,针对手机.PDA和游戏主机等嵌入式设备而设计. Ophone目前支持OpenGL ES 1.0 ,OpenGL ES 1.0 是以 OpenGL 1.3 规范为基础的,OpenGL ES 1.1 是以 OpenGL 1.5 规范为基础的.本文主要介绍利用OpenGL ES绘制图形方面的基本步骤. 本文内容由三部分构成.首先通过EGL获得OpenGL ES的编程接口;其次介绍构建3D程序的基本概念;最后是一个应用程序示例. OpenGL E

《OpenGL ES应用开发实践指南:Android卷》—— 1.4 创建Renderer类

1.4 创建Renderer类 现在要定义一个渲染器,以便我们开始清空屏幕.让我们快速浏览一下渲染器接口定义的方法: onSurfaceCreated(GL10 glUnused, EGLConfig config) 当Surface被创建的时候,GLSurfaceView会调用这个方法:这发生在应用程序第一次运行的时候,并且,当设备被唤醒或者用户从其他activity切换回来时,这个方法也可能会被调用.在实践中,这意味着,当应用程序运行时,本方法可能会被调用多次. onSurfaceChang