EOS 2  1.1.0
Einfache Objektbasierte Sprache
GLTextureBuffer.java
gehe zur Dokumentation dieser Datei
1 package de.lathanda.eos.robot.gui;
2 
3 import java.awt.Color;
4 import java.util.HashMap;
5 import com.jogamp.opengl.GL;
6 import com.jogamp.opengl.GL2;
7 import com.jogamp.opengl.GLException;
8 import com.jogamp.opengl.GLProfile;
9 import com.jogamp.opengl.util.texture.Texture;
10 import com.jogamp.opengl.util.texture.awt.AWTTextureData;
11 
12 import de.lathanda.eos.robot.geom3d.Material;
13 
14 public class GLTextureBuffer {
15  // ***************** factory *****************
16  private static HashMap<Material, GLTextureBuffer> texturebuffer = new HashMap<>();
17 
18  public static synchronized GLTextureBuffer get(Material m, GL gl) {
19  GLTextureBuffer texture = texturebuffer.get(m);
20  if (texture == null) {
21  texture = new GLTextureBuffer(m);
22  texturebuffer.put(m, texture);
23  }
24  return texture;
25  }
26  public static synchronized void clear(GL gl) {
27  for(GLTextureBuffer buffer : texturebuffer.values()) {
28  buffer.destroy(gl);
29  }
30  }
31 
32  //****************** class *******************
33  private final Material m;
34  private HashMap<GL, Texture> tbuffer = new HashMap<>();
36  this.m = m;
37  }
38  public void destroy(GL gl) {
39  Texture t = tbuffer.get(gl);
40  //free resources on graphic card
41  if (t != null) {
42  t.destroy(gl);
43  tbuffer.remove(gl);
44  }
45  }
46  public void openMaterial(Color base, GL2 gl) {
47  gl.glColor4ub((byte) base.getRed(), (byte) base.getGreen(), (byte) base.getBlue(), (byte) base.getAlpha());
48 
49  if (m.ka != null) {
50  gl.glMaterialfv(GL.GL_FRONT, GL2.GL_AMBIENT, m.ka, 0);
51  } else {
52  gl.glMaterialfv(GL.GL_FRONT, GL2.GL_AMBIENT, base.getComponents(null), 0);
53  }
54  if (m.kd != null) {
55  gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, m.kd, 0);
56  } else {
57  gl.glMaterialfv(GL.GL_FRONT, GL2.GL_DIFFUSE, base.getComponents(null), 0);
58  }
59  if (m.ks != null) {
60  gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, m.ks, 0);
61  } else {
62  gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, base.getComponents(null), 0);
63  }
64  if (m.image != null) {
65  try {
66  Texture t = getTexture(gl);
67  t.enable(gl);
68  t.bind(gl);
69  gl.glEnable(GL.GL_TEXTURE);
70  gl.glTexEnvi(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_REPLACE);
71  } catch (GLException gle) {
72  System.err.println(gle.getMessage());
73  }
74  }
75  }
76 
77  private Texture getTexture(GL2 gl) {
78  Texture t = tbuffer.get(gl);
79  if (t == null) {
80  AWTTextureData atd = new AWTTextureData(GLProfile.get(GLProfile.GL2), GL.GL_RGBA, GL.GL_RGBA, false,
81  m.image);
82  t = new Texture(gl, atd);
83  t.setTexParameterf(gl, GL2.GL_TEXTURE_WRAP_S, GL2.GL_REPEAT);
84  t.setTexParameterf(gl, GL2.GL_TEXTURE_WRAP_T, GL2.GL_REPEAT);
85  tbuffer.put(gl, t);
86  }
87  return t;
88  }
89 
90  public void closeMaterial(GL2 gl) {
91  if (m.image != null) {
92  Texture t = getTexture(gl);
93  t.disable(gl);
94  gl.glDisable(GL.GL_TEXTURE);
95  }
96  }
97 
98 }
static synchronized GLTextureBuffer get(Material m, GL gl)
static synchronized void clear(GL gl)
Impressum