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