/* ----- my texture loading functions (will eventually be one function) //texture loading functions GLuint loadTexture() { glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); GLuint texture; glGenTextures(1, &texture); glActiveTexture(GL_TEXTURE0); glEnable(GL_TEXTURE_2D); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT_OES); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT_OES); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //load texture from web NSString *fileNameString = [NSString stringWithFormat:@"%@",textureName]; NSString *texturePathString = [pathString stringByAppendingString:[NSString stringWithFormat:@"%@", fileNameString]]; NSData *texData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", texturePathString]]]; UIImage *image = [[UIImage alloc] initWithData:texData]; if (image == nil) return 0; GLuint width = CGImageGetWidth(image.CGImage); GLuint height = CGImageGetHeight(image.CGImage); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); void *imageData = malloc( height * width * 4 ); CGContextRef context = CGBitmapContextCreate( imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big ); CGColorSpaceRelease( colorSpace ); CGContextClearRect( context, CGRectMake( 0, 0, width, height ) ); CGContextDrawImage( context, CGRectMake( 0, 0, width, height ), image.CGImage ); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData); CGContextRelease(context); free(imageData); [image release]; return texture; } } //second texture loading code, just a 2d now, will be cube map later GLuint loadEnvTexture() { glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); GLuint texture2; glGenTextures(1, &texture2); glActiveTexture(GL_TEXTURE1); glEnable(GL_TEXTURE_2D); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT_OES); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT_OES); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); NSString *fileNameString2 = [NSString stringWithFormat:@"%@",envTextureName]; NSLog(@"fileNameString:%@", fileNameString2); NSString *texturePathString2 = [pathString stringByAppendingString:[NSString stringWithFormat:@"%@", fileNameString2]]; NSLog(@"texturePathString:%@", texturePathString2); NSData *envTexData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", texturePathString2]]]; UIImage *image2 = [[UIImage alloc] initWithData:envTexData]; if (image2 == nil) return 0; GLuint width2 = CGImageGetWidth(image2.CGImage); GLuint height2 = CGImageGetHeight(image2.CGImage); CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB(); void *imageData2 = malloc( height2 * width2 * 4 ); CGContextRef context2 = CGBitmapContextCreate( imageData2, width2, height2, 8, 4 * width2, colorSpace2, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big ); CGColorSpaceRelease( colorSpace2 ); CGContextClearRect( context2, CGRectMake( 0, 0, width2, height2 ) ); CGContextDrawImage( context2, CGRectMake( 0, 0, width2, height2 ), image2.CGImage ); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width2, height2, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData2); CGContextRelease(context2); free(imageData2); [image2 release]; return texture2; } /* ------ I later call these functions in my UI loading code. (i know, bad place for that)*/ [self loadShaders]; loadTexture(); loadEnvTexture(); /* ------ and also compile, link and load my vertex and fragment shaders before rendering*/ /* ------ here is the load shaders code */ - (BOOL)loadShaders { GLuint vertShader, fragShader; NSString *vertShaderPathname, *fragShaderPathname; // Create shader program. program = glCreateProgram(); // Create and compile vertex shader. vertShaderPathname = [[NSBundle mainBundle] pathForResource:@"Shader" ofType:@"vsh"]; if (![self compileShader:&vertShader type:GL_VERTEX_SHADER file:vertShaderPathname]) { NSLog(@"Failed to compile vertex shader"); return FALSE; } // Create and compile fragment shader. fragShaderPathname = [[NSBundle mainBundle] pathForResource:@"Shader" ofType:@"fsh"]; if (![self compileShader:&fragShader type:GL_FRAGMENT_SHADER file:fragShaderPathname]) { NSLog(@"Failed to compile fragment shader"); return FALSE; } // Attach vertex shader to program. glAttachShader(program, vertShader); // Attach fragment shader to program. glAttachShader(program, fragShader); // Bind attribute locations. // This needs to be done prior to linking. glBindAttribLocation(program, ATTRIB_VERTEX, "position"); glBindAttribLocation(program, ATTRIB_NORMAL, "normal"); glBindAttribLocation(program, ATTRIB_TEXCOORD, "texcoord"); // Link program. if (![self linkProgram:program]) { NSLog(@"Failed to link program: %d", program); if (vertShader) { glDeleteShader(vertShader); vertShader = 0; } if (fragShader) { glDeleteShader(fragShader); fragShader = 0; } if (program) { glDeleteProgram(program); program = 0; } return FALSE; } //tell it to use the shader programs glUseProgram(program); //Bind to textures glUniform1i(uniformTexture, 0); glUniform1i(uniformTexture2, 1); // Get uniform locations. uniforms[UNIFORM_MVP] = glGetUniformLocation(program, "mvp"); uniforms[UNIFORM_COLOR] = glGetUniformLocation(program, "color"); uniformTexture = glGetUniformLocation(program, "texture"); uniformTexture2 = glGetUniformLocation(program, "envTexture"); // Release vertex and fragment shaders. if (vertShader) glDeleteShader(vertShader); if (fragShader) glDeleteShader(fragShader); return TRUE; } /* ------ lastly, my fragment shader code*/ varying mediump vec2 v_texcoord; uniform sampler2D texture; uniform sampler2D envTexture; void main() { gl_FragColor = texture2D(texture, v_texcoord); gl_FragColor += texture2D(envTexture, v_texcoord); }