我试图让我的应用程序的用户能够加载他们选择的图像来制作背景。通过 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);
}