http://Welcome.to/Genesis3D-University
View Weapon in Front Tutorial
this Tutorial is created by Thomas
Werth it will show you how to render a Weapon Actor in front of your
view.
This Tutorial is based on the SpecificActor Tutorial and the VWeap
Tutorial.Now we go ahed deep into the code :)
Here ' the vweap.c file
#include <windows.h>
#include <math.h>
#include <assert.h>
#include "genesis.h"
#include "ErrorLog.h"
#include "vweap.h"
extern geVec3d CpitAngle;
extern geVec3d CpitPos; //these are player's Position and Angles
float DegreesToRadiansb(float degrees) { return (degrees * GE_PI)/180.0f;
}
geBoolean VWeap_Spawn(geEngine *Engine, geWorld *World, geVFile *MainFS,
geSound_System *SoundSystem)
{
geEntity_EntitySet * Set;
geEntity * Entity;
VWeap * SActor;
geVFile *AFile;
geActor_Def *ActorDef;
geActor * Actor;
Set = geWorld_GetEntitySet(World, "VWeap");
if (Set == NULL)
return GE_TRUE;
for (Entity= geEntity_EntitySetGetNextEntity(Set, NULL); Entity;
Entity= geEntity_EntitySetGetNextEntity(Set, Entity))
{
SActor= (VWeap*)geEntity_GetUserData(Entity);
//AFile = geVFile_Open(MainFS, "actors\\vweap.act",GE_VFILE_OPEN_READONLY);
AFile = geVFile_Open(MainFS, "actors\\pm.act",GE_VFILE_OPEN_READONLY);
if (!AFile)
{
geErrorLog_AddString(-1, "VWeap_Spawn: Couldn't Open Actor File:
", SActor->ActorFile);
return GE_FALSE;
}
ActorDef = geActor_DefCreateFromFile (AFile);
SActor->pActorDef = ActorDef;
Actor = geActor_Create (ActorDef);
SActor->pActor = Actor;
if (geWorld_AddActor (World, Actor, GE_ACTOR_RENDER_NORMAL, 0xffffffff)==
GE_FALSE)
return GE_FALSE;
} //for schleife
return GE_TRUE;
}
geBoolean VWeap_Render(geWorld *World)
{
geEntity_EntitySet *pSet;
geEntity *pEntity;
geBoolean bHasMotion;
pSet = geWorld_GetEntitySet(World, "VWeap");
if (pSet == NULL)
return GE_TRUE;
pEntity= geEntity_EntitySetGetNextEntity(pSet, NULL);
while (pEntity)
{
geVec3d pos, scale;
geXForm3d xform;
VWeap *SActor= (VWeap*)geEntity_GetUserData(pEntity);
scale= SActor->scale;
//geActor_SetScale(SActor->pActor, scale.X, scale.Y, scale.Z);
geActor_SetScale(SActor->pActor, 0.35f, 0.35f, 0.35f);
pos= SActor->Origin;
geXForm3d_SetIdentity(&xform);
bHasMotion= GE_FALSE;
SActor->Orientation.X=CpitAngle.X;
SActor->Orientation.Y=CpitAngle.Y;
SActor->Orientation.Z=CpitAngle.Z;
//Hier muss die Waffe noch richtig gedreht werden
//code
geXForm3d_RotateX(&xform, (DegreesToRadiansb(CpitAngle.X)+DegreesToRadiansb(90.0f)));
geXForm3d_RotateZ(&xform, (DegreesToRadiansb(CpitAngle.Z)+DegreesToRadiansb(-180.0f)));
geXForm3d_RotateY(&xform, DegreesToRadiansb(CpitAngle.Y));
//code ende für gerade biegen
// Diese Drehungen sind notwendig damit sich das Object
// relativ zu dem Player dreht
geXForm3d_RotateX(&xform, CpitAngle.X);
geXForm3d_RotateY(&xform, CpitAngle.Y);
geXForm3d_RotateZ(&xform, CpitAngle.Z);
pos.X=CpitPos.X;
pos.Y=CpitPos.Y + 100;
pos.Z=CpitPos.Z;
geXForm3d_Translate(&xform, pos.X, pos.Y, pos.Z);
geActor_ClearPose(SActor->pActor, &xform);
pEntity= geEntity_EntitySetGetNextEntity(pSet, pEntity);
}
return GE_TRUE;
}
here's VWeap.h file
#ifndef VWEAP_H
#define VWEAP_H
#ifdef __cplusplus
extern "C" {
#endif
#include "Genesis.h"
#pragma warning( disable : 4068 )
#pragma GE_Type("Item.ico")
typedef struct VWeap
{
#pragma GE_Private
geFloat lastTime; // Last animation played
geActor * pActor; // Actual Actor
geActor_Def * pActorDef; // Actor Definition
geActor_Def * pActorDefA; // Actor Definition
geFloat startTime; // animation start time
geVec3d Rotation; // Vector for rotation
#pragma GE_Published
geVec3d Origin; //Origin of entity
geVec3d scale; // Vector for scaling
geVec3d Orientation; // Vector for orientation
char *ActorFile; // Disabled =)
geBoolean RenderInMirrors; // Will the actor be rendered in mirrors?
#pragma GE_Origin(Origin)
#pragma GE_DefaultValue(scale, "1 1 1")
#pragma GE_DefaultValue(Orientation, "-90 0 0")
#pragma GE_DefaultValue(RenderInMirrors, "True")
#pragma GE_Documentation(scale, "Amount to scale the actor")
#pragma GE_Documentation(Orientation, "Orientation of the actor (in
degrees)")
#pragma GE_Documentation(RenderInMirrors, "Will the actor be rendered
in mirrors?")
} VWeap;
geBoolean VWeap_Spawn(geEngine *Engine, geWorld *World, geVFile *MainFS,
geSound_System *SoundSystem);
geBoolean VWeap_Render(geWorld *World);
#ifdef __cplusplus
}
#endif
#endif
include both files to your project.
Now we need to add some lines in Client.c
First we have to declare some global var's at the beginning of
Client.c
//added for viewWeapon2
geVec3d CpitAngle;
geVec3d CpitPos;
In BuildClientViewXForm we need to declare these Var's
double x, z;
right after
assert(Client->ViewPlayer >= 0 &&
Client->ViewPlayer < NETMGR_MAX_PLAYERS);
//Get the human clientplayer
Player = &Client->Players[Client->ViewPlayer];
//Copy his xform
Client->ViewXForm = Player->XForm;
insert this code
//added for viewweaponboth
x = sin(Client->Angles.Y)*20;
z = cos(Client->Angles.Y)*20;
after these lines
geXForm3d_RotateZ(&Client->ViewXForm,
Client->Angles.Z);
geXForm3d_RotateX(&Client->ViewXForm, Client->Angles.X);
geXForm3d_RotateY(&Client->ViewXForm, Client->Angles.Y);
add this
CpitAngle.Z=Client->Angles.Z;//+RAD(180.0f);
CpitAngle.X=Client->Angles.X;//+RAD(180.0f);
CpitAngle.Y=Client->Angles.Y;//+RAD(180.0f);
geXForm3d_GetIn(&Client->ViewXForm, &In);
geVec3d_AddScaled(&Pos, &In, +30.0f, &CpitPos);
geXForm3d_GetLeft(&Client->ViewXForm, &In);
geVec3d_AddScaled(&CpitPos, &In, -15.0f, &CpitPos);
In Render_World the next changes are waiting
just before the finish of these function add
//added for viewweapon2
// Render the weapon2
if (bShowWeapon)
if (!VWeap_Render(World))
GenVS_Error("Client_RenderWorld: Weapon_Render failed.\n");
In GameMgr_SetWorld in GameMgr.c Add near the end
//added for viewweapon2
// Create The weapon2
if (!VWeap_Spawn(GameMgr_GetEngine(GMgr), GameMgr_GetWorld(GMgr), MainFS,
GameMgr_GetSoundSystem(GMgr)))
GenVS_Error("GameMgr_SetWorld: VWeap_Spawn failed.\n");
Now you only need to add a vweap Entity into your Level.(remember to
copy the pm.act file into your actor dir).
Start your Game and watch the effect.
You can download the needed files here
Enjoy and have fun .
http://www.natom.de home of
Thomas Werth's Natom (here you get more Tutorials, Apps , Games , Books
and more)
*********
Get
Genesis3D University on CD !
The Genesis3d
UNIVERSITY [G3DU] CD
Many tutorials, file downloads and more, all on
one CD, imagine that!
Over 530 megs of files! Just look over all of this website, to
get an idea of the CD contents.
To review the CD content and to Find out how to get yours.
HERE
******
|