📄 model.c
字号:
/*=================================================================================
FILE: model.c
SERVICES: Functionality for creating and displaying generated models.
DESCRIPTION: This file contains the functions required to build and display
generated models.
Copyright (c) 1999-2003 QUALCOMM Incorporated.
All Rights Reserved.
QUALCOMM Proprietary/GTDR
=================================================================================*/
/* include files */
#include "model.h"
/* local function prototypes */
static ObjType *AllocateObj( uint32 NumVertices, uint16 NumVlist,
const uint16 *VlistSize );
/*=================================================================================
FUNCTION: AllocateObj
DESCRIPTION: Allocate all memory needed for a simple model. Memory should
be freed with Obj_FreeObj() since there are a number of memory
allocations internal to the structure.
PROTOTYPE: static ObjType *AllocateObj( uint32 NumVertices, uint16 NumVlist,
const uint16 *VlistSize )
PARAMETERS:
NumVertices - number of vertices contained in model
NumVlist - Number of vertice lists needed to render model
VlistSize - Array of Vlist sizes, one for each of NumVlist.
RETURN VALUE:
Pointer to model or NULL;
====================================================================================*/
static ObjType *AllocateObj( uint32 NumVertices, uint16 NumVlist,
const uint16 *VlistSize )
{
ObjType *Model = NULL;
uint32 i;
uint32 VlistSum = 0;
for (i = 0; i < NumVlist; i++)
{
VlistSum += ALIGN4(sizeof(int16) * VlistSize[i]);
}
Model = MALLOC( ALIGN4(sizeof(*Model)) +
ALIGN4(sizeof(*Model->Model) * NumVertices) +
ALIGN4(sizeof(*Model->Vertices) * NumVertices) +
ALIGN4(sizeof(*Model->Norm) * NumVertices) +
ALIGN4(sizeof(*Model->RMode) * NumVlist) +
ALIGN4(sizeof(*Model->VlistArray) * NumVlist ) +
ALIGN4(sizeof(*Model->VlistArray) * NumVlist ) +
ALIGN4(VlistSum) );
if (Model)
{
MEMSET( Model, 0, sizeof(*Model) );
Model->NumVertices = NumVertices;
Model->Model = (AEE3DTLVertex *) ((byte *) Model + ALIGN4(sizeof(*Model)));
Model->Vertices = (AEE3DPoint *) ((byte *) Model->Model +
ALIGN4(sizeof(*Model->Model) * NumVertices));
Model->NumVlist = NumVlist;
Model->Norm = (AEE3DPoint16*) ((byte *) Model->Vertices +
ALIGN4(sizeof(*Model->Vertices) * NumVertices));
Model->RMode = (AEE3DPrimitiveType *) ((byte *) Model->Norm +
ALIGN4(sizeof(*Model->Norm) * NumVertices));
Model->VlistArray = (VlistType *) ((byte *) Model->RMode +
ALIGN4(sizeof(*Model->VlistArray) * NumVlist ));
MEMSET( Model->VlistArray, 0, sizeof(*Model->VlistArray) * NumVlist );
Model->VlistArray[0].VlistSize = VlistSize[0];
Model->VlistArray[0].Vlist = (int16*) ((byte *) Model->VlistArray +
ALIGN4(sizeof(*Model->VlistArray) * NumVlist ));
for (i = 1; i < NumVlist; i++)
{
Model->VlistArray[i].VlistSize = VlistSize[i];
Model->VlistArray[i].Vlist = (int16*) ((byte *) Model->VlistArray[i-1].Vlist +
ALIGN4(sizeof(int16) * VlistSize[i-1]));
}
}
return Model;
}
/*============================================================================
FUNCTION: Obj_GetTriNorm
DESCRIPTION: Loops through each triangle set in an object and calculates
the normal for each vertex.
PROTOTYPE:
int Obj_GetTriNorm(TutorI3D* p3D, ObjType *Model)
PARAMETERS:
pMe [in] - Pointer to the applet instance.
Model [in] - Everything you need to know about this model
DEPENDENCIES
none
RETURN VALUE:
SUCCESS: succeeded to calculate all normals
EFAILED: failed
SIDE EFFECTS
none
===============================================================================*/
int Obj_GetTriNorm(TutorI3D* pMe, ObjType *Model)
{
uint32 VlistNum;
if(!Model || !pMe)
{
return EFAILED;
}
for (VlistNum = 0; VlistNum < Model->NumVlist; VlistNum++)
{
if( I3D_CalcVertexArrayNormal( pMe->m_p3D, Model->Norm,
(uint16*)Model->VlistArray[VlistNum].Vlist,
Model->VlistArray[VlistNum].NumTriangles,
Model->Vertices, Model->RMode[VlistNum]) != SUCCESS)
return EFAILED;
}
return SUCCESS;
}
/*============================================================================
FUNCTION: Obj_RenderObj
DESCRIPTION: Loops through each triangle set in a model and calls the
appropriate rendering function. A transformation can be
applied before rendering, and you can specify whether
to use the vertex colors for rendering, or the current
light settings.
PROTOTYPE:
void Obj_RenderObj( TutorI3D *p3D, ObjType *Model,
const AEE3DTransformMatrix* transformMtx,
boolean withLight )
PARAMETERS:
pMe [in] - Pointer to the applet instance.
Model [in] - Everything you need to know about this model
transformMtx [in] - transform matrix to apply.
Set transformMtx to NULL for no transformation
withLight [in] - whether to use lighting or not. If no lighting used,
vertex colors are used.
DEPENDENCIES
none
RETURN VALUE:
TRUE: success
FALSE: fail
SIDE EFFECTS
none
===============================================================================*/
boolean Obj_RenderObj( TutorI3D *pMe, ObjType *Model, const AEE3DTransformMatrix* transformMtx,
boolean withLight )
{
uint32 VlistNum;
AEE3DTransformMatrix Transform;
if(!pMe || !Model) return FALSE;
// initialize the transform matrix we're going to apply to the identity
if(I3DUtil_SetIdentityMatrix( pMe->m_p3DUtil, &Transform ) != SUCCESS)
{
return FALSE;
}
// if a transform matrix is supplied, apply it to our transform matrix
if(transformMtx)
{
if(I3DUtil_MatrixMultiply(pMe->m_p3DUtil,&Transform, transformMtx) != SUCCESS)
{
return FALSE;
}
}
// set the model to view transform matrix to use for the model
if(I3D_SetModelViewTransform(pMe->m_p3D, &Transform) != SUCCESS)
{
return FALSE;
}
// apply the model to view transform matrix to the models vertices
if(I3D_ApplyModelViewTransform( pMe->m_p3D, Model->Model,
Model->Vertices, Model->NumVertices ) != SUCCESS)
{
return FALSE;
}
// go through each vertex list, calculate the pixels colors based on lighting
// if required, and call the appropriate render function for the vertex list.
for (VlistNum = 0; VlistNum < Model->NumVlist; VlistNum++)
{
// if we're rendering with light, calculate the pixel colors based on current
// lighting
if(withLight)
{
if(I3D_CalcVertexArrayColor(pMe->m_p3D, Model->Model,
(uint16*)Model->VlistArray[VlistNum].Vlist,
Model->NumVertices,
Model->Norm,
Model->RMode[VlistNum]) != SUCCESS)
{
return FALSE;
}
}
// call the appropriate render function for the vertex list, to render
// the triangles.
switch (Model->RMode[VlistNum])
{
case AEE3D_TRIANGLE_FAN:
if(I3D_RenderTriangleFan( pMe->m_p3D, Model->Model,
(const uint16*)Model->VlistArray[VlistNum].Vlist,
Model->VlistArray[VlistNum].VlistSize-2, Model->NumVertices) != SUCCESS)
{
return FALSE;
}
break;
case AEE3D_TRIANGLE_STRIP:
if(I3D_RenderTriangleStrip( pMe->m_p3D, Model->Model,
(const uint16*)Model->VlistArray[VlistNum].Vlist,
(Model->VlistArray[VlistNum].VlistSize-2), Model->NumVertices) != SUCCESS)
{
return FALSE;
}
break;
case AEE3D_TRIANGLE:
if(I3D_RenderTriangles( pMe->m_p3D, Model->Model,
(const uint16*)Model->VlistArray[VlistNum].Vlist,
Model->VlistArray[VlistNum].VlistSize/3,Model->NumVertices) != SUCCESS)
{
return FALSE;
}
break;
}
}
return TRUE;
}
/*============================================================================
FUNCTION:
Obj_FreeObj
DESCRIPTION:
Returns the memory allocated to this model to the heap
PROTOTYPE:
void Obj_FreeObj( ObjType *Model )
PARAMETERS:
ObjType *model - pointer to allocated model
DEPENDENCIES
none
RETURN VALUE:
none
SIDE EFFECTS
none
==============================================================================*/
void Obj_FreeObj( ObjType *Model )
{
if (Model)
{
FREE( Model );
}
}
/*****************MODELS BELOW*********************************************/
/*==========================================================================
FUNCTION:
Obj_BuildAxisModel
DESCRIPTION:
Allocate and build an xyz axis model. The axis are
modeled as long rectangles, with a pyramid at the end
pointing in the positive direction of that axis.
Model should be freed using the function Obj_FreeObj().
PROTOTYPE:
ObjType* Obj_BuildAxisModel( TutorI3D *pMe )
PARAMETERS:
TutorI3D *pMe - Pointer to the applet instance.
DEPENDENCIES
none
RETURN VALUE:
Pointer to model or NULL;
SIDE EFFECTS
none
===========================================================================*/
ObjType* Obj_BuildAxisModel( TutorI3D *pMe )
{
ObjType *Model = NULL;
// Array representing the number of vertices in each vertex list
const uint16 VlistSize[9] = { 36,12,6, 36,12,6, 36,12,6};
// Initial properties for each vertex, including color
const AEE3DTLVertex AxisModelInit[] =
{
// x,y,z,w r,g,b,a s,t
// y axis stick
{0,0,0,0, 0,0,0,255, 0,0}, // stick is black
{0,0,0,0, 0,0,0,255, 0,0},
{0,0,0,0, 0,0,0,255, 0,0},
{0,0,0,0, 0,0,0,255, 0,0},
{0,0,0,0, 0,0,0,255, 0,0},
{0,0,0,0, 0,0,0,255, 0,0},
{0,0,0,0, 0,0,0,255, 0,0},
{0,0,0,0, 0,0,0,255, 0,0},
// y axis arrow
{0,0,0,0, 0,0,255,255, 0,0}, // arrow tip is blue
{0,0,0,0, 255,0,0,255, 0,0}, // base vertices are red
{0,0,0,0, 255,0,0,255, 0,0},
{0,0,0,0, 255,0,0,255, 0,0},
{0,0,0,0, 255,0,0,255, 0,0},
// x axis stick
{0,0,0,0, 0,0,0,255, 0,0}, // stick is black
{0,0,0,0, 0,0,0,255, 0,0},
{0,0,0,0, 0,0,0,255, 0,0},
{0,0,0,0, 0,0,0,255, 0,0},
{0,0,0,0, 0,0,0,255, 0,0},
{0,0,0,0, 0,0,0,255, 0,0},
{0,0,0,0, 0,0,0,255, 0,0},
{0,0,0,0, 0,0,0,255, 0,0},
// xaxis arrow
{0,0,0,0, 0,0,255,255, 0,0}, // arrow tip is blue
{0,0,0,0, 255,0,0,255, 0,0}, // base vertices are red
{0,0,0,0, 255,0,0,255, 0,0},
{0,0,0,0, 255,0,0,255, 0,0},
{0,0,0,0, 255,0,0,255, 0,0},
// z axis stick
{0,0,0,0, 0,0,0,255, 0,0}, // stick is black
{0,0,0,0, 0,0,0,255, 0,0},
{0,0,0,0, 0,0,0,255, 0,0},
{0,0,0,0, 0,0,0,255, 0,0},
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -