Commit f5d88ce8 authored by Antoine's avatar Antoine

HobGobelin uses SOFA Matrix4.

parent e750fd17
......@@ -9,27 +9,28 @@ import org.sofa.math.Vector3
import org.graphstream.ui.graphicGraph.GraphicNode
import org.graphstream.ui.graphicGraph.GraphicSprite
import org.graphstream.ui.graphicGraph.GraphicElement
import org.sofa.opengl.MatrixStack
class HobgobelinCamera(graph:GraphicGraph) extends BaseCamera(graph) {
//------------------------------------------------------------------------
// Attributes
/** The graph-space -> pixel-space transformation. */
protected val Tx:Matrix4 = Matrix4()
val Tx:MatrixStack[Matrix4] = new MatrixStack[Matrix4](Matrix4())
/** The inverse transform of Tx. */
protected val xT:Matrix4 = null
val xT:MatrixStack[Matrix4] = new MatrixStack[Matrix4](Matrix4())
//------------------------------------------------------------------------
// Access
override def transformPxToGu(x:Double, y:Double):GSPoint3 = xT.fastMult(new GSPoint3(x, y, 0))
override def transformPxToGu(x:Double, y:Double):GSPoint3 = xT.top.fastMult(new GSPoint3(x, y, 0))
override def transformGuToPx(x:Double, y:Double, z:Double):GSPoint3 = Tx.fastMult(new GSPoint3(x, y, z))
override def transformGuToPx(x:Double, y:Double, z:Double):GSPoint3 = Tx.top.fastMult(new GSPoint3(x, y, z))
override def transformPxToGu(p:GSPoint3):GSPoint3 = xT.fastMult(p)
override def transformPxToGu(p:GSPoint3):GSPoint3 = xT.top.fastMult(p)
override def transformGuToPx(p:GSPoint3):GSPoint3 = Tx.fastMult(p)
override def transformGuToPx(p:GSPoint3):GSPoint3 = Tx.top.fastMult(p)
override def isNodeVisibleIn(node:GraphicNode, X1:Double, Y1:Double, X2:Double, Y2:Double):Boolean = {
// TODO
......@@ -55,19 +56,27 @@ class HobgobelinCamera(graph:GraphicGraph) extends BaseCamera(graph) {
// Commands
def pushPXView(x:Double, y:Double, width:Double, height:Double) {
setBounds
setPadding
Tx.setIdentity
xT.setIdentity
Tx.push
Tx.push
Tx.top.orthographic(0, width, 0, height, -10000, 10000)
xT.top.inverseOrthographic(0, width, 0, height, -10000, 10000)
}
def popPXView() {
Tx.pop
xT.pop
}
def pushGUView(x:Double, y:Double, width:Double, height:Double) {
Tx.push
xT.push
setBounds
setPadding
}
def popGUView() {
Tx.pop
xT.pop
}
}
\ No newline at end of file
......@@ -18,6 +18,9 @@ import org.sofa.opengl.SGL
import javax.media.opengl.glu.GLU
import scala.collection.JavaConversions._
import java.awt.Graphics2D
import org.sofa.opengl.ShaderProgram
import org.sofa.opengl.mesh.Plane
import org.sofa.opengl.VertexArray
/** A thing that has some OpenGL properties. */
trait JoglGraphRenderer {
......@@ -93,20 +96,27 @@ class HobgobelinGraphRenderer extends GraphRendererBase with JoglGraphRenderer {
var canvas:GLAutoDrawable = null
var sgl:SGL = null
var gl:SGL = null
var inited:Boolean = false
override def init(canvas:GLAutoDrawable) {
this.canvas = canvas
val gl = canvas.getGL.getGL2ES2; import gl._;
sgl = new org.sofa.opengl.backend.SGLJogl2ES2(gl.getGL2ES2, GLU.createGLU)
val ogl = canvas.getGL.getGL2ES2; import ogl._;
gl = new org.sofa.opengl.backend.SGLJogl2ES2(ogl.getGL2ES2, GLU.createGLU)
gl.clearColor(0f, 0f, 0f, 0f)
gl.clearDepth(1f)
gl.enable(GL_DEPTH_TEST)
initShaders
initMeshes
sgl.clearColor(0f, 0f, 0f, 0f)
sgl.clearDepth(1f)
sgl.enable(GL_DEPTH_TEST)
inited = true
}
override def reshape(x:Int, y:Int, width:Int, height:Int) {
sgl.viewport(0, 0, width, height)
gl.viewport(0, 0, width, height)
}
//--------------------------------------------------------------------
......@@ -132,27 +142,61 @@ class HobgobelinGraphRenderer extends GraphRendererBase with JoglGraphRenderer {
override def elementStyleChanged(element:Element, oldStyle:StyleGroup, newStyle:StyleGroup) {
}
//------------------------------------------------------------------
// Shading
protected var plainShader:ShaderProgram = null
protected def initShaders() {
plainShader = ShaderProgram(gl, "plain",
"src-scala/org/graphstream/ui/hobgobelin/shaders/plain.vsh",
"src-scala/org/graphstream/ui/hobgobelin/shaders/plain.fsh")
}
//------------------------------------------------------------------
// Mesh
protected var backgroundMesh:Plane = null
protected var background:VertexArray = null
protected def initMeshes() {
backgroundMesh = new Plane(2, 2, 100f, 100f)
val p = plainShader.getAttribLocation("P")
background = new VertexArray(gl, backgroundMesh.indices, (p, 3, backgroundMesh.vertices))
}
//------------------------------------------------------------------
// Rendering
override def render(g:java.awt.Graphics2D, x:Int, y:Int, width:Int, height:Int) {
val camera = this.camera.asInstanceOf[HobgobelinCamera]
renderGraphBackground
camera.pushPXView(x, y, width, height)
renderLayer(g, backRenderer, true)
camera.pushGUView(x, y, width, height)
renderLayer(g, backRenderer, false)
renderGraphElements
renderGraphForeground
renderLayer(g, foreRenderer, false)
camera.popGUView
renderLayer(g, foreRenderer, true)
camera.popPXView
if(inited) {
val camera = this.camera.asInstanceOf[HobgobelinCamera]
camera.pushPXView(x, y, width, height)
renderGraphBackground
renderLayer(g, backRenderer, true)
camera.pushGUView(x, y, width, height)
renderLayer(g, backRenderer, false)
renderGraphElements
renderGraphForeground
renderLayer(g, foreRenderer, false)
camera.popGUView
renderLayer(g, foreRenderer, true)
camera.popPXView
}
}
protected def renderGraphBackground() {
sgl.clearColor(graph.getStyle.getFillColor(0))
sgl.clear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
gl.clearColor(graph.getStyle.getFillColor(0))
gl.clear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
plainShader.use
plainShader.uniform("C", 1, 0, 0, 0.5f)
camera.Tx.rotate(90, 1, 0, 0)
plainShader.uniformMatrix("MVP", camera.Tx.top)
background.draw(backgroundMesh.drawAs)
}
protected def renderGraphElements() {
......
uniform vec4 C;
void main(void) {
gl_FragColor = C;
}
\ No newline at end of file
#version 120
attribute vec3 P;
uniform mat4 MVP;
void main(void) {
gl_Position = MVP * vec4(P, 1.0);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment