About Store Forum Documentation Contact



Post Reply 
viewports
Author Message
Rabishan Offline
Member

Post: #1
viewports
hi,

can we draw meshes in the viewport.

thanks.
09-18-2011 12:48 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #2
RE: viewports
yes, use Renderer inside viewport draw
09-18-2011 10:36 PM
Find all posts by this user Quote this message in a reply
Rabishan Offline
Member

Post: #3
RE: viewports
thanks. it worked
09-19-2011 05:45 AM
Find all posts by this user Quote this message in a reply
Rabishan Offline
Member

Post: #4
RE: viewports
sorry to bother you again but seems like i've got another problem. the camera of the viewport and game camera seems to disturb each other. how can this be solved. i saw tutorial of multiple viewports but got no idea for this.

after i run the code mesh appears in the viewport for split seconds and then dissapears. and viewport shows game world(sky).
(This post was last modified: 09-19-2011 10:16 AM by Rabishan.)
09-19-2011 10:05 AM
Find all posts by this user Quote this message in a reply
Rabishan Offline
Member

Post: #5
RE: viewports
hi,

to make things easier to understand i've posted a simple version of my code. here is the class of gui object that contains viewport. it draws a mesh of a character in the viewport.

the game camera is the same as the one in esenthel RPG2.

when i don't update game camera, the following class draws the mesh fine. but when i update game camera, the mesh is drawn for a single frame and then it does not show up in the viewport.

i've also seen multiple viewport example. there you pass a pointer to indicate the camera index during creating a viewport. how can we do the same here.

PHP Code:
class MainStatusBar:public GuiObjs
    
{
    
Viewport v;
    
Camera   camera;
    static 
void Exit    (MainStatusBar &m) {m.hide();}
    public:
        
MeshPtr   mesh;
        
CSkeleton cskel;
        
MainStatusBar(){}
        static 
void Render()
            {
            switch(
Renderer())
                {
                    case 
RM_PREPARE:
                        
mainStatusBar.mesh->draw(mainStatusBar.cskel);
                        break;
                }
            }
        static 
void ViewportDraw(Viewport &viewport)
            {
            
Renderer(Render);
            
SetMatrix(MatrixIdentity);
            }
        
void create()
            {
            
load("gui/obj/inventory.gobj");
            
Gui+=T;
            
Window &window=getWindow("window").asWindow();
            
window+=v.create((&getViewport("viewport"))->rect,ViewportDraw);
            
mesh="obj/chr/warrior/body.mesh";
            
cskel.create(Skeletons("obj/chr/warrior/body.skel"));
            }
        
void update()
            {
            
camera.set();
            
cskel.clear().animate(L"../data/anim/walk.anim",Time.time()).updateMatrix(MatrixIdentity).updateVelocities();
            
Camera *cam=NULL;
            if(
Gui.viewport()==&vcam=&camera;
            if(
cam)
                {
                if(
Ms.b(0))
                    {
                    
cam->yaw-=Ms.dc().x;
                    
cam->pitch+=Ms.dc().y;
                    }
                if(
Ms.wheel()<0)cam->dist*=1.2f;
                if(
Ms.wheel()>0)cam->dist/=1.2f;
                
cam->setSpherical(); 
                }
            
camera.updateVelocities();
            }
        }
extern mainStatusBar

thanks.
09-21-2011 01:03 PM
Find all posts by this user Quote this message in a reply
Rabishan Offline
Member

Post: #6
RE: viewports
ok i found the solution. below is the code which loads a gui object with viewport in the game world.

PHP Code:
/******************************************************************************/
#include "stdafx.h"
#include "../../../../../data/enum/_enums.h"
/******************************************************************************/
Camera desired_camera[2]; // create a helper desired camera
/******************************************************************************/
STRUCT(Player Game::Chr)
    
//{
    
virtual Bool update();
};
/******************************************************************************/
class ViewPort:public GuiObjs
    
{
    static 
void Exit    (ViewPort &v) {v.hide();}
    public:
        
Viewport v;
        
MeshPtr mesh,armor,pants,boots,gauntlets;
        
CSkeleton cskel// controlled skeleton
        
ViewPort(){}
        static 
void Render();
        static 
void ViewportDraw(Viewport &viewport);
        
void create();
        
void update(int i=0);
        
void draw();
    }
viewPort;
/******************************************************************************/
void ViewPort::Render()
    {
    switch(
Renderer())
        {
        case 
RM_PREPARE:
            {
            
viewPort.mesh->draw(viewPort.cskel);
            
viewPort.armor->draw(viewPort.cskel);
            
viewPort.pants->draw(viewPort.cskel);
            
viewPort.boots->draw(viewPort.cskel);
            
viewPort.gauntlets->draw(viewPort.cskel);
            }break;
        }
    }

void ViewPort::ViewportDraw(Viewport &viewport// function wich will be called when drawing the Gui Viewport element
    
{
    
Int camera_index=Mid((UIntPtr)viewport.user,0,Elms(desired_camera)-1);
    
desired_camera[camera_index].set();
    
Renderer(Render);
    
SetMatrix(MatrixIdentity);
    }

void ViewPort::create()
    {
    
load("gui/obj/viewPort.gobj");
    
Gui+=T;
    
Window &window=getWindow("w").asWindow();
    
window+=v.create((&getViewport("v"))->rect,ViewportDraw,(Ptr)1);
    
mesh="obj/chr/warrior/body.mesh";
    
armor="obj/chr/warrior/armor.mesh";
    
pants="obj/chr/warrior/pants.mesh";
    
boots="obj/chr/warrior/boot.mesh";
    
gauntlets="obj/chr/warrior/gauntlet.mesh";
    
cskel.create(Skeletons("obj/chr/warrior/body.skel"));
    }

void ViewPort::update(int i)
    {
    
cskel.clear().animate(L"../data/anim/walk.anim",Time.time()).updateMatrix(MatrixIdentity).updateVelocities();
    
Camera *cam=NULL// start with none
    
if(icam=&desired_camera[1];
    if(
cam// if found a camera
        
{
        if(
Ms.b(0)) // only when mouse button pressed
            
{
            
cam->yaw-=Ms.dc().x;
            
cam->pitch+=Ms.dc().y;
            }
        if(
Ms.wheel()<0)cam->dist*=1.2f;
        if(
Ms.wheel()>0)cam->dist/=1.2f;
        
cam->setSpherical(); // apply changes by calling 'setSpherical' which sets camera matrix from current values
        
}
    
desired_camera[1].updateVelocities();
    }
/******************************************************************************/
Game::ObjMemx<Game::Static> Statics;
Game::ObjMemx<PlayerPlayers;
/******************************************************************************/
Bool Player::update()
    {
    if(
action)
        {
        if(
Kb.b(KB_W) || Kb.b(KB_S) || Kb.b(KB_A) || Kb.b(KB_D) || Kb.b(KB_Q) || Kb.b(KB_E))actionBreak();
        }
    if(!
action)
        {
        
input.turn.x=Kb.b(KB_Q)-Kb.b(KB_E);
        
input.turn.y=Kb.b(KB_T)-Kb.b(KB_G);
        
input.move.x=Kb.b(KB_D)-Kb.b(KB_A);
        
input.move.z=Kb.b(KB_W)-Kb.b(KB_S);
        
input.move.y=Kb.b(KB_SPACE)-Kb.b(KB_LSHIFT);
        
input.dodge Kb.bd(KB_D)-Kb.bd(KB_A);
        
input.crouchKb.(KB_LSHIFT);
        
input.walk  Kb.(KB_LCTRL );
        
input.jump  =(Kb.bp(KB_SPACE ) ? 3.5f 0);
        }
    return 
super::update();
    }
/******************************************************************************/
void InitPre()
    {
    
App.name("Camera Collisions");
    
App.flag=APP_FULL_TOGGLE;
    
DataPath("../data");
    
Paks.add("engine.pak");
    
D.full(false).sync(true).ambPower(0.3f).hpRt(true);
    }
/******************************************************************************/
Bool Init()
    {
    
Physics.create(CSS_NONE,true,"../Installation/PhysX");
    
Sky.atmospheric();
    
Sun.image=Images("gfx/sky/sun.gfx"); Sun.light_color=1-D.ambColor();
    
Game::World.init()
        .
setObjType(Statics,OBJ_STATIC)
        .
setObjType(Players,OBJ_PLAYER)
        .New(
"world/apple.world");
    
viewPort.create();
    return 
true;
    }
/******************************************************************************/
void Shut()
    {
    }
/******************************************************************************/
Bool Update()
    {
    if(
Kb.bp(KB_ESC))return false;
    if(
Gui.msLit()==&viewPort.v
        
viewPort.update(1);
    else{
        
Game::World.update(desired_camera[0].at);
        
desired_camera[0].yaw-=Ms.d().x;         // update camera yaw   angle according to mouse delta x
        
desired_camera[0].pitch+=Ms.d().y;         // update camera pitch angle according to mouse delta y
        
Clamp(desired_camera[0].pitch,-PI_2,PI_4); // clamp to possible camera pitch angle
        
desired_camera[0].dist=Max(1.0f,desired_camera[0].dist*ScaleFactor(Ms.wheel()*-0.2f)); // update camera distance according to mouse wheel
        
desired_camera[0].at=Players[0].pos();
        
desired_camera[0].setSpherical(); // set as spherical from current values, this will set the camera's matrix (desired_camera.matrix)
        
Ball ball(0.1fdesired_camera[0].at); // we place it at starting point (where the camera is looking at)
        
Physics.move(balldesired_camera[0].matrix.pos-ball.pos); // use physics movement to move the ball as far as it can go without any collisions
        
Cam.setPosDir(ball.posdesired_camera[0].matrix.zdesired_camera[0].matrix.y); // we'll use 'desired_camera.matrix' directions which were set in 'setSpherical' camera method
        
Cam.updateVelocities().set(); // update camera velocities and activate it
        
}
    
Gui.update();
    
viewPort.update();
    return 
true;
    }
/******************************************************************************/
void Render()
    {
    
Game::World.draw();
    }
void Draw()
    {
    
Renderer(Render);
    
Gui.draw();
    if(
Gui.msLit()==&viewPort.v
        
D.text(0,0.9f,S+"Camera "+1);
    else 
        
D.text(0,0.9f,S+"Camera "+0);
    }
/******************************************************************************/ 
09-27-2011 08:28 AM
Find all posts by this user Quote this message in a reply
Post Reply