我试图让我的应用程序的用户能够加载他们选择的图像来制作背景。通过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);
}
答案 0 :(得分:4)
试试这个:
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范围内。
答案 1 :(得分:0)
我把
if (texture == null && img != null) {
texture = TextureIO.newTexture(img, true);
texture.enable();
texture.bind();
}
在try,catch语句中。