Game Development Community

Speedometer: Draw a filled needle.

by Chris "C2" Byars · in Torque Game Engine · 06/16/2006 (2:20 pm) · 2 replies

Well, I am somewhat clueless when it comes to rendering code in C++.

I simply want the needle in the GuiSpeedometerHud to be a filled rectangle, rather than just a rectangle. Can anyone spot me the code for that? The bolded is apparently where it needs to be changed to be more than just lines.

void GuiSpeedometerHud::onRender(Point2I offset, const RectI &updateRect)
{
   // Must have a connection and player control object
   GameConnection* conn = GameConnection::getConnectionToServer();
   if (!conn)
      return;
   ShapeBase* control = dynamic_cast<ShapeBase*>(conn->getControlObject());

   Parent::onRender(offset,updateRect);

   // Use the vehicle's velocity as it's speed...
   mSpeed = control->getVelocity().len();
   if (mSpeed > mMaxSpeed)
      mSpeed = mMaxSpeed;

   // Render the needle
   glPushMatrix();
   Point2F center = mCenter;
   if (center.x == F32(0) && center.y == F32(0)) {
      center.x = mBounds.extent.x / 2;
      center.y = mBounds.extent.y / 2;
   }
   glTranslatef(mBounds.point.x + center.x,mBounds.point.y + center.y,0);

   F32 rotation = mMinAngle + (mMaxAngle - mMinAngle) * (mSpeed / mMaxSpeed);
   glRotatef(rotation,0.0f,0.0f,-1.0f);

   glEnable(GL_BLEND);
   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
   glDisable(GL_TEXTURE_2D);

   glColor4f(mColor.red, mColor.green, mColor.blue, mColor.alpha);
[b]   glBegin(GL_LINE_LOOP);
      glVertex2f(+mNeedleLength,-mNeedleWidth);
      glVertex2f(+mNeedleLength,+mNeedleWidth);
      glVertex2f(-mTailLength ,+mNeedleWidth);
      glVertex2f(-mTailLength ,-mNeedleWidth);[/b]
   glEnd();
   glPopMatrix();
}

This would be very appreciated. :) Thanks in advance.

#1
06/16/2006 (2:52 pm)
Ok

glBegin(GL_LINE_LOOP);

this is requesting you draw a line loop so it will draw one continous line for each point specified.

you are probably looking to change this to

glBegin(GL_TRIANGLES);

here is a quick list of the available primitives in gl

GL_POINTS 
GL_LINES
GL_LINE_LOOP
GL_LINE_STRIP
GL_TRIANGLES 
GL_TRIANGLE_STRIP 
GL_TRIANGLE_FAN 
GL_QUADS 
GL_QUAD_STRIP 
GL_POLYGON


this code does not show the calculation of mNeedleLength or mTailLength
so it's a little tough to say anything more but just try swapping the primitive type to one of the above.

Edit:
as well you are going to need to work on how many points you are using.
there will not be enough for the new primitive to draw your stuff.
all the data should be fine tho and just duplicate some points.

Edit2:
ok its late in the day..

but use the GL_QUADS and you will get what you want without no more data.

:)
#2
06/16/2006 (3:52 pm)
Exactly what I needed, thanks Badguy. :)