Skip to content

Commit e018f01

Browse files
Guillaume SabranGuillaume Sabran
Guillaume Sabran
authored and
Guillaume Sabran
committed
get opengl to work and can use coordinates
1 parent a618039 commit e018f01

File tree

10 files changed

+255
-12
lines changed

10 files changed

+255
-12
lines changed

Diff for: HelloOpenGL_Swift.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
21D0C3DD1B8CDEA300E51C72 /* CC3Kazmath.c in Sources */ = {isa = PBXBuildFile; fileRef = 21D0C3D51B8CDEA300E51C72 /* CC3Kazmath.c */; };
2222
21D0C3DE1B8CDEA300E51C72 /* CC3Math.m in Sources */ = {isa = PBXBuildFile; fileRef = 21D0C3D91B8CDEA300E51C72 /* CC3Math.m */; };
2323
21D0C3E01B8CDEC700E51C72 /* OpenGLView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 21D0C3DF1B8CDEC700E51C72 /* OpenGLView.swift */; };
24+
6FB8CAF71D2EEBA3005B634B /* mountain.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 6FB8CAF61D2EEBA3005B634B /* mountain.jpg */; };
2425
/* End PBXBuildFile section */
2526

2627
/* Begin PBXContainerItemProxy section */
@@ -68,6 +69,7 @@
6869
21D0C3D91B8CDEA300E51C72 /* CC3Math.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CC3Math.m; sourceTree = "<group>"; };
6970
21D0C3DA1B8CDEA300E51C72 /* ccTypes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ccTypes.h; sourceTree = "<group>"; };
7071
21D0C3DF1B8CDEC700E51C72 /* OpenGLView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OpenGLView.swift; sourceTree = "<group>"; };
72+
6FB8CAF61D2EEBA3005B634B /* mountain.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = mountain.jpg; sourceTree = "<group>"; };
7173
/* End PBXFileReference section */
7274

7375
/* Begin PBXFrameworksBuildPhase section */
@@ -124,6 +126,7 @@
124126
21D0C3DF1B8CDEC700E51C72 /* OpenGLView.swift */,
125127
21D0C3A01B8CDE4000E51C72 /* Main.storyboard */,
126128
21D0C3A31B8CDE4000E51C72 /* Assets.xcassets */,
129+
6FB8CAF61D2EEBA3005B634B /* mountain.jpg */,
127130
21D0C3A51B8CDE4000E51C72 /* LaunchScreen.storyboard */,
128131
21D0C3A81B8CDE4000E51C72 /* Info.plist */,
129132
21D0C3CA1B8CDE6E00E51C72 /* Cocos3DMathLib */,
@@ -284,6 +287,7 @@
284287
files = (
285288
21D0C3A71B8CDE4000E51C72 /* LaunchScreen.storyboard in Resources */,
286289
21D0C3CE1B8CDE9100E51C72 /* SimpleVertex.glsl in Resources */,
290+
6FB8CAF71D2EEBA3005B634B /* mountain.jpg in Resources */,
287291
21D0C3A41B8CDE4000E51C72 /* Assets.xcassets in Resources */,
288292
21D0C3A21B8CDE4000E51C72 /* Main.storyboard in Resources */,
289293
21D0C3CF1B8CDE9100E51C72 /* SimpleFragment.glsl in Resources */,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Bucket
3+
type = "1"
4+
version = "2.0">
5+
</Bucket>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Scheme
3+
LastUpgradeVersion = "0800"
4+
version = "1.3">
5+
<BuildAction
6+
parallelizeBuildables = "YES"
7+
buildImplicitDependencies = "YES">
8+
<BuildActionEntries>
9+
<BuildActionEntry
10+
buildForTesting = "YES"
11+
buildForRunning = "YES"
12+
buildForProfiling = "YES"
13+
buildForArchiving = "YES"
14+
buildForAnalyzing = "YES">
15+
<BuildableReference
16+
BuildableIdentifier = "primary"
17+
BlueprintIdentifier = "21D0C3981B8CDE4000E51C72"
18+
BuildableName = "HelloOpenGL_Swift.app"
19+
BlueprintName = "HelloOpenGL_Swift"
20+
ReferencedContainer = "container:HelloOpenGL_Swift.xcodeproj">
21+
</BuildableReference>
22+
</BuildActionEntry>
23+
</BuildActionEntries>
24+
</BuildAction>
25+
<TestAction
26+
buildConfiguration = "Debug"
27+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
28+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
shouldUseLaunchSchemeArgsEnv = "YES">
30+
<Testables>
31+
<TestableReference
32+
skipped = "NO">
33+
<BuildableReference
34+
BuildableIdentifier = "primary"
35+
BlueprintIdentifier = "21D0C3AC1B8CDE4000E51C72"
36+
BuildableName = "HelloOpenGL_SwiftTests.xctest"
37+
BlueprintName = "HelloOpenGL_SwiftTests"
38+
ReferencedContainer = "container:HelloOpenGL_Swift.xcodeproj">
39+
</BuildableReference>
40+
</TestableReference>
41+
<TestableReference
42+
skipped = "NO">
43+
<BuildableReference
44+
BuildableIdentifier = "primary"
45+
BlueprintIdentifier = "21D0C3B71B8CDE4000E51C72"
46+
BuildableName = "HelloOpenGL_SwiftUITests.xctest"
47+
BlueprintName = "HelloOpenGL_SwiftUITests"
48+
ReferencedContainer = "container:HelloOpenGL_Swift.xcodeproj">
49+
</BuildableReference>
50+
</TestableReference>
51+
</Testables>
52+
<MacroExpansion>
53+
<BuildableReference
54+
BuildableIdentifier = "primary"
55+
BlueprintIdentifier = "21D0C3981B8CDE4000E51C72"
56+
BuildableName = "HelloOpenGL_Swift.app"
57+
BlueprintName = "HelloOpenGL_Swift"
58+
ReferencedContainer = "container:HelloOpenGL_Swift.xcodeproj">
59+
</BuildableReference>
60+
</MacroExpansion>
61+
<AdditionalOptions>
62+
</AdditionalOptions>
63+
</TestAction>
64+
<LaunchAction
65+
buildConfiguration = "Debug"
66+
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
67+
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
68+
launchStyle = "0"
69+
useCustomWorkingDirectory = "NO"
70+
ignoresPersistentStateOnLaunch = "NO"
71+
debugDocumentVersioning = "YES"
72+
debugServiceExtension = "internal"
73+
allowLocationSimulation = "YES">
74+
<BuildableProductRunnable
75+
runnableDebuggingMode = "0">
76+
<BuildableReference
77+
BuildableIdentifier = "primary"
78+
BlueprintIdentifier = "21D0C3981B8CDE4000E51C72"
79+
BuildableName = "HelloOpenGL_Swift.app"
80+
BlueprintName = "HelloOpenGL_Swift"
81+
ReferencedContainer = "container:HelloOpenGL_Swift.xcodeproj">
82+
</BuildableReference>
83+
</BuildableProductRunnable>
84+
<AdditionalOptions>
85+
</AdditionalOptions>
86+
</LaunchAction>
87+
<ProfileAction
88+
buildConfiguration = "Release"
89+
shouldUseLaunchSchemeArgsEnv = "YES"
90+
savedToolIdentifier = ""
91+
useCustomWorkingDirectory = "NO"
92+
debugDocumentVersioning = "YES">
93+
<BuildableProductRunnable
94+
runnableDebuggingMode = "0">
95+
<BuildableReference
96+
BuildableIdentifier = "primary"
97+
BlueprintIdentifier = "21D0C3981B8CDE4000E51C72"
98+
BuildableName = "HelloOpenGL_Swift.app"
99+
BlueprintName = "HelloOpenGL_Swift"
100+
ReferencedContainer = "container:HelloOpenGL_Swift.xcodeproj">
101+
</BuildableReference>
102+
</BuildableProductRunnable>
103+
</ProfileAction>
104+
<AnalyzeAction
105+
buildConfiguration = "Debug">
106+
</AnalyzeAction>
107+
<ArchiveAction
108+
buildConfiguration = "Release"
109+
revealArchiveInOrganizer = "YES">
110+
</ArchiveAction>
111+
</Scheme>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>SchemeUserState</key>
6+
<dict>
7+
<key>HelloOpenGL_Swift.xcscheme</key>
8+
<dict>
9+
<key>orderHint</key>
10+
<integer>0</integer>
11+
</dict>
12+
</dict>
13+
<key>SuppressBuildableAutocreation</key>
14+
<dict>
15+
<key>21D0C3981B8CDE4000E51C72</key>
16+
<dict>
17+
<key>primary</key>
18+
<true/>
19+
</dict>
20+
<key>21D0C3AC1B8CDE4000E51C72</key>
21+
<dict>
22+
<key>primary</key>
23+
<true/>
24+
</dict>
25+
<key>21D0C3B71B8CDE4000E51C72</key>
26+
<dict>
27+
<key>primary</key>
28+
<true/>
29+
</dict>
30+
</dict>
31+
</dict>
32+
</plist>

Diff for: HelloOpenGL_Swift/Base.lproj/Main.storyboard

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6211" systemVersion="14A298i" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11163.2" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
33
<dependencies>
4-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6204"/>
4+
<deployment identifier="iOS"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11133"/>
6+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
57
</dependencies>
68
<scenes>
79
<!--View Controller-->
810
<scene sceneID="tne-QT-ifu">
911
<objects>
10-
<viewController id="BYZ-38-t0r" customClass="ViewController" customModuleProvider="target" sceneMemberID="viewController">
12+
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="HelloOpenGL_Swift" customModuleProvider="target" sceneMemberID="viewController">
1113
<layoutGuides>
1214
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
1315
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
1416
</layoutGuides>
1517
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
16-
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
18+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
1719
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
18-
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
20+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
1921
</view>
2022
</viewController>
2123
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>

Diff for: HelloOpenGL_Swift/OpenGLView.swift

+79-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import UIKit
1414
import QuartzCore
1515
import OpenGLES
1616
import GLKit
17+
import CoreMedia
18+
import AVFoundation
1719

1820
struct Vertex {
1921
var Position: (Float, Float, Float)
@@ -24,12 +26,16 @@ class OpenGLView: UIView {
2426
var _context: EAGLContext?
2527
var _colorRenderBuffer = GLuint()
2628
var _colorSlot = GLuint()
29+
var _texCoordSlot = GLuint()
2730
var _currentRotation = Float()
2831
var _depthRenderBuffer = GLuint()
2932
var _eaglLayer: CAEAGLLayer?
3033
var _modelViewUniform = GLuint()
3134
var _positionSlot = GLuint()
3235
var _projectionUniform = GLuint()
36+
var _textureUniform = GLuint()
37+
var _textureID = GLuint()
38+
var spriteImage = UIImage(named: "mountain.jpg")!.cgImage!;
3339

3440
var _vertices = [
3541
Vertex(Position: ( 1, -1, 0), Color: (1, 0, 0, 1)),
@@ -97,6 +103,9 @@ class OpenGLView: UIView {
97103
if (self.setupDisplayLink() != 0) {
98104
NSLog("OpenGLView init(): setupDisplayLink() failed")
99105
}
106+
// if (self.setupTexture() != 0) {
107+
// NSLog("OpenGLView init(): setupTexture() failed")
108+
// }
100109
}
101110

102111
required init?(coder aDecoder: NSCoder) {
@@ -108,7 +117,7 @@ class OpenGLView: UIView {
108117
}
109118

110119
func compileShader(shaderName: String, shaderType: GLenum, shader: UnsafeMutablePointer<GLuint>) -> Int {
111-
let shaderPath = Bundle.main().pathForResource(shaderName, ofType:"glsl")
120+
let shaderPath = Bundle.main.pathForResource(shaderName, ofType:"glsl")
112121
var error : NSError?
113122
let shaderString: NSString?
114123
do {
@@ -188,8 +197,11 @@ class OpenGLView: UIView {
188197

189198
_positionSlot = GLuint(glGetAttribLocation(program, "Position"))
190199
_colorSlot = GLuint(glGetAttribLocation(program, "SourceColor"))
200+
_texCoordSlot = GLuint(glGetAttribLocation(program, "TexCoordIn"))
201+
_textureUniform = GLuint(glGetUniformLocation(program, "Texture"))
191202
glEnableVertexAttribArray(_positionSlot)
192203
glEnableVertexAttribArray(_colorSlot)
204+
glEnableVertexAttribArray(_texCoordSlot);
193205

194206
_projectionUniform = GLuint(glGetUniformLocation(program, "Projection"))
195207
_modelViewUniform = GLuint(glGetUniformLocation(program, "Modelview"))
@@ -219,14 +231,23 @@ class OpenGLView: UIView {
219231
glUniformMatrix4fv(GLint(_modelViewUniform), 1, 0, modelView!.glMatrix)
220232
glViewport(0, 0, GLsizei(self.frame.size.width), GLsizei(self.frame.size.height));
221233

222-
let positionSlotFirstComponent = UnsafePointer<Int>(bitPattern:0)
234+
let positionSlotFirstComponent = UnsafePointer<Int>(bitPattern: 0)
223235
glEnableVertexAttribArray(_positionSlot)
224236
glVertexAttribPointer(_positionSlot, 3, GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(sizeof(Vertex)), positionSlotFirstComponent)
225237

226238
glEnableVertexAttribArray(_colorSlot)
227-
let colorSlotFirstComponent = UnsafePointer<Int>(bitPattern:sizeof(Float) * 3)
239+
let colorSlotFirstComponent = UnsafePointer<Int>(bitPattern: sizeof(Float) * 3)
228240
glVertexAttribPointer(_colorSlot, 4, GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(sizeof(Vertex)), colorSlotFirstComponent)
229241

242+
glEnableVertexAttribArray(_texCoordSlot)
243+
let texCoordFirstComponent = UnsafePointer<Int>(bitPattern: sizeof(Float) * 3)
244+
glVertexAttribPointer(_texCoordSlot, 2, GLenum(GL_FLOAT), GLboolean(GL_FALSE), Int32(sizeof(Vertex)), texCoordFirstComponent)
245+
246+
glActiveTexture(UInt32(GL_TEXTURE0))
247+
glBindTexture(GLenum(GL_TEXTURE_2D), _textureID)
248+
glUniform1i(GLint(_textureUniform), 0)
249+
250+
230251
let vertexBufferOffset = UnsafeMutablePointer<Void>(bitPattern: 0)
231252
glDrawElements(GLenum(GL_TRIANGLES), GLsizei((_indices.count * sizeof(GLubyte))/sizeof(GLubyte)),
232253
GLenum(GL_UNSIGNED_BYTE), vertexBufferOffset)
@@ -259,7 +280,7 @@ class OpenGLView: UIView {
259280

260281
func setupDisplayLink() -> Int {
261282
let displayLink : CADisplayLink = CADisplayLink(target: self, selector: #selector(OpenGLView.render(displayLink:)))
262-
displayLink.add(to: RunLoop.current(), forMode: RunLoopMode.defaultRunLoopMode.rawValue)
283+
displayLink.add(to: RunLoop.current, forMode: RunLoopMode(rawValue: RunLoopMode.defaultRunLoopMode.rawValue))
263284
return 0
264285
}
265286

@@ -283,6 +304,60 @@ class OpenGLView: UIView {
283304
return 0
284305
}
285306

307+
// func setupTexture() -> Int {
308+
// let width: Int = spriteImage.width
309+
// let height: Int = spriteImage.height
310+
// let spriteData = UnsafeMutablePointer<Void>(calloc(Int(UInt(CGFloat(width) * CGFloat(height) * 4)), sizeof(GLubyte.self)))
311+
//
312+
// let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
313+
// let spriteContext: CGContext = CGContext(data: spriteData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: width*4, space: spriteImage.colorSpace!, bitmapInfo: bitmapInfo.rawValue)!
314+
//
315+
// spriteContext.draw(in: CGRect(x: 0, y: 0, width: CGFloat(width) , height: CGFloat(height)), image: spriteImage)
316+
//// CGContextRelease(spriteContext)
317+
//
318+
// var texName: GLuint = GLuint()
319+
// glGenTextures(1, &texName)
320+
// glBindTexture(GLenum(GL_TEXTURE_2D), texName)
321+
//
322+
// glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MIN_FILTER), GL_NEAREST)
323+
// glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_RGBA, GLsizei(width), GLsizei(height), 0, GLenum(GL_RGBA), UInt32(GL_UNSIGNED_BYTE), spriteData)
324+
//
325+
// free(spriteData)
326+
// _textureID = texName
327+
// _textureID = getTextureFromImageWithName(fileName: "mountain.jpg")
328+
// return 0
329+
// }
330+
331+
// func getTextureFromImageWithName(fileName: String) -> GLuint {
332+
//
333+
// let spriteImage: CGImage? = UIImage(named: fileName)!.cgImage
334+
//
335+
// if (spriteImage == nil) {
336+
// print("Failed to load image!")
337+
// exit(1)
338+
// }
339+
//
340+
// let width: Int = spriteImage!.width
341+
// let height: Int = spriteImage!.height
342+
// let spriteData = UnsafeMutablePointer<GLubyte>(calloc(Int(UInt(CGFloat(width) * CGFloat(height) * 4)), sizeof(GLubyte)))
343+
//
344+
// let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
345+
// let spriteContext: CGContext = CGContext(data: spriteData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: width*4, space: spriteImage!.colorSpace!, bitmapInfo: bitmapInfo.rawValue)!
346+
//
347+
// spriteContext.draw(in: CGRect(x: 0, y: 0, width: CGFloat(width) , height: CGFloat(height)), image: spriteImage!)
348+
//// CGContextRelease(spriteContext)
349+
//
350+
// var texName: GLuint = GLuint()
351+
// glGenTextures(1, &texName)
352+
// glBindTexture(GLenum(GL_TEXTURE_2D), texName)
353+
//
354+
// glTexParameteri(GLenum(GL_TEXTURE_2D), GLenum(GL_TEXTURE_MIN_FILTER), GL_NEAREST)
355+
// glTexImage2D(GLenum(GL_TEXTURE_2D), 0, GL_RGBA, GLsizei(width), GLsizei(height), 0, GLenum(GL_RGBA), UInt32(GL_UNSIGNED_BYTE), spriteData)
356+
//
357+
// free(spriteData)
358+
// return texName
359+
// }
360+
286361
func setupRenderBuffer() -> Int {
287362
glGenRenderbuffers(1, &_colorRenderBuffer)
288363
glBindRenderbuffer(GLenum(GL_RENDERBUFFER), _colorRenderBuffer)

Diff for: HelloOpenGL_Swift/SimpleFragment.glsl

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1+
uniform sampler2D Texture;
2+
varying mediump vec2 CameraTextureCoord;
13
varying lowp vec4 DestinationColor;
24

35
void main(void) {
4-
gl_FragColor = DestinationColor;
5-
}
6+
mediump vec2 offset = 0.5 * vec2( cos(0.0), sin(0.0));
7+
mediump vec4 cr = texture2D(Texture, CameraTextureCoord + offset);
8+
mediump vec4 cga = texture2D(Texture, CameraTextureCoord);
9+
mediump vec4 cb = texture2D(Texture, CameraTextureCoord - offset);
10+
// gl_FragColor = vec4(0.5, 0.5, 0.5, 1.0);
11+
// gl_FragColor = vec4(cr.r, cga.g, cb.b, cga.a);
12+
gl_FragColor = vec4(DestinationColor.x * CameraTextureCoord.x, DestinationColor.y * CameraTextureCoord.y, DestinationColor.z, DestinationColor.w);
13+
// gl_FragColor = vec4(DestinationColor.x * (1.0 - CameraTextureCoord.x), DestinationColor.y * CameraTextureCoord.y, DestinationColor.z, DestinationColor.w);
14+
// gl_FragColor = DestinationColor;
15+
}

0 commit comments

Comments
 (0)