gpt4 book ai didi

java - Jogl 中的透明多边形

转载 作者:行者123 更新时间:2023-12-01 05:33:14 30 4
gpt4 key购买 nike

我的问题是关于jogl的。我在 Canvas 显示中绘制图像。但我在图像上绘制多边形。我画多边形,但多边形是透明的,而不是颜色的。我可以做吗?

 gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL.GL_BLEND);
BufferedImage bufferedImage = null;
int w = 0;
int h = 0;
try {

我得到图像及其宽度和高度

        bufferedImage = ImageIO.read(OpenGLFuncition.class.getResource("kibris.png"));
w = bufferedImage.getWidth();
h = bufferedImage.getHeight();
} catch (IOException ex) {
}

我创建图像区域

    WritableRaster raster =
Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
w,
h,
4,
null);

我设置图像绘制的颜色

    ComponentColorModel colorModel =
new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
new int[]{8, 8, 8, 8},
true,
false,
ComponentColorModel.OPAQUE,
DataBuffer.TYPE_BYTE);
BufferedImage dukeImg =
new BufferedImage(colorModel,
raster,
false,
null);

图形

    Graphics2D g = dukeImg.createGraphics();
g.drawImage(bufferedImage, null, null);
DataBufferByte dukeBuf =
(DataBufferByte) raster.getDataBuffer();
byte[] dukeRGBA = dukeBuf.getData();
ByteBuffer bb = ByteBuffer.wrap(dukeRGBA);
bb.position(0);
bb.mark();
gl.glBindTexture(GL.GL_TEXTURE_2D, 13);
gl.glPixelStorei(GL.GL_UNPACK_ALIGNMENT, 1);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);
gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA, w, h, 0, GL.GL_RGBA,
GL.GL_UNSIGNED_BYTE, bb);
int left = 0;
int top = 0;
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glBindTexture(GL.GL_TEXTURE_2D, 13);
gl.glBegin(GL.GL_POLYGON);
gl.glTexCoord2d(0, 0);
gl.glVertex2d(left, top);
gl.glTexCoord2d(1, 0);
gl.glVertex2d(left + w, top);
gl.glTexCoord2d(1, 1);
gl.glVertex2d(left + w, top + h);
gl.glTexCoord2d(0, 1);
gl.glVertex2d(left, top + h);
gl.glEnd();

多边形绘制

 for (int i = 0; coordinateLoc.length - 1 > i; i++) {
gl.glBegin(GL.GL_POLYGON);
gl.glColor3ub(coordinateLoc[i].get(coordinateLoc[i].size() - 1).getColourred(), coordinateLoc[i].get(coordinateLoc[i].size() - 1).getColourgreen(), coordinateLoc[i].get(coordinateLoc[i].size() - 1).getColourblue());
for (CoordinateXY ca : coordinateLoc[i]) {
// System.out.println(ca.getCoordinateX());
gl.glVertex2i(ca.getCoordinateX(), 449 - ca.getCoordinateY());

}
gl.glEnd();

最佳答案

你应该调用 glcolor() 否则一切都会透明。

gl.glTexCoord2d(0, 1);
gl.glcolor(red,green,blue,alpha);
gl.glVertex2d(left, top + h)

关于java - Jogl 中的透明多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8630194/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com