Advertisement
Guest User

Untitled

a guest
Apr 25th, 2015
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.30 KB | None | 0 0
  1. //
  2. // GameViewController.m
  3. // AIUEO
  4. //
  5. // Created by Kodama Yoshinori on 3/8/15.
  6. // Copyright (c) 2015 EuphonicTeck. All rights reserved.
  7. //
  8.  
  9. #import "GameViewController.h"
  10. #import <OpenGLES/ES2/glext.h>
  11.  
  12. #define BUFFER_OFFSET(i) ((char *)NULL + (i))
  13.  
  14. // Uniform index.
  15. enum
  16. {
  17. UNIFORM_MODELVIEWPROJECTION_MATRIX,
  18. UNIFORM_NORMAL_MATRIX,
  19. NUM_UNIFORMS
  20. };
  21. GLint uniforms[NUM_UNIFORMS];
  22.  
  23. // Attribute index.
  24. enum
  25. {
  26. ATTRIB_VERTEX,
  27. ATTRIB_NORMAL,
  28. NUM_ATTRIBUTES
  29. };
  30.  
  31. GLfloat gCubeVertexData[216] =
  32. {
  33. // Data layout for each line below is:
  34. // positionX, positionY, positionZ, normalX, normalY, normalZ,
  35. 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
  36. 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
  37. 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
  38. 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
  39. 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
  40. 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
  41.  
  42. 0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
  43. -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
  44. 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
  45. 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
  46. -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
  47. -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,
  48.  
  49. -0.5f, 0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
  50. -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
  51. -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
  52. -0.5f, 0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
  53. -0.5f, -0.5f, -0.5f, -1.0f, 0.0f, 0.0f,
  54. -0.5f, -0.5f, 0.5f, -1.0f, 0.0f, 0.0f,
  55.  
  56. -0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
  57. 0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
  58. -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
  59. -0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
  60. 0.5f, -0.5f, -0.5f, 0.0f, -1.0f, 0.0f,
  61. 0.5f, -0.5f, 0.5f, 0.0f, -1.0f, 0.0f,
  62.  
  63. 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
  64. -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
  65. 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
  66. 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
  67. -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
  68. -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 1.0f,
  69.  
  70. 0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
  71. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
  72. 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
  73. 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
  74. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, -1.0f,
  75. -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -1.0f
  76. };
  77.  
  78. @interface GameViewController () {
  79. GLuint _program;
  80.  
  81. GLKMatrix4 _modelViewProjectionMatrix;
  82. GLKMatrix3 _normalMatrix;
  83. float _rotation;
  84.  
  85. GLuint _vertexArray;
  86. GLuint _vertexBuffer;
  87. }
  88. @property (strong, nonatomic) EAGLContext *context;
  89. @property (strong, nonatomic) GLKBaseEffect *effect;
  90.  
  91. - (void)setupGL;
  92. - (void)tearDownGL;
  93.  
  94. - (BOOL)loadShaders;
  95. - (BOOL)compileShader:(GLuint *)shader type:(GLenum)type file:(NSString *)file;
  96. - (BOOL)linkProgram:(GLuint)prog;
  97. - (BOOL)validateProgram:(GLuint)prog;
  98. @end
  99.  
  100. @implementation GameViewController
  101.  
  102. - (void)viewDidLoad
  103. {
  104. [super viewDidLoad];
  105.  
  106. self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
  107.  
  108. if (!self.context) {
  109. NSLog(@"Failed to create ES context");
  110. }
  111.  
  112. GLKView *view = (GLKView *)self.view;
  113. view.context = self.context;
  114. view.drawableDepthFormat = GLKViewDrawableDepthFormat24;
  115.  
  116. [self setupGL];
  117. }
  118.  
  119. - (void)dealloc
  120. {
  121. [self tearDownGL];
  122.  
  123. if ([EAGLContext currentContext] == self.context) {
  124. [EAGLContext setCurrentContext:nil];
  125. }
  126. }
  127.  
  128. - (void)didReceiveMemoryWarning
  129. {
  130. [super didReceiveMemoryWarning];
  131.  
  132. if ([self isViewLoaded] && ([[self view] window] == nil)) {
  133. self.view = nil;
  134.  
  135. [self tearDownGL];
  136.  
  137. if ([EAGLContext currentContext] == self.context) {
  138. [EAGLContext setCurrentContext:nil];
  139. }
  140. self.context = nil;
  141. }
  142.  
  143. // Dispose of any resources that can be recreated.
  144. }
  145.  
  146. - (BOOL)prefersStatusBarHidden {
  147. return YES;
  148. }
  149.  
  150. - (void)setupGL
  151. {
  152. [EAGLContext setCurrentContext:self.context];
  153.  
  154. [self loadShaders];
  155.  
  156. self.effect = [[GLKBaseEffect alloc] init];
  157. self.effect.light0.enabled = GL_TRUE;
  158. self.effect.light0.diffuseColor = GLKVector4Make(1.0f, 0.4f, 0.4f, 1.0f);
  159.  
  160. glEnable(GL_DEPTH_TEST);
  161.  
  162. glGenVertexArraysOES(1, &_vertexArray);
  163. glBindVertexArrayOES(_vertexArray);
  164.  
  165. glGenBuffers(1, &_vertexBuffer);
  166. glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
  167. glBufferData(GL_ARRAY_BUFFER, sizeof(gCubeVertexData), gCubeVertexData, GL_STATIC_DRAW);
  168.  
  169. glEnableVertexAttribArray(GLKVertexAttribPosition);
  170. glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(0));
  171. glEnableVertexAttribArray(GLKVertexAttribNormal);
  172. glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, 24, BUFFER_OFFSET(12));
  173.  
  174. glBindVertexArrayOES(0);
  175. }
  176.  
  177. - (void)tearDownGL
  178. {
  179. [EAGLContext setCurrentContext:self.context];
  180.  
  181. glDeleteBuffers(1, &_vertexBuffer);
  182. glDeleteVertexArraysOES(1, &_vertexArray);
  183.  
  184. self.effect = nil;
  185.  
  186. if (_program) {
  187. glDeleteProgram(_program);
  188. _program = 0;
  189. }
  190. }
  191.  
  192. #pragma mark - GLKView and GLKViewController delegate methods
  193.  
  194. - (void)update
  195. {
  196. float aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height);
  197. GLKMatrix4 projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 100.0f);
  198.  
  199. self.effect.transform.projectionMatrix = projectionMatrix;
  200.  
  201. GLKMatrix4 baseModelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -4.0f);
  202. baseModelViewMatrix = GLKMatrix4Rotate(baseModelViewMatrix, _rotation, 0.0f, 1.0f, 0.0f);
  203.  
  204. // Compute the model view matrix for the object rendered with GLKit
  205. GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -1.5f);
  206. modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, _rotation, 1.0f, 1.0f, 1.0f);
  207. modelViewMatrix = GLKMatrix4Multiply(baseModelViewMatrix, modelViewMatrix);
  208.  
  209. self.effect.transform.modelviewMatrix = modelViewMatrix;
  210.  
  211. // Compute the model view matrix for the object rendered with ES2
  212. modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, 1.5f);
  213. modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, _rotation, 1.0f, 1.0f, 1.0f);
  214. modelViewMatrix = GLKMatrix4Multiply(baseModelViewMatrix, modelViewMatrix);
  215.  
  216. _normalMatrix = GLKMatrix3InvertAndTranspose(GLKMatrix4GetMatrix3(modelViewMatrix), NULL);
  217.  
  218. _modelViewProjectionMatrix = GLKMatrix4Multiply(projectionMatrix, modelViewMatrix);
  219.  
  220. _rotation += self.timeSinceLastUpdate * 0.5f;
  221. }
  222.  
  223. - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
  224. {
  225. glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
  226. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  227.  
  228. glBindVertexArrayOES(_vertexArray);
  229.  
  230. // Render the object with GLKit
  231. [self.effect prepareToDraw];
  232.  
  233. glDrawArrays(GL_TRIANGLES, 0, 36);
  234.  
  235. // Render the object again with ES2
  236. glUseProgram(_program);
  237.  
  238. glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, _modelViewProjectionMatrix.m);
  239. glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, 0, _normalMatrix.m);
  240.  
  241. glDrawArrays(GL_TRIANGLES, 0, 36);
  242. }
  243.  
  244. #pragma mark - OpenGL ES 2 shader compilation
  245.  
  246. - (BOOL)loadShaders
  247. {
  248. GLuint vertShader, fragShader;
  249. NSString *vertShaderPathname, *fragShaderPathname;
  250.  
  251. // Create shader program.
  252. _program = glCreateProgram();
  253.  
  254. // Create and compile vertex shader.
  255. vertShaderPathname = [[NSBundle mainBundle] pathForResource:@"Shader" ofType:@"vsh"];
  256. if (![self compileShader:&vertShader type:GL_VERTEX_SHADER file:vertShaderPathname]) {
  257. NSLog(@"Failed to compile vertex shader");
  258. return NO;
  259. }
  260.  
  261. // Create and compile fragment shader.
  262. fragShaderPathname = [[NSBundle mainBundle] pathForResource:@"Shader" ofType:@"fsh"];
  263. if (![self compileShader:&fragShader type:GL_FRAGMENT_SHADER file:fragShaderPathname]) {
  264. NSLog(@"Failed to compile fragment shader");
  265. return NO;
  266. }
  267.  
  268. // Attach vertex shader to program.
  269. glAttachShader(_program, vertShader);
  270.  
  271. // Attach fragment shader to program.
  272. glAttachShader(_program, fragShader);
  273.  
  274. // Bind attribute locations.
  275. // This needs to be done prior to linking.
  276. glBindAttribLocation(_program, GLKVertexAttribPosition, "position");
  277. glBindAttribLocation(_program, GLKVertexAttribNormal, "normal");
  278.  
  279. // Link program.
  280. if (![self linkProgram:_program]) {
  281. NSLog(@"Failed to link program: %d", _program);
  282.  
  283. if (vertShader) {
  284. glDeleteShader(vertShader);
  285. vertShader = 0;
  286. }
  287. if (fragShader) {
  288. glDeleteShader(fragShader);
  289. fragShader = 0;
  290. }
  291. if (_program) {
  292. glDeleteProgram(_program);
  293. _program = 0;
  294. }
  295.  
  296. return NO;
  297. }
  298.  
  299. // Get uniform locations.
  300. uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX] = glGetUniformLocation(_program, "modelViewProjectionMatrix");
  301. uniforms[UNIFORM_NORMAL_MATRIX] = glGetUniformLocation(_program, "normalMatrix");
  302.  
  303. // Release vertex and fragment shaders.
  304. if (vertShader) {
  305. glDetachShader(_program, vertShader);
  306. glDeleteShader(vertShader);
  307. }
  308. if (fragShader) {
  309. glDetachShader(_program, fragShader);
  310. glDeleteShader(fragShader);
  311. }
  312.  
  313. return YES;
  314. }
  315.  
  316. - (BOOL)compileShader:(GLuint *)shader type:(GLenum)type file:(NSString *)file
  317. {
  318. GLint status;
  319. const GLchar *source;
  320.  
  321. source = (GLchar *)[[NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil] UTF8String];
  322. if (!source) {
  323. NSLog(@"Failed to load vertex shader");
  324. return NO;
  325. }
  326.  
  327. *shader = glCreateShader(type);
  328. glShaderSource(*shader, 1, &source, NULL);
  329. glCompileShader(*shader);
  330.  
  331. #if defined(DEBUG)
  332. GLint logLength;
  333. glGetShaderiv(*shader, GL_INFO_LOG_LENGTH, &logLength);
  334. if (logLength > 0) {
  335. GLchar *log = (GLchar *)malloc(logLength);
  336. glGetShaderInfoLog(*shader, logLength, &logLength, log);
  337. NSLog(@"Shader compile log:\n%s", log);
  338. free(log);
  339. }
  340. #endif
  341.  
  342. glGetShaderiv(*shader, GL_COMPILE_STATUS, &status);
  343. if (status == 0) {
  344. glDeleteShader(*shader);
  345. return NO;
  346. }
  347.  
  348. return YES;
  349. }
  350.  
  351. - (BOOL)linkProgram:(GLuint)prog
  352. {
  353. GLint status;
  354. glLinkProgram(prog);
  355.  
  356. #if defined(DEBUG)
  357. GLint logLength;
  358. glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &logLength);
  359. if (logLength > 0) {
  360. GLchar *log = (GLchar *)malloc(logLength);
  361. glGetProgramInfoLog(prog, logLength, &logLength, log);
  362. NSLog(@"Program link log:\n%s", log);
  363. free(log);
  364. }
  365. #endif
  366.  
  367. glGetProgramiv(prog, GL_LINK_STATUS, &status);
  368. if (status == 0) {
  369. return NO;
  370. }
  371.  
  372. return YES;
  373. }
  374.  
  375. - (BOOL)validateProgram:(GLuint)prog
  376. {
  377. GLint logLength, status;
  378.  
  379. glValidateProgram(prog);
  380. glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &logLength);
  381. if (logLength > 0) {
  382. GLchar *log = (GLchar *)malloc(logLength);
  383. glGetProgramInfoLog(prog, logLength, &logLength, log);
  384. NSLog(@"Program validate log:\n%s", log);
  385. free(log);
  386. }
  387.  
  388. glGetProgramiv(prog, GL_VALIDATE_STATUS, &status);
  389. if (status == 0) {
  390. return NO;
  391. }
  392.  
  393. return YES;
  394. }
  395.  
  396. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement