Jump to content

  • Log In with Google      Sign In   
  • Create Account


Editing pixel of a texture


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 Bismut   Members   -  Reputation: 115

Like
0Likes
Like

Posted 10 August 2013 - 12:19 AM

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


Sponsor:

#2 radioteeth   Prime Members   -  Reputation: 905

Like
1Likes
Like

Posted 10 August 2013 - 01:26 AM

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)



#3 Sponji   Members   -  Reputation: 1126

Like
0Likes
Like

Posted 10 August 2013 - 01:30 AM

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


Derp

#4 Bismut   Members   -  Reputation: 115

Like
0Likes
Like

Posted 10 August 2013 - 01:50 AM

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


#5 Sponji   Members   -  Reputation: 1126

Like
0Likes
Like

Posted 10 August 2013 - 03:01 AM

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

#6 haegarr   Crossbones+   -  Reputation: 3651

Like
0Likes
Like

Posted 10 August 2013 - 03:16 AM

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



#7 WiredCat   Members   -  Reputation: 147

Like
0Likes
Like

Posted 10 August 2013 - 06:48 AM

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 ___, 11 August 2013 - 07:15 AM.


#8 Sponji   Members   -  Reputation: 1126

Like
1Likes
Like

Posted 10 August 2013 - 08:50 AM

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, 10 August 2013 - 08:53 AM.

Derp

#9 Waterlimon   Crossbones+   -  Reputation: 2314

Like
0Likes
Like

Posted 10 August 2013 - 09:20 AM

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.


Waterlimon (imagine this is handwritten please)


#10 Bismut   Members   -  Reputation: 115

Like
0Likes
Like

Posted 10 August 2013 - 12:04 PM

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


#11 Bismut   Members   -  Reputation: 115

Like
0Likes
Like

Posted 10 August 2013 - 12:06 PM

And my shader setup

 

vertexSrc = "attribute vec4 position; attribute vec4 inputTextureCoordinate; varying vec2 textureCoordinate; void main() { gl_Position = position; textureCoordinate = inputTextureCoordinate.xy;}";
fragmentSrc = "varying highp vec2 textureCoordinate; uniform sampler2D videoFrame; precision mediump float; void main() { gl_FragColor = texture2D(videoFrame, textureCoordinate); }";
 
//Position of the triangle (x,y,z) why 4 D:!
//the value is for example, 0.4f is 40% of the glViewPort.
float size = 1.0f;
vertexArray[0]  =  -size; vertexArray[1]  =   size; vertexArray[2]   =  0.0f;
vertexArray[3]  =  -size; vertexArray[4]  =  -size; vertexArray[5]   =  0.0f;
vertexArray[6]  =   size; vertexArray[7]  =  -size; vertexArray[8]   =  0.0f;
vertexArray[9]  =   size; vertexArray[10]  =  size; vertexArray[11]  =  0.0f;
 
// Get dimensions from IwGL
int w = SCREEN_WIDTH;
int h = SCREEN_HEIGHT;
 
//Set the place where we will draw, in this case is a rectangle of wxh with starting position at (0,0).
glViewport(0, 0, w, h) ;
glClearColor(0, 0, 0, 1);

//Create a program to attach vertex and fragments shaders (Source code) to it.
GLuint shaderProgram = glCreateProgram();
//Loading source codes.
GLuint vertexShader = createShader(GL_VERTEX_SHADER, vertexSrc);
GLuint fragmentShader = createShader(GL_FRAGMENT_SHADER, fragmentSrc);
 
//Attach a source code to the whole program (ShaderProgram)
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
 
//Set the location of the "position" variable defined before.
//If I remove this, the code works exactly the same way. I think OpenGL set por default
//to 0 the first variable in the code.
glBindAttribLocation(shaderProgram, 0, "position");
 
//Linking phase, create a whole functionally program with all attached shaders.
glLinkProgram(shaderProgram);
 
//It's like executing a .exe in windows.
glUseProgram(shaderProgram);



#12 radioteeth   Prime Members   -  Reputation: 905

Like
2Likes
Like

Posted 10 August 2013 - 12:57 PM

Your texture coordinates aren't being passed into the shader. I don't see any texture coordinates.


Edited by radioteeth, 10 August 2013 - 12:58 PM.


#13 BlueSpud   Members   -  Reputation: 390

Like
0Likes
Like

Posted 16 August 2013 - 04:06 PM

A quad rendering to the screen could very well be one color if there are no texture coordinates involved. If the coordinates are not given, OpenGl will just take the color from one of the corners (its always the same, I just am not sure which one) and makes the whole polygon that.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS