Editing pixel of a texture

Started by
11 comments, last by BlueSpud 10 years, 8 months ago

Hello,

I'm trying to edit the pixels of a texture in order to create a program like Paint (windows). The idea is that the user can use the mouse to draw in a screen.

This is my code, I create an array of pixels and set it with white and red pixels. But when I try to render the texture I can only see a red quad. (It's only rendering one color)

I'm using OpenGL 2.0


for(int y = 0; y < SCREEN_WIDTH; ++y) 
    for(int x = 0; x < SCREEN_HEIGHT; ++x)
    {
        if(x % 2 == 0)
        {
           screenData[y][x][0] = 179;
           screenData[y][x][1] = 53;
           screenData[y][x][2] = 53;
        }
        else
        {
           screenData[y][x][0] = 255;
           screenData[y][x][1] = 255;
           screenData[y][x][2] = 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_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1280, 720, 0, GL_RGB, GL_UNSIGNED_BYTE, &screenData);
Advertisement

remove the ampersand infront of 'screenData' at the end of your glTexImage2D call. you're passing in the address of the address of the data, when you just want the address of the data (which is what 'screenData' alone already is)

And your loops are probably wrong, you're using x < height and y < width.

Derp

Thanks for the replies.

The problem is the same after the changes, Just the red color is rendering.

// Clear screen
for(int y = 0; y < SCREEN_HEIGHT; ++y) 
for(int x = 0; x < SCREEN_WIDTH; ++x)
{
if(x % 2 == 0)
{
screenData[y][x][0] = 179;
screenData[y][x][1] = 53;
screenData[y][x][2] = 53;
}
else
{
screenData[y][x][0] = 255;
screenData[y][x][1] = 255;
screenData[y][x][2] = 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_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1280, 720, 0, GL_RGB, GL_UNSIGNED_BYTE, screenData);

The problem is probably somewhere else, how are you drawing it? Also, gDebugger is a nice tool to debug opengl programs, I would suggest using it to make sure that the texture is correct.

Derp

Generation of texture co-ordinates may be wrong (not shown in the OP's snippet). Do you use normalized co-ordinates or full size co-ordinates for (u,v)?

remove clamp add

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

but anyway we don't see your drawing routine that might cause trouble.

anyway change your 3d array to 1d (its not so hard)

and why do you use glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

here is how i make texture from pixels:

ofc GLCAM_TEX is unsigned int




glEnable(GL_TEXTURE_2D);
pData = new unsigned int [256*256*3];
glGenTextures(1, &GLCAM_TEX);           //Texture binding
glBindTexture(GL_TEXTURE_2D, GLCAM_TEX);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, pData);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		  glDisable(GL_TEXTURE_2D);

glEnable(GL_TEXTURE_2D);
pData = new unsigned int [256*256*3];
glGenTextures(1, &GLCAM_TEX);           //Texture binding
glBindTexture(GL_TEXTURE_2D, GLCAM_TEX);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, pData);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		  glDisable(GL_TEXTURE_2D);

You should use unsigned char instead of unsigned int, now you're allocating too much memory for the data.

Derp

You might want to also make the second color soomething like green or blue, it might be just blurring them for whatever reason and you dont see the difference because both colors are close to red.

o3o

Here are my drawing routines, and my shaders.

Drawing routine:

//We clear the COLOR_BUFFER_BIT, this is the buffer that handle color issues.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
//Set the Vertex Attribute with VertexArray
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, vertexArray);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
 
// Call IwGL swap instead of egl directly
IwGLSwapBuffers();

This topic is closed to new replies.

Advertisement