Forum

Draw Model Frame: Draws a model

Discuss programming topics for the various GPL'd game engine sources.

Moderator: InsideQC Admins

Draw Model Frame: Draws a model

Postby Baker » Tue May 03, 2011 10:04 pm

Draws a model. Loads it if not in cache. Short.

For an in-engine model viewer I am working on that will let you browse stuff in the menu.

Code: Select all
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
// gl_drawaliasframe.c

#include "quakedef.h"
#include "gl_local.h"      // Baker: needed

void _GL_Draw_MDL (model_t *myModel, int myFrameNum, int mySkinNum, float x, float y, float z, float pitch, float yaw, float roll, float myScale);

/*
=================
GL_Draw_MDL
=================
*/

void GL_Draw_MDL (const char *myModelString, int myFrameNum, int mySkinNum, float x, float y, float z, float pitch, float yaw, float roll, float scale)
{
   model_t *GetModel = Mod_ForName (myModelString, false);

   if (!GetModel)
   {
      Con_Printf ("Model \"%s\" not found\n", myModelString);
      return;
   }

   _GL_Draw_MDL (GetModel, myFrameNum, mySkinNum, x, y, z, pitch, yaw, roll, scale);
}

void _GL_Draw_MDL (model_t *myModel, int myFrameNum, int mySkinNum, float x, float y, float z, float pitch, float yaw, float roll, float myScale)
{
   aliashdr_t   *paliashdr = (aliashdr_t *)Mod_Extradata (myModel);             // Locate the proper data
   int         polys      = paliashdr->numtris;                        // Count the polys
   int         framenum   = bound(0, myFrameNum, (paliashdr->numframes-1));   // Ensure range validity
   int         posenum    = paliashdr->frames[framenum].firstpose;
   int         numposes   = paliashdr->frames[framenum].numposes;
   int         skinnum    = bound(0, mySkinNum,  (paliashdr->numskins-1) );   // Ensure range validity
   int         anim       = (int)(cl.time*10) & 3;                         // Blinky (frame groups)
   int         texture    = paliashdr->gl_texturenum[mySkinNum][anim];        // Get texture
   int         fb_texture = paliashdr->fb_texturenum[mySkinNum][anim];        // Get additive fullbright texture

   if (numposes > 1)
   {
      float interval = paliashdr->frames[framenum].interval;
      posenum += (int)(cl.time / interval) % numposes;
   }

   glPushMatrix ();
   
   {   // Rotate as specified R_RotateForEntity
      
      glTranslatef (x, y, z);         // Set position
      
      glRotatef ( yaw,   0, 0, 1);   // Rotate as specified
      glRotatef (-pitch, 0, 1, 0);
      glRotatef ( roll , 1, 0, 0);

      glScalef  (myScale, myScale, myScale);
   }

   {   // Scale
      glTranslatef (paliashdr->scale_origin[0], paliashdr->scale_origin[1], paliashdr->scale_origin[2]);
      glScalef (paliashdr->scale[0], paliashdr->scale[1], paliashdr->scale[2]);
   }
      
   GL_DisableMultitexture ();
   GL_Bind (texture);
   glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

   {
      trivertx_t   *verts = (trivertx_t *)((byte *)paliashdr + paliashdr->posedata);
      int         *order = (int *)((byte *)paliashdr + paliashdr->commands);
      int         count;

      verts += posenum * paliashdr->poseverts;

      while ((count = *order++))
      {
         // get the vertex count and primitive type
         if (count < 0)
         {
            count = -count;
            glBegin (GL_TRIANGLE_FAN);
         }
         else
         {
            glBegin (GL_TRIANGLE_STRIP);
         }

         do
         {      
            glTexCoord2f (((float *)order)[0], ((float *)order)[1]);
            order += 2;

            glColor3f   (1, 1, 1);
            glVertex3f  (verts->v[0], verts->v[1], verts->v[2]);

            verts++;
         } while (--count);

         glEnd ();
      }
   }

   glPopMatrix ();
   glColor3f (1, 1, 1);
}
The night is young. How else can I annoy the world before sunsrise? 8) Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
User avatar
Baker
 
Posts: 3666
Joined: Tue Mar 14, 2006 5:15 am

Return to Engine Programming

Who is online

Users browsing this forum: No registered users and 1 guest