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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
| package snowmanpicking;
import com.jogamp.common.nio.Buffers;
import com.jogamp.opengl.util.gl2.GLUT;
import java.nio.IntBuffer;
import javax.media.opengl.*; import javax.media.opengl.glu.GLU;
public class Render implements GLEventListener {
public static int cursorX, cursorY; private float posX, posY, posZ, lookAtX, lookAtY, lookAtZ; public static int mode, hits; int BUFSIZE = 1024; IntBuffer selectBuf = Buffers.newDirectIntBuffer(BUFSIZE); public static int snowman_display_list;
void processKeyboard(char key, int x, int y) { System.out.printf("key: %d\n", key); }
void picked(int name, int sw) { System.out.printf("my name = %d in %d\n", name, sw); }
void drawSnowMan(GL2 gl, GLUT glut) { gl.glColor3f(1.0f, 1.0f, 1.0f);
gl.glTranslatef(0.0f, 0.75f, 0.0f); glut.glutSolidSphere(0.75f, 20, 20);
gl.glTranslatef(0.0f, 1.0f, 0.0f); glut.glutSolidSphere(0.25f, 20, 20);
gl.glPushMatrix(); gl.glColor3f(0.0f, 0.0f, 0.0f); gl.glTranslatef(0.05f, 0.10f, 0.18f); glut.glutSolidSphere(0.05f, 10, 10); gl.glTranslatef(-0.1f, 0.0f, 0.0f); glut.glutSolidSphere(0.05f, 10, 10); gl.glPopMatrix();
gl.glColor3f(1.0f, 0.5f, 0.5f); gl.glRotatef(0.0f, 1.0f, 0.0f, 0.0f); glut.glutSolidCone(0.08f, 0.5f, 10, 2); }
int createDL(GL2 gl) { int snowManDL;
snowManDL = gl.glGenLists(1);
gl.glNewList(snowManDL, GL2.GL_COMPILE); drawSnowMan(gl, new GLUT()); gl.glEndList();
return (snowManDL); }
void draw(GL2 gl) { gl.glColor3f(0.9f, 0.9f, 0.9f); gl.glBegin(GL2.GL_QUADS); gl.glVertex3f(-100.0f, 0.0f, -100.0f); gl.glVertex3f(-100.0f, 0.0f, 100.0f); gl.glVertex3f(100.0f, 0.0f, 100.0f); gl.glVertex3f(100.0f, 0.0f, -100.0f); gl.glEnd();
for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { gl.glPushMatrix(); gl.glPushName(i * 2 + j); gl.glTranslated(i * 3.0, 0, -j * 3.0); gl.glCallList(snowman_display_list); gl.glPopName(); gl.glPopMatrix(); } } }
@Override public void dispose(GLAutoDrawable drawable) { }
@Override public void display(GLAutoDrawable drawable) { final GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
if (mode == GL2.GL_SELECT) startPicking(gl);
gl.glLoadIdentity();
GLU glu = new GLU(); glu.gluLookAt(posX, posY, posZ, lookAtX, lookAtY, lookAtZ, 0.0f, 1.0f, 0.0f);
draw(gl);
if (mode == GL2.GL_SELECT) stopPicking(gl); }
@Override public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { final GL2 gl = drawable.getGL().getGL2(); GLU glu = new GLU();
float ratio; if (height == 0) height = 1; ratio = 1.0f * width / height; gl.glMatrixMode(GL2.GL_PROJECTION); gl.glLoadIdentity();
gl.glViewport(0, 0, width, height);
glu.gluPerspective(45, ratio, 0.1, 1000);
gl.glMatrixMode(GL2.GL_MODELVIEW); }
@Override public void init(GLAutoDrawable drawable) { final GL2 gl = drawable.getGL().getGL2();
posX = 1.5f; posY = 3.75f; posZ = 3f;
lookAtX = 1.5f; lookAtY = 1.75f; lookAtZ = 0f;
gl.glEnable(GL2.GL_DEPTH_TEST); gl.glEnable(GL2.GL_CULL_FACE);
snowman_display_list = createDL(gl); gl.setSwapInterval(1); }
void startPicking(GL2 gl) { System.out.println("Start Picking"); IntBuffer viewport = Buffers.newDirectIntBuffer(4); float ratio;
gl.glSelectBuffer(BUFSIZE, selectBuf);
gl.glGetIntegerv(GL2.GL_VIEWPORT, viewport);
gl.glRenderMode(GL2.GL_SELECT);
gl.glInitNames();
gl.glMatrixMode(GL2.GL_PROJECTION); gl.glPushMatrix(); gl.glLoadIdentity();
GLU glu = new GLU(); glu.gluPickMatrix(cursorX, viewport.get(3) - cursorY, 5, 5, viewport);
ratio = (float) (viewport.get(2) + 0.0f) / (float) viewport.get(3); glu.gluPerspective(45, ratio, 0.1, 1000); gl.glMatrixMode(GL2.GL_MODELVIEW); }
void stopPicking(GL2 gl) { System.out.println("Stop Picking");
gl.glMatrixMode(GL2.GL_PROJECTION); gl.glPopMatrix(); gl.glMatrixMode(GL2.GL_MODELVIEW); gl.glFlush();
hits = gl.glRenderMode(GL2.GL_RENDER);
if (hits > 0) { System.out.println("# of hits: " + hits); processHits(hits, selectBuf); } else { System.out.println("no hits"); } mode = GL2.GL_RENDER; }
void processHits(int hits, IntBuffer buffer) { int name = -1; int i = 0, nrOfHits = 0; while(nrOfHits < hits && i < BUFSIZE) { if(buffer.get(i) > 0){ name = buffer.get(i); nrOfHits++; } i++; } if (name < 0) { System.out.printf("You didn't click a snowman!"); } else { System.out.printf("You picked snowman "); System.out.printf("%d ", name); } System.out.printf("\n"); } } |