Cocoa Create OpenGL Texture

I am working on my first OpenGL application using Cocoa (I used OpenGL ES on the iPhone) and I am having problems loading the texture from the image file. Here is my texture loading code:

@interface MyOpenGLView : NSOpenGLView 
{
GLenum texFormat[ 1 ];   // Format of texture (GL_RGB, GL_RGBA)
NSSize texSize[ 1 ];     // Width and height

GLuint textures[1];     // Storage for one texture
}

- (BOOL) loadBitmap:(NSString *)filename intoIndex:(int)texIndex
{
BOOL success = FALSE;
NSBitmapImageRep *theImage;
int bitsPPixel, bytesPRow;
unsigned char *theImageData;

NSData* imgData = [NSData dataWithContentsOfFile:filename options:NSUncachedRead error:nil];

theImage = [NSBitmapImageRep imageRepWithData:imgData]; 

if( theImage != nil )
{
    bitsPPixel = [theImage bitsPerPixel];
    bytesPRow = [theImage bytesPerRow];
    if( bitsPPixel == 24 )        // No alpha channel
        texFormat[texIndex] = GL_RGB;
    else if( bitsPPixel == 32 )   // There is an alpha channel
        texFormat[texIndex] = GL_RGBA;
    texSize[texIndex].width = [theImage pixelsWide];
    texSize[texIndex].height = [theImage pixelsHigh];

    if( theImageData != NULL )
    {
        NSLog(@"Good so far...");
        success = TRUE;

        // Create the texture

        glGenTextures(1, &textures[texIndex]);

        NSLog(@"tex: %i", textures[texIndex]);

        NSLog(@"%i", glIsTexture(textures[texIndex]));

        glPixelStorei(GL_UNPACK_ROW_LENGTH, [theImage pixelsWide]);

        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

        // Typical texture generation using data from the bitmap
        glBindTexture(GL_TEXTURE_2D, textures[texIndex]);

        NSLog(@"%i", glIsTexture(textures[texIndex]));

        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texSize[texIndex].width, texSize[texIndex].height, 0, texFormat[texIndex], GL_UNSIGNED_BYTE, [theImage bitmapData]);

        NSLog(@"%i", glIsTexture(textures[texIndex]));
    }
}


return success;
}

It seems that the glGenTextures () function does not actually create the texture, because textures[0]0 remains. In addition, logging glIsTexture(textures[texIndex])always returns false.

Any suggestions?

Thank,

Kyle

+3
source share
2 answers

OK, I get it. Turns out I was trying to load textures before setting up my context. As soon as I loaded the textures at the end of the initialization method, it worked fine.

Thanks for answers.

Kyle

0
source
glGenTextures(1, &textures[texIndex] );

textures?

glIsTexture true , . , glGenTextures, glBindTexture, .

, glGenTextures glBegin glEnd - .

:

, , 2.

, iPhone OpenGL ES , .

+1

Source: https://habr.com/ru/post/1728697/


All Articles