VERC: Painlessly Drawing TriAPI In Orthogonal Mode Last edited 7 months ago2024-05-05 18:41:59 UTC

Orthogonal Triangle Renderering inside The Half-Life SDK

I decided to whip up this tutorial to show you the simplicity of drawing triangles in orthogonal mode using TriAPI. This is really simple, and I'm even doing all the work for you! An example regarding how to draw a quad over the entire screen is included at the end of the article.

Step #1

Load up tri.cpp and add this shell at the very bottom:
/*
=================
HUD_DrawOrthoTriangles
Orthogonal Triangles -- (relative to resolution,
smackdab on the screen) add them here
=================
*/
void HUD_DrawOrthoTriangles( void )
{
}

Step #2

Open up HUD_Redraw.cpp, and prototype the function above redraw():
void HUD_DrawOrthoTriangles( void );
int CHud :: Redraw( float flTime, int intermission )
{

Step #3

Add the function inside redraw, so it's called. HUD_DrawOrthoTriangles is called from the HUD draw functions, because at this point the engine is already in orthogonal mode. Hence, the Z coord is a "magic 0", because it's not rendering in world space in ortho, it's 2D.
          ...
          pList = pList->pNext;
     }
}

//omega;draw orthogonal triangles
HUD_DrawOrthoTriangles();

// are we in demo mode? do we need to
// draw the logo in the top corner?
if (m_iLogo)
...

Step #4

Make use of it! Example:
#define ORTHOEXAMPLE
#ifdef ORTHOEXAMPLE
void OrthoExample()
{
    gEngfuncs.pTriAPI->RenderMode(kRenderTransAdd); //additive

    // use hotglow, or any other sprite for the texture
    gEngfuncs.pTriAPI->SpriteTexture( (struct model_s *)
        gEngfuncs.GetSpritePointer(SPR_Load("sprites/hotglow.spr")),
        0);

    gEngfuncs.pTriAPI->CullFace( TRI_NONE ); //no culling
    gEngfuncs.pTriAPI->Begin(TRI_QUADS); //start our quad

    //remember, always list vertices in counter-clockwise
    // order, unless you want the quad to be backwards =)
    // the third value of vertex3f will always be 0 in ortho mode,
    // don't change it unless you wan't funny things to happen.

    //top left
    gEngfuncs.pTriAPI->TexCoord2f(0.0f, 1.0f);
    gEngfuncs.pTriAPI->Vertex3f(0, 0, 0);

    //bottom left
    gEngfuncs.pTriAPI->TexCoord2f(0.0f, 0.0f);
    gEngfuncs.pTriAPI->Vertex3f(0, ScreenHeight, 0);

    //bottom right
    gEngfuncs.pTriAPI->TexCoord2f(1.0f, 0.0f);
    gEngfuncs.pTriAPI->Vertex3f(ScreenWidth, ScreenHeight, 0);

    //top right
    gEngfuncs.pTriAPI->TexCoord2f(1.0f, 1.0f);
    gEngfuncs.pTriAPI->Vertex3f(ScreenWidth, 0, 0);

    gEngfuncs.pTriAPI->End(); //end our list of vertexes
    gEngfuncs.pTriAPI->RenderMode(kRenderNormal); //return to normal
}
#endif
void HUD_DrawOrthoTriangles( void )
{
#ifdef ORTHOEXAMPLE
     OrthoExample();
#endif
}
There is a mistake in the original tutorial
The UV coordinates in the original tutorial are incorrect as it will draw the sprite flipped up/down and left/right.

For the sake of "archive integrity", the mistake is still there in the original code above. If you want to fix the coordinates, replace the appropriate code section by this one:
//top left
gEngfuncs.pTriAPI->TexCoord2f(0, 0);
gEngfuncs.pTriAPI->Vertex3f(0, 0, 0);

//bottom left
gEngfuncs.pTriAPI->TexCoord2f(0, 1);
gEngfuncs.pTriAPI->Vertex3f(0, ScreenHeight, 0);

//bottom right
gEngfuncs.pTriAPI->TexCoord2f(1, 1);
gEngfuncs.pTriAPI->Vertex3f(ScreenWidth, ScreenHeight, 0);

//top right
gEngfuncs.pTriAPI->TexCoord2f(1, 0);
gEngfuncs.pTriAPI->Vertex3f(ScreenWidth, 0, 0);
That about does it for this "tutorial". You now have the framework to draw triangles onto the hud with ease using TriAPI. Have fun, and enjoy! If you have any questions, email me at omega@thewavelength.net.

-omega
This article was originally published on Valve Editing Resource Collective (VERC).
The archived page is available here.
TWHL only publishes archived articles from defunct websites, or with permission. For more information on TWHL's archiving efforts, please visit the TWHL Archiving Project page.

Comments

You must log in to post a comment. You can login or register a new account.