1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
<! Keep this here for posterity, but it will be removed as the first example>
<! in the tutorial>
<h3><a name="Render_Surface_example"></a>RenderSurface</h3>
The first example provides the means for creating a surface for 3D rendering.
In this
example, a small C++ class called MyGraphics will provide a simple
mechanism for initializing some OpenGL state and using OpenGL to render
the famous OpenGL teapot.<br>
<br>
<blockquote><tt></tt></blockquote>
<table cellpadding="0" cellspacing="0" border="0" width="100%"
bgcolor="#cccccc">
<tbody>
<tr>
<td valign="top" bgcolor="#cccccc">
<blockquote><font color="#33ff33"><tt><br>
</tt><font color="#33ff33"><tt><font size="+1"
color="#000000">//C++ source file - Open Producer - Copyright (C) 2002
Don Burns<font size="+1" color="#000000"><br>
//Distributed under the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE
(LGPL)<br>
//as published by the Free Software Foundation.<br>
<br>
// Simple example of use of Producer::RenderSurface<br>
// The MyGraphics class is a simple sample of how one would implement<br>
// graphics drawing with Producer::RenderSurface<br>
<br>
#include <Producer/RenderSurface><br>
#include "MyGraphics"<br>
<br>
int main(int argc, char **argv )<br>
{<br>
// Declare a RenderSurface<br>
Producer::RenderSurface rs;<br>
<br>
// Optional. Set the window size.<br>
// Arguments are x, y, width, height<br>
// If this call is not made, the RenderSurface<br>
// will occupy the entire screen with no border<br>
rs.setWindowRect( 100, 100, 640, 480 );<br>
// Give the window a name in the border<br>
rs.setWindowName( "Producer Example using Render
Surface" );<br>
<br>
// required. This creates the window and<br>
// sets up the graphics context.</tt></font><tt><font size="+1" color="#000000"> <font color="#000000" size="+1"><br>
rs.realize();<br>
<br>
// Add your own rendering code.<br>
MyGraphics gfx(rs);<br>
gfx.init();<br>
<br>
while( true )<br>
{<br>
// If possible,
RenderSurface will synchronize<br>
// the application run-time
to the vertical<br>
// retrace signal<br>
rs.sync();<br>
<br>
// Call your own draw routine<br>
gfx.draw(); <br>
<br>
// Swap Buffers<br>
rs.swapBuffers();<br>
}<br>
return 0;<br>
}<font size="+1" color="#000000"> </font></tt> <br>
</blockquote>
</td>
</tr>
</tbody>
</table>
<div align="center"><b><font face="Helvetica, Arial, sans-serif"
size="-1">Code example 1 - Simple Render Surface<br>
</b></div>
<h4>Explanation of the code</h4>
<font size="+1"><tt></tt><br>
<table cellpadding="0" cellspacing="0" border="0" width="100%"
bgcolor="#cccccc">
<tbody>
<tr>
<td valign="top" bgcolor="#cccccc">
<blockquote><font size="+1"><tt><br>
#include <Producer/RenderSurface></tt><br>
<tt>#include "MyGraphics"<br>
</tt><br>
</blockquote>
</td>
</tr>
</tbody>
</table>
<table cellpadding="0" cellspacing="0" border="0" width="100%"
bgcolor="#cccccc">
</table>
<blockquote><font size="+1"><br>
</blockquote>
Note that Producer uses the
convention of standard C++ by omitting the suffix of the header files.
Producer/RenderSurface is the include file for the RenderSurface
class. MyGraphics is a local class that is simply used for
example. It has little bearing on the functionality of
RenderSurface although it does access the width() and height() methods
of RenderSurface for determining the width and height of the window.
Two methods in MyGraphics are called in the example, init() which
initializes graphics state and draw() which draws the famous OpenGL
teapot.<br>
<br>
<table cellpadding="0" cellspacing="0" border="0" width="100%"
bgcolor="#cccccc">
<tbody>
<tr>
<td valign="top" bgcolor="#cccccc">
<blockquote>
<blockquote><font size="+1"><tt>int main(int argc, char **argv )</tt><br>
<font size="+1"><tt>{</tt><br>
<font size="+1"><tt> // Declare a
RenderSurface</tt><br>
<font size="+1"><tt>
Producer::RenderSurface rs;</tt><br>
</blockquote>
</blockquote>
</td>
</tr>
</tbody>
</table>
<br>
RendersSurface, like all Open
Producer classes is namespace protected by the Producer namespace.
Classess are allowed to be declared on the stack as demonstrated
in this example, but the usual care should be taken when doing this.<br>
<br>
<table cellpadding="0" cellspacing="0" border="0" width="100%"
bgcolor="#cccccc">
<tbody>
<tr>
<td valign="top" bgcolor="#cccccc">
<blockquote>
<blockquote><font size="+1"><tt>// Optional. Set the
window size</tt><br>
<font size="+1"><tt>// Arguments are x, y, width, height</tt><br>
<font size="+1"><tt>// If this call is not made, the
RenderSurface</tt><br>
<font size="+1"><tt>// will occupy the entire screen with no
border</tt><br>
<font size="+1"><tt>rs.setWindowRect( 100, 100, 640, 480 );</tt><br>
<br>
<font size="+1"><tt>// Give the window a name in the border</tt><font
size="+1"><tt><br>
rs.setWindowName( "Producer Example using Render Surface" );</tt><br>
</blockquote>
</blockquote>
</td>
</tr>
</tbody>
</table>
<br>
<br>
Two optional methods of <font
size="+1"><tt><b>RenderSurface</b></tt> <font
face="Helvetica, Arial, sans-serif">are invoked here. <b><font
size="+1"><tt> setWindowRect()</tt></b> <font
face="Helvetica, Arial, sans-serif">provides the screen <b>x, y</b> <font
face="Helvetica, Arial, sans-serif">coordinates of the lower left
corner of the window and its <b>width</b> <font
face="Helvetica, Arial, sans-serif">and <b>height</b> <font
face="Helvetica, Arial, sans-serif">respectively. <b><font
size="+1"><tt>setWindowName()</tt></b><font
face="Helvetica, Arial, sans-serif"> provides the window with a name
which will be displayed in the border. As the comments indicate,
if setWindowRect() is not called, the window will occupy the entire
screen, as as a borderless window. <br>
<table cellpadding="0" cellspacing="0" border="0" width="100%"
bgcolor="#cccccc">
<tbody>
<tr>
<td valign="top" bgcolor="#cccccc">
<blockquote>
<blockquote><tt><font size="+1">// required. This creates
the window and<br>
// sets up the graphics context.<br>
rs.realize();</tt><br>
</blockquote>
</blockquote>
</td>
</tr>
</tbody>
</table>
<br>
The <b><font size="+1"><tt>realize()</tt></font></b> <font
face="Helvetica, Arial, sans-serif">method creates the window and sets
up the OpenGL graphics rendering context. It is important to make
this call before any calls are made to OpenGL as there will be no valid
context before this. However, much of the configuration of the
RenderSurface is only available before it is realized. Once
realized certain configuration parameters (such as buffer quality
attributes) are not configurable. A complete list of which methods
are meaningful before realization and are ignored after realization is
given in the reference page. <br>
<table cellpadding="0" cellspacing="0" border="0" width="100%"
bgcolor="#cccccc">
<tbody>
<tr>
<td valign="top" bgcolor="#cccccc">
<blockquote>
<blockquote><font size="+1"><tt>// Add your own rendering code.</tt><br>
<font size="+1"><tt>MyGraphics gfx(rs);</tt><br>
<font size="+1"><tt>gfx.init();</tt><br>
</blockquote>
</blockquote>
</td>
</tr>
</tbody>
</table>
<br>
This is code that is added by
the user for graphics rendering and is here for example.<br>
<br>
<table cellpadding="0" cellspacing="0" border="0" width="100%"
bgcolor="#cccccc">
<tbody>
<tr>
<td valign="top" bgcolor="#cccccc">
<blockquote>
<blockquote>
<blockquote><font size="+1"><tt>while( true )</tt><br>
<font size="+1"><tt>{</tt><br>
<blockquote><font size="+1"><tt>// If possible,
RenderSurface will synchronize</tt><br>
<font size="+1"><tt>// the application run-time to the
vertical</tt><br>
<font size="+1"><tt>// retrace signal</tt><br>
<font size="+1"><tt>rs.sync();</tt><br>
<br>
<font size="+1"><tt>// Call your own draw routine</tt><br>
<font size="+1"><tt>gfx.draw();</tt><br>
<br>
<font size="+1"><tt>// Swap Buffers</tt><br>
<font size="+1"><tt>rs.swapBuffers();</tt><br>
</blockquote>
<font size="+1"><tt>}</tt><br>
</blockquote>
</blockquote>
</blockquote>
</td>
</tr>
</tbody>
</table>
<br>
Note the very important point
that the control of the main loop is given to the programmer.
There is no "mainLoop()" method in Producer, where the programmer
must relinquish control over the run time and wait for the system to
call his callbacks. It is therefore up to the programmer to
provide the main loop.<br>
<br>
The loop begins with a synchronization (rs.sync()). Some
implementations will provide a method of synchronizing the application
with the vertical retrace signal of the graphics subsystem. The
advantages of doing this in a real-time application are discussed in a
separate section. This is, however, optional and may be left out.
Without it, the application will race ahead of the graphics and
will be throttled only by a very low level FIFO. See
"Synchronizing your Application to the Graphics Subsystem".<br>
<br>
The user's implementation of graphics rendering occurs in gfx.draw().
<br>
<br>
All rendering in Producer will assume double frame buffers. The
call to swapBuffers will delimit the end of the graphics frame as far as
the application is concerned.<br>
|