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 
CRenderer()29 CRenderer::CRenderer() {
30 
31 	refCount = 1;
32 
33 	m_bHooked = FALSE;
34 
35 	Register();
36 	Initialize();
37 }
38 
~CRenderer()39 CRenderer::~CRenderer() {
40 	if ( m_bHooked ) {
41 		UnRegister();
42 	}
43 }
44 
Register()45 void CRenderer::Register() {
46 	g_QglTable.m_pfnHookGL2DWindow( this );
47 	g_QglTable.m_pfnHookGL3DWindow( this );
48 	m_bHooked = TRUE;
49 }
50 
UnRegister()51 void CRenderer::UnRegister() {
52 	if ( g_QglTable.m_nSize ) {
53 		g_QglTable.m_pfnUnHookGL2DWindow( this );
54 		g_QglTable.m_pfnUnHookGL3DWindow( this );
55 	}
56 	m_bHooked = FALSE;
57 }
58 
Initialize()59 void CRenderer::Initialize() {
60 
61 }
62 
Draw2D(VIEWTYPE vt)63 void CRenderer::Draw2D( VIEWTYPE vt ) {
64 
65 	g_QglTable.m_pfn_qglPushAttrib( GL_ALL_ATTRIB_BITS );
66 	g_QglTable.m_pfn_qglPushMatrix();
67 
68 	switch ( vt )
69 	{
70 	case XY:
71 		break;
72 	case XZ:
73 		g_QglTable.m_pfn_qglRotatef( 270.0f, 1.0f, 0.0f, 0.0f );
74 		break;
75 	case YZ:
76 		g_QglTable.m_pfn_qglRotatef( 270.0f, 1.0f, 0.0f, 0.0f );
77 		g_QglTable.m_pfn_qglRotatef( 270.0f, 0.0f, 0.0f, 1.0f );
78 		break;
79 	}
80 
81 	CCamera *cam = firstCam;
82 	while ( cam ) {
83 		cam->GetCam()->draw( ( ( Listener && cam == g_pCurrentEditCam ) ? true : false ) );
84 		cam = cam->GetNext();
85 	}
86 
87 	g_QglTable.m_pfn_qglPopMatrix();
88 	g_QglTable.m_pfn_qglPopAttrib();
89 }
90 
Draw3D()91 void CRenderer::Draw3D() {
92 	// FIXME: really need a mainloop callback from the editor core
93 	static long start;
94 	static float cycle;
95 	static long msecs;
96 	static long current;
97 
98 	if ( g_iPreviewRunning ) {
99 		if ( g_iPreviewRunning == 1 ) {
100 			start = Q_QGetTickCount();
101 			GetCurrentCam()->GetCam()->startCamera( start );
102 			cycle = GetCurrentCam()->GetCam()->getTotalTime();
103 			msecs = (long)( cycle * 1000 );
104 			current = start;
105 			g_iPreviewRunning = 2;
106 		}
107 
108 		if ( current < start + msecs ) {
109 			float fov;
110 			vec3_t origin = {0.0f, 0.0f, 0.0f}, dir = {0.0f, 0.0f, 0.0f}, angles;
111 
112 			GetCurrentCam()->GetCam()->getCameraInfo( current, &origin[0], &dir[0], &fov );
113 			VectorSet( angles, asin( dir[2] ) * 180 / 3.14159, atan2( dir[1], dir[0] ) * 180 / 3.14159, 0 );
114 			g_CameraTable.m_pfnSetCamera( origin, angles );
115 			current = Q_QGetTickCount();
116 		}
117 		else {
118 			g_iPreviewRunning = 0;
119 			GetCurrentCam()->GetCam()->setRunning( false );
120 			g_FuncTable.m_pfnSysUpdateWindows( W_XY_OVERLAY | W_CAMERA );
121 		}
122 	}
123 
124 	g_QglTable.m_pfn_qglPushAttrib( GL_ALL_ATTRIB_BITS );
125 
126 	CCamera *cam = firstCam;
127 	while ( cam ) {
128 		cam->GetCam()->draw( ( ( Listener && cam == g_pCurrentEditCam ) ? true : false ) );
129 		cam = cam->GetNext();
130 	}
131 
132 	if ( g_iPreviewRunning ) {
133 		int x, y, width, height, i;
134 		float degInRad;
135 
136 		g_CameraTable.m_pfnGetCamWindowExtents( &x, &y, &width, &height );
137 
138 		// setup orthographic projection mode
139 		g_QglTable.m_pfn_qglMatrixMode( GL_PROJECTION );
140 		g_QglTable.m_pfn_qglLoadIdentity();
141 		g_QglTable.m_pfn_qglDisable( GL_DEPTH_TEST );
142 		g_QglTable.m_pfn_qglOrtho( 0, (float)width, 0, (float)height, -100, 100 );
143 		g_QglTable.m_pfn_qglMatrixMode( GL_MODELVIEW );
144 
145 		g_QglTable.m_pfn_qglLoadIdentity();
146 		g_QglTable.m_pfn_qglColor3f( 1.f, 1.f, 1.f );
147 		g_QglTable.m_pfn_qglBegin( GL_LINE_LOOP );
148 		g_QglTable.m_pfn_qglVertex2f( 10, 10 );
149 		g_QglTable.m_pfn_qglVertex2f( 40, 10 );
150 		g_QglTable.m_pfn_qglVertex2f( 40, 25 );
151 		g_QglTable.m_pfn_qglVertex2f( 10, 25 );
152 		g_QglTable.m_pfn_qglEnd();
153 
154 		g_QglTable.m_pfn_qglBegin( GL_LINE_LOOP );
155 		for ( i = 0; i < 360; i += 60 ) {
156 			degInRad = i * ( 3.14159265358979323846 / 180.f );
157 			g_QglTable.m_pfn_qglVertex2f( 18 + cos( degInRad ) * 5, 18 + sin( degInRad ) * 5 );
158 		}
159 		g_QglTable.m_pfn_qglEnd();
160 
161 		degInRad = ( 360 - ( ( current - start ) % 360 ) ) * ( 3.14159265358979323846 / 180.f );
162 		g_QglTable.m_pfn_qglBegin( GL_LINES );
163 		g_QglTable.m_pfn_qglVertex2f( 18, 18 );
164 		g_QglTable.m_pfn_qglVertex2f( 18 + cos( degInRad ) * 5, 18 + sin( degInRad ) * 5 );
165 		g_QglTable.m_pfn_qglVertex2f( 32, 18 );
166 		g_QglTable.m_pfn_qglVertex2f( 32 + cos( degInRad ) * 5, 18 + sin( degInRad ) * 5 );
167 		g_QglTable.m_pfn_qglEnd();
168 
169 		g_QglTable.m_pfn_qglBegin( GL_LINE_LOOP );
170 		for ( i = 0; i < 360; i += 60 ) {
171 			degInRad = i * ( 3.14159265358979323846 / 180.f );
172 			g_QglTable.m_pfn_qglVertex2f( 32 + cos( degInRad ) * 5, 18 + sin( degInRad ) * 5 );
173 		}
174 		g_QglTable.m_pfn_qglEnd();
175 
176 		g_QglTable.m_pfn_qglBegin( GL_LINES );
177 		g_QglTable.m_pfn_qglVertex2f( 40, 22 );
178 		g_QglTable.m_pfn_qglVertex2f( 52, 31 );
179 		g_QglTable.m_pfn_qglVertex2f( 40, 13 );
180 		g_QglTable.m_pfn_qglVertex2f( 52, 4 );
181 		g_QglTable.m_pfn_qglEnd();
182 	}
183 
184 	g_QglTable.m_pfn_qglPopAttrib();
185 }
186