1 /*  NAME:
2         WFRenderer.c
3 
4     DESCRIPTION:
5         Quesa wireframe renderer.
6 
7     COPYRIGHT:
8         Copyright (c) 1999-2004, Quesa Developers. All rights reserved.
9 
10         For the current release of Quesa, please see:
11 
12             <http://www.quesa.org/>
13 
14         Redistribution and use in source and binary forms, with or without
15         modification, are permitted provided that the following conditions
16         are met:
17 
18             o Redistributions of source code must retain the above copyright
19               notice, this list of conditions and the following disclaimer.
20 
21             o Redistributions in binary form must reproduce the above
22               copyright notice, this list of conditions and the following
23               disclaimer in the documentation and/or other materials provided
24               with the distribution.
25 
26             o Neither the name of Quesa nor the names of its contributors
27               may be used to endorse or promote products derived from this
28               software without specific prior written permission.
29 
30         THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31         "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32         LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33         A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34         OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35         SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
36         TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
37         PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
38         LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
39         NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40         SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41     ___________________________________________________________________________
42 */
43 //=============================================================================
44 //      Include files
45 //-----------------------------------------------------------------------------
46 #include "WFPrefix.h"
47 #include "WFRenderer.h"
48 
49 #include "GLPrefix.h"
50 #include "GLDrawContext.h"
51 
52 
53 
54 
55 
56 //=============================================================================
57 //      Public functions
58 //-----------------------------------------------------------------------------
59 //      WFRenderer_StartFrame : Start a frame.
60 //-----------------------------------------------------------------------------
61 TQ3Status
WFRenderer_StartFrame(TQ3ViewObject theView,TQ3WireframeData * instanceData,TQ3DrawContextObject theDrawContext)62 WFRenderer_StartFrame(TQ3ViewObject				theView,
63 						TQ3WireframeData		*instanceData,
64 						TQ3DrawContextObject	theDrawContext)
65 {	TQ3XDrawContextValidation		drawContextFlags;
66 	TQ3Status						qd3dStatus;
67 	TQ3ColorRGB						lineColour;
68 	TQ3ColorARGB					theColour;
69 	float							lineLum;
70 #pragma unused(theView)
71 
72 
73 
74 	// If the draw context has changed, update ourselves
75 	qd3dStatus = Q3XDrawContext_GetValidationFlags(theDrawContext, &drawContextFlags);
76 	if (qd3dStatus == kQ3Success && drawContextFlags != kQ3XDrawContextValidationClearFlags)
77 		{
78 		// If we don't have a draw context, rebuild everything
79 		if (instanceData->glContext == NULL)
80 			drawContextFlags = kQ3XDrawContextValidationAll;
81 
82 
83         // Otherwise, make sure it's active (in case we can re-use it)
84         else
85             GLDrawContext_SetCurrent(instanceData->glContext, kQ3True);
86 
87 
88 
89 		// Handle some common cases
90 		if (drawContextFlags != kQ3XDrawContextValidationAll)
91 			{
92 			if (drawContextFlags & kQ3XDrawContextValidationClearFunction)
93 				{
94 				GLDrawContext_SetClearFlags(theDrawContext, &instanceData->glClearFlags);
95 				drawContextFlags &= ~kQ3XDrawContextValidationClearFunction;
96 				}
97 
98 			if (drawContextFlags & kQ3XDrawContextValidationBackgroundShader)
99 				{
100 				GLDrawContext_SetBackgroundColour(theDrawContext);
101 				drawContextFlags &= ~kQ3XDrawContextValidationBackgroundShader;
102 				}
103 
104 			if (drawContextFlags & kQ3XDrawContextValidationWindowClip)
105 				{
106 				if (GLDrawContext_UpdateWindowClip(instanceData->glContext))
107 					drawContextFlags &= ~kQ3XDrawContextValidationWindowClip;
108 				}
109 
110 			if (drawContextFlags & kQ3XDrawContextValidationWindowPosition)
111 				{
112 				if (GLDrawContext_UpdateWindowPosition(instanceData->glContext))
113 					drawContextFlags &= ~kQ3XDrawContextValidationWindowPosition;
114 				}
115 
116 			if (drawContextFlags & (kQ3XDrawContextValidationWindowSize |
117 				kQ3XDrawContextValidationPane))
118 				{
119 				if (GLDrawContext_UpdateSize(theDrawContext, instanceData->glContext) == kQ3Success)
120 					{
121 					drawContextFlags &= ~kQ3XDrawContextValidationWindowSize;
122 					drawContextFlags &= ~kQ3XDrawContextValidationPane;
123 					}
124 				}
125 			}
126 
127 
128 
129 		// Handle more complex cases by doing a rebuild
130 		if (drawContextFlags != kQ3XDrawContextValidationClearFlags)
131 			{
132 			// Dispose of the old GL context
133 			if (instanceData->glContext != NULL)
134 				GLDrawContext_Destroy(&instanceData->glContext);
135 
136 
137 			// And try and build a new one
138 			instanceData->glContext = GLDrawContext_New(theView, theDrawContext, &instanceData->glClearFlags);
139 			if (instanceData->glContext == NULL)
140 				return(kQ3Failure);
141 			}
142 
143 
144 
145 		// Set up the default line colour
146 		Q3ColorRGB_Set(&instanceData->qd3dLineColour, 0.0f, 0.0f, 0.0f);
147 		if (instanceData->glClearFlags && GL_COLOR_BUFFER_BIT)
148 			{
149 			Q3DrawContext_GetClearImageColor(theDrawContext, &theColour);
150 			lineColour.r = theColour.r;
151 			lineColour.g = theColour.g;
152 			lineColour.b = theColour.b;
153 			Q3ColorRGB_Luminance(&lineColour, &lineLum);
154 
155 			if (lineLum < 0.5f)
156 				Q3ColorRGB_Set(&instanceData->qd3dLineColour, 1.0f, 1.0f, 1.0f);
157 			}
158 
159 
160 
161 		// Clear the draw context flags
162 		Q3XDrawContext_ClearValidationFlags(theDrawContext);
163 		}
164 
165 
166 
167 	// Activate our context (forcing it to be set at least once per frame)
168 	GLDrawContext_SetCurrent(instanceData->glContext, kQ3True);
169 
170 
171 
172 	// Clear the context
173 	glClear(instanceData->glClearFlags);
174 
175 	return(kQ3Success);
176 }
177 
178 
179 
180 
181 
182 //=============================================================================
183 //      WFRenderer_EndFrame : End a frame.
184 //-----------------------------------------------------------------------------
185 TQ3Status
WFRenderer_EndFrame(TQ3ViewObject theView,TQ3WireframeData * instanceData,TQ3DrawContextObject theDrawContext)186 WFRenderer_EndFrame(TQ3ViewObject			theView,
187 					TQ3WireframeData		*instanceData,
188 					TQ3DrawContextObject	theDrawContext)
189 {
190 #pragma unused( theView, theDrawContext )
191 	TQ3Status		qd3dStatus;
192 
193 
194 
195 	// Activate our context
196 	GLDrawContext_SetCurrent(instanceData->glContext, kQ3False);
197 
198 
199 
200 	// Swap the back buffer, and block until it's complete
201 	GLDrawContext_SwapBuffers(instanceData->glContext);
202 	glFinish();
203 
204 
205 
206 	// Let the view know that we're done
207 	qd3dStatus = Q3XView_EndFrame(theView);
208 
209 	return(qd3dStatus);
210 }
211 
212 
213 
214 
215 
216 //=============================================================================
217 //      WFRenderer_StartPass : Start a pass.
218 //-----------------------------------------------------------------------------
219 TQ3Status
WFRenderer_StartPass(TQ3ViewObject theView,TQ3WireframeData * instanceData,TQ3CameraObject theCamera,TQ3GroupObject theLights)220 WFRenderer_StartPass(TQ3ViewObject			theView,
221 						TQ3WireframeData	*instanceData,
222 						TQ3CameraObject		theCamera,
223 						TQ3GroupObject		theLights)
224 {
225 #pragma unused(theView)
226 #pragma unused(instanceData)
227 #pragma unused(theCamera)
228 #pragma unused(theLights)
229 
230 	glEnableClientState(GL_VERTEX_ARRAY);
231 
232 	return(kQ3Success);
233 }
234 
235 
236 
237 
238 
239 //=============================================================================
240 //      WFRenderer_EndPass : End a pass.
241 //-----------------------------------------------------------------------------
242 TQ3ViewStatus
WFRenderer_EndPass(TQ3ViewObject theView,TQ3WireframeData * instanceData)243 WFRenderer_EndPass(TQ3ViewObject theView, TQ3WireframeData *instanceData)
244 {
245 	TQ3DrawContextObject	theDrawContext;
246 	TQ3Boolean				swapFlag;
247 
248 
249 
250 	// Activate our context
251 	GLDrawContext_SetCurrent(instanceData->glContext, kQ3False);
252 
253 
254 
255 	// Swap the back buffer
256 	Q3View_GetDrawContext( theView, &theDrawContext );
257 	if ( (kQ3Failure == Q3Object_GetProperty( theDrawContext,
258 		kQ3DrawContextPropertySwapBufferInEndPass, sizeof(swapFlag), NULL,
259 		&swapFlag )) ||
260 		(swapFlag == kQ3True) )
261 	{
262 		GLDrawContext_SwapBuffers(instanceData->glContext);
263 	}
264 	Q3Object_Dispose( theDrawContext );
265 
266 
267 
268 	return(kQ3ViewStatusDone);
269 }
270 
271 
272 
273 
274 
275 //=============================================================================
276 //      WFRenderer_Cancel: Cancel a pass.
277 //-----------------------------------------------------------------------------
278 void
WFRenderer_Cancel(TQ3ViewObject theView,TQ3WireframeData * instanceData)279 WFRenderer_Cancel(TQ3ViewObject theView, TQ3WireframeData *instanceData)
280 {
281 #pragma unused(theView)
282 #pragma unused(instanceData)
283 }
284