]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - contrib/camera/renderer.cpp
reformat code! now the code is only ugly on the *inside*
[xonotic/netradiant.git] / contrib / camera / renderer.cpp
1 /*
2    Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3    For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 /*
23    Camera plugin for GtkRadiant
24    Copyright (C) 2002 Splash Damage Ltd.
25  */
26
27 #include "camera.h"
28
29 CRenderer::CRenderer()
30 {
31
32     refCount = 1;
33
34     m_bHooked = FALSE;
35
36     Register();
37     Initialize();
38 }
39
40 CRenderer::~CRenderer()
41 {
42     if (m_bHooked) {
43         UnRegister();
44     }
45 }
46
47 void CRenderer::Register()
48 {
49     g_QglTable.m_pfnHookGL2DWindow(this);
50     g_QglTable.m_pfnHookGL3DWindow(this);
51     m_bHooked = TRUE;
52 }
53
54 void CRenderer::UnRegister()
55 {
56     if (g_QglTable.m_nSize) {
57         g_QglTable.m_pfnUnHookGL2DWindow(this);
58         g_QglTable.m_pfnUnHookGL3DWindow(this);
59     }
60     m_bHooked = FALSE;
61 }
62
63 void CRenderer::Initialize()
64 {
65
66 }
67
68 void CRenderer::Draw2D(VIEWTYPE vt)
69 {
70
71     g_QglTable.m_pfn_qglPushAttrib(GL_ALL_ATTRIB_BITS);
72     g_QglTable.m_pfn_qglPushMatrix();
73
74     switch (vt) {
75         case XY:
76             break;
77         case XZ:
78             g_QglTable.m_pfn_qglRotatef(270.0f, 1.0f, 0.0f, 0.0f);
79             break;
80         case YZ:
81             g_QglTable.m_pfn_qglRotatef(270.0f, 1.0f, 0.0f, 0.0f);
82             g_QglTable.m_pfn_qglRotatef(270.0f, 0.0f, 0.0f, 1.0f);
83             break;
84     }
85
86     CCamera *cam = firstCam;
87     while (cam) {
88         cam->GetCam()->draw(((Listener && cam == g_pCurrentEditCam) ? true : false));
89         cam = cam->GetNext();
90     }
91
92     g_QglTable.m_pfn_qglPopMatrix();
93     g_QglTable.m_pfn_qglPopAttrib();
94 }
95
96 void CRenderer::Draw3D()
97 {
98     // FIXME: really need a mainloop callback from the editor core
99     static long start;
100     static float cycle;
101     static long msecs;
102     static long current;
103
104     if (g_iPreviewRunning) {
105         if (g_iPreviewRunning == 1) {
106             start = Q_QGetTickCount();
107             GetCurrentCam()->GetCam()->startCamera(start);
108             cycle = GetCurrentCam()->GetCam()->getTotalTime();
109             msecs = (long) (cycle * 1000);
110             current = start;
111             g_iPreviewRunning = 2;
112         }
113
114         if (current < start + msecs) {
115             float fov;
116             vec3_t origin = {0.0f, 0.0f, 0.0f}, dir = {0.0f, 0.0f, 0.0f}, angles;
117
118             GetCurrentCam()->GetCam()->getCameraInfo(current, &origin[0], &dir[0], &fov);
119             VectorSet(angles, asin(dir[2]) * 180 / 3.14159, atan2(dir[1], dir[0]) * 180 / 3.14159, 0);
120             g_CameraTable.m_pfnSetCamera(origin, angles);
121             current = Q_QGetTickCount();
122         } else {
123             g_iPreviewRunning = 0;
124             GetCurrentCam()->GetCam()->setRunning(false);
125             g_FuncTable.m_pfnSysUpdateWindows(W_XY_OVERLAY | W_CAMERA);
126         }
127     }
128
129     g_QglTable.m_pfn_qglPushAttrib(GL_ALL_ATTRIB_BITS);
130
131     CCamera *cam = firstCam;
132     while (cam) {
133         cam->GetCam()->draw(((Listener && cam == g_pCurrentEditCam) ? true : false));
134         cam = cam->GetNext();
135     }
136
137     if (g_iPreviewRunning) {
138         int x, y, width, height, i;
139         float degInRad;
140
141         g_CameraTable.m_pfnGetCamWindowExtents(&x, &y, &width, &height);
142
143         // setup orthographic projection mode
144         g_QglTable.m_pfn_qglMatrixMode(GL_PROJECTION);
145         g_QglTable.m_pfn_qglLoadIdentity();
146         g_QglTable.m_pfn_qglDisable(GL_DEPTH_TEST);
147         g_QglTable.m_pfn_qglOrtho(0, (float) width, 0, (float) height, -100, 100);
148         g_QglTable.m_pfn_qglMatrixMode(GL_MODELVIEW);
149
150         g_QglTable.m_pfn_qglLoadIdentity();
151         g_QglTable.m_pfn_qglColor3f(1.f, 1.f, 1.f);
152         g_QglTable.m_pfn_qglBegin(GL_LINE_LOOP);
153         g_QglTable.m_pfn_qglVertex2f(10, 10);
154         g_QglTable.m_pfn_qglVertex2f(40, 10);
155         g_QglTable.m_pfn_qglVertex2f(40, 25);
156         g_QglTable.m_pfn_qglVertex2f(10, 25);
157         g_QglTable.m_pfn_qglEnd();
158
159         g_QglTable.m_pfn_qglBegin(GL_LINE_LOOP);
160         for (i = 0; i < 360; i += 60) {
161             degInRad = i * (3.14159265358979323846 / 180.f);
162             g_QglTable.m_pfn_qglVertex2f(18 + cos(degInRad) * 5, 18 + sin(degInRad) * 5);
163         }
164         g_QglTable.m_pfn_qglEnd();
165
166         degInRad = (360 - ((current - start) % 360)) * (3.14159265358979323846 / 180.f);
167         g_QglTable.m_pfn_qglBegin(GL_LINES);
168         g_QglTable.m_pfn_qglVertex2f(18, 18);
169         g_QglTable.m_pfn_qglVertex2f(18 + cos(degInRad) * 5, 18 + sin(degInRad) * 5);
170         g_QglTable.m_pfn_qglVertex2f(32, 18);
171         g_QglTable.m_pfn_qglVertex2f(32 + cos(degInRad) * 5, 18 + sin(degInRad) * 5);
172         g_QglTable.m_pfn_qglEnd();
173
174         g_QglTable.m_pfn_qglBegin(GL_LINE_LOOP);
175         for (i = 0; i < 360; i += 60) {
176             degInRad = i * (3.14159265358979323846 / 180.f);
177             g_QglTable.m_pfn_qglVertex2f(32 + cos(degInRad) * 5, 18 + sin(degInRad) * 5);
178         }
179         g_QglTable.m_pfn_qglEnd();
180
181         g_QglTable.m_pfn_qglBegin(GL_LINES);
182         g_QglTable.m_pfn_qglVertex2f(40, 22);
183         g_QglTable.m_pfn_qglVertex2f(52, 31);
184         g_QglTable.m_pfn_qglVertex2f(40, 13);
185         g_QglTable.m_pfn_qglVertex2f(52, 4);
186         g_QglTable.m_pfn_qglEnd();
187     }
188
189     g_QglTable.m_pfn_qglPopAttrib();
190 }