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