@@ -32,6 +32,36 @@ var Indices: [GLubyte] = [
32
32
]
33
33
34
34
35
+ //helper extensions to pass arguments to GL land
36
+ extension Array {
37
+ func size ( ) -> Int {
38
+ return self . count * sizeofValue( self [ 0 ] )
39
+ }
40
+ }
41
+
42
+ extension Int32 {
43
+ func __conversion( ) -> GLenum {
44
+ return GLuint ( self )
45
+ }
46
+
47
+ func __conversion( ) -> GLboolean {
48
+ return GLboolean ( UInt8 ( self ) )
49
+ }
50
+ }
51
+
52
+ extension Int {
53
+ func __conversion( ) -> Int32 {
54
+ return Int32 ( self )
55
+ }
56
+
57
+ func __conversion( ) -> GLubyte {
58
+ return GLubyte ( self )
59
+ }
60
+
61
+ }
62
+
63
+
64
+
35
65
class OpenGLView : UIView {
36
66
37
67
var eaglLayer : CAEAGLLayer !
@@ -57,7 +87,7 @@ class OpenGLView: UIView {
57
87
/* Lifecycle
58
88
------------------------------------------*/
59
89
60
- init ( coder aDecoder: NSCoder ! ) {
90
+ required init ( coder aDecoder: NSCoder ) {
61
91
super. init ( coder: aDecoder)
62
92
63
93
self . setupLayer ( )
@@ -77,7 +107,7 @@ class OpenGLView: UIView {
77
107
func setupLayer( ) {
78
108
// CALayer's are, by default, non-opaque, which is 'bad for performance with OpenGL',
79
109
// so let's set our CAEAGLLayer layer to be opaque.
80
- self . eaglLayer = self . layer as CAEAGLLayer
110
+ self . eaglLayer = self . layer as! CAEAGLLayer
81
111
self . eaglLayer. opaque = true
82
112
}
83
113
@@ -88,7 +118,7 @@ class OpenGLView: UIView {
88
118
var api : EAGLRenderingAPI = EAGLRenderingAPI . OpenGLES2
89
119
self . context = EAGLContext ( API: api)
90
120
91
- if ( ! self . context) {
121
+ if ( self . context == nil ) {
92
122
println ( " Failed to initialize OpenGLES 2.0 context! " )
93
123
exit ( 1 )
94
124
}
@@ -101,41 +131,44 @@ class OpenGLView: UIView {
101
131
102
132
func setupRenderBuffer( ) {
103
133
glGenRenderbuffers ( 1 , & self . colorRenderBuffer)
104
- glBindRenderbuffer ( GL_RENDERBUFFER, self . colorRenderBuffer)
134
+ glBindRenderbuffer ( GLenum ( GL_RENDERBUFFER) , self . colorRenderBuffer)
105
135
self . context. renderbufferStorage ( Int ( GL_RENDERBUFFER) , fromDrawable: self . eaglLayer)
106
136
}
107
137
108
138
func setupFrameBuffer( ) {
109
139
var frameBuffer : GLuint = GLuint ( )
110
140
glGenFramebuffers ( 1 , & frameBuffer)
111
- glBindFramebuffer ( GL_FRAMEBUFFER . asUnsigned ( ) , frameBuffer)
112
- glFramebufferRenderbuffer ( GL_FRAMEBUFFER . asUnsigned ( ) , GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, self . colorRenderBuffer)
141
+ glBindFramebuffer ( GLenum ( GL_FRAMEBUFFER ) , frameBuffer)
142
+ glFramebufferRenderbuffer ( GLenum ( GL_FRAMEBUFFER ) , GLenum ( GL_COLOR_ATTACHMENT0) , GLenum ( GL_RENDERBUFFER) , self . colorRenderBuffer)
113
143
}
114
144
115
- func compileShader( shaderName: NSString , shaderType: GLenum ) -> GLuint {
145
+ func compileShader( shaderName: String , shaderType: GLenum ) -> GLuint {
116
146
117
147
// Get NSString with contents of our shader file.
118
- var shaderPath : NSString = NSBundle . mainBundle ( ) . pathForResource ( shaderName, ofType: " glsl " )
148
+ var shaderPath : String ! = NSBundle . mainBundle ( ) . pathForResource ( shaderName, ofType: " glsl " )
119
149
var error : NSError ? = nil
120
- var shaderString = NSString . stringWithContentsOfFile ( shaderPath, encoding: NSUTF8StringEncoding, error: & error)
121
- if ( ! shaderString) {
150
+ var shaderString = NSString ( contentsOfFile : shaderPath, encoding: NSUTF8StringEncoding, error: & error)
151
+ if ( shaderString == nil ) {
122
152
println ( " Failed to set contents shader of shader file! " )
123
153
}
124
154
125
155
// Tell OpenGL to create an OpenGL object to represent the shader, indicating if it's a vertex or a fragment shader.
126
156
var shaderHandle : GLuint = glCreateShader ( shaderType)
127
157
158
+ if shaderHandle == 0 {
159
+ NSLog ( " Couldn't create shader " )
160
+ }
128
161
// Conver shader string to CString and call glShaderSource to give OpenGL the source for the shader.
129
- var shaderStringUTF8 : CString = shaderString!. UTF8String
130
- var shaderStringLength : GLint = GLint . convertFromIntegerLiteral ( Int32 ( shaderString!. length) )
162
+ var shaderStringUTF8 = shaderString!. UTF8String
163
+ var shaderStringLength : GLint = GLint ( Int32 ( shaderString!. length) )
131
164
glShaderSource ( shaderHandle, 1 , & shaderStringUTF8, & shaderStringLength)
132
165
133
166
// Tell OpenGL to compile the shader.
134
167
glCompileShader ( shaderHandle)
135
168
136
169
// But compiling can fail! If we have errors in our GLSL code, we can here and output any errors.
137
170
var compileSuccess : GLint = GLint ( )
138
- glGetShaderiv ( shaderHandle, GL_COMPILE_STATUS, & compileSuccess)
171
+ glGetShaderiv ( shaderHandle, GLenum ( GL_COMPILE_STATUS) , & compileSuccess)
139
172
if ( compileSuccess == GL_FALSE) {
140
173
println ( " Failed to compile shader! " )
141
174
// TODO: Actually output the error that we can get from the glGetShaderInfoLog function.
@@ -148,8 +181,8 @@ class OpenGLView: UIView {
148
181
func compileShaders( ) {
149
182
150
183
// Compile our vertex and fragment shaders.
151
- var vertexShader : GLuint = self . compileShader ( " SimpleVertex " , shaderType: GL_VERTEX_SHADER)
152
- var fragmentShader : GLuint = self . compileShader ( " SimpleFragment " , shaderType: GL_FRAGMENT_SHADER)
184
+ var vertexShader : GLuint = self . compileShader ( " SimpleVertex " , shaderType: GLenum ( GL_VERTEX_SHADER) )
185
+ var fragmentShader : GLuint = self . compileShader ( " SimpleFragment " , shaderType: GLenum ( GL_FRAGMENT_SHADER) )
153
186
154
187
// Call glCreateProgram, glAttachShader, and glLinkProgram to link the vertex and fragment shaders into a complete program.
155
188
var programHandle : GLuint = glCreateProgram ( )
@@ -159,7 +192,7 @@ class OpenGLView: UIView {
159
192
160
193
// Check for any errors.
161
194
var linkSuccess : GLint = GLint ( )
162
- glGetProgramiv ( programHandle, GL_LINK_STATUS, & linkSuccess)
195
+ glGetProgramiv ( programHandle, GLenum ( GL_LINK_STATUS) , & linkSuccess)
163
196
if ( linkSuccess == GL_FALSE) {
164
197
println ( " Failed to create shader program! " )
165
198
// TODO: Actually output the error that we can get from the glGetProgramInfoLog function.
@@ -171,8 +204,8 @@ class OpenGLView: UIView {
171
204
172
205
// Finally, call glGetAttribLocation to get a pointer to the input values for the vertex shader, so we
173
206
// can set them in code. Also call glEnableVertexAttribArray to enable use of these arrays (they are disabled by default).
174
- self . positionSlot = glGetAttribLocation ( programHandle, " Position " )
175
- self . colorSlot = glGetAttribLocation ( programHandle, " SourceColor " )
207
+ self . positionSlot = GLuint ( glGetAttribLocation ( programHandle, " Position " ) )
208
+ self . colorSlot = GLuint ( glGetAttribLocation ( programHandle, " SourceColor " ) )
176
209
glEnableVertexAttribArray ( self . positionSlot)
177
210
glEnableVertexAttribArray ( self . colorSlot)
178
211
}
@@ -184,64 +217,36 @@ class OpenGLView: UIView {
184
217
glBindVertexArrayOES ( VAO) ;
185
218
186
219
glGenBuffers ( 1 , & vertexBuffer)
187
- glBindBuffer ( GL_ARRAY_BUFFER, vertexBuffer)
188
- glBufferData ( GL_ARRAY_BUFFER, Vertices . size ( ) , Vertices, GL_STATIC_DRAW)
220
+ glBindBuffer ( GLenum ( GL_ARRAY_BUFFER) , vertexBuffer)
221
+ glBufferData ( GLenum ( GL_ARRAY_BUFFER) , Vertices . size ( ) , Vertices, GLenum ( GL_STATIC_DRAW) )
189
222
190
- let positionSlotFirstComponent = ConstUnsafePointer < Int > ( 0 )
223
+ // let positionSlotFirstComponent : UnsafePointer <Int>(& 0)
191
224
glEnableVertexAttribArray ( positionSlot)
192
- glVertexAttribPointer ( positionSlot, 3 , GL_FLOAT, GL_FALSE, sizeof ( Vertex) , positionSlotFirstComponent )
225
+ glVertexAttribPointer ( positionSlot, 3 , GLenum ( GL_FLOAT) , GLboolean ( UInt8 ( GL_FALSE) ) , GLsizei ( sizeof ( Vertex) ) , nil )
193
226
194
227
glEnableVertexAttribArray ( colorSlot)
195
- let colorSlotFirstComponent = ConstUnsafePointer < Int > ( sizeof ( Float) * 3 )
196
- glVertexAttribPointer ( colorSlot, 4 , GL_FLOAT, GL_FALSE, sizeof ( Vertex) , colorSlotFirstComponent )
228
+ // let colorSlotFirstComponent = UnsafePointer <Int>(sizeof(Float) * 3)
229
+ glVertexAttribPointer ( colorSlot, 4 , GLenum ( GL_FLOAT) , GLboolean ( UInt8 ( GL_FALSE) ) , GLsizei ( sizeof ( Vertex) ) , nil )
197
230
198
231
glGenBuffers ( 1 , & indexBuffer)
199
- glBindBuffer ( GL_ELEMENT_ARRAY_BUFFER, indexBuffer)
200
- glBufferData ( GL_ELEMENT_ARRAY_BUFFER, Indices . size ( ) , Indices, GL_STATIC_DRAW)
232
+ glBindBuffer ( GLenum ( GL_ELEMENT_ARRAY_BUFFER) , indexBuffer)
233
+ glBufferData ( GLenum ( GL_ELEMENT_ARRAY_BUFFER) , Indices . size ( ) , Indices, GLenum ( GL_STATIC_DRAW) )
201
234
202
- glBindBuffer ( GL_ARRAY_BUFFER, 0 )
235
+ glBindBuffer ( GLenum ( GL_ARRAY_BUFFER) , 0 )
203
236
glBindVertexArrayOES ( 0 )
204
237
}
205
238
206
239
func render( ) {
207
240
glBindVertexArrayOES ( VAO) ;
208
241
glViewport ( 0 , 0 , GLint ( self . frame. size. width) , GLint ( self . frame. size. height) ) ;
209
242
210
- glDrawElements ( GL_TRIANGLES, Indices . count, GL_UNSIGNED_BYTE, nil )
243
+ glDrawElements ( GLenum ( GL_TRIANGLES) , GLsizei ( Indices . count) , GLenum ( GL_UNSIGNED_BYTE) , nil )
211
244
212
245
self . context. presentRenderbuffer ( Int ( GL_RENDERBUFFER) )
213
246
214
247
glBindVertexArrayOES ( 0 )
215
248
}
216
249
}
217
250
218
-
219
- //helper extensions to pass arguments to GL land
220
- extension Array {
221
- func size ( ) -> Int {
222
- return self . count * sizeofValue( self [ 0 ] )
223
- }
224
- }
225
-
226
- extension Int32 {
227
- func __conversion( ) -> GLenum {
228
- return GLuint ( self . asUnsigned ( ) )
229
- }
230
-
231
- func __conversion( ) -> GLboolean {
232
- return GLboolean . convertFromIntegerLiteral ( UInt8 ( self ) )
233
- }
234
- }
235
-
236
- extension Int {
237
- func __conversion( ) -> Int32 {
238
- return Int32 ( self )
239
- }
240
-
241
- func __conversion( ) -> GLubyte {
242
- return GLubyte ( self )
243
- }
244
-
245
- }
246
251
///////////////////////////////////////
247
252
0 commit comments