Jump to content
  • Advertisement
Sign in to follow this  
JavierGonz

OpenGL Editing pixel of a texture

This topic is 1828 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites

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);

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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)? 

Share this post


Link to post
Share on other sites

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);
Edited by ___

Share this post


Link to post
Share on other sites
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.

Edited by Sponji

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!