Game Development Community

Drwing simple lines in 3D

by Tim Hutcheson · in Torque Game Engine · 10/04/2005 (3:00 pm) · 9 replies

I have been trying to use the dgl functions to throw a line in fron of my AI vehicle, to verify the steering code. But, alas, no line appears.

Here's the code I'm using, where center, front and desired are valid points in world coordinates that the steering algorithm uses. I raised the endpoints abit to be sure it isn't obscured by the vehicle. Is there something more needed?

center.z +=.5;
      front.z +=.5;
      desired.z +=.5;
      Point3F sCenter,sFront,sDesired;
      dglPointToScreen( center, sCenter );
      dglPointToScreen( front, sFront );
      dglPointToScreen( desired, sDesired );
      ColorF lightGreenLine(0,1,0,0.3);
      dglDrawLine(sFront.x,sFront.y,sDesired.x,sDesired.y, lightGreenLine);

#1
10/04/2005 (3:58 pm)
Hmm. I just observed that dglPointToScreen() returns the original point in 3d WorldCoordinates. I thought it returned screen coordinates, (but was a little surprised that it returned a Point3F instead of a Point2F.
#2
10/04/2005 (6:49 pm)
So I've figured out I need to wrap code like that into something called by the appropriate onRender method and any other openGL stuff needed, as in the guiSpeedometerHud::onRender method. My question is what is the appropriate callback method for rendering lines into the game display. Not to clear yet, still searching the code.

Hope someone can give a quick heads up to save some time.
#3
10/04/2005 (7:01 pm)
You know you can use script for drawing lines right? check out, creator/editor/editorRender.cs
#4
10/04/2005 (7:09 pm)
Thanks, I'll look at that and try to adapt it. But I'd really like to do this in the AIVehicles code as a visual aid to some steering problems. For now I can call out to script and get it done if this works.
#5
10/04/2005 (8:36 pm)
What I figured out so far. Since I figured out that each object gets a renderImage callback (if it exists), I added the following to the AIWheeledVehicle code; where center,desired and front are as computed in getSteeringAngle.

But still doesn't work, anyone know how to do this? thanks.

void AIWheeledVehicle::renderImage(SceneState *state, SceneRenderImage *image)
{
	Parent::renderImage(state, image);

   glDisable(GL_LIGHTING);
   glPushMatrix();
   dglMultMatrix(&getRenderTransform());

   glBegin(GL_LINES);
   glVertex3f(center.x, center.y, center.z);
   glVertex3f(front.x, front.y, front.z);
   glVertex3f(desired.x, desired.y, desired.z);
   glVertex3f(center.x, center.y, center.z);
   glEnd();

   glPopMatrix();
   glEnable(GL_DEPTH_TEST);
   glEnable(GL_LIGHTING);


}
#6
10/04/2005 (8:49 pm)
What I figured out so far. Since I figured out that each object gets a renderImage callback (if it exists), I added the following to the AIWheeledVehicle code; where center,desired and front are as computed in getSteeringAngle.

But still doesn't work, anyone know how to do this? thanks.

void AIWheeledVehicle::renderImage(SceneState *state, SceneRenderImage *image)
{
	Parent::renderImage(state, image);

   glDisable(GL_LIGHTING);
   glPushMatrix();
   dglMultMatrix(&getRenderTransform());

   glBegin(GL_LINES);
   glVertex3f(center.x, center.y, center.z);
   glVertex3f(front.x, front.y, front.z);
   glVertex3f(desired.x, desired.y, desired.z);
   glVertex3f(center.x, center.y, center.z);
   glEnd();

   glPopMatrix();
   glEnable(GL_DEPTH_TEST);
   glEnable(GL_LIGHTING);


}
#7
10/05/2005 (6:34 am)
Update on my mini-resource:

The following actually works quite well (as far as drawing lines is concerened), drawing a tri-colored triangle indicating the steering points based on the center and front of vehicle and the desired destination. Not the simple method I wanted but it works. Now if I can just get the triangle pointed to the right place.

void AIWheeledVehicle::renderImage(SceneState *state, SceneRenderImage *image)
{
	Parent::renderImage(state, image);

   glDisable(GL_LIGHTING);
   glPushMatrix();
   dglMultMatrix(&getRenderTransform());

   // Box for the center of Mass, for reference
   glDisable(GL_DEPTH_TEST);
   glColor3f(1, 1, 1);
   wireCube(Point3F(0.05,0.05,0.05),mDataBlock->massCenter);

   glDisable(GL_CULL_FACE);

   Point3F front, center, desired;
   center.x = 0.0; center.y = 0.0; center.z = 0.0;  
   front = mDataBlock->front - mDataBlock->center;
   desired = mDataBlock->desired - mDataBlock->center;

   glBegin(GL_LINES);
   glColor3f(1, 0, 0);
   glVertex3f(front.x, front.y, front.z);
   glVertex3f(desired.x, desired.y, desired.z);
   glColor3f(0, 1, 0);
   glVertex3f(desired.x, desired.y, desired.z);
   glVertex3f(center.x, center.y, center.z);
   glColor3f(0, 0, 1);
   glVertex3f(center.x, center.y, center.z);
   glVertex3f(front.x, front.y, front.z);
   glEnd();

   glPopMatrix();
   glEnable(GL_DEPTH_TEST);
   glEnable(GL_LIGHTING);

}
#8
10/05/2005 (9:02 am)
If any one is following this thread, the code below is the present status of the routine. It produces a nice indication of the steering constructs but what I need is to transform the desired and front points back into the vehicle frame. Going off to look for how to do this within the renderImage context, but if anyone knows directly how to do this I would appreciate the heads up.

void AIWheeledVehicle::renderImage(SceneState *state, SceneRenderImage *image)
{
	Parent::renderImage(state, image);
   
   if (mDataBlock->gShowSteeringConstructs) {
      glDisable(GL_LIGHTING);
      glPushMatrix();
      dglMultMatrix(&getRenderTransform());

      glDisable(GL_DEPTH_TEST);
      glDisable(GL_CULL_FACE);

      Point3F front, center, desired;
      center.x = 0.0; center.y = 0.0; center.z = 0.0;  
      front = mDataBlock->front - mDataBlock->center;
      desired = mDataBlock->desired - mDataBlock->center;
center.x,center.y,front.x,front.y);

      glBegin(GL_LINES);
      glColor3f(1, 0, 0);
      glVertex3f(front.x, front.y, front.z);
      glVertex3f(desired.x, desired.y, desired.z);
      glEnd();
      wireCube(Point3F(0.05,0.05,0.05),front);

      glBegin(GL_LINES);
      glColor3f(0, 1, 0);
      glVertex3f(desired.x, desired.y, desired.z);
      glVertex3f(center.x, center.y, center.z);
      glEnd();
      wireCube(Point3F(0.05,0.05,0.05),desired);

      glBegin(GL_LINES);
      glColor3f(0, 0, 1);
      glVertex3f(center.x, center.y, center.z);
      glVertex3f(front.x, front.y, front.z);
      glEnd();
      wireCube(Point3F(0.05,0.05,0.05),center);

      glPopMatrix();
      glEnable(GL_DEPTH_TEST);
      glEnable(GL_LIGHTING);
   }
}
#9
10/05/2005 (10:09 am)
I wound up using this to rotate the points into the vehicle frame:

Point3F center = mDataBlock->center;
Point3F front = mDataBlock->front;
Point3F desired = mDataBlock->desired;
MatrixF wt = this->getWorldTransform();
wt.mulP(center);
wt.mulP(desired);
wt.mulP(front);


But one of the points, "front" still seems weird. But it's useful now!