annotate R/rdyncall/demo/SDL.R @ 9:8a45a05ff64e

- go binding cleanup, not enforcing relative paths anymore, readme update
author cslag
date Sat, 26 Mar 2016 01:09:51 +0100
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 # Package: rdyncall
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
2 # File: demo/SDL.R
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
3 # Description: 3D Rotating Cube Demo using SDL,OpenGL and GLU. (dynport demo)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5 dynport(SDL)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
6 dynport(GL)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7 dynport(GLU)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
9 # Globals.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
10
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
11 surface <- NULL
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13 # Init.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15 init <- function()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17 err <- SDL_Init(SDL_INIT_VIDEO)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18 if (err != 0) error("SDL_Init failed")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19 surface <<- SDL_SetVideoMode(512,512,32,SDL_DOUBLEBUF+SDL_OPENGL)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22 # GL Display Lists
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24 makeCubeDisplaylist <- function()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26 vertices <- as.double(c(
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27 -1,-1,-1,
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28 1,-1,-1,
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29 -1, 1,-1,
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30 1, 1,-1,
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31 -1,-1, 1,
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32 1,-1, 1,
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
33 -1, 1, 1,
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34 1, 1, 1
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35 ))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
36
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
37 colors <- as.raw( col2rgb( rainbow(8) ) )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
38
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
39 triangleIndices <- as.integer(c(
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
40 0, 2, 1,
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
41 2, 3, 1,
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
42 1, 3, 7,
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
43 1, 7, 5,
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
44 4, 5, 7,
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
45 4, 7, 6,
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
46 6, 2, 0,
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
47 6, 0, 4,
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
48 2, 7, 3,
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
49 2, 6, 7,
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
50 4, 0, 5,
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
51 0, 1, 5
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
52 ))
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
53
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
54 glEnableClientState(GL_VERTEX_ARRAY)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
55 glVertexPointer(3, GL_DOUBLE, 0, vertices )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
56
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
57 glEnableClientState(GL_COLOR_ARRAY)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
58 glColorPointer(3, GL_UNSIGNED_BYTE, 0, colors )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
59
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
60 displaylistId <- glGenLists(1)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
61 glNewList( displaylistId, GL_COMPILE )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
62 glPushAttrib(GL_ENABLE_BIT)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
63 glEnable(GL_DEPTH_TEST)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
64 glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, triangleIndices)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
65 glPopAttrib()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
66 glEndList()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
67
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
68 glDisableClientState(GL_VERTEX_ARRAY)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
69 glDisableClientState(GL_COLOR_ARRAY)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
70
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
71
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
72 return(displaylistId)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
73 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
74
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
75 # Mainloop.
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
76
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
77 mainloop <- function()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
78 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
79 displaylistId <- makeCubeDisplaylist()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
80 evt <- new.struct(SDL_Event)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
81 blink <- 0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
82 tbase <- SDL_GetTicks()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
83 quit <- FALSE
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
84 while(!quit)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
85 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
86 tnow <- SDL_GetTicks()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
87 tdemo <- ( tnow - tbase ) / 1000
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
88
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
89 glClearColor(0,0,blink,0)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
90 glClear(GL_COLOR_BUFFER_BIT+GL_DEPTH_BUFFER_BIT)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
91
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
92 glMatrixMode(GL_PROJECTION)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
93 glLoadIdentity()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
94 aspect <- 512/512
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
95 gluPerspective(60, aspect, 3, 1000)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
96
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
97 glMatrixMode(GL_MODELVIEW)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
98 glLoadIdentity()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
99 gluLookAt(0,0,5,0,0,0,0,1,0)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
100 glRotated(sin(tdemo)*60.0, 0, 1, 0);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
101 glRotated(cos(tdemo)*90.0, 1, 0, 0);
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
102
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
103 glCallList(displaylistId)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
104
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
105 glCallList(displaylistId)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
106
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
107 SDL_GL_SwapBuffers()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
108
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
109 SDL_WM_SetCaption(paste("time:", tdemo),NULL)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
110 blink <- blink + 0.01
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
111 while (blink > 1) blink <- blink - 1
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
112 while( SDL_PollEvent(evt) != 0 )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
113 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
114 if ( evt$type == SDL_QUIT ) quit <- TRUE
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
115 else if (evt$type == SDL_MOUSEBUTTONDOWN )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
116 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
117 button <- evt$button
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
118 cat("button ",button$button," at ",button$x,",",button$y,"\n")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
119 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
120 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
121 glerr <- glGetError()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
122 if (glerr != 0)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
123 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
124 cat("GL Error:", gluErrorString(glerr) )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
125 quit <- 1
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
126 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
127 SDL_Delay(30)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
128 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
129 glDeleteLists(displaylistId, 1)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
130 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
131
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
132 cleanup <- function()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
133 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
134 SDL_Quit()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
135 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
136
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
137 run <- function()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
138 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
139 init()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
140 mainloop()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
141 cleanup()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
142 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
143
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
144 run()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
145
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
146