File: RenderSurfaceExample.html

package info (click to toggle)
openscenegraph 1.2.0-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 26,924 kB
  • ctags: 25,229
  • sloc: cpp: 239,326; ansic: 2,178; sh: 1,990; yacc: 548; perl: 237; makefile: 227; lex: 151
file content (275 lines) | stat: -rw-r--r-- 10,679 bytes parent folder | download | duplicates (2)
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. 
  &nbsp;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 &lt;Producer/RenderSurface&gt;<br>
#include "MyGraphics"<br>
        <br>
int main(int argc, char **argv )<br>
{<br>
&nbsp;&nbsp;&nbsp; // Declare a RenderSurface<br>
&nbsp;&nbsp;&nbsp; Producer::RenderSurface rs;<br>
        <br>
&nbsp;&nbsp;&nbsp; // Optional.&nbsp; Set the window size.<br>
&nbsp;&nbsp;&nbsp; // Arguments are x, y, width, height<br>
&nbsp;&nbsp;&nbsp; // If this call is not made, the RenderSurface<br>
&nbsp;&nbsp;&nbsp; // will occupy the entire screen with no border<br>
&nbsp;&nbsp;&nbsp; rs.setWindowRect( 100, 100, 640, 480 );<br>
&nbsp;&nbsp;&nbsp; // Give the window a name in the border<br>
&nbsp;&nbsp;&nbsp; rs.setWindowName( "Producer Example using Render
Surface" );<br>
        <br>
&nbsp;&nbsp;&nbsp; // required.&nbsp; This creates the window and<br>
&nbsp;&nbsp;&nbsp; // sets up the graphics context.</tt></font><tt><font size="+1" color="#000000"> <font color="#000000" size="+1"><br>
&nbsp;&nbsp;&nbsp; rs.realize();<br>
        <br>
&nbsp;&nbsp;&nbsp; // Add your own rendering code.<br>
&nbsp;&nbsp;&nbsp; MyGraphics gfx(rs);<br>
&nbsp;&nbsp;&nbsp; gfx.init();<br>
        <br>
&nbsp;&nbsp;&nbsp; while( true )<br>
&nbsp;&nbsp;&nbsp; {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // If possible,
RenderSurface will synchronize<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // the application run-time
to the vertical<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // retrace signal<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rs.sync();<br>
        <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Call your own draw routine<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gfx.draw(); <br>
        <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Swap Buffers<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rs.swapBuffers();<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; 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 &lt;Producer/RenderSurface&gt;</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.
&nbsp;Producer/RenderSurface is the include file for the RenderSurface
class. &nbsp;MyGraphics is a local class that is simply used for
example. &nbsp;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.
&nbsp; 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>&nbsp;&nbsp;&nbsp; // Declare a
RenderSurface</tt><br>
          <font size="+1"><tt>&nbsp;&nbsp;&nbsp;
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.
&nbsp;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.&nbsp; 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>&nbsp;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. &nbsp;<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. &nbsp;As the comments indicate,
if setWindowRect() is not called, the window will occupy the entire
screen, as as a borderless window. &nbsp;<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.&nbsp; 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. &nbsp;It is important to make
this call before any calls are made to OpenGL as there will be no valid
context before this. &nbsp;However, much of the configuration of the
RenderSurface is only available before it is realized. &nbsp;Once
realized certain configuration parameters (such as buffer quality
attributes) are not configurable. &nbsp;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.
&nbsp;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. &nbsp;It is therefore up to the programmer to
provide the main loop.<br>
<br>
The loop begins with a synchronization (rs.sync()). &nbsp;Some
implementations will provide a method of synchronizing the application
with the vertical retrace signal of the graphics subsystem. &nbsp;The
advantages of doing this in a real-time application are discussed in a
separate section. &nbsp;This is, however, optional and may be left out.
&nbsp;Without it, the application will race ahead of the graphics and
will be throttled only by a very low level FIFO. &nbsp;See
"Synchronizing your Application to the Graphics Subsystem".<br>
<br>
The user's implementation of graphics rendering occurs in gfx.draw().
&nbsp;<br>
<br>
All rendering in Producer will assume double frame buffers. &nbsp;The
call to swapBuffers will delimit the end of the graphics frame as far as
the application is concerned.<br>