DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

RenderWindow.cpp (6537B)


      1 /*
      2 ===========================================================================
      3 
      4 Doom 3 BFG Edition GPL Source Code
      5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 
      6 
      7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").  
      8 
      9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
     10 it under the terms of the GNU General Public License as published by
     11 the Free Software Foundation, either version 3 of the License, or
     12 (at your option) any later version.
     13 
     14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
     15 but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 GNU General Public License for more details.
     18 
     19 You should have received a copy of the GNU General Public License
     20 along with Doom 3 BFG Edition Source Code.  If not, see <http://www.gnu.org/licenses/>.
     21 
     22 In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code.  If not, please request a copy in writing from id Software at the address below.
     23 
     24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
     25 
     26 ===========================================================================
     27 */
     28 
     29 #pragma hdrstop
     30 #include "../idlib/precompiled.h"
     31 
     32 #include "DeviceContext.h"
     33 #include "Window.h"
     34 #include "UserInterfaceLocal.h"
     35 #include "RenderWindow.h"
     36 
     37 // NO LONGER SUPPORTED!
     38 //
     39 // D3 could render a 3D model in a subrect of a full screen
     40 // GUI for the main menus, but we have cut that ability so
     41 // we don't need to deal with offset viewports on all platforms.
     42 
     43 idRenderWindow::idRenderWindow(idUserInterfaceLocal *g) : idWindow(g) {
     44 	gui = g;
     45 	CommonInit();
     46 }
     47 
     48 idRenderWindow::~idRenderWindow() {
     49 	renderSystem->FreeRenderWorld( world ); 
     50 }
     51 
     52 void idRenderWindow::CommonInit() {
     53 	world = renderSystem->AllocRenderWorld();
     54 	needsRender = true;
     55 	lightOrigin = idVec4(-128.0f, 0.0f, 0.0f, 1.0f);
     56 	lightColor = idVec4(1.0f, 1.0f, 1.0f, 1.0f);
     57 	modelOrigin.Zero();
     58 	viewOffset = idVec4(-128.0f, 0.0f, 0.0f, 1.0f);
     59 	modelAnim = NULL;
     60 	animLength = 0;
     61 	animEndTime = -1;
     62 	modelDef = -1;
     63 	updateAnimation = true;
     64 }
     65 
     66 
     67 void idRenderWindow::BuildAnimation(int time) {
     68 	
     69 	if (!updateAnimation) {
     70 		return;
     71 	}
     72 
     73 	if (animName.Length() && animClass.Length()) {
     74 		worldEntity.numJoints = worldEntity.hModel->NumJoints();
     75 		worldEntity.joints = ( idJointMat * )Mem_Alloc16( SIMD_ROUND_JOINTS( worldEntity.numJoints ) * sizeof( *worldEntity.joints ), TAG_JOINTMAT );
     76 		modelAnim = gameEdit->ANIM_GetAnimFromEntityDef( animClass, animName );
     77 		if ( modelAnim ) {
     78 			animLength = gameEdit->ANIM_GetLength( modelAnim );
     79 			animEndTime = time + animLength;
     80 		}
     81 	}
     82 	updateAnimation = false;
     83 
     84 }
     85 
     86 void idRenderWindow::PreRender() {
     87 	if (needsRender) {
     88 		world->InitFromMap( NULL );
     89 		idDict spawnArgs;
     90 		spawnArgs.Set("classname", "light");
     91 		spawnArgs.Set("name", "light_1");
     92 		spawnArgs.Set("origin", lightOrigin.ToVec3().ToString());
     93 		spawnArgs.Set("_color", lightColor.ToVec3().ToString());
     94 		gameEdit->ParseSpawnArgsToRenderLight( &spawnArgs, &rLight );
     95 		lightDef = world->AddLightDef( &rLight );
     96 		if ( !modelName[0] ) {
     97 			common->Warning( "Window '%s' in gui '%s': no model set", GetName(), GetGui()->GetSourceFile() );
     98 		}
     99 		memset( &worldEntity, 0, sizeof( worldEntity ) );
    100 		spawnArgs.Clear();
    101 		spawnArgs.Set("classname", "func_static");
    102 		spawnArgs.Set("model", modelName);
    103 		spawnArgs.Set("origin", modelOrigin.c_str());
    104 		gameEdit->ParseSpawnArgsToRenderEntity( &spawnArgs, &worldEntity );
    105 		if ( worldEntity.hModel ) {
    106 			idVec3 v = modelRotate.ToVec3();
    107 			worldEntity.axis = v.ToMat3();
    108 			worldEntity.shaderParms[0] = 1;
    109 			worldEntity.shaderParms[1] = 1;
    110 			worldEntity.shaderParms[2] = 1;
    111 			worldEntity.shaderParms[3] = 1;
    112 			modelDef = world->AddEntityDef( &worldEntity );
    113 		}
    114 		needsRender = false;
    115 	}
    116 }
    117 
    118 void idRenderWindow::Render( int time ) {
    119 	rLight.origin = lightOrigin.ToVec3();
    120 	rLight.shaderParms[SHADERPARM_RED] = lightColor.x();
    121 	rLight.shaderParms[SHADERPARM_GREEN] = lightColor.y();
    122 	rLight.shaderParms[SHADERPARM_BLUE] = lightColor.z();
    123 	world->UpdateLightDef(lightDef, &rLight);
    124 	if ( worldEntity.hModel ) {
    125 		if (updateAnimation) {
    126 			BuildAnimation(time);
    127 		}
    128 		if (modelAnim) {
    129 			if (time > animEndTime) {
    130 				animEndTime = time + animLength;
    131 			}
    132 			gameEdit->ANIM_CreateAnimFrame(worldEntity.hModel, modelAnim, worldEntity.numJoints, worldEntity.joints, animLength - (animEndTime - time), vec3_origin, false );
    133 		}
    134 		worldEntity.axis = idAngles(modelRotate.x(), modelRotate.y(), modelRotate.z()).ToMat3();
    135 		world->UpdateEntityDef(modelDef, &worldEntity);
    136 	}
    137 }
    138 
    139 
    140 
    141 
    142 void idRenderWindow::Draw(int time, float x, float y) {
    143 	PreRender();
    144 	Render(time);
    145 
    146 	memset( &refdef, 0, sizeof( refdef ) );
    147 	refdef.vieworg = viewOffset.ToVec3();;
    148 	//refdef.vieworg.Set(-128, 0, 0);
    149 
    150 	refdef.viewaxis.Identity();
    151 	refdef.shaderParms[0] = 1;
    152 	refdef.shaderParms[1] = 1;
    153 	refdef.shaderParms[2] = 1;
    154 	refdef.shaderParms[3] = 1;
    155 
    156 	refdef.fov_x = 90;
    157 	refdef.fov_y = 2 * atan((float)drawRect.h / drawRect.w) * idMath::M_RAD2DEG;
    158 
    159 	refdef.time[0] = time;
    160 	refdef.time[1] = time;
    161 	world->RenderScene(&refdef);
    162 }
    163 
    164 void idRenderWindow::PostParse() {
    165 	idWindow::PostParse();
    166 }
    167 
    168 // 
    169 //  
    170 idWinVar *idRenderWindow::GetWinVarByName(const char *_name, bool fixup, drawWin_t** owner ) {
    171 // 
    172 	if (idStr::Icmp(_name, "model") == 0) {
    173 		return &modelName;
    174 	}
    175 	if (idStr::Icmp(_name, "anim") == 0) {
    176 		return &animName;
    177 	}
    178 	if (idStr::Icmp(_name, "lightOrigin") == 0) {
    179 		return &lightOrigin;
    180 	}
    181 	if (idStr::Icmp(_name, "lightColor") == 0) {
    182 		return &lightColor;
    183 	}
    184 	if (idStr::Icmp(_name, "modelOrigin") == 0) {
    185 		return &modelOrigin;
    186 	}
    187 	if (idStr::Icmp(_name, "modelRotate") == 0) {
    188 		return &modelRotate;
    189 	}
    190 	if (idStr::Icmp(_name, "viewOffset") == 0) {
    191 		return &viewOffset;
    192 	}
    193 	if (idStr::Icmp(_name, "needsRender") == 0) {
    194 		return &needsRender;
    195 	}
    196 
    197 // 
    198 //  
    199 	return idWindow::GetWinVarByName(_name, fixup, owner);
    200 // 
    201 }
    202 
    203 bool idRenderWindow::ParseInternalVar(const char *_name, idTokenParser *src) {
    204 	if (idStr::Icmp(_name, "animClass") == 0) {
    205 		ParseString(src, animClass);
    206 		return true;
    207 	}
    208 	return idWindow::ParseInternalVar(_name, src);
    209 }