2007-11-04 03:51:54 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
Copyright (C) 1999-2007 id Software, Inc. and contributors.
|
|
|
|
For a list of contributors, see the accompanying CONTRIBUTORS file.
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
This file is part of GtkRadiant.
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant 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.
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant 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.
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with GtkRadiant; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2007-11-04 03:51:54 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "entity_entitymodel.h"
|
|
|
|
#include "entity.h"
|
|
|
|
|
|
|
|
//
|
|
|
|
// CEntityMiscModel implementation
|
|
|
|
//
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
CEntityMiscModel::CEntityMiscModel ( entity_t *e ){
|
|
|
|
refCount = 1;
|
|
|
|
m_entity = e;
|
|
|
|
m_model = NULL;
|
|
|
|
VectorSet( m_translate, 0,0,0 );
|
|
|
|
VectorSet( m_euler, 0,0,0 );
|
|
|
|
VectorSet( m_scale, 1,1,1 );
|
|
|
|
VectorSet( m_pivot, 0,0,0 );
|
|
|
|
m4x4_identity( m_transform );
|
|
|
|
m4x4_identity( m_inverse_transform );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
CEntityMiscModel::~CEntityMiscModel (){
|
|
|
|
if ( m_cachereq.GetBuffer()[0] != ':'
|
|
|
|
&& m_version.c_str()[0] != '\0' ) {
|
|
|
|
GetModelCache()->DeleteByID( m_cachereq.GetBuffer(), m_version.c_str() );
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// IRender
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CEntityMiscModel::Draw( int state, int rflags ) const {
|
|
|
|
// push the current modelview matrix
|
|
|
|
// FIXME: put in a check for stack recursion depth..
|
|
|
|
// or avoid recursion of opengl matrix stack
|
|
|
|
g_QglTable.m_pfn_qglPushMatrix();
|
|
|
|
// apply the parent-to-local transform
|
|
|
|
g_QglTable.m_pfn_qglMultMatrixf( m_transform );
|
|
|
|
|
|
|
|
pivot_draw( m_pivot );
|
|
|
|
|
|
|
|
// draw children
|
|
|
|
if ( m_model && m_model->pRender ) {
|
|
|
|
m_model->pRender->Draw( state, rflags );
|
|
|
|
}
|
|
|
|
|
|
|
|
g_QglTable.m_pfn_qglPopMatrix();
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ISelect
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
bool CEntityMiscModel::TestRay( const ray_t *ray, vec_t *dist ) const {
|
|
|
|
vec_t dist_start = *dist;
|
|
|
|
vec_t dist_local = *dist;
|
2007-11-04 03:51:54 +00:00
|
|
|
ray_t ray_local = *ray;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( aabb_test_ray( &m_BBox, ray ) == 0 ) {
|
|
|
|
return false;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
ray_transform( &ray_local, m_inverse_transform );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( m_model && m_model->pSelect ) {
|
|
|
|
if ( m_model->pSelect->TestRay( &ray_local, &dist_local ) ) {
|
|
|
|
*dist = dist_local;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{ *dist = dist_local; }
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return *dist < dist_start;
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//IEdit
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CEntityMiscModel::Translate( const vec3_t translation ){
|
|
|
|
VectorIncrement( translation, m_translate );
|
|
|
|
UpdateCachedData();
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CEntityMiscModel::Rotate( const vec3_t pivot, const vec3_t rotation ){
|
|
|
|
m4x4_t rotation_matrix;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
m4x4_identity( rotation_matrix );
|
|
|
|
m4x4_pivoted_rotate_by_vec3( rotation_matrix, rotation, eXYZ, pivot );
|
|
|
|
m4x4_transform_point( rotation_matrix, m_translate );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorIncrement( rotation, m_euler );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
UpdateCachedData();
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CEntityMiscModel::OnKeyValueChanged( entity_t *e, const char *key, const char* value ){
|
|
|
|
if ( strcmp( key, "model" ) == 0 ) {
|
|
|
|
SetName( value );
|
|
|
|
}
|
|
|
|
else if ( strcmp( key, "_frame" ) == 0 ) {
|
|
|
|
SetName( ValueForKey( e, "model" ) );
|
|
|
|
}
|
|
|
|
else if ( strcmp( key, "angle" ) == 0 ) {
|
|
|
|
VectorSet( m_euler, 0.f, 0.f, 0.f );
|
|
|
|
m_euler[2] = atof( value );
|
|
|
|
UpdateCachedData();
|
|
|
|
}
|
|
|
|
else if ( strcmp( key, "angles" ) == 0 ) {
|
|
|
|
VectorSet( m_euler, 0.f, 0.f, 0.f );
|
|
|
|
if ( value[0] != '\0' ) {
|
|
|
|
sscanf( value, "%f %f %f", &m_euler[1], &m_euler[2], &m_euler[0] );
|
|
|
|
}
|
|
|
|
UpdateCachedData();
|
|
|
|
}
|
|
|
|
else if ( strcmp( key, "modelscale" ) == 0 || strcmp( key,"modelscale_vec" ) == 0 ) {
|
|
|
|
const char *s;
|
|
|
|
VectorSet( m_scale, 1.f, 1.f, 1.f );
|
|
|
|
s = ValueForKey( e,"modelscale" );
|
|
|
|
if ( s[0] != '\0' ) {
|
|
|
|
float f = atof( s );
|
|
|
|
if ( f != 0 ) {
|
|
|
|
VectorSet( m_scale, f, f, f );
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
Sys_FPrintf( SYS_WRN, "WARNING: ignoring 0 modelscale key\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s = ValueForKey( e,"modelscale_vec" );
|
|
|
|
if ( s[0] != '\0' ) {
|
|
|
|
sscanf( s, "%f %f %f", &m_scale[0], &m_scale[1], &m_scale[2] );
|
|
|
|
if ( m_scale[0] == 0.0 && m_scale[1] == 0.0 && m_scale[2] == 0.0 ) {
|
|
|
|
VectorSet( m_scale, 1,1,1 );
|
|
|
|
Sys_FPrintf( SYS_WRN, "WARNING: ignoring 0 0 0 modelscale_vec key\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
UpdateCachedData();
|
|
|
|
}
|
|
|
|
else if ( strcmp( key, "origin" ) == 0 ) {
|
|
|
|
sscanf( value, "%f %f %f", &m_translate[0], &m_translate[1], &m_translate[2] );
|
|
|
|
UpdateCachedData();
|
|
|
|
}
|
|
|
|
else if ( strncmp( key,"_remap",6 ) == 0 ) {
|
|
|
|
SetName( ValueForKey( e, "model" ) );
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// CEntityMiscModel
|
|
|
|
//
|
|
|
|
|
|
|
|
// private:
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CEntityMiscModel::BuildCacheRequestString( const char *name ){
|
|
|
|
bool hasRemaps = false;
|
|
|
|
|
|
|
|
m_cachereq.Format( "%s:%i", name, IntForKey( m_entity,"_frame" ) );
|
|
|
|
|
|
|
|
for ( epair_t* ep = m_entity->epairs ; ep ; ep = ep->next )
|
|
|
|
{
|
|
|
|
if ( strncmp( ep->key,"_remap",6 ) == 0 ) {
|
|
|
|
if ( !hasRemaps ) {
|
|
|
|
hasRemaps = true;
|
|
|
|
m_cachereq += "?";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_cachereq += "&";
|
|
|
|
}
|
|
|
|
m_cachereq += ep->value;
|
|
|
|
}
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CEntityMiscModel::SetName( const char *name ){
|
|
|
|
Str m_oldcachereq = m_cachereq;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( name[0] == '\0' ) {
|
|
|
|
return;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
BuildCacheRequestString( name );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( strcmp( m_oldcachereq, m_cachereq ) == 0 ) {
|
|
|
|
return;
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( m_cachereq.GetBuffer()[0] != ':'
|
|
|
|
&& m_version.c_str()[0] != '\0' ) {
|
|
|
|
GetModelCache()->DeleteByID( m_cachereq.GetBuffer(), m_version.c_str() );
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
m_model = NULL;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( name[0] != '\0' ) {
|
|
|
|
const char* dot = strrchr( name, '.' );
|
|
|
|
if ( dot != NULL ) {
|
|
|
|
m_version = ++dot;
|
|
|
|
m_model = GetModelCache()->GetByID( m_cachereq.GetBuffer(), m_version.c_str() );
|
|
|
|
}
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
UpdateCachedData();
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CEntityMiscModel::UpdateCachedData(){
|
|
|
|
aabb_t aabb_temp;
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
m4x4_identity( m_transform );
|
|
|
|
m4x4_pivoted_transform_by_vec3( m_transform, m_translate, m_euler, eXYZ, m_scale, m_pivot );
|
|
|
|
memcpy( m_inverse_transform, m_transform, sizeof( m4x4_t ) );
|
|
|
|
m4x4_invert( m_inverse_transform );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
aabb_clear( &aabb_temp );
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( m_model && m_model->pRender ) {
|
|
|
|
aabb_extend_by_aabb( &aabb_temp, m_model->pRender->GetAABB() );
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
VectorSet( aabb_temp.extents, 8, 8, 8 );
|
|
|
|
}
|
2007-11-04 03:51:54 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
aabb_for_transformed_aabb( &m_BBox, &aabb_temp, m_transform );
|
2007-11-04 03:51:54 +00:00
|
|
|
}
|