Advertisement
Fonix

Effects.mm

Aug 30th, 2012
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. - (void) prepareToDraw {
  2.    
  3.     camModelViewMatrix = GLKMatrix4Multiply(cameraMatrix, modelViewMatrix);
  4.    
  5.     modelViewProjectionMatrix = GLKMatrix4Multiply(projectionMatrix, camModelViewMatrix);
  6.    
  7.     GLKMatrix3 normalMatrix = GLKMatrix3InvertAndTranspose(GLKMatrix4GetMatrix3(modelViewMatrix), NULL);
  8.    
  9.     glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, modelViewProjectionMatrix.m);
  10.     glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, 0, normalMatrix.m);
  11.    
  12.    
  13.     float* lp = [self vec3ToFloat:lightPosition];
  14.     glUniform3fv(uniforms[UNIFORM_VEC3_lightPosition], 1, lp);
  15.     delete lp;
  16.    
  17.     float* ldc = [self vec4ToFloat:lightDiffuseColour];
  18.     glUniform4fv(uniforms[UNIFORM_VEC4_lightDiffuseColour], 1, ldc);
  19.     delete ldc;
  20.    
  21.     float* lsc = [self vec4ToFloat:lightSpecularColour];
  22.     glUniform4fv(uniforms[UNIFORM_VEC4_lightSpecularColour], 1, lsc);
  23.     delete lsc;
  24.    
  25.     GLKVector3 lightHalfVec = GLKVector3Normalize(GLKVector3Add(cameraPosition, lightPosition));
  26.     float* lhv = [self vec3ToFloat:lightHalfVec];
  27.     glUniform3fv(uniforms[UNIFORM_VEC3_lightHalfVector], 1, lhv);
  28.     delete lhv;
  29.    
  30.     float* nc = [self vec4ToFloat:numberColour];
  31.     glUniform4fv(uniforms[UNIFORM_VEC4_NumberColour], 1, nc);
  32.     delete nc;
  33.    
  34.     float* fc = [self vec4ToFloat:faceColour];
  35.     glUniform4fv(uniforms[UNIFORM_VEC4_FaceColour], 1, fc);
  36.     delete fc;
  37.  
  38.    
  39.     glActiveTexture(GL_TEXTURE0);
  40.     glBindTexture(GL_TEXTURE_2D, texture);
  41.     glUniform1i(uniforms[UNIFORM_Texture], 0);
  42.  
  43.    
  44.     glUseProgram(TexAndLighting);
  45.    
  46.    
  47. }
  48.  
  49. - (void) drawOutline {
  50.    
  51.     camModelViewMatrix = GLKMatrix4Multiply(cameraMatrix, modelViewMatrix);
  52.    
  53.     modelViewProjectionMatrix = GLKMatrix4Multiply(projectionMatrix, camModelViewMatrix);;
  54.    
  55.     glUniformMatrix4fv(toon_mvp, 1, 0, modelViewProjectionMatrix.m);
  56.    
  57.     float* oc = [self vec4ToFloat:outlineColour];
  58.     glUniform4fv(toon_outline, 1, oc);
  59.     delete oc;
  60.    
  61.     glUseProgram(Toon);
  62. }
  63.  
  64. //------ this is how i load the 2 different shaders --------
  65.  
  66. - (BOOL)loadShaders
  67. {
  68.    
  69.     GLuint vertShader, fragShader;
  70.     NSString *vertShaderPathname, *fragShaderPathname;
  71.    
  72.     // Create shader program.
  73.     TexAndLighting = glCreateProgram();
  74.    
  75.     // Create and compile vertex shader.
  76.     vertShaderPathname = [[NSBundle mainBundle] pathForResource:@"Shader" ofType:@"vsh"];
  77.    
  78.     if (![self compileShader:&vertShader type:GL_VERTEX_SHADER file:vertShaderPathname]) {
  79.         NSLog(@"Failed to compile vertex shader");
  80.         return NO;
  81.     }
  82.    
  83.     // Create and compile fragment shader.
  84.     fragShaderPathname = [[NSBundle mainBundle] pathForResource:@"Shader" ofType:@"fsh"];
  85.     if (![self compileShader:&fragShader type:GL_FRAGMENT_SHADER file:fragShaderPathname]) {
  86.         NSLog(@"Failed to compile fragment shader");
  87.         return NO;
  88.     }
  89.    
  90.     // Attach vertex shader to program.
  91.     glAttachShader(TexAndLighting, vertShader);
  92.    
  93.     // Attach fragment shader to program.
  94.     glAttachShader(TexAndLighting, fragShader);
  95.    
  96.     // Bind attribute locations.
  97.     // This needs to be done prior to linking.
  98.     //glBindAttribLocation(_program, ATTRIB_VERTEX, "position");
  99.     //glBindAttribLocation(_program, ATTRIB_NORMAL, "normal");
  100.    
  101.    
  102.     // Link program.
  103.     if (![self linkProgram:TexAndLighting]) {
  104.         NSLog(@"Failed to link program: %d", TexAndLighting);
  105.        
  106.         if (vertShader) {
  107.             glDeleteShader(vertShader);
  108.             vertShader = 0;
  109.         }
  110.         if (fragShader) {
  111.             glDeleteShader(fragShader);
  112.             fragShader = 0;
  113.         }
  114.         if (TexAndLighting) {
  115.             glDeleteProgram(TexAndLighting);
  116.             TexAndLighting = 0;
  117.         }
  118.        
  119.         return NO;
  120.     }
  121.    
  122.     // Get uniform locations.
  123.     uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX] = glGetUniformLocation(TexAndLighting, "modelViewProjectionMatrix");
  124.     uniforms[UNIFORM_NORMAL_MATRIX] = glGetUniformLocation(TexAndLighting, "normalMatrix");
  125.     uniforms[UNIFORM_VEC3_lightPosition] = glGetUniformLocation(TexAndLighting, "lightPosition");
  126.     uniforms[UNIFORM_VEC4_lightDiffuseColour] = glGetUniformLocation(TexAndLighting, "lightDiffuseColour");
  127.     uniforms[UNIFORM_VEC4_lightSpecularColour] = glGetUniformLocation(TexAndLighting, "lightSpecularColour");
  128.     uniforms[UNIFORM_VEC4_lightAmbientColour] = glGetUniformLocation(TexAndLighting, "lightAmbientColour");
  129.     uniforms[UNIFORM_VEC3_lightHalfVector] = glGetUniformLocation(TexAndLighting, "lightHalfVector");
  130.     uniforms[UNIFORM_Texture] = glGetUniformLocation(TexAndLighting, "Texture");
  131.     uniforms[UNIFORM_VEC4_NumberColour] = glGetUniformLocation(TexAndLighting, "numberColour");
  132.     uniforms[UNIFORM_VEC4_FaceColour] = glGetUniformLocation(TexAndLighting, "faceColour");
  133.    
  134.     texCoord = glGetAttribLocation(TexAndLighting, "TexCoordIn");
  135.     vertCoord = glGetAttribLocation(TexAndLighting, "position");
  136.     normal = glGetAttribLocation(TexAndLighting, "normal");
  137.     //glEnableVertexAttribArray(texCoord);
  138.    
  139.    
  140.     // Release vertex and fragment shaders.
  141.     if (vertShader) {
  142.         glDetachShader(TexAndLighting, vertShader);
  143.         glDeleteShader(vertShader);
  144.     }
  145.     if (fragShader) {
  146.         glDetachShader(TexAndLighting, fragShader);
  147.         glDeleteShader(fragShader);
  148.     }
  149.    
  150.     return YES;
  151. }
  152.  
  153. - (BOOL)loadToonShaders
  154. {
  155.    
  156.     GLuint vertShader, fragShader;
  157.     NSString *vertShaderPathname, *fragShaderPathname;
  158.    
  159.     // Create shader program.
  160.     Toon = glCreateProgram();
  161.    
  162.     // Create and compile vertex shader.
  163.     vertShaderPathname = [[NSBundle mainBundle] pathForResource:@"Toon" ofType:@"vsh"];
  164.    
  165.     if (![self compileShader:&vertShader type:GL_VERTEX_SHADER file:vertShaderPathname]) {
  166.         NSLog(@"Failed to compile vertex shader");
  167.         return NO;
  168.     }
  169.    
  170.     // Create and compile fragment shader.
  171.     fragShaderPathname = [[NSBundle mainBundle] pathForResource:@"Toon" ofType:@"fsh"];
  172.     if (![self compileShader:&fragShader type:GL_FRAGMENT_SHADER file:fragShaderPathname]) {
  173.         NSLog(@"Failed to compile fragment shader");
  174.         return NO;
  175.     }
  176.    
  177.     // Attach vertex shader to program.
  178.     glAttachShader(Toon, vertShader);
  179.    
  180.     // Attach fragment shader to program.
  181.     glAttachShader(Toon, fragShader);
  182.    
  183.     // Bind attribute locations.
  184.     // This needs to be done prior to linking.
  185.     //glBindAttribLocation(_program, effect->vertCoord, "position");
  186.     //glBindAttribLocation(_program, effect->normal, "normal");
  187.    
  188.    
  189.     // Link program.
  190.     if (![self linkProgram:Toon]) {
  191.         NSLog(@"Failed to link program: %d", Toon);
  192.        
  193.         if (vertShader) {
  194.             glDeleteShader(vertShader);
  195.             vertShader = 0;
  196.         }
  197.         if (fragShader) {
  198.             glDeleteShader(fragShader);
  199.             fragShader = 0;
  200.         }
  201.         if (Toon) {
  202.             glDeleteProgram(Toon);
  203.             Toon = 0;
  204.         }
  205.        
  206.         return NO;
  207.     }
  208.    
  209.     // Get uniform locations.
  210.     toon_mvp = glGetUniformLocation(Toon, "modelViewProjectionMatrix");
  211.     toon_outline = glGetUniformLocation(Toon, "outlineColour");
  212.    
  213.     toon_vertCoord = glGetAttribLocation(Toon, "position");
  214.    
  215.    
  216.     // Release vertex and fragment shaders.
  217.     if (vertShader) {
  218.         glDetachShader(Toon, vertShader);
  219.         glDeleteShader(vertShader);
  220.     }
  221.     if (fragShader) {
  222.         glDetachShader(Toon, fragShader);
  223.         glDeleteShader(fragShader);
  224.     }
  225.    
  226.     return YES;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement