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 233 234 235 236 237 238 239 240
|
import com.sun.opengl.util.BufferUtil;
import com.sun.opengl.util.GLUT;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.nio.ByteBuffer;
import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.glu.GLU;
public class Renderer implements GLEventListener, MouseListener{
private final int RENDER = 1;
private final int SELECT = 2;
private final int SelBufferSize = 512;
private int snowman_display_list;
private float[]cam = new float[9];
private int mode = RENDER;
private int cursorX, cursorY;
private Main main;
private GLAutoDrawable drawable;
public Renderer(Main main){
this.main = main;
}
private void drawSnowMan(GL gl) {
GLUT glut = new GLUT();
// Draw Body
gl.glTranslatef(0.0f, 0.75f, 0.0f);
glut.glutSolidSphere(0.75f, 20, 20);
// Draw Head
gl.glTranslatef(0.0f, 1.0f, 0.0f);
glut.glutSolidSphere(0.25f, 20, 20);
// Draw Eyes
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();
// Draw Nose
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);
}
private int createDL(GL gl){
int snowManDL;
// Create the id for the list
snowManDL = gl.glGenLists(1);
gl.glNewList(snowManDL,gl.GL_COMPILE);
drawSnowMan(gl);
gl.glEndList();
return(snowManDL);
}
public void init(GLAutoDrawable arg0) {
final GL gl = arg0.getGL();
this.drawable = arg0;
gl.glEnable(gl.GL_DEPTH_TEST);
gl.glEnable(gl.GL_CULL_FACE);
snowman_display_list = createDL(gl);
cam[0] = 1.5f;
cam[1] = 3.75f;
cam[2] = 3f;
cam[3] = 1.5f;
cam[4] = 1.75f;
cam[5] = 0f;
cam[6] = 0f;
cam[7] = 1f;
cam[8] = 0f;
arg0.addMouseListener(this);
}
private void draw(GL gl) {
// Draw ground
gl.glColor3f(0.9f, 0.9f, 0.9f);
gl.glBegin(gl.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();
// Draw 4 SnowMen
gl.glColor3f(1.0f, 1.0f, 1.0f);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
gl.glPushMatrix();
gl.glPushName(i * 2 + j);
gl.glTranslatef((float)(i * 3.0), (float)0, (float)(-j * 3.0));
gl.glColor3f(1.0f, 1.0f, 1.0f);
gl.glCallList(snowman_display_list);
gl.glPopName();
gl.glPopMatrix();
}
}
}
private void drawPickingMode(GL gl) {
// Draw 4 SnowMen
gl.glDisable(gl.GL_DITHER);
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
gl.glPushMatrix();
switch (i * 2 + j) {
case 0:
gl.glColor3ub((byte)255, (byte)0, (byte)0);
break;
case 1:
gl.glColor3ub((byte)0, (byte)255, (byte)0);
break;
case 2:
gl.glColor3ub((byte)0, (byte)0, (byte)255);
break;
case 3:
gl.glColor3ub((byte)130, (byte)0, (byte)130);
break;
}
gl.glTranslatef((float)(i * 3.0), (float)0, (float)(-j * 3.0));
gl.glCallList(snowman_display_list);
gl.glPopMatrix();
}
}
gl.glEnable(gl.GL_DITHER);
}
private void processPick(GL gl) {
int[]viewport = new int[4];
ByteBuffer pixel = BufferUtil.newByteBuffer(3);
gl.glGetIntegerv(gl.GL_VIEWPORT, viewport, 0);
gl.glReadPixels(cursorX, viewport[3] - cursorY, 1, 1, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, pixel);
System.out.println(pixel.get(0)+" "+pixel.get(1)+" "+pixel.get(2));
if (pixel.get(0) == 255) {
System.out.println("You picked the 1st snowman on the 1st row");
} else if (pixel.get(1) == 255) {
System.out.println("You picked the 1st snowman on the 2nd row");
} else if (pixel.get(2) == 255) {
System.out.println("You picked the 2nd snowman on the 1st row");
} else if (pixel.get(0) == 250) {
System.out.println("You picked the 2nd snowman on the 2nd row");
} else {
System.out.println("You didn't click a snowman!");
}
System.out.println("\n");
}
public void display(GLAutoDrawable arg0) {
final GL gl = arg0.getGL();
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
new GLU().gluLookAt(cam[0], cam[1], cam[2], cam[3], cam[4], cam[5], cam[6], cam[7], cam[8]);
if (mode == SELECT)
drawPickingMode(gl);
else
draw(gl);
if (mode == SELECT) {
processPick(gl);
mode = RENDER;
}
else
arg0.swapBuffers();
}
public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int w, int h) {
final GL gl = arg0.getGL();
float ratio;
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;
ratio = 1.0f * w / h;
// Reset the coordinate system before modifying
gl.glMatrixMode(gl.GL_PROJECTION);
gl.glLoadIdentity();
// Set the viewport to be the entire window
gl.glViewport(0, 0, w, h);
// Set the clipping volume
new GLU().gluPerspective(45,ratio,0.1,1000);
// setting the camera now
gl.glMatrixMode(gl.GL_MODELVIEW);
}
public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {}
public void mouseClicked(MouseEvent e) {
if(e.getButton()==MouseEvent.BUTTON1){
cursorX = e.getPoint().x;
cursorY = e.getPoint().y;
mode = SELECT;
this.main.refresh();
}
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
} |