Advertisement
jake_13_c

Open Gl ES 3D pyramids

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