1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| 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); 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(); 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) 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]; } } |