PDA

View Full Version : Drawing Pixels



Bismut
08-08-2013, 11:48 PM
Hello everyone,

I'm trying to render a 2D texture using glTexImage2D and an array of pixels but my problem is that just one color is rendering.

As you can see, I set the values of screenData (pixels) at random in order to see if the code is working but instead of drawing a lot of different color pixels
I get only the last code setted or something like that. Always one color only.
And for example, if I called glTexSubImage2D with screenData but one pixel modified, the whole texture color changed.

This is the code for the 2D texture:



srand(20312);
// Clear screen
for(int y = 0; y < SCREEN_HEIGHT; ++y)
for(int x = 0; x < SCREEN_WIDTH; ++x)
{
screenData[y][x][0] = rand() % 255;
screenData[y][x][1] = rand() % 255;
screenData[y][x][2] = rand() % 255;
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
//Generate a texture and holds the ID in blackboard
glGenTextures(1, &blackboard);
//Set the current texture in order to modify it
glBindTexture(GL_TEXTURE_2D, blackboard);

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_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1280, 720, 0, GL_RGB, GL_UNSIGNED_BYTE, screenData);


I hope you can help me.

Alphaomega86
08-13-2013, 03:11 AM
I think the problem is, that you use a three dimensional array. glTexImage2D takes a one dimensional array.



srand(20312);
// Clear screen
for(int i = 0; i < (SCREEN_HEIGHT * SCREEN_WIDTH); i += 3) {
screenData[i + 0] = rand() % 255;
screenData[i + 1] = rand() % 255;
screenData[i + 2] = rand() % 255;
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
//Generate a texture and holds the ID in blackboard
glGenTextures(1, &blackboard);
//Set the current texture in order to modify it
glBindTexture(GL_TEXTURE_2D, blackboard);

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_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1280, 720, 0, GL_RGB, GL_UNSIGNED_BYTE, screenData);

Dan Bartlett
08-13-2013, 03:27 AM
Are you submitting texture coordinates correctly? If you forget to enable the texture coordinate array then all the vertices will sample the same part of the texture, which makes the model appear as only one colour.