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