annotate R/sdl.rnw @ 23:87fd9f34eaa0

- pre-C99 syntax fix
author cslag
date Sun, 22 May 2016 20:55:57 +0200
parents 0cfcc391201f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
1 % \VignetteIndexEntry{Multimedia programming with R, rdyncall, libSDL and OpenGL}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
2 % \VignetteKeyword{low-level}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
3 % \VignetteKeyword{rdyncall}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4 % \VignetteKeyword{dyncall}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5 % \VignetteKeyword{multimedia}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
6 \documentclass{article}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7 \usepackage{url}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8 \begin{document}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
9 \title{Multimedia programming with R, rdyncall, libSDL and OpenGL}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
10 \author{Daniel Adler}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
11 \maketitle
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12 \section{Introduction}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 Via the rdyncall package it is possible to write system-level
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15 software in R. In this vignette we focus on multimedia programming
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16 in R using the SDL and OpenGL shared libraries.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17 This vignette describes several issues involved in using the rdyncall
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18 package with C level interfaces of OpenGL and SDL. This text is
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19 not at all a reference to OpenGL or SDL. There are other much better
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20 text books and tutorials available.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22 See \url{http://www.libsdl.org} and \url{http://www.opengl.org} for further
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23 links to tutorials.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25 \section{Dynamic R bindings of shared libraries}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27 Initially, one needs to dynamically link SDL, OpenGL and OpenGL Utility
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28 Library to the R session.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29 This can be done using the rdyncall package and the dynports.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31 <<>>=
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32 library(rdyncall)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
33 dynport(SDL)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34 dynport(GL)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35 dynport(GLU)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
36 @
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
37
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
38 \section{Initialize SDL}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
39
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
40 We need to initialize the SDL library using the \verb@SDL_Init()@ function
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
41 giving a bitmask of subsystems to be enabled (such as Video, Audio, Joystick etc.).
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
42
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
43 <<>>=
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
44 subsystems <- SDL_INIT_VIDEO
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
45 SDL_Init(subsystems)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
46 @
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
47
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
48 The variable subsystem is a bitmask encoded in a double constant, also
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
49 the C API expects to have an unsigned int. rdyncall cares for a correct
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
50 coercion to unsigned int. By addition of several double constants,
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
51 we can initialize multiple subsystems.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
52
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
53 To see what subsystems SDL offers, we can explore the SDL dynport using e.g.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
54 the apropos base R command:
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
55
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
56 <<>>=
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
57 apropos("SDL_INIT_")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
58 @
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
59
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
60 \section{Opening an SDL/OpenGL Video Surface}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
61
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
62 Let us open a new OpenGL Display using \verb@SDL_SetVideoMode()@. The parameters are:
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
63
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
64 \begin{description}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
65 \item[width] The width in pixel.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
66 \item[height] The height in pixel
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
67 \item[bpp]
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
68 \item[flags] Bit flags to be combined with '+'. \verb@SDL_OPENGL@ and \verb@SDL_DOUBLEBUF@ are typical for OpenGL real-time applications.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
69 \end{description}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
70
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
71 Some useful Flags:
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
72
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
73 \begin{description}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
74 \item[SDL\_OPENGL] Request an OpenGL context to be initialized for this surfacce.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
75 \item[SDL\_DOUBLEBUF] Rendering into a back buffer and allow to flip the surfaces in one shot.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
76 \item[SDL\_FULLSCREEN] Put the display in fullscreen mode.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
77 \end{description}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
78
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
79 <<eval=FALSE>>=
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
80 width <- 640
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
81 height <- 480
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
82 bitsperpixel <- 32
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
83 flags <- SDL_OPENGL+SDL_DOUBLEBUF
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
84 surface <- SDL_SetVideoMode(width,height,bitsperpixel,flags)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
85 @
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
86
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
87 The surface object can be printed in R:
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
88
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
89 <<eval=FALSE>>=
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
90 print(surface)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
91 @
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
92
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
93 \section{Clear the screen and update}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
94
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
95 To clear the display with a constant color value, one needs to specifying the
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
96 \emph{clear color} first and then clear the \emph{color buffer}.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
97
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
98 <<eval=FALSE>>=
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
99 glClearColor(0.1,0.2,0.3,0.0)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
100 glClear(GL_COLOR_BUFFER_BIT)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
101 @
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
102
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
103 To see the results, we need to flip the surface - all operations in
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
104 \emph{double buffering} graphics mode are done in the back and will
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
105 be presented in one shot to prevent semi-finished drawing artefacts.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
106
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
107 <<eval=FALSE>>=
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
108 SDL_GL_SwapBuffers()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
109 @
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
110
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
111 \section{Writing the mainloop}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
112
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
113 This was interactive, but SDL and OpenGL are designed for writing
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
114 multimedia applications and we do this in R.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
115
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
116 These types of applications run a simple loop such as the interactive
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
117 loop of R (read-evaluate-print loop).
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
118 The loop will run until the application should quit. A reference play time
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
119 will be computed for each loop iteration using \verb@SDL_GetTicks()@ (divided by 1000
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
120 gives seconds).
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
121 We will clear our display using a blinking color which changes intensity from
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
122 black to a specified color. We drop the loop after 3 seconds.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
123
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
124 <<eval=FALSE>>=
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
125 blinkspeed <- 2.0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
126
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
127 draw <- function(t) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
128 intensity <- t %% blinkspeed / blinkspeed
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
129 glClearColor(0.8*intensity,0.6*intensity,1.0*intensity,0.0)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
130 glClear(GL_COLOR_BUFFER_BIT)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
131 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
132
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
133 mainloop <- function()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
134 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
135 quit <- FALSE
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
136
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
137 starttime <- SDL_GetTicks() / 1000
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
138
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
139 playtime <- 0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
140 while(!quit) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
141 # blink the screen using an intensity blending from 0 to 1
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
142
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
143 draw(playtime)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
144
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
145 SDL_GL_SwapBuffers()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
146
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
147 # update playtime:
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
148 now <- SDL_GetTicks() / 1000.0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
149 playtime <- now - starttime
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
150
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
151 # stop after three seconds
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
152 if (playtime > 3.0) quit <- TRUE
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
153 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
154 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
155 # run the loop
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
156 mainloop()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
157 @
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
158
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
159 \section{Rendering 3D graphics}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
160
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
161 To render a 3D scene, one specifies the 3D projection first, sets up the
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
162 camera position in space and then specifies the positions and primitive
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
163 geometries such as points, lines and triangles.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
164
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
165 First we specify the attributes of our virtual camera, that is
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
166 the field of view angle, the aspect ratio between width and height (which
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
167 should match the one of our surface) and the near and far z-plane orthogonal
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
168 to the camera center projection vector.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
169
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
170 <<eval=FALSE>>=
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
171 setupProjection <- function()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
172 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
173 glMatrixMode(GL_PROJECTION)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
174 glLoadIdentity()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
175 fovy <- 60.0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
176 aspect <- width / height
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
177 znear <- 10
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
178 zfar <- 100
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
179 gluPerspective(fovy, aspect, znear, zfar)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
180 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
181 @
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
182
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
183 Next, we need to setup the position of our camera in space using
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
184 the gluLookAt API which gets three vectors eye, center and up.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
185
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
186 <<eval=FALSE>>=
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
187 setupCamera <- function(eye=c(0,0,-2),center=c(0,0,0),up=c(0,1,0))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
188 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
189 glMatrixMode(GL_MODELVIEW)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
190 glLoadIdentity()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
191 gluLookAt(eye[[1]],eye[[2]],eye[[3]],center[[1]],center[[2]],center[[3]],up[[1]],up[[2]],up[[3]])
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
192 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
193 @
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
194
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
195 What follows is a short routine to draw a million 3D coordinates generated
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
196 by rnorm.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
197
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
198 <<eval=FALSE>>=
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
199 draw <- function()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
200 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
201 npoints <- 1000000
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
202 dims <- 3
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
203 data <- rnorm(npoints*dims)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
204 type <- GL_DOUBLE
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
205 stride <- 0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
206 glEnableClientState(GL_VERTEX_ARRAY_POINTER)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
207 glVertexPointer(dims,type,stride,data)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
208 stgrtindex <- 0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
209 glDrawArrays(GL_POINTS, startindex, npoints)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
210 glDisableClientState(GL_VERTEX_ARRAY_POINTER)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
211 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
212 @
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
213
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
214 \section{Handling User-Input Events}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
215
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
216 We will exchange the default behaviour of stopping after three seconds
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
217 with processing user-input events such as mouse, keyboard and joystick input
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
218 or pressing the close window button.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
219
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
220 SDL uses a C structure to report user-interface events. One calls
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
221 a function called \verb@SDL_PollEvent()@ given the reference of this structure.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
222
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
223 <<eval=FALSE>>=
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
224 processEvents <- function(env)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
225 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
226 evt <- new.struct(SDL_Event)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
227 quit <- FALSE
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
228 while(!quit) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
229 while( SDL_PollEvent(evt) ) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
230 type <- evt$type
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
231 if (type == SDL_QUIT) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
232 env$quit <- TRUE
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
233 } else if (type == SDL_MOUSEMOTION) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
234 motion <- evt$motion
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
235 cat(motion$xrel,",",motion$yrel,"\n")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
236 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
237 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
238 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
239 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
240 @
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
241
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
242 \end{document}
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
243
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
244