Thanks for your answer.
The code for loading textures is as follows:
-(BOOL)loadTexture:(NSString *)_name {
if ([_name isEqualToString:@""]) {
return NO;
}
NSString *substring = [_name substringFromIndex:_name.length-4];
if ([substring isNotEqualTo:@".JPG"] && [substring isNotEqualTo:@".jpg"] && [substring isNotEqualTo:@".png"]) {
return NO;
}
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = bundle.bundlePath;
path = [NSString stringWithFormat:@"%@/Contents/Resources/%@",path,_name];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:path];
if (image == nil) {
return NO;
}
NSRect bigRect;
bigRect.origin = NSZeroPoint;
bigRect.size = [image size];
[image lockFocus];
NSBitmapImageRep *upsideDown = [[NSBitmapImageRep alloc] initWithFocusedViewRect: bigRect];
[image unlockFocus];
// since OpenGL draws images upside down, we need to flip this image along
// the y axis. I know a cool trick for doing this when texture mapping,
// but I want something a little more general for now
NSInteger bytesPerRow = [upsideDown bytesPerRow];
NSInteger bitsPerPixel = [upsideDown bitsPerPixel];
BOOL hasAlpha = [upsideDown hasAlpha];
NSInteger height = (int)bigRect.size.height;
NSBitmapImageRep *_imageRep = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:NULL
pixelsWide:(int)bigRect.size.width
pixelsHigh:height
bitsPerSample:[upsideDown bitsPerSample]
samplesPerPixel:[upsideDown samplesPerPixel]
hasAlpha:hasAlpha
isPlanar:[upsideDown isPlanar]
colorSpaceName:NSCalibratedRGBColorSpace
bytesPerRow:bytesPerRow
bitsPerPixel:bitsPerPixel];
unsigned char *from = [upsideDown bitmapData];
unsigned char *to = [_imageRep bitmapData];
for (int i = 0; i < height; i++) {
bcopy((from + bytesPerRow + i), (to + bytesPerRow + (height - i - 1)), bytesPerRow);
}
GLenum format;
format = hasAlpha ? GL_RGBA : GL_RGB;
GLuint _tex;
glGenTextures(1, &_tex);
glBindTexture(GL_TEXTURE_2D, _tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ROW_LENGTH, (GLint)bytesPerRow / (bitsPerPixel >> 3));
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
bigRect.size.width,
bigRect.size.height,
0,
format,
GL_UNSIGNED_BYTE,
from);
[image release];
[upsideDown release];
[_imageRep release];
texturename[texturecount] = _name;
[texturename[texturecount] retain];
int *temp = malloc((texturecount*sizeof(int))+1*sizeof(int));
for (int count = 0; count<texturecount; count++) {
temp[count] = textureid[count];
}
free(textureid);
textureid = temp;
textureid[texturecount] = _tex;
texturecount ++;
return YES;
}
It's a whole lot of Code I copied it from another thread.
It worked fine for my old app.
I have a class called TextureLoader which keeps all the information like texture name and the corresponding opengl name as an int.
When a texture is asked for it will beloaded by the TextureLoader
-(GLuint)textureidforTexturename:(NSString *)_name {
for (int x = 0; x < texturecount; x++) {
if ([texturename[x] isEqualToString:_name]) {
return textureid[x];
}
}
if ([self loadTexture:_name]) {
return textureid[texturecount-1];
}
else {
printf("not found\n");
return 0;
}
}
So what I do at the start of my app is create a window and inside a NSOpenGLView.
when I just clear the buffer and draw on the right side of my screen it comes up yellow and red for example. So this works.
Tanks a lot :)