4

画像を表示しようとしましたが、表示されません。

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

長方形を描画しますが、テクスチャは表示されません。

4

1 に答える 1