]> icculus.org git repositories - icculus/iodoom3.git/blob - neo/ui/RenderWindow.cpp
hello world
[icculus/iodoom3.git] / neo / ui / RenderWindow.cpp
1 /*
2 ===========================================================================
3
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company. 
6
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).  
8
9 Doom 3 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 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 Source Code.  If not, see <http://www.gnu.org/licenses/>.
21
22 In addition, the Doom 3 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 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 #include "../idlib/precompiled.h"
30 #pragma hdrstop
31
32 #include "DeviceContext.h"
33 #include "Window.h"
34 #include "UserInterfaceLocal.h"
35 #include "RenderWindow.h"
36
37 idRenderWindow::idRenderWindow(idDeviceContext *d, idUserInterfaceLocal *g) : idWindow(d, g) {
38         dc = d;
39         gui = g;
40         CommonInit();
41 }
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( worldEntity.numJoints * sizeof( *worldEntity.joints ) );
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.x = drawRect.x;
157         refdef.y = drawRect.y;
158         refdef.width = drawRect.w;
159         refdef.height = drawRect.h;
160         refdef.fov_x = 90;
161         refdef.fov_y = 2 * atan((float)drawRect.h / drawRect.w) * idMath::M_RAD2DEG;
162
163         refdef.time = time;
164         world->RenderScene(&refdef);
165 }
166
167 void idRenderWindow::PostParse() {
168         idWindow::PostParse();
169 }
170
171 // 
172 //  
173 idWinVar *idRenderWindow::GetWinVarByName(const char *_name, bool fixup, drawWin_t** owner ) {
174 // 
175         if (idStr::Icmp(_name, "model") == 0) {
176                 return &modelName;
177         }
178         if (idStr::Icmp(_name, "anim") == 0) {
179                 return &animName;
180         }
181         if (idStr::Icmp(_name, "lightOrigin") == 0) {
182                 return &lightOrigin;
183         }
184         if (idStr::Icmp(_name, "lightColor") == 0) {
185                 return &lightColor;
186         }
187         if (idStr::Icmp(_name, "modelOrigin") == 0) {
188                 return &modelOrigin;
189         }
190         if (idStr::Icmp(_name, "modelRotate") == 0) {
191                 return &modelRotate;
192         }
193         if (idStr::Icmp(_name, "viewOffset") == 0) {
194                 return &viewOffset;
195         }
196         if (idStr::Icmp(_name, "needsRender") == 0) {
197                 return &needsRender;
198         }
199
200 // 
201 //  
202         return idWindow::GetWinVarByName(_name, fixup, owner);
203 // 
204 }
205
206 bool idRenderWindow::ParseInternalVar(const char *_name, idParser *src) {
207         if (idStr::Icmp(_name, "animClass") == 0) {
208                 ParseString(src, animClass);
209                 return true;
210         }
211         return idWindow::ParseInternalVar(_name, src);
212 }