All pastes #961261 Raw Edit

Miscellany

public text v1 · immutable
#961261 ·published 2008-03-28 19:40 UTC
rendered paste body
using Tao.Glfw;
using Tao.OpenGl;

namespace NScene {
	public class OpenGL : IBackend {
		private Scene : NObject.Scene;
		
		public this(scene : NObject.Scene) {
			Scene = scene;
		}
		
		public Setup(width : int, height : int) : void {
			_ = Glfw.glfwInit();
			_ = Glfw.glfwOpenWindow(width, height, 0, 0, 0, 0, 0, 0, Glfw.GLFW_WINDOW);
            _ = Glfw.glfwSetWindowSizeCallback(Glfw.GLFWwindowsizefun(Reshape));
		}
		
		Render(object : NObject) : void {
			match(object) {
				| NObject.Mesh(color, center, scale, triangles) =>
					Gl.glColor3f(color[0], color[1], color[2]);
					Gl.glPushMatrix();
					Gl.glTranslatef(center[0], center[1], center[2]);
					Gl.glScalef(scale[0], scale[1], scale[2]);
					
					Gl.glBegin(Gl.GL_TRIANGLES);
					foreach(tri in triangles)
						Render(tri);
					Gl.glEnd();
				
				| NObject.Triangle(points) =>
					Render(points[0]);
					Render(points[1]);
					Render(points[2]);
				
				| NObject.Point(pos) =>
					Gl.glVertex3f(pos[0], pos[1], pos[2]);
				
				| _ => ();
			}
		}
		
		public RenderScene() : void {
			Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT);
			
			Render(Scene.Root);
			
			Glfw.glfwSwapBuffers();
			
			when(Glfw.glfwGetWindowParam(Glfw.GLFW_OPENED) == Gl.GL_TRUE)
				RenderScene();
		}
		
		Reshape(width : int, height : int) : void {
            def h = (height :> float) / (width :> float);

            def zNear = 5;
            def zFar  = 30;
            def xMax  = zNear * 0.5f;

            Gl.glViewport(0, 0, width, height);
            Gl.glMatrixMode(Gl.GL_PROJECTION);
            Gl.glLoadIdentity();
            Gl.glFrustum(-xMax, xMax, -xMax * h, xMax * h, zNear, zFar);
            Gl.glMatrixMode(Gl.GL_MODELVIEW);
            Gl.glLoadIdentity();
            Gl.glTranslatef(0, 0, -20);
		}
	}
}