roland
|
 |
«
Posted
2011-11-29 04:30:31 » |
|
Hi, I am trying to dynamically edit an image. I have put the pixels into a byte buffer and then made a byte array from that. How can i set and get a single pixel at x,y? Thanks 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public void GetPixelsFromImage() { int size = m_iWidth*m_iHeight*16; GL11.glBindTexture(GL11.GL_TEXTURE_2D, m_iTextureID); m_bbPixels = ByteBuffer.allocateDirect(size); GL11.glGetTexImage(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, GL11.GL_INT, m_bbPixels); m_pixels = new byte[size]; m_bbPixels.get(m_pixels); } public void SetPixels() { GL11.glBindTexture(GL11.GL_TEXTURE_2D, m_iTextureID); GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA8, m_iWidth, m_iHeight, 0,GL11.GL_RGBA, GL11.GL_INT, m_bbPixels); } public void CopyPixelsToByteBuffer() { m_bbPixels.put(m_pixels); } public void SetPixel(int x, int y, int rgb) { }
public int GetPixel(int x, int y) { } |
|
|
|
|
|
theagentd
|
 |
«
Reply #1 - Posted
2011-11-29 05:21:39 » |
|
1 2 3 4
| public void setPixel(int x, int y, int rgba){ int index = (y * width + x) * 4; } |
I'm veeeery suspicious of your use of GL_INT...
|
Myomyomyo.
|
|
|
gbeebe
|
 |
«
Reply #2 - Posted
2011-11-29 05:30:52 » |
|
edit: I was wrong.
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
|
|
theagentd
|
 |
«
Reply #4 - Posted
2011-11-29 06:18:13 » |
|
GL_INT is signed. I'd use GL_UNSIGNED_BYTE. If you use bytes instead, the index + <0-3> is correct. If you want to, you can wrap your ByteBuffer in an IntBuffer, and just set a whole pixel with a single command.
|
Myomyomyo.
|
|
|
roland
|
 |
«
Reply #5 - Posted
2011-11-29 06:46:39 » |
|
Ok, thanks again. I tried wrapping the ByteBuffer in an IntBuffer, but when trying to set the pixels back with glTexImage2D using the intbuffer instead of the bytebuffer, it said the intbuffer wasn't big enough 
|
|
|
|
|
theagentd
|
 |
«
Reply #6 - Posted
2011-11-29 09:33:39 » |
|
You're supposed to set all channels of a pixel in one command -> one set is one pixel. You also shouldn't multiply the index by 4, as an int is already four bytes.
|
Myomyomyo.
|
|
|
roland
|
 |
«
Reply #7 - Posted
2011-11-30 04:55:30 » |
|
Thanks. Ok it's almost working, but when I get all the pixel colours and write them onto another image (to test that its working), the image turns out blue. Other than that, it's fine. The shading is correct Image: http://i41.tinypic.com/13zv9cj.jpg where it is blue it should be brown(its a ground texture). Do you have any idea why this could be? 1 2 3 4 5 6 7 8 9 10 11 12 13 14
| for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int colour = m_texture.GetPixelAt(x, y); int r = colour % 256; int g = (int)(colour / 256) % 256; int b = (int)((colour / 256) / 256) % 256; int a = (int)(((colour / 256) / 256)/256) % 256; GL11.glColor3b((byte)r,(byte)g,(byte)b); Graphics.DrawSquare(x,y, 1, 1); } } |
|
|
|
|
|
ra4king
|
 |
«
Reply #8 - Posted
2011-11-30 05:36:59 » |
|
How about a cleaner solution: 1 2 3 4
| byte r = (byte)((colour >>> 24) & 0xff); byte g = (byte)((colour >>> 16) & 0xff); byte b = (byte)((colour >>> 8) & 0xff); byte a = (byte)(colour & 0xff); |
|
|
|
|
roland
|
 |
«
Reply #9 - Posted
2011-11-30 07:12:06 » |
|
Thanks for the reply ra4king  As well as being cleaner, that fixed most of it. Although the alpha is 0 at every pixel when it should be 255 and the pixels are too bright. It must have something to do with my offscreen rendering code. I just have no idea what 
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
ra4king
|
 |
«
Reply #10 - Posted
2011-11-30 09:09:21 » |
|
That's because byte values only go from -128 to 127. However, I do believe OpenGL correctly translates that to 0 - 255.
EDIT: wait...255 should be -1, how do you get 0?
|
|
|
|
roland
|
 |
«
Reply #11 - Posted
2011-11-30 10:15:54 » |
|
Im just guessing. Some pixels are very transparent, the rest you can't see at all
|
|
|
|
|
|