Texture Fails to Appear

glMatt
Unregistered
 
Post: #1
Greetings,

In an attempt to figure out how to texture-map an OpenGL object using XCode 2.0, I created a very simple 2D OpenGL view showing a 4-sided GL_POLYGON textured with a .bmp image. Sadly, the rectangle appears, but no texture.

Much of the code was borrowed from an OpenGL example program, "GLSLShowpiece", included in the Developer folder. Interface Builder was used to create the custom class NSOpenGLView, "TextureMapMain". I added the OpenGL and Glut frameworks to the project's "Frameworks", "Linked Frameworks" section. A .bmp image was added to "Resources".

There is no "(id)initFromFrame:" in this program. NSOpenGL views will not activate initWithFrame by themselves, according to Apple. Anyway, OpenGL seems to work without it.

There is little to be found out there on WWW about OpenGL and Macs, so I am willing to bet we have all visited the NeHe website and tried out the MacOSX download of Lesson06 -- right? That's the one where the dark blue spinning cube shows up as a bright red spinning cube. I modified the image and texture loading part of that program with the same code used in the "- (void)loadTextures" method shown below, and it worked like a dream. So, I know that part of the code works.

So, what is the problem and how am I going to make that darned texture appear?

Matt


Here is the Run Log:
Quote:[Session started at 2006-01-02 17:18:42 -0500.]
2006-01-02 17:18:43.307 TextureMap[1831] initiateGL
2006-01-02 17:18:43.417 TextureMap[1831] image size: 400 x 300
2006-01-02 17:18:43.417 TextureMap[1831] image_rect size: 400 x 300
2006-01-02 17:18:43.431 TextureMap[1831] bitmapImageRep bitsPerPixel: 24
2006-01-02 17:18:43.432 TextureMap[1831] bitmapImageRep bytesPerPlane: 360000
2006-01-02 17:18:43.432 TextureMap[1831] bitmapImageRep bytesPerRow: 1200
2006-01-02 17:18:43.432 TextureMap[1831] bitmapImageRep samplesPerPixel: 3
2006-01-02 17:18:43.432 TextureMap[1831] texture_format: GL_RGB
2006-01-02 17:18:43.432 TextureMap[1831] glGenTextures(1, &texture): 1

TextureMap has exited with status 0.

TextureMapMain.h
Quote:/* TextureMapMain */

#import <Cocoa/Cocoa.h>
#import <OpenGL/gl.h>
#import <OpenGL/glext.h>
#import <OpenGL/glu.h>
#import <OpenGL/OpenGL.h>
#import <GLUT/glut.h>

@interface TextureMapMain : NSOpenGLView
{
GLuint texture;
}

- (BOOL)initiateGL;
- (void)loadTextures;
- (void)animateView:(id)timer;
- (void)drawRect:(NSRect)rect;
- (void)awakeFromNib;
- (void)dealloc;

@end


TextureMapMain.m
Quote:#import "TextureMapMain.h"

@implementation TextureMapMain


- (BOOL)initiateGL
{
BOOL success;
success = YES;

glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glShadeModel(GL_FLAT);
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LEQUAL);

return success;
}

- (void)loadTextures // worked awesome in a different program!
{
NSBundle *mainBundle;
NSString *path_image;
NSImage *image;
NSBitmapImageRep *bitmap_image_rep;
NSRect image_rect;
int texture_format;

mainBundle = [NSBundle mainBundle];
path_image = [mainBundle pathForResource:@"moving" ofType:@"bmp"];
image = [[NSImage alloc] initWithContentsOfFile:path_image];
NSSize imgsize = [image size];
[image setFlipped:YES];

NSLog(@"image size: %d x %d", (int)imgsize.width, (int)imgsize.height);
image_rect = NSMakeRect(0, 0, imgsize.width, imgsize.height);
NSLog(@"image_rect size: %d x %d", (int)image_rect.size.width, (int)image_rect.size.height);
[image lockFocus];
bitmap_image_rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:image_rect];
[image unlockFocus];
[image autorelease];
NSLog(@"bitmapImageRep bitsPerPixel: %d", [bitmap_image_rep bitsPerPixel]);
NSLog(@"bitmapImageRep bytesPerPlane: %d", [bitmap_image_rep bytesPerPlane]);
NSLog(@"bitmapImageRep bytesPerRow: %d", [bitmap_image_rep bytesPerRow]);
NSLog(@"bitmapImageRep samplesPerPixel: %d", [bitmap_image_rep samplesPerPixel]);

if ([bitmap_image_rep bitsPerPixel] < 32) {
texture_format = GL_RGB;
NSLog(@"texture_format: GL_RGB");
} else {
texture_format = GL_RGBA;
NSLog(@"texture_format: GL_RGBA");
}

glGenTextures(1, &texture);
NSLog(@"glGenTextures(1, &texture): %d", texture);
glBindTexture(GL_TEXTURE_2D, texture);
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, texture_format, imgsize.width, imgsize.height, 0,
texture_format, GL_UNSIGNED_BYTE, [bitmap_image_rep bitmapData]);

[bitmap_image_rep autorelease];
}

- (void)animateView:(id)timer
{
[self setNeedsDisplay:YES];
}

- (void)drawRect:(NSRect)rect
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, texture);

glBegin(GL_POLYGON);
glTexCoord2f(0.0, 0.0); glVertex3f(-0.5, -0.5, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(0.5, -0.5, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.5, 0.5, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-0.5, 0.5, 0.0);
glEnd();
glFlush();

[[self openGLContext] flushBuffer];
}

- (void)awakeFromNib
{
long swap_value = 1;
[[self openGLContext] makeCurrentContext];
[[self openGLContext] setValues:&swap_value forParameter:NSOpenGLCPSwapInterval];

if ([self initiateGL]) {
NSLog(@"initiateGL");
} else {
NSLog(@"Warning! initiateGL failed.");
}

[self initiateGL];
[self loadTextures];

NSTimer *timer;
float frames_per_second = 1.0 / 30.0;
timer = [[NSTimer scheduledTimerWithTimeInterval:frames_per_second
target:self
selector:@selector(animateView:)
userInfo:nil
repeats:YES] retain];
}

- (void)dealloc
{
[super dealloc];
}

@end
Quote this message in a reply
Moderator
Posts: 613
Joined: 2004.09
Post: #2
Just a guess but try loading in a 512x512 sized texture

Kyle Richter
DragonForged.com
Twitter: @kylerichter
Quote this message in a reply
glMatt
Unregistered
 
Post: #3
Whoa!

As if by magic, the texture appears.

64x64
128x128
256x256
512x512
1024x1024
etc.

I forgot all about that!

Thanks, Kodex.

Matt
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #4
You can also use 64x128, 256x1024, etc. They don't need to be square, just powers of 2.
Quote this message in a reply
glMatt
Unregistered
 
Post: #5
The funny thing is, I used to mess uround with .bmp textures on the X-Plane flight sim and noticed all that. Remembering this would have saved me hours of trouble.

Now, I have a square with a picture inside that bounces about the window. 32 bit PNG's rule!
Quote this message in a reply
Post Reply