Cpper
C/C++高级工程师 Android高级软件工程师 IT集成工程师 音频工程师 熟悉c,c++,java,c#,py,js,asp等多种语言 程序猿
首先上代码吧
个人感觉写的很清爽
/*!==========================================================================
 *
 *  盖莫游戏引擎(GaiMo Game Engine)
 *
 *  版权所有 (C) 2009-2009 成都盖莫软件技术工作室 保留所有权利
 *  Copyright (C) 成都盖莫软件技术工作室.  All Rights Reserved.
 *
 *  了解更多情况,请访问 
http://www.gaimo.net
 ***************************************************************************
*/
//! 本demo测试引擎渲染到纹理功能
#include <iostream>
#include 
<GEngine/Header_Config.hpp>

using namespace std;
using namespace core;

#define FRAME_NUMBER 12

//! 定义图形纹理指针
RefPtr<core::Texture>  texture_ptr[FRAME_NUMBER];
//! 渲染到纹理指针
RefPtr<Renderable> rendertotexture;
core::TextDesc
* text = NULL;
Matrix4f mat;
float pos[] = {0,0,0};
float length[] = {16,16,16};
void  CreateTexture();
void  InitCamera();
void  Render();

int main(int argc, char *argv[])
{
    
//! 窗体描述符
    ScreenDesc desc;
    desc.name_ 
= "RenderToTextureTest";
    
//! 生成窗体
    RefPtr<Screen> screen = CreateScreen(desc);

    
//! 生成纹理
    CreateTexture();

    
//! 初始化摄像机
    InitCamera();

    
while(screen->IsOpen())
    {
        Render();
    }

    DestroyScreen();
    
return EXIT_SUCCESS;
}

//! 构造纹理
void CreateTexture()
{
    
//! 载入资源包
    GLOBAL_FILESYSTEM_PTR->RegisterPackage("..\\package\\logo.zip");
    RefPtr
<core::ReadFile> imagefile;

    RefPtr
<Image> image[FRAME_NUMBER];
    
for(int i = 0;i < FRAME_NUMBER ;i++)
    {
        std::
string name = ToString<int>(i);
        imagefile 
= GLOBAL_FILESYSTEM_PTR->GetRead("logo.jpg");
        image[i] 
= GLOBAL_IMAGEMANAGER_PTR->CreateImage(name,imagefile);
    }

    
//! 反色
    image[0]->GetInvertImage();
    GLOBAL_TEXTUREMANAGER_PTR
->CreateTexture2(ToString<int>(0),image[0]);
    
//! 水平翻转
    image[1]->FlipImage(Image::IMAGE_FLIP_X);
    GLOBAL_TEXTUREMANAGER_PTR
->CreateTexture2(ToString<int>(1),image[1]);
    
//! 竖直翻转
    image[2]->FlipImage(Image::IMAGE_FLIP_Y);
    GLOBAL_TEXTUREMANAGER_PTR
->CreateTexture2(ToString<int>(2),image[2]);
    
//! Gamma调整
    image[3]->AdjustGamma(0.4);
    GLOBAL_TEXTUREMANAGER_PTR
->CreateTexture2(ToString<int>(3),image[3]);
    
//! 亮度调整
    image[4]->AdjustBrightness(0.4);
    GLOBAL_TEXTUREMANAGER_PTR
->CreateTexture2(ToString<int>(4),image[4]);
    
//! 对比度调整
    image[5]->AdjustContrast(0.6);
    GLOBAL_TEXTUREMANAGER_PTR
->CreateTexture2(ToString<int>(5),image[5]);
    
//! 翻转
    image[6]->FlipImage(Image::IMAGE_FLIP_XY);
    GLOBAL_TEXTUREMANAGER_PTR
->CreateTexture2(ToString<int>(6),image[6]);
    
//! 旋转
    image[7]->RotatedImage(Image::IMAGE_ROTATE_90);
    GLOBAL_TEXTUREMANAGER_PTR
->CreateTexture2(ToString<int>(7),image[7]);
    
//! Gamma调整
    image[8]->AdjustGamma(0.1);
    GLOBAL_TEXTUREMANAGER_PTR
->CreateTexture2(ToString<int>(8),image[8]);
    
//! 亮度调整
    image[9]->AdjustBrightness(0.8);
    GLOBAL_TEXTUREMANAGER_PTR
->CreateTexture2(ToString<int>(9),image[9]);
    
//! 对比度调整
    image[10]->AdjustContrast(0.3);
    GLOBAL_TEXTUREMANAGER_PTR
->CreateTexture2(ToString<int>(10),image[10]);
    GLOBAL_TEXTUREMANAGER_PTR
->CreateTexture2(ToString<int>(11),image[11]);

    
for(int i = 0; i < FRAME_NUMBER; i++)
    {
        
//! 纹理构建
        GLOBAL_TEXTUREMANAGER_PTR->GetObject(ToString<int>(i))->Generate();
    }

    text 
= new core::TextDesc("..\\package\\accid.ttf",12,0,0,3,Color::Cyan);
    text
->SetText(engine_wstring(L"maker:gaimosoft,ccsdu2004@yahoo.com.cn"));

    
//! 获取渲染到纹理指针
    rendertotexture = GLOBAL_RENDERER_PTR->GetRenderToTexture(640,480,GL_RGBA,GL_FLOAT);

    glEnable(GL_DEPTH_TEST);
}

void InitCamera()
{
    RefPtr
<Camera> orthocamera = GLOBAL_CAMERAMANAGER_PTR->CreateCamera("orthocamera");
    orthocamera
->SetOrtho(0,640,480,0,-1.2,0.8);
    RefPtr
<Camera> camera = GLOBAL_CAMERAMANAGER_PTR->GetActiveCamera();
    camera
->GetViewPort()->Render();
    camera
->SetPerspective(45.0f,640.0f/480.0f,1.0f,600.0f);
    camera
->SetPosition(Vector3f(20,20,20));
    camera
->SetView(Vector3f());
    camera
->ApplayProjectMatrix();
}

void RenderTexture()
{
    std::
string texturename;
    
for(int i = 0; i < FRAME_NUMBER; i++)
    {
        texturename 
= ToString<int>(i);
        GLOBAL_TEXTUREMANAGER_PTR
->GetObject(texturename)->AttachRenderTarget();
        UI_Brush::RenderTexture((i
%4)*160,(i/4)*160,160,160);
    }
}

//! 处理鼠标响应
void MouseCheck()
{
    
if(GLOBAL_INPUT_PTR->IsPressedKey(KEY_F1))
    {
        mat
*=mat.Rotate((rand()%200)/900.0f,1,0,0);
    }
    
else if(GLOBAL_INPUT_PTR->IsPressedKey(KEY_F2))
    {
        mat
*=mat.Rotate((rand()%200)/900.0f,0,1,0);
    }
    
else if(GLOBAL_INPUT_PTR->IsPressedKey(KEY_F3))
    {
        mat
*=mat.Rotate((rand()%200)/900.0f,0,0,1);
    }
}

void RenderText()
{
    UI_Brush::PreRender();
    
static int offset = 0;
    text
->Render(Rectf(offset,460,400,20));
    offset 
++;
    
if(offset >= 600)
        offset 
= -500;
    UI_Brush::PostRender();
}

void RenderToTexture()
{
    rendertotexture
->BeginRender();
    GLOBAL_RENDERER_PTR
->Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    GLOBAL_CAMERAMANAGER_PTR
->GetObject("orthocamera")->ApplayProjectMatrix();
    RenderTexture();
    RenderText();
    rendertotexture
->AfterRender();
}

void RenderToScreen()
{
    GLOBAL_RENDERER_PTR
->Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    GLOBAL_CAMERAMANAGER_PTR
->GetActiveCamera()->ApplayProjectMatrix();
    GLOBAL_CAMERAMANAGER_PTR
->GetActiveCamera()->ApplayViewMatrix();
    GLOBAL_RENDERER_PTR
->DrawCube(pos,mat.ptr(),length);
}

void Render()
{
    MouseCheck();
    RenderToTexture();
    RenderToScreen();
}

基本步骤就是生成窗体
初始化摄像机
生成纹理
渲染到纹理
渲染到窗体
完了

图图如下:
似乎我比较喜欢贴立方体啊 呵呵
posted on 2010-09-17 18:22 ccsdu2009 阅读(503) 评论(0)  编辑 收藏 引用 所属分类: Game引擎

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理