Jogl gldrawpixels()问题
我希望这最终是一个简单的问题。
我在 Netbeans 中使用 JOGL 创建一个像素棋盘,您可以在其中指定每行/列中的图块数量,以及每个图块的颜色(使用 0 alpha 绘制该图块,使用全 alpha 绘制该图块) )。速度不是问题,效率也不是问题。
这是我的问题:当我尝试使用 glDrawPixels() 时,我的 glCanvas 稀疏填充而不是完全填充。正如你所看到的,在继续更复杂的事情之前,我目前正在尝试获得一个简单的绘图工作案例。我正在用填充字节的值填充一维字节数组。接下来,我将其交给 glDrawPixels() 以写入颜色缓冲区。
我是否错误地处理了字节数组?在实践中(与我见过的几个教程相反),该方法仅采用 Buffer 的形式来存储实际数据,因此这就是我包装字节数组的原因。
此外,该方法似乎期望数据是您为其指定的维度的两倍。例如,您可以在这里看到,虽然我告诉它管理宽度乘高度的区域,但它会抛出异常,除非字节数组实际上是宽度的两倍乘以高度的两倍。
我使用 glDrawPixels() 因为我读到这是管理像素的正确方法。
byte[] src = new byte[(width*2)*(height*2)];
for(int a=0; a<height*2; a++){
for(int b=0; b<width*2; b++){
src[a*b+b]= 127;
}
}
gl.glDrawPixels(width, height,
GL.GL_RED, GL.GL_UNSIGNED_BYTE,
ByteBuffer.wrap(src));
请注意,宽度和高度是我的画布的尺寸(256x256),因此此操作应该占用整个画布的区域。
我真的很感激你能给我的任何帮助。我对涉及完全不同方法的解决方案很满意(只要它们仍然意味着使用 JOGL)。话虽如此,我宁愿不在片段着色器中执行此操作。感谢您抽出时间。
I hope that this ends up being a simple question.
I'm using JOGL in Netbeans to create a checkerboard of pixels where you can specify the number of tiles in each row/col, and also the color of each of these tiles (draw this tile with 0 alpha, draw that tile with full alpha). Speed is not an issue, nor is efficiency.
Here's my problem: When I attempt to use glDrawPixels() my glCanvas is sparsely filled rather then completely filled. As you can see I'm currently trying to get a simple case of the drawing working before moving on to the more involved stuff. I'm filling a 1D byte array with the value of a filled byte. Next I give this to glDrawPixels() to write to the color buffer.
Am I handling the byte array incorrectly? In practice (as opposed to several tutorials I've seen) the method only takes a form of a Buffer for the actual data, so that's why I've wrapped up the byte array.
Also, the method seems to expect twice the data than the dimensions you specify to it. For instance you can see here that although I'm telling it to manage an area of width by height, it throws an exception unless the byte array is actually double the width by double the height.
I'm using glDrawPixels() because I've read that it's the correct way to manage pixels.
byte[] src = new byte[(width*2)*(height*2)];
for(int a=0; a<height*2; a++){
for(int b=0; b<width*2; b++){
src[a*b+b]= 127;
}
}
gl.glDrawPixels(width, height,
GL.GL_RED, GL.GL_UNSIGNED_BYTE,
ByteBuffer.wrap(src));
Note that width and height are the dimensions of my canvas(256x256), so this operation should take up the entire canvas's area.
I'd really appreciate any help you can give me. I'm fine with solutions that involve completely different approaches (as long as they still mean using JOGL). That being said I'd rather not do this in a fragment shader. Thanks for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的缓冲区布局错误。 n×m 个像素、每个像素 k 个字节的图像具有 n*m*k 个元素。要访问缓冲区,您必须提前 width*y + x 元素。尽管缓冲区大小和传递的参数正确,但任何缓冲区溢出都是由于错误的像素解包设置造成的。
所以改成这样:
Your buffer layout is wrong. A image of n×m pixels and k bytes per pixel has n*m*k elements. And to access the buffer you have to advance width*y + x elements. Any buffer overruns despite correct buffer size and parameters passed is due to wrong pixel unpack settings.
So change it to this: