这是刚写的渲染到纹理代码 还有点问题 以后慢慢修改吧
先上interface
///////////////////////////////////////////////////////////
/// 定义渲染到纹理对象
///////////////////////////////////////////////////////////
class G_DLL_API RenderToTexture : public Renderable
{
public:
////////////////////////////////////////////////////////
//! 构造,析构渲染到纹理
////////////////////////////////////////////////////////
RenderToTexture();
virtual ~RenderToTexture();
public:
////////////////////////////////////////////////////////
//! 设置渲染纹理,目标回调函数
////////////////////////////////////////////////////////
virtual void SetRenderTexture(RenderCallBack rendertexture) = 0;
virtual void SetRenderTarget(RenderCallBack rendertarget) = 0;
////////////////////////////////////////////////////////
//! 设置渲染摄像机透视参数
////////////////////////////////////////////////////////
virtual void SetRenderPara(float fov,float near,float far) = 0;
////////////////////////////////////////////////////////
//! 设置纹理清屏色
////////////////////////////////////////////////////////
virtual void SetClearColor(const Color& color) = 0;
DECLARE_OBJECT(RenderToTexture)
};
下面是测试的例子代码:
#include <GEngine/Gaimo.hpp>
using namespace std;
using namespace core;
RefPtr<Texture> boxtexture = NULL;
RefPtr<Camera> camera = NULL;
//! 获取引擎视频驱动器
RefPtr<VideoDriver> videodriver = NULL;
///////////////////////////////////////////////////////////////////////////////
// 渲染回调函数
///////////////////////////////////////////////////////////////////////////////
void G_CALL Render()
{
glBegin(GL_QUADS);
glTexCoord2f(1, 1);
glVertex2f( 1, 1);
glTexCoord2f(0, 1);
glVertex2f(-5, 1);
glTexCoord2f(0, 0);
glVertex2f(-5,-5);
glTexCoord2f(1, 0);
glVertex2f( 1,-5);
glTexCoord2f(1, 1);
glVertex2f(3, 3);
glTexCoord2f(0, 1);
glVertex2f(-3,3);
glTexCoord2f(0, 0);
glVertex2f(-3,-3);
glTexCoord2f(1, 0);
glVertex2f(3,-3);
glEnd();
}
///////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////
void G_CALL RenderTexture()
{
boxtexture->AttachRenderTarget();
glBegin(GL_QUADS);
glTexCoord2f(1, 1);
glVertex2f(0,0);
glTexCoord2f(0, 1);
glVertex2f(-8,0);
glTexCoord2f(0, 0);
glVertex2f(-8,-8);
glTexCoord2f(1, 0);
glVertex2f(0,-8);
glEnd();
}
int main(int argc, char **argv)
{
//! 初始化引擎设备并得到设备指针
RefPtr<Device> device = core::InitDevice("渲染到纹理");
//! 得到引擎场景指针
RefPtr<SceneManager> scenemanager = device->GetSceneManager();
//! 得到引擎资源指针
RefPtr<ResourceManager> resourcemanager = device->GetResourceManager();
//! 获取引擎纹理管理器指针
RefPtr<TextureManager> texturemanager = resourcemanager->GetTextureManager();
//! 获取引擎图形管理器
RefPtr<ImageManager> imagemanager = resourcemanager->GetImageManager();
//! 获取引擎视频驱动器
videodriver = device->GetVideoDriver();
//! 得到tile图形和其纹理
core::RefPtr<Image> box = imagemanager->CreateObject("cube","..\\image//logo.bmp");
boxtexture = texturemanager->CreateObject("cube",box);
boxtexture->Generate();
//! 获取全局摄像机,设置摄像机参数
camera = scenemanager->GetActiveCamera();
camera->SetPosition(Vector3f(0,0,18));
camera->SetView(Vector3f(0,0,0));
camera->SetDirection(Vector3f(0,1,0));
camera->SetViewPort(Recti(0,0,640,480));
camera->SetPerspective(50.0f,640.0f/480.0f,1.0f,100.0f);
videodriver->EnableCulling();
//! 获取渲染到纹理指针
RefPtr<RenderToTexture> render = device->GetRenderer()->GetRenderToTexture(256,256);
render->SetRenderTexture(&RenderTexture);
render->SetRenderTarget(&Render);
render->SetClearColor(Color::Blue);
render->SetRenderPara(45.0f,1.0f,100.0f);
BEGIN_LOOP(device)
camera->Render();
render->Render();
END_LOOP(device)
return 0;
}
贴图比较简单就不上了 这个以后还要修改之!