Vertex Array Texture Help

Hi,

I am learning how to use textures with vertex arrays, so I manage to compile (what I think is all I need to get the job done) the following code:

  
  
const unsigned NUM_VERTEX = 4;  
  
typedef struct {  
	GLfloat x;  
	GLfloat y;  
} Vertex2D;  
  
Vertex2D vertices[NUM_VERTEX];  
  
const GLfloat texCoords[] = {  
	0.0f, 1.0f,  
	1.0f, 1.0f,  
	0.0f, 0.0f,  
	1.0f, 0.0f  
};  
  
const Vertex2D normals[] = {  
	{0.0, 0.0},  
	{0.0, 0.0},  
	{0.0, 0.0},  
	{0.0, 0.0}  
};  
  
TextureQuad::TextureQuad() {  
	// init shape vertex poitns  
	float k = TWO_PI / NUM_VERTEX;  
	float radius = 100.0f;  
	for (unsigned i = 0; i < NUM_VERTEX; i++) {  
		vertices[i].x = cos(i * k) * radius;  
		vertices[i].y = sin(i * k) * radius;  
	}  
	  
	// init textures  
	textureImage.loadImage("texture.png");  
	textureImage.setImageType(OF_IMAGE_COLOR);  
	textureImagePixels = textureImage.getPixels();  
	  
	texture.allocate(textureImage.width, textureImage.height, GL_RGB);  
	texture.loadData(textureImagePixels, textureImage.width, textureImage.height, GL_RGB);  
	  
	glEnable(GL_TEXTURE_2D);  
	glEnable(GL_BLEND);  
	glBlendFunc(GL_ONE, GL_SRC_COLOR);  
	  
	glGenTextures(1, &texturegl[0]);  
	glBindTexture(GL_TEXTURE_2D, texturegl[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_RGBA, textureImage.width, textureImage.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureImagePixels);  
}  
  
void TextureQuad::display() {  
	glPushMatrix();  
	glTranslatef(ofGetWidth() * 0.5f, ofGetHeight() * 0.5f, 0.0f);  
	  
	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);  
	  
	glEnableClientState(GL_VERTEX_ARRAY);  
	glEnableClientState(GL_NORMAL_ARRAY);  
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);  
	  
	glBindTexture(GL_TEXTURE_2D, texturegl[0]);  
	glVertexPointer(2, GL_FLOAT, 0, &vertices);  
	glNormalPointer(GL_FLOAT, 0, &normals);  
	glTexCoordPointer(2, GL_FLOAT, 0, &texCoords);  
	  
	glDrawArrays(GL_TRIANGLE_FAN, 0, NUM_VERTEX);  
	  
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);  
	glDisableClientState(GL_NORMAL_ARRAY);  
	glDisableClientState(GL_VERTEX_ARRAY);  
	  
	glPopMatrix();  
}  
  

But is not showing the texture image, I think the problem is in the constructor where:

  
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureImage.width, textureImage.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureImagePixels);  

and textureImagePixels is not the right data for the glTexImage2D method, but I am not sure, there may be something else going on I am not aware of.

Any help/advice will be much appreciated!

Cheers
rS

I am following alone this tutorial http://iphonedevelopment.blogspot.com/2009/05/opengl-es-from-ground-up-part-6-25.html

And I think the problem I have is my port of this section:

  
  
NSString *path = [[NSBundle mainBundle] pathForResource:@"texture" ofType:@"png"];  
    NSData *texData = [[NSData alloc] initWithContentsOfFile:path];  
    UIImage *image = [[UIImage alloc] initWithData:texData];  
    if (image == nil)  
        NSLog(@"Do real error checking here");  
  
    GLuint width = CGImageGetWidth(image.CGImage);  
    GLuint height = CGImageGetHeight(image.CGImage);  
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();  
    void *imageData = malloc( height * width * 4 );  
    CGContextRef context = CGBitmapContextCreate( imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );  
    CGColorSpaceRelease( colorSpace );  
    CGContextClearRect( context, CGRectMake( 0, 0, width, height ) );  
    CGContextTranslateCTM( context, 0, height - height );  
    CGContextDrawImage( context, CGRectMake( 0, 0, width, height ), image.CGImage );  
  
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);  
  
    CGContextRelease(context);  
  
    free(imageData);  
    [image release];  
    [texData release];  
  

to this (using ofTexture and ofImage):

  
  
	textureImage.loadImage("texture.png");  
	textureImage.setImageType(OF_IMAGE_COLOR);  
	textureImagePixels = textureImage.getPixels();  
	  
	texture.allocate(textureImage.width, textureImage.height, GL_RGB);  
	texture.loadData(textureImagePixels, textureImage.width, textureImage.height, GL_RGB);  
  
	glEnable(GL_TEXTURE_2D);  
	glEnable(GL_BLEND);  
	glBlendFunc(GL_ONE, GL_SRC_COLOR);  
	  
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);  
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);  
	  
	  
	glGenTextures(1, &texturegl[0]);  
	glBindTexture(GL_TEXTURE_2D, texturegl[0]);  
	  
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureImage.width, textureImage.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureImagePixels);  
  

and here is the final code:

  
  
#include "TextureQuad.h"  
  
const unsigned NUM_VERTEX = 4;  
  
typedef struct {  
	GLfloat x;  
	GLfloat y;  
	GLfloat z;  
} Vertex3D;  
  
static const Vertex3D vertices[] = {  
	{-1.0,  1.0, 0.0},  
	{ 1.0,  1.0, 0.0},  
	{-1.0, -1.0, 0.0},  
	{ 1.0, -1.0, 0.0}  
};  
  
static const GLfloat texCoords[] = {  
	0.0f, 1.0f,  
	1.0f, 1.0f,  
	0.0f, 0.0f,  
	1.0f, 0.0f  
};  
  
static const Vertex3D normals[] = {  
	{0.0, 0.0, 1.0},  
	{0.0, 0.0, 1.0},  
	{0.0, 0.0, 1.0},  
	{0.0, 0.0, 1.0}  
};  
  
  
TextureQuad::TextureQuad() {  
	// init textures  
	textureImage.loadImage("texture.png");  
	textureImage.setImageType(OF_IMAGE_COLOR);  
	textureImagePixels = textureImage.getPixels();  
	  
	texture.allocate(textureImage.width, textureImage.height, GL_RGB);  
	texture.loadData(textureImagePixels, textureImage.width, textureImage.height, GL_RGB);  
	  
	glEnable(GL_TEXTURE_2D);  
	glEnable(GL_BLEND);  
	glBlendFunc(GL_ONE, GL_SRC_COLOR);  
	  
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);  
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);  
	  
	glGenTextures(1, &texturegl[0]);  
	glBindTexture(GL_TEXTURE_2D, texturegl[0]);  
	  
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureImage.width, textureImage.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureImagePixels);  
}  
  
  
void TextureQuad::display() {  
	glPushMatrix();  
	  
	glEnableClientState(GL_VERTEX_ARRAY);  
	glEnableClientState(GL_NORMAL_ARRAY);  
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);  
	  
	glBindTexture(GL_TEXTURE_2D, texturegl[0]);  
	glVertexPointer(3, GL_FLOAT, 0, &vertices);  
	glNormalPointer(GL_FLOAT, 0, &normals);  
	glTexCoordPointer(2, GL_FLOAT, 0, &texCoords);  
	  
	glDrawArrays(GL_TRIANGLE_STRIP, 0, NUM_VERTEX);  
	  
	glDisableClientState(GL_VERTEX_ARRAY);  
	glDisableClientState(GL_NORMAL_ARRAY);  
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);  
	  
	glPopMatrix();  
}  
  

Any sparkle of wisdom will be much appreciated!

Cheers
rS

Its been long time since I post this, but I am still trying to figure this out what is the problem.

Any help will be much appreciated!

Cheers
rS