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

0

您可能应该将清除颜色 alpha 设置为 0.0 而不是 0.5

此加载纹理的代码有效。

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);
于 2014-12-12T06:23:40.350 回答