Drawing textures in an OpenGL object with os x

I tried to display the image, but it does not display.

Here is my code for a subclass of NSOpenGLView:

// // MyOpenGLView.m // OpenGLTests // // Created by Tom Schamberger on 10.12.12. // Copyright (c) 2012 Tom Schamberger. All rights reserved. // #import "MyOpenGLView.h" @implementation MyOpenGLView - (id)initWithFrame:(NSRect)frame { NSOpenGLPixelFormat * pf = [MyOpenGLView basicPixelFormat]; self = [super initWithFrame: frame pixelFormat: pf]; return self; } + (NSOpenGLPixelFormat*) basicPixelFormat { NSOpenGLPixelFormatAttribute attributes [] = { NSOpenGLPFAWindow, NSOpenGLPFADoubleBuffer, // double buffered NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute)16, // 16 bit depth buffer (NSOpenGLPixelFormatAttribute)nil }; return [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes]; } - (void) prepareOpenGL { glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); glEnable(GL_CULL_FACE); glFrontFace(GL_CCW); glPolygonOffset (1.0f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 0.5f); if([self loadTextures]) NSLog(@"Load"); } - (void)drawRect:(NSRect)dirtyRect { glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBindTexture( GL_TEXTURE_2D, texture); glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); glColor3f(1, 1, 1); glBegin( GL_QUADS ); glTexCoord2f( 0.0f, 0.0f); glVertex2f( -0.5f, -0.5f); glTexCoord2f( 1.0f, 0.0f); glVertex2f( 0.5f, -0.5f); glTexCoord2f( 1.0f, 1.0f); glVertex2f( 0.5f, 0.5f); glTexCoord2f( 0.0f, 1.0f); glVertex2f( -0.5f, 0.5f); glEnd(); glFlush(); } - (BOOL) loadTextures { NSImage *img = [NSImage imageNamed:@"NeHe.bmp"]; if(img == nil) return FALSE; else if(img.size.height == 0 || img.size.width == 0) return FALSE; NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithData: [img TIFFRepresentation]]; glGenTextures( 1, &texture); glBindTexture( GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, rep.size.width, rep.size.height, 0, GL_RGB, GL_UNSIGNED_BYTE, rep.bitmapData); return TRUE; } @end 

It draws a rectangle, but the texture is not displayed.

+4
source share
1 answer

You should probably set the clear color viola to 0.0, not 0.5

This texture loading code works.

 backGroundPath = @"Background.bmp"; NSData* backGroundData = [NSData dataWithContentsOfFile:backGroundPath options:NSUncachedRead error:nil]; backGroundImage = [NSBitmapImageRep imageRepWithData:backGroundData]; glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glGenTextures( 1, backGroundTexture); glBindTexture(GL_TEXTURE_2D, backGroundTexture[0]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, backGroundImage.size.width, backGroundImage.size.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, backGroundImage.bitmapData); 
0
source

All Articles