Hello CrystalSpace team!
 
I am trying to work the DrawLine() function so I can draw a mesh or a 'tree' of 
lines in 3d space I can rotate the camera around (they are supposed to 
symbolize connections between objects in 3d space). I think I manged to 
successfuly translate position between camera space and world space, However, 
the line is drawn 'behind' all 3d graphics and is being obscured by it. On a 
related post I found from 2006, jorrit said 'Note that this will draw on top of 
all 3D geometry that is already present and it must be done in 2D mode', but 
the opposite seems to be happening-the line is behind, not on top of, all other 
graphics.
I realize this is a totally newbie question but, as mentioned before, the 
learning curve on crystal space is especially steep.
 
My hardware specs:
Intel Pentium Dual CPU
E2160 @ 1.80GHz
1.8GHz, 0.99GB of ram
I am using no graphics hardware accelerator
(that is, I have no graphics card-using only on-board graphics capabilities).
 
My software specs:
win XP professional version 2002 service pack 2
crystal space ver. 1.2.1
using Microsoft Visual c++ 2008.
 
also, does the DrawLine() code have to be in the ProcessFrame function in order 
to work?
 
I have attached my code here-it's a simple adaptation of the simple1 demo.
 
Sorry for the lengthy mail, 
Thank you very much and I am looking forward to your reply and help, 
 
Shahar


      
#include "space01.h"

CS_IMPLEMENT_APPLICATION

appClass::appClass()
{

}

appClass::~appClass()
{

}

void appClass::ProcessFrame()
{
        // First get elapsed time from the vitural clock.
        csTicks elapsed_time = vc->GetElapsedTicks ();

        // Now rotate the camera according to keyboard state
        float speed = (elapsed_time / 1000.0) * (0.03 * 20);

        iCamera* c = view->GetCamera();
        if (kbd->GetKeyState (CSKEY_SHIFT))
        {
                // If the user is holding down shift, the arrow keys will cause
                // the camera to strafe up, down left or right from it's
                // current position.
                if (kbd->GetKeyState (CSKEY_RIGHT))
                        c->Move (CS_VEC_RIGHT * 4 * speed);
                if (kbd->GetKeyState (CSKEY_LEFT))
                        c->Move (CS_VEC_LEFT * 4 * speed);
                if (kbd->GetKeyState (CSKEY_UP))
                        c->Move (CS_VEC_UP * 4 * speed);
                if (kbd->GetKeyState (CSKEY_DOWN))
                        c->Move (CS_VEC_DOWN * 4 * speed);
        }
        else
        {
                // left and right cause the camera to rotate on the global Y
                // axis; page up and page down cause the camera to rotate on the
                // _camera's_ X axis (more on this in a second) and up and down
                // arrows cause the camera to go forwards and backwards.
                if (kbd->GetKeyState (CSKEY_RIGHT))
                        rotY += speed;
                if (kbd->GetKeyState (CSKEY_LEFT))
                        rotY -= speed;
                if (kbd->GetKeyState (CSKEY_PGDN))
                        rotX += speed;
                if (kbd->GetKeyState (CSKEY_PGUP))
                        rotX -= speed;
                if (kbd->GetKeyState (CSKEY_UP))
                        c->Move (CS_VEC_FORWARD * 4 * speed);
                if (kbd->GetKeyState (CSKEY_DOWN))
                        c->Move (CS_VEC_BACKWARD * 4 * speed);
        }

        // We now assign a new rotation transformation to the camera. You
        // can think of the rotation this way: starting from the zero
        // position, you first rotate "rotY" roadians on your Y axis to get
        // the first roatation. From there you rotate "rotX" radians on 
        // your X axis to get the final rotation. We multiply the
        // individual rotations on each axis together to get a single
        // rotation matirx. The rotations are applied in right to left
        // order.
        csMatrix3 rot = csXRotMatrix3 (rotX) * csYRotMatrix3 (rotY);
        csOrthoTransform ot (rot, c->GetTransform().GetOrigin ());
        c->SetTransform (ot);
        
        // Tell 3D driver we're going to display 3D things.
        //if (!g3d->BeginDraw(
        //      engine->GetBeginDrawFlags() | CSDRAW_3DGRAPHICS))
        //      return;
        if (!g3d->BeginDraw( CSDRAW_2DGRAPHICS )) return; 
        
        float fov = g3d->GetPerspectiveAspect() ; //c->GetFOV();
        csVector3 startVec = c->GetTransform().Other2This( csVector3( 0.0, 0.0, 
0.0 ) );
        csVector3 endVec = c->GetTransform().Other2This( csVector3( 20.0, 20.0, 
30.0 ) );
        g3d->DrawLine( startVec, endVec, fov, g3d->GetDriver2D()->FindRGB( 255, 
0, 0 ) );  //g2d->FindRGB( 1, 0, 0 )
        // Tell the camera to render into the frame buffer.
        view->Draw ();
}

void appClass::FinishFrame()
{
        g3d->FinishDraw ();
        g3d->Print (0);
}

bool appClass::OnKeyboard(iEvent& ev)
{
        csKeyEventType eventtype = csKeyEventHelper::GetEventType(&ev);
        if (eventtype == csKeyEventTypeDown)
        {
                utf32_char code = csKeyEventHelper::GetCookedCode(&ev);
                if (code == CSKEY_ESC)
                {
                        csRef<iEventQueue> q =
                          csQueryRegistry<iEventQueue> (GetObjectRegistry());
                        if (q.IsValid()) 
q->GetEventOutlet()->Broadcast(csevQuit ( 
                                GetObjectRegistry ()));
                }
        }
        return false;
}

bool appClass::OnInitialize(int argc, char *argv[])
{
        if (!csInitializer::RequestPlugins(GetObjectRegistry(),
                CS_REQUEST_VFS,
                CS_REQUEST_OPENGL3D,
                CS_REQUEST_ENGINE,
                CS_REQUEST_FONTSERVER,
                CS_REQUEST_IMAGELOADER,
                CS_REQUEST_LEVELLOADER,
                CS_REQUEST_REPORTER,
                CS_REQUEST_REPORTERLISTENER,
                CS_REQUEST_END))
                return ReportError("Failed to initialize plugins!");
        csBaseEventHandler::Initialize(GetObjectRegistry());
        if (!RegisterQueue(GetObjectRegistry(), 
csevAllEvents(GetObjectRegistry())))
                return ReportError("Failed to set up event handler!");
        return true;
}

void appClass::onExit()
{
}

csVector3 appClass::makeBranch( iMaterialWrapper* matWrap, float BLength, 
csVector3 orig, float angle1, float angle2)
{       
        csBox3 temp_box;
        csRef<iMeshFactoryWrapper> branch_mesh_factory = 
engine->CreateMeshFactory( 
                "crystalspace.mesh.object.genmesh", "boxes_general" );  
        csRef<iGeneralFactoryState> branch_mesh_fact_state = 
                scfQueryInterface<iGeneralFactoryState> 
(branch_mesh_factory->GetMeshObjectFactory());
        temp_box = csBox3(csVector3( 0.0, 0.0, 0.0 ), csVector3( 0.5, BLength, 
0.5 ) ); //---------
        
        csVector3 endVec = csVector3( 0.0, BLength, 0.0 );                      
                                        //---------

        //boxes_fact_state->GenerateSphere( ellips, 32 );
        //branch_mesh_fact_state->GenerateCapsule( BLength, 0.2, 16 );
        branch_mesh_fact_state->GenerateBox( temp_box );
        //boxes_fact_state->CalculateNormals();
                
        
branch_mesh_factory->GetMeshObjectFactory()->SetMaterialWrapper(matWrap);
                
        csRef<iMeshWrapper> branch_wrapper = 
                engine->CreateMeshWrapper ( branch_mesh_factory, "box_mesh", 
room, orig );      //--------
        
//----------------------------------------------------------------------------------------
        // NOW START THE PROCESS OF ROTATING THE "BRANCH" IN 3D SPACE:
        
        csMatrix3 rotationMatX = csXRotMatrix3( angle1 ) ;                      
                                        //--------
        csMatrix3 rotationMatZ = csZRotMatrix3( angle2 );
        csMatrix3 totalRotMatrix = rotationMatX * rotationMatZ ; 
        endVec = totalRotMatrix * endVec;                                       
                                                        //--------
        endVec += orig ;
        
        branch_wrapper->GetMovable()->GetTransform().SetOrigin( orig );
        
        //branch_wrapper->GetMovable()->GetTransform().RotateThis( 
csVector3(1.0, 0.0, 0.0), angle1 );
        //branch_wrapper->GetMovable()->GetTransform().LookAt( endVec, 
csVector3( 0.0, 1.0, 0.0 ) ); //csVector3( 3.5, 1.0, 0.0 ), orig 
        branch_wrapper->GetMovable()->SetTransform( totalRotMatrix ); 
//rotationMatX
        branch_wrapper->GetMovable()->UpdateMove();
        
        engine->RemoveObject(branch_mesh_factory);
        
        return endVec;
}

void appClass::CreateSprites()
{

}       //      appClass::CreateSprites()

bool appClass::Application()
{
        if (!OpenApplication(GetObjectRegistry()))
    return ReportError("Error opening system!");

        g3d = csQueryRegistry<iGraphics3D> (GetObjectRegistry());
        if (!g3d) return ReportError("Failed to locate 3D renderer!");

        engine = csQueryRegistry<iEngine> (GetObjectRegistry());
        if (!engine) return ReportError("Failed to locate 3D engine!");

        vc = csQueryRegistry<iVirtualClock> (GetObjectRegistry());
        if (!vc) return ReportError("Failed to locate Virtual Clock!");

        kbd = csQueryRegistry<iKeyboardDriver> (GetObjectRegistry());
        if (!kbd) return ReportError("Failed to locate Keyboard Driver!");

        loader = csQueryRegistry<iLoader> (GetObjectRegistry());
        if (!loader) return ReportError("Failed to locate Loader!");
        
        // First disable the lighting cache. our app is simple enough
        // not to need this.
        engine->SetLightingCacheMode (0);

        rotY = rotX = 0;

        CreateRoom();

        view.AttachNew(new csView (engine, g3d));
        iGraphics2D* g2d = g3d->GetDriver2D ();
        view->SetRectangle (0, 0, g2d->GetWidth (), g2d->GetHeight ());
  
        view->GetCamera ()->SetSector (room);
        view->GetCamera ()->GetTransform ().SetOrigin (csVector3 (0, 5, -3));
  
        //CreateSprites();
        
        if (!loader->LoadTexture ("wood_txt", "/lib/std/andrew_wood.gif" )) 
//stone2DOT3.png
                ReportError("Error loading 'wood_txt' texture!");
        
        Wrap = engine->GetMaterialList ()->FindByName ("wood_txt");
        
        csVector3 posVec;

        posVec = makeBranch( Wrap, 6.0, csVector3(0.0, 0.0, 0.0), -PI/3, PI/4 );
        posVec = makeBranch( Wrap, 4.0, posVec, PI/4, -PI/3 );
        posVec = makeBranch( Wrap, 6.0, posVec, PI/3, -PI/4 );
        posVec = makeBranch( Wrap, 3.0, posVec, -PI/4, PI/3 );
        
        //float fov = g3d->GetPerspectiveAspect();
        //g3d->DrawLine( csVector3( 0.0, 5.0, 10.0 ), csVector3(10.0, 10.0, 0.0 
), fov, g2d->FindRGB( 0, 0, 255 ));

        Run();

        return true;
}

void appClass::CreateRoom()
{
        // Load the texture from the standard library.  This is located in
        // CS/data/standard.zip and mounted as /lib/std using the Virtual
        // File System (VFS) plugin.
        if (!loader->LoadTexture ("bombtxt", "/lib/std/bomb002.gif"))
                ReportError("Error when loading 'bombtxt' texture!");

        iMaterialWrapper* tm =
                engine->GetMaterialList ()->FindByName ("bombtxt");
        
        room = engine->CreateSector ("room");
        /////////////////////////////////////
        engine->SetAmbientLight( csColor( 0.5, 0.5, 0.5 ) );
        /////////////////////////////////////
          
        csRef<iMeshWrapper> walls (
                engine->CreateSectorWallsMesh (room, "walls"));
        iMeshObject* walls_object = walls->GetMeshObject ();
        iMeshObjectFactory* walls_factory = walls_object->GetFactory();
        csRef<iThingFactoryState> walls_state = 
    scfQueryInterface<iThingFactoryState> (walls_factory);
        walls_state->AddInsideBox (
                csVector3 (-8.0, -8.0, -8.0), csVector3 (8.0, 8.0, 8.0));
        walls_state->SetPolygonMaterial (CS_POLYRANGE_LAST, tm);
        walls_state->SetPolygonTextureMapping (CS_POLYRANGE_LAST, 3);  

  engine->Prepare ();
}

/*---------------*
 * Main function
 *---------------*/
int main(int argc, char* argv[])
{
        return csApplicationRunner<appClass>::Run(argc, argv);
}
#ifndef __SPACE01_H__   
#define __SPACE01_H__

#include <crystalspace.h>

struct iSector;

class appClass : public csApplicationFramework, public csBaseEventHandler
{
private:
        csRef<iEngine> engine;
        csRef<iLoader> loader;
        csRef<iGraphics3D> g3d;
        csRef<iKeyboardDriver> kbd;
        csRef<iVirtualClock> vc;
        csRef<iView> view;
        
        iMaterialWrapper* Wrap;

        iSector* room;
        float rotX, rotY;
        
        void ProcessFrame ();
        void FinishFrame ();

        bool OnKeyboard(iEvent&);

        void CreateRoom();
        void CreateSprites();
        
        csVector3 makeBranch( iMaterialWrapper* matWrap, float BLength, 
csVector3 orig, float angle1, float angle2 );

public:
        appClass();
        ~appClass();

        void onExit();
        bool OnInitialize(int argc, char* argv[]);
        
        bool Application();
        
        CS_EVENTHANDLER_NAMES("application.space01")
        CS_EVENTHANDLER_NIL_CONSTRAINTS
};

#endif // __SPACE01_H__
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with 
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
Crystal-main mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/crystal-main
Unsubscribe: 
mailto:[email protected]?subject=unsubscribe

Reply via email to