0

好的,我正在使用 Java 和 JoGL。我正在尝试加载并显示纹理,但纹理根本没有显示出来。我根本没有收到任何错误,所以我不知道问题可能是什么。

import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.*;
import com.sun.opengl.util.j2d.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import java.nio.*;
import javax.imageio.*;
import javax.swing.*;


public class chartest extends JFrame implements GLEventListener, KeyListener
{
    private int texture;

    public void display(GLAutoDrawable gLDrawable)
    {
      final GL gl = gLDrawable.getGL();
      gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
      gl.glLoadIdentity();


      gl.glColor3f(1.0f, 1.0f, 1.0f);
      gl.glBindTexture(GL.GL_TEXTURE_2D, texture);

      gl.glBegin(GL.GL_QUADS);              // Draw A Quad
        gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(-12.0f, -19.0f,  -15.0f);
        gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f( 12.0f, -19.0f,  -15.0f);
        gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3f( 12.0f,  19.0f,  -15.0f);
        gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3f(-12.0f,  19.0f,  -15.0f);
      gl.glEnd();               // Done Drawing The Quad
      gl.glFlush();
    }

    public void displayChanged(GLAutoDrawable g, boolean b, boolean b2){}

    public void init(GLAutoDrawable gLDrawable)
    {
      final GL gl = gLDrawable.getGL();
      gl.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
      gl.glShadeModel(GL.GL_FLAT);
      gl.glEnable(GL.GL_TEXTURE_2D);

      texture = genTexture(gl);
      gl.glBindTexture(GL.GL_TEXTURE_2D, texture);
      URL url = this.getClass().getResource("test.png");

      try
      {
        BufferedImage img = ImageIO.read(url);
        makeRGBTexture(gl, new GLU(), img, GL.GL_TEXTURE_2D, false);
      }

      catch(Exception e)
      {
        e.printStackTrace();
      }

      gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MIN_FILTER,GL.GL_LINEAR); 
      gl.glTexParameteri(GL.GL_TEXTURE_2D,GL.GL_TEXTURE_MAG_FILTER,GL.GL_LINEAR); 
    }

    public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height)
    {
      final GL gl = gLDrawable.getGL();
      final GLU glu = new GLU();

      if (height <= 0) // avoid a divide by zero error!
        height = 1;
      final float h = (float)width / (float)height;
      gl.glViewport(0, 0, width, height);
      gl.glMatrixMode(GL.GL_PROJECTION);
      gl.glLoadIdentity();
      glu.gluPerspective(45.0f, h, 1.0, 20.0);
      gl.glMatrixMode(GL.GL_MODELVIEW);
      gl.glLoadIdentity();
    }

    public void keyPressed(KeyEvent e)
    {
        if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
        System.exit(0);
    }
    public void keyReleased(KeyEvent e) {}
    public void keyTyped(KeyEvent e) {}

    public static void main(String[] args) {

        chartest c = new chartest();

        GLCanvas canvas = new GLCanvas(new GLCapabilities());
        canvas.addGLEventListener(c);
        c.add(canvas);
        c.setSize(640, 480);
        canvas.addKeyListener(c);
        c.addWindowListener(new WindowAdapter()
        {
          public void windowClosing(WindowEvent e)
          {
            System.exit(0);
          }
        });
        c.setVisible(true);
        canvas.requestFocus();
    }



    //-----------------------
    private void makeRGBTexture(GL gl, GLU glu, BufferedImage img, int target, boolean mipmapped)
    {
      ByteBuffer dest = null;
      switch (img.getType())
      {
        case BufferedImage.TYPE_3BYTE_BGR:
        case BufferedImage.TYPE_CUSTOM:
        {
          System.out.println("Custom");
          byte[] data = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
          dest = ByteBuffer.allocateDirect(99999);
          dest.order(ByteOrder.nativeOrder());
          dest.put(data, 0, data.length);
          break;
        }
        case BufferedImage.TYPE_INT_RGB:
        {
          int[] data = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
          dest = ByteBuffer.allocateDirect(data.length * BufferUtil.SIZEOF_INT);
          dest.order(ByteOrder.nativeOrder());
          dest.asIntBuffer().put(data, 0, data.length);
          break;
        }
        default:
          throw new RuntimeException("Unsupported image type " + img.getType());
      }

      if (mipmapped)
      {
        glu.gluBuild2DMipmaps(target, GL.GL_RGB8, img.getWidth(), img.getHeight(), GL.GL_RGB, GL.GL_UNSIGNED_BYTE, dest);
      }
      else
      {
        gl.glTexImage2D(target, 0, GL.GL_RGB, img.getWidth(), img.getHeight(), 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, dest);
      }
    }

    private int genTexture(GL gl)
    {
      final int[] tmp = new int[1];
      gl.glGenTextures(1, tmp, 0);
      return tmp[0];
    }
}
4

2 回答 2

2

我没有看到您检查任何 OpenGL 错误。JOGL 提供了一种简单的方法来做到这一点。只需放在glDrawable.setGL(new DebugGL(glDrawable.getGL()));init 方法的开头即可。

如果这没有帮助,我会检查图像数据和纹理变量是否不为零。

于 2009-03-08T21:56:43.923 回答
1

我设法通过使用 com.sun.opengl.util.texture.Texture 和我在这里找到的教程来加载纹理。

于 2009-03-09T01:07:39.253 回答