2

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

2 回答 2

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 范围内。

于 2012-06-19T15:58:41.760 回答
1

我放

  if (texture == null && img != null) {

        texture = TextureIO.newTexture(img, true);
        texture.enable();
        texture.bind();
    }

在 try, catch 语句中。

于 2013-11-29T17:29:41.283 回答