Crystal Space
Welcome, Guest. Please login or register.
September 03, 2014, 12:58:28 am

Login with username, password and session length
Search:     Advanced search
9009 Posts in 2043 Topics by 8379 Members
Latest Member: Hamlock3
* Home Help Search Login Register
+  Crystal Space
|-+  Crystal Space Development
| |-+  Support
| | |-+  csPen alpha problem
« previous next »
Pages: [1] Print
Author Topic: csPen alpha problem  (Read 4161 times)
muffinpeddler
Full Member
***
Posts: 122


View Profile Email
« on: September 01, 2005, 04:33:11 am »

I've noticed that when I draw with csPen, all alpha is removed, so everything is solid, no transparency on any object I set the color for.  I can't even set alpha for what I draw with the pen.  Is this a bug or am I supposed to set something before using csPen?
Logged
jorrit
Administrator
Hero Member
*****
Posts: 1706


View Profile
« Reply #1 on: September 01, 2005, 07:40:01 am »

I've noticed that when I draw with csPen, all alpha is removed, so everything is solid, no transparency on any object I set the color for.  I can't even set alpha for what I draw with the pen.  Is this a bug or am I supposed to set something before using csPen?

What version of CS?

Greetings,
Logged
muffinpeddler
Full Member
***
Posts: 122


View Profile Email
« Reply #2 on: September 02, 2005, 04:13:34 am »

It is CVS built about 2+ weeks ago.  The problem could be fixed in new realses, but I didn't see anyone tend to this problem, so maybe not. 
Logged
jorrit
Administrator
Hero Member
*****
Posts: 1706


View Profile
« Reply #3 on: September 02, 2005, 05:39:01 am »

It is CVS built about 2+ weeks ago.  The problem could be fixed in new realses, but I didn't see anyone tend to this problem, so maybe not. 

Can you show me a bit of code that you're using?

Greetings,
Logged
muffinpeddler
Full Member
***
Posts: 122


View Profile Email
« Reply #4 on: September 02, 2005, 09:07:33 pm »

Well, I had to dig some of this out, so it might not be right (why doesn't doxygen document constructors??)

Construct it.
Code:
pen = new csPen(g, g3d);

Then, in PostProcessFrame()
Code:
pen->DrawRect(100, 100, 200, 200, true);
That last line was my first test that I tried with csPen to see if it would work at all.  It had the
same result as subsequent calls to csPen rendering functions: all alpha was stripped.

And there is also another problem with csPen, regarding arcs.  As instructed in the docs.  I call pen->DrawArc(50, 50, 100, 100, 0, 2*PI, false) to draw a full circle or ellipse.  However, the circle doesn't complete, and there is a gap.  It is very apparent when you use filling, as there is a slice of pie taken out of the circle.

Thanks.


Logged
res
Develazyoper
CS Developer
Full Member
*****
Posts: 206


View Profile Email
« Reply #5 on: September 06, 2005, 02:10:09 am »

Does inserting
Code:
  mesh.alphaType.alphaType = csAlphaMode::alphaSmooth;
on line 60 of libs/cstool/pen.cpp help?
Logged
muffinpeddler
Full Member
***
Posts: 122


View Profile Email
« Reply #6 on: September 06, 2005, 01:02:40 pm »

I shall try that, and get back to you, thanks.
Logged
muffinpeddler
Full Member
***
Posts: 122


View Profile Email
« Reply #7 on: September 07, 2005, 04:49:16 am »

No luck Sad

Any other suggestions? 
Logged
res
Develazyoper
CS Developer
Full Member
*****
Posts: 206


View Profile Email
« Reply #8 on: September 07, 2005, 11:35:20 am »

Any other suggestions? 

Can you hack some csPen drawing code similar to yours into simple1 and give me that code so I can experiment myself?
Logged
muffinpeddler
Full Member
***
Posts: 122


View Profile Email
« Reply #9 on: September 07, 2005, 09:47:28 pm »

Sure.  Here is the modified header and source file:

simple1.h:
Code:
#ifndef __SIMPLE1_H__
#define __SIMPLE1_H__

#include <crystalspace.h>

/**
 * This is the main class of this Tutorial. It contains the
 * basic initialization code and the main event handler.
 *
 * csApplicationFramework provides a handy object-oriented wrapper around the
 * Crystal Space initialization and start-up functions.
 *
 * csBaseEventHandler provides a base object which does absolutely nothing
 * with the events that are sent to it.
 */
class Simple : public csApplicationFramework, public csBaseEventHandler
{
private:

  /// A pointer to the 3D engine.
  csRef<iEngine> engine;

  /// A pointer to the map loader plugin.
  csRef<iLoader> loader;

  /// A pointer to the 3D renderer plugin.
  csRef<iGraphics3D> g3d;

  /// A pointer to the keyboard driver.
  csRef<iKeyboardDriver> kbd;

  /// A pointer to the virtual clock.
  csRef<iVirtualClock> vc;

  /// A pointer to the view which contains the camera.
  csRef<iView> view;

  /// A pointer to the sector the camera will be in.
  iSector* room;

    //ADDED BY MUFFINPEDDLER
  csPen *pen;
 
  /// Current orientation of the camera.
  float rotX, rotY;

  bool OnKeyboard (iEvent&);

  void ProcessFrame ();

  //ADDED BY MUFFINPEDDLER
  void PostProcessFrame();


  void FinishFrame ();

  /// Here we will create our little, simple world.
  void CreateRoom ();

public:

  /// Construct our game. This will just set the application ID for now.
  Simple ();

  /// Destructor.
  ~Simple ();

  /// Final cleanup.
  void OnExit ();


  bool OnInitialize (int argc, char* argv[]);


  bool Application ();

};

#endif // __SIMPLE1_H__

simple1.cpp:
Code:
#include "simple1.h"

CS_IMPLEMENT_APPLICATION

//---------------------------------------------------------------------------

Simple::Simple ()
{
  SetApplicationName ("CrystalSpace.Simple1");
}

Simple::~Simple ()
{
}

void Simple::ProcessFrame ()
{
  // First get elapsed time from the virtual clock.
  csTicks elapsed_time = vc->GetElapsedTicks ();
  // Now rotate the camera according to keyboard state
  float speed = (elapsed_time / 1000.0) * (0.06 * 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_PGUP))
      rotX += speed;
    if (kbd->GetKeyState (CSKEY_PGDN))
      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" radians on your Y axis to get
  // the first rotation.  From there you rotate "rotX" radians on the
  // your X axis to get the final rotation.  We multiply the
  // individual rotations on each axis together to get a single
  // rotation matrix.  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;

  // Tell the camera to render into the frame buffer.
  view->Draw ();
}

/**
 * ADDED BY MUFFINPEDDLER
 */

//////////////////////////////////

void Simple::PostProcessFrame()
{
  if(!g3d->BeginDraw(engine->GetBeginDrawFlags() | CSDRAW_2DGRAPHICS))
    return;
   
  pen->SetColor(0.8f, 0.2f, 0.2f, 0.5f);
  pen->DrawRect(100, 100, 300, 300, true);

}

//////////////////////////////////

void Simple::FinishFrame ()
{
  // Just tell the 3D renderer that everything has been rendered.
  g3d->FinishDraw ();
  g3d->Print (0);
}

bool Simple::OnKeyboard(iEvent& ev)
{
  // We got a keyboard event.
  csKeyEventType eventtype = csKeyEventHelper::GetEventType(&ev);
  if (eventtype == csKeyEventTypeDown)
  {
    // The user pressed a key (as opposed to releasing it).
    utf32_char code = csKeyEventHelper::GetCookedCode(&ev);
    if (code == CSKEY_ESC)
    {
      // The user pressed escape to exit the application.
      // The proper way to quit a Crystal Space application
      // is by broadcasting a cscmdQuit event. That will cause the
      // main runloop to stop. To do that we get the event queue from
      // the object registry and then post the event.
      csRef<iEventQueue> q =
        CS_QUERY_REGISTRY(GetObjectRegistry(), iEventQueue);
      if (q.IsValid()) q->GetEventOutlet()->Broadcast(cscmdQuit);
    }
  }
  return false;
}

bool Simple::OnInitialize(int argc, char* argv[])
{
  // RequestPlugins() will load all plugins we specify. In addition
  // it will also check if there are plugins that need to be loaded
  // from the config system (both the application config and CS or
  // global configs). In addition it also supports specifying plugins
  // on the commandline.
  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!");

  // Now we need to setup an event handler for our application.
  // Crystal Space is fully event-driven. Everything (except for this
  // initialization) happens in an event.
  if (!RegisterQueue(GetObjectRegistry()))
    return ReportError("Failed to set up event handler!");

  return true;
}

void Simple::OnExit()
{
}

bool Simple::Application()
{
  // Open the main system. This will open all the previously loaded plug-ins.
  // i.e. all windows will be opened.
  if (!OpenApplication(GetObjectRegistry()))
    return ReportError("Error opening system!");

  // Now get the pointer to various modules we need. We fetch them
  // from the object registry. The RequestPlugins() call we did earlier
  // registered all loaded plugins with the object registry.
  g3d = CS_QUERY_REGISTRY(GetObjectRegistry(), iGraphics3D);
  if (!g3d) return ReportError("Failed to locate 3D renderer!");

  engine = CS_QUERY_REGISTRY(GetObjectRegistry(), iEngine);
  if (!engine) return ReportError("Failed to locate 3D engine!");

  vc = CS_QUERY_REGISTRY(GetObjectRegistry(), iVirtualClock);
  if (!vc) return ReportError("Failed to locate Virtual Clock!");

  kbd = CS_QUERY_REGISTRY(GetObjectRegistry(), iKeyboardDriver);
  if (!kbd) return ReportError("Failed to locate Keyboard Driver!");

  loader = CS_QUERY_REGISTRY(GetObjectRegistry(), iLoader);
  if (!loader) return ReportError("Failed to locate Loader!");

  // We need a View to the virtual world.
  view.AttachNew(new csView (engine, g3d));
  iGraphics2D* g2d = g3d->GetDriver2D ();
  // We use the full window to draw the world.
  view->SetRectangle (0, 0, g2d->GetWidth (), g2d->GetHeight ());

  // First disable the lighting cache. Our app is simple enough
  // not to need this.
  engine->SetLightingCacheMode (0);

  // Here we create our world.
  CreateRoom();

  // Let the engine prepare all lightmaps for use and also free all images
  // that were loaded for the texture manager.
  engine->Prepare ();

  // these are used store the current orientation of the camera
  rotY = rotX = 0;

  // Now we need to position the camera in our world.
  view->GetCamera ()->SetSector (room);
  view->GetCamera ()->GetTransform ().SetOrigin (csVector3 (0, 5, -3));

  // ADDED BY MUFFINPEDDLER
  pen = new csPen(g3d->GetDriver2D(), g3d);

  // This calls the default runloop. This will basically just keep
  // broadcasting process events to keep the game going.
  Run();

  return true;
}

void Simple::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 ("stone", "/lib/std/stone4.gif"))
    ReportError("Error loading 'stone4' texture!");

  iMaterialWrapper* tm = engine->GetMaterialList ()->FindByName ("stone");

  // We create a new sector called "room".
  room = engine->CreateSector ("room");

  // Creating the walls for our room.
  csRef<iMeshWrapper> walls (engine->CreateSectorWallsMesh (room, "walls"));
  csRef<iThingState> ws =
    SCF_QUERY_INTERFACE (walls->GetMeshObject (), iThingState);
  csRef<iThingFactoryState> walls_state = ws->GetFactory ();
  walls_state->AddInsideBox (csVector3 (-5, 0, -5), csVector3 (5, 20, 5));
  walls_state->SetPolygonMaterial (CS_POLYRANGE_LAST, tm);
  walls_state->SetPolygonTextureMapping (CS_POLYRANGE_LAST, 3);

  // Now we need light to see something.
  csRef<iLight> light;
  iLightList* ll = room->GetLights ();

  light = engine->CreateLight(0, csVector3(-3, 5, 0), 10, csColor(1, 0, 0));
  ll->Add (light);

  light = engine->CreateLight(0, csVector3(3, 5,  0), 10, csColor(0, 0, 1));
  ll->Add (light);

  light = engine->CreateLight(0, csVector3(0, 5, -3), 10, csColor(0, 1, 0));
  ll->Add (light);
}

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


There you go.  I stripped some comments out and added "ADDED BY MUFFINPEDDLER" to the parts I added, which was just the PostProcessFrame function and csPen stuff.

Tell me if you find any solutions, thanks!
Logged
res
Develazyoper
CS Developer
Full Member
*****
Posts: 206


View Profile Email
« Reply #10 on: September 22, 2005, 06:46:34 pm »

There have been some csPen changes made around Sep 12, did things improve for you?
Logged
muffinpeddler
Full Member
***
Posts: 122


View Profile Email
« Reply #11 on: September 26, 2005, 05:24:18 am »

Actually, it's rather funny, but the changes made everything worse, and still didn't fix the alpha problem.  Now, filling does not work, and I get a sort of binary gradient on the outline of the shape I try to fill, like a color on one part and a color on the next.  Hmm...It could be I just updated that file and the header, but I don't know.  Any suggestions?
Logged
res
Develazyoper
CS Developer
Full Member
*****
Posts: 206


View Profile Email
« Reply #12 on: September 26, 2005, 01:02:50 pm »

I think those colors are deliberate, and afaics, to switch between the color that is set ("top" vs "bottom"), you need SwapColors():
Code:
myPen->SetColor (topColor);
myPen->SwapColor();
myPen->SetColor (bottomColor);
myPen->SwapColor();

It is perhaps argueable whether that is terribly intuitive, least because it requires you to jump some hoops just to set a single solid color.

Anyway, means I still have to look at the alpha issue; will let you know what I found (after I looked at it, that is Wink
Logged
muffinpeddler
Full Member
***
Posts: 122


View Profile Email
« Reply #13 on: September 28, 2005, 04:03:12 am »

Hmm...It looks very odd, because shapes that should be filled with a solid color are only drawn (outline only) and the outline has 2 colors on it, rather than just one.

Thank you so much fo looking into it!
Logged
Pages: [1] Print 
« previous next »
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 4.256 seconds with 15 queries.