我试图让我的应用程序的用户能够加载他们选择的图像来制作背景。通过 Java 加载图像没有问题,但我无法将图像放入纹理中......我最终在 GLCanvas 上看到一个大的灰色框。这是我到目前为止的代码:
//if there's an image to overlay, render it
if (renderImage) {
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
if (texture == null && img != null) {
texture = TextureIO.newTexture(img, true);
texture.enable();
texture.bind();
}
gl.glBegin(GL.GL_POLYGON);
gl.glNormal3f(0,0,1);
gl.glTexCoord2d(-texture.getWidth(), -texture.getHeight());
gl.glVertex2d(-25, -25);
gl.glTexCoord2d(-texture.getWidth(), texture.getHeight());
gl.glVertex2d(canvas.getWidth(),0);
gl.glTexCoord2d(texture.getWidth(), texture.getHeight());
gl.glVertex2d(canvas.getWidth(), canvas.getHeight());
gl.glTexCoord2d(texture.getWidth(), -texture.getHeight());
gl.glVertex2d(0, canvas.getHeight());
gl.glEnd();
gl.glFlush();
}
//otherwise, render "grass"
else {
gl.glClearColor(0.0f, 0.65f, 0.0f, 0.0f);
//Clear buffer and set background color to green (the "grass" on the sides of the intersection)
gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
}
最佳答案
试试这个:
gl.glBegin(GL.GL_QUADS);
gl.glNormal3f(0,0,1);
gl.glTexCoord2d(0.0, 0.0);
gl.glVertex2d(0.0, 0.0);
gl.glTexCoord2d(1.0, 0.0);
gl.glVertex2d(canvas.getWidth(), 0.0);
gl.glTexCoord2d(1.0, 1.0);
gl.glVertex2d(canvas.getWidth(), canvas.getHeight());
gl.glTexCoord2d(0.0, 1.0);
gl.glVertex2d(0.0, canvas.getHeight());
gl.glEnd();
非重复纹理坐标默认在 0.0 到 1.0 范围内。
关于java - 使用纹理对象使用 JOGL 进行基本纹理绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11104332/