1
2
3
4
5 package nl.tudelft.goal.ut2004.visualizer.timeline.map;
6
7 import java.awt.Rectangle;
8 import java.io.File;
9 import java.io.IOException;
10 import java.nio.DoubleBuffer;
11 import java.nio.FloatBuffer;
12 import java.nio.IntBuffer;
13
14 import javax.media.opengl.GL;
15 import javax.media.opengl.GL2;
16 import javax.media.opengl.GLException;
17 import javax.media.opengl.glu.GLU;
18
19 import static javax.media.opengl.GL.*;
20 import static javax.media.opengl.GL2.*;
21 import com.jogamp.opengl.util.gl2.GLUT;
22 import com.jogamp.opengl.util.texture.Texture;
23 import com.jogamp.opengl.util.texture.TextureIO;
24
25 import cz.cuni.amis.pogamut.base3d.worldview.object.Location;
26
27
28
29
30
31 public class GLTools {
32
33 private static GLU glu = new GLU();
34 private static GLUT glut = new GLUT();
35
36 public static Location getWorldCoordinates(GL gla, GLU glu, Location screen) {
37 GL2 gl = gla.getGL2();
38
39
40 FloatBuffer mvBuffer = FloatBuffer.allocate(16);
41 gl.glGetFloatv(GL_MODELVIEW_MATRIX, mvBuffer);
42
43 FloatBuffer prBuffer = FloatBuffer.allocate(16);
44 gl.glGetFloatv(GL_PROJECTION_MATRIX, prBuffer);
45
46 IntBuffer vpBuffer = IntBuffer.allocate(16);
47 gl.glGetIntegerv(GL_VIEWPORT, vpBuffer);
48
49 FloatBuffer result = FloatBuffer.allocate(3);
50
51 glu.gluUnProject((float) screen.x, (float) screen.y, (float) screen.z, mvBuffer, prBuffer, vpBuffer, result);
52
53 return new Location(result.get(0), result.get(1), result.get(2));
54 }
55
56 public static Location getScreenCoordinates(GL gla, GLU glu, Location worldPosition) {
57 GL2 gl = gla.getGL2();
58
59
60 FloatBuffer mvBuffer = FloatBuffer.allocate(16);
61 gl.glGetFloatv(GL_MODELVIEW_MATRIX, mvBuffer);
62
63 FloatBuffer prBuffer = FloatBuffer.allocate(16);
64 gl.glGetFloatv(GL_PROJECTION_MATRIX, prBuffer);
65
66 IntBuffer vpBuffer = IntBuffer.allocate(16);
67 gl.glGetIntegerv(GL_VIEWPORT, vpBuffer);
68
69 FloatBuffer result = FloatBuffer.allocate(3);
70
71 glu.gluProject((float) worldPosition.x, (float) worldPosition.y, (float) worldPosition.z, mvBuffer, prBuffer,
72 vpBuffer, result);
73
74 return new Location(result.get(0), result.get(1), result.get(2));
75 }
76
77 private static Texture texture;
78
79
80
81
82
83
84
85
86
87 public static void renderWindow(GL gla, int x, int y, int width, int height) {
88 GL2 gl = gla.getGL2();
89 pushMatrixMode(gl);
90 setOrthoViewport(gl);
91
92 try {
93 double z = 0;
94 if (texture == null) {
95 texture = TextureIO.newTexture(new File("c:/temp/windowTexture.PNG"), false);
96 }
97 texture.bind(gl);
98 texture.enable(gl);
99
100 gl.glBegin(GL_QUADS);
101 {
102 gl.glColor3d(1, 1, 1);
103 gl.glTexCoord2d(0, 0);
104 gl.glVertex3d(x, y, z);
105
106 gl.glTexCoord2d(1, 0);
107 gl.glVertex3d(x + width, y, z);
108
109 gl.glTexCoord2d(1, 1);
110 gl.glVertex3d(x + width, y + height, z);
111
112 gl.glTexCoord2d(0, 1);
113 gl.glVertex3d(x, y + height, z);
114 }
115 gl.glEnd();
116
117 texture.disable(gl);
118 } catch (IOException ex) {
119
120 ex.printStackTrace();
121 } catch (GLException ex) {
122
123 ex.printStackTrace();
124 }
125
126 popMatrixMode(gl);
127 }
128
129
130
131
132 public static void setOrthoViewport(GL gla) {
133 GL2 gl = gla.getGL2();
134 Rectangle viewport = getViewport(gl);
135 gl.glMatrixMode(GL_PROJECTION);
136 gl.glLoadIdentity();
137 gl.glOrtho(viewport.getMinX(), viewport.getMaxX(), viewport.getMinY(), viewport.getMaxY(), -1, 1);
138 gl.glMatrixMode(GL_MODELVIEW);
139 }
140
141 public static Rectangle getViewport(GL gl) {
142 int viewport[] = new int[4];
143 gl.glGetIntegerv(GL_VIEWPORT, viewport, 0);
144 return new Rectangle(viewport[0], viewport[1], viewport[2], viewport[3]);
145 }
146
147 public static void pushMatrixMode(GL gla) {
148 GL2 gl = gla.getGL2();
149
150 gl.glMatrixMode(GL_PROJECTION);
151 gl.glPushMatrix();
152 gl.glLoadIdentity();
153 gl.glMatrixMode(GL_MODELVIEW);
154 gl.glPushMatrix();
155 gl.glLoadIdentity();
156 }
157
158 public static void popMatrixMode(GL gla) {
159 GL2 gl = gla.getGL2();
160
161 gl.glMatrixMode(GL_PROJECTION);
162 gl.glPopMatrix();
163 gl.glMatrixMode(GL_MODELVIEW);
164 gl.glPopMatrix();
165 }
166 }