0
\$\begingroup\$

So I'm setting up texture creationg like so, for the data 3 pixels, 1 red, 1 green and 1 blue pixel:

this->dt = new unsigned char[9];
dt[0] = 255; dt[1] = 0; dt[2] = 0; // red pixel
dt[3] = 0; dt[4] = 255; dt[5] = 0; // green pixel
dt[6] = 0; dt[7] = 0; dt[8] = 255; // blue pixel
glGenTextures(1, &this->id);
glBindTexture(GL_TEXTURE_2D, this->id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 3, 1, 0, GL_RGB, GL_UNSIGNED_BYTE, this->dt);
glBindTexture(GL_TEXTURE_2D, 0);

Then I'm doing the drawing like:

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, this->tex.id);
glBegin(GL_QUADS);

glTexCoord2f(0.0, 0.0);
glVertex2d(10.0, 10.0);

glTexCoord2f(1.0, 0.0);
glVertex2d(60.0, 10.0);

glTexCoord2f(1.0, 1.0);
glVertex2d(60.0, 60.0);

glTexCoord2f(0.0, 1.0);
glVertex2d(10.0, 60.0);

glEnd();
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);

As a result, I don't get the 3 pixels repeating (like on the rightside of the image), instead I got (on the leftside of the image):

texture result

I don't know where I wen't wrong, been trying to follow guides as much as possible.

Update, followed thispost on stackoverflow, got better results, but still not quite right, and why is it shading like so?

better result

With the following code:

double quad[] = {
    10.0, 10.0, 0.0,
    60.0, 10.0, 0.0,
    60.0, 60.0, 0.0,
    10.0, 60.0, 0.0
};
double tex[] = {
    0.0, 0.0, 0.0,
    1.0, 0.0, 0.0,
    1.0, 1.0, 0.0,
    0.0, 1.0, 0.0
};
//glColor3d(1.0, 1.0, 1.0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, this->id);

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glVertexPointer(3, GL_DOUBLE, 0, quad);
glColorPointer(3, GL_UNSIGNED_BYTE, 0, this->dt);
glTexCoordPointer(3, GL_DOUBLE, 0, tex);
glDrawArrays(GL_QUADS, 0, 4);

glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);

Update 2, seems to I got texture working for sprites which are same width and height, as far as I can tell I had mismatch calls between glEnable and glDisable:

working texture

But, if I use a sprite which is has more height than width, the result is:

messed texture

Quick update 3, got the texture working on others aswell, turns out I was setting x & y coordinates of the sprite in the wrong order, swapped x & y for-loops (Y first, X second).

working texture

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

As an answer / solution to my own question, where it firstly wen't wrong was mismatches between gl calls, eg. I had glEnable(GL_TEXTURE_2D), but no glDisable(GL_TEXTURE_2D). Think of em like coding brackets, you open one, gotta close it.

As final code I ended up with, creating texture:

glEnable(GL_TEXTURE_2D);
glGenTextures(1, &this->id);
glBindTexture(GL_TEXTURE_2D, this->id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
if (data) {
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
}
else {
    std::cout << "\nUnable to load texture";
}
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);

And drawing:

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, this->tex.id);
glBegin(GL_QUADS);

glTexCoord2f(0.0, 0.0);
glVertex2d(10.0, 10.0);

glTexCoord2f(1.0, 0.0);
glVertex2d(110.0, 10.0);

glTexCoord2f(1.0, 1.0);
glVertex2d(110.0, 110.0);

glTexCoord2f(0.0, 1.0);
glVertex2d(10.0, 110.0);

glEnd();
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);

Which resulted for me (on the image, on the left is a quad textured, center cube textured and on the right just a regular sprite):

texturing

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .