Skip to content

Instantly share code, notes, and snippets.

@eiennohito
Created April 18, 2012 17:39
Show Gist options
  • Save eiennohito/2415328 to your computer and use it in GitHub Desktop.
Save eiennohito/2415328 to your computer and use it in GitHub Desktop.
package org.eiennohito.drawing;
import org.eiennohito.math.Matrix;
import org.eiennohito.math.Vector;
import org.eiennohito.model.Vertex;
import org.eiennohito.services.GLCallbackHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.media.opengl.GL;
import static javax.media.opengl.GL.*;
/**
* @author eiennohito
* @date 17.02.2008
*/
@Service
public class GLRenderer implements Renderer {
private GL gl = null;
@Autowired
private GLCallbackHolder callbacks;
public GL getGl() {
return gl;
}
public void setGl(GL gl) {
this.gl = gl;
}
public GLRenderer applyGL(GL gl) {
setGl(gl);
return this;
}
public void drawLineSegment(Vertex start, Vertex end) {
gl.glPushAttrib(GL_CURRENT_BIT);
gl.glBegin(GL_LINES);
start.fillGL(gl);
end.fillGL(gl);
gl.glEnd();
gl.glPopAttrib();
}
public void start() {
gl.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();
gl.glLightfv(GL.GL_LIGHT0, GL_POSITION, new float[] {0, 0, 1000, 1}, 0);
while (!callbacks.isEmpty()) {
callbacks.pop().doInGl(gl);
}
}
public void end() {
gl = null;
}
public void applyMatrix(Matrix currentMatrix) {
currentMatrix.fillGL(gl);
}
public void renderPolygon(Vertex[] vertices) {
gl.glPushAttrib(GL_CURRENT_BIT);
Vector s1 = vertices[1].getPosition().substract(vertices[0].getPosition());
Vector s2 = vertices[2].getPosition().substract(vertices[0].getPosition());
Vector normal = s1.crossProduct(s2).normalized();
gl.glNormal3fv(normal.getBuffer(), 0);
gl.glBegin(GL_POLYGON);
for (Vertex vertex : vertices) {
vertex.fillGL(gl);
}
gl.glEnd();
gl.glPopAttrib();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment