Jelajahi Sumber

removed usage of unique; added debug writelns

Zoadian 11 tahun lalu
induk
melakukan
29241ce2be

+ 128 - 115
source/app.d

@@ -37,6 +37,8 @@ struct Quaternion {
 }
 
 
+
+
 final class Mesh {
 public:
 	Vertex[] vertexData;
@@ -44,11 +46,11 @@ public:
 	UV[] textureData;
 	Color[] colorData;
 
-	Unique!(VertexArrayObject) vao;
-	Unique!(VertexBufferObject!(VertexBufferObjectTarget.Array)) vboVertexData;
-	Unique!(VertexBufferObject!(VertexBufferObjectTarget.Array)) vboNormalData;
-	Unique!(VertexBufferObject!(VertexBufferObjectTarget.Array)) vboTextureData;
-	Unique!(VertexBufferObject!(VertexBufferObjectTarget.Array)) vboColorData;
+	VertexArrayObject vao;
+	VertexBufferObject!(VertexBufferObjectTarget.Array) vboVertexData;
+	VertexBufferObject!(VertexBufferObjectTarget.Array) vboNormalData;
+	VertexBufferObject!(VertexBufferObjectTarget.Array) vboTextureData;
+	VertexBufferObject!(VertexBufferObjectTarget.Array) vboColorData;
 
 	this(string filePath) {
 		writeln("loading scene: ", filePath);
@@ -87,9 +89,11 @@ public:
 
 		//-----------------------------
 		// upload
+		writeln("uploading mesh: ", filePath);
 		vao = new VertexArrayObject();
 		vao.bind();
-		
+
+		writeln("vertex data");
 		vboVertexData = new VertexBufferObject!(VertexBufferObjectTarget.Array);
 		vboVertexData.bind();
 		GLuint attribIndex = 0;
@@ -97,7 +101,8 @@ public:
 		glEnableVertexAttribArray(attribIndex);
 		glVertexAttribPointer(attribIndex, 3, GL_FLOAT, GL_FALSE, 0, cast(void*)0);
 		vboVertexData.unbind();
-		
+
+		writeln("normal data");
 		vboNormalData = new VertexBufferObject!(VertexBufferObjectTarget.Array);
 		vboNormalData.bind();
 		attribIndex = 1;
@@ -105,7 +110,8 @@ public:
 		glEnableVertexAttribArray(attribIndex);
 		glVertexAttribPointer(attribIndex, 3, GL_FLOAT, GL_FALSE, 0, cast(void*)0);
 		vboNormalData.unbind();
-		
+
+		writeln("uv data");
 		vboTextureData = new VertexBufferObject!(VertexBufferObjectTarget.Array);
 		vboTextureData.bind();
 		attribIndex = 2;
@@ -113,7 +119,8 @@ public:
 		glEnableVertexAttribArray(attribIndex);
 		glVertexAttribPointer(attribIndex, 2, GL_FLOAT, GL_FALSE, 0, cast(void*)0);
 		vboTextureData.unbind();
-		
+
+		writeln("color data");
 		vboColorData = new VertexBufferObject!(VertexBufferObjectTarget.Array);
 		vboColorData.bind();
 		attribIndex = 3;
@@ -123,81 +130,51 @@ public:
 		vboColorData.unbind();		
 		
 		vao.unbind();
+		writeln("done");
 	}
 }
 
 
-
-class Tester {
-	Unique!(Window) _window;
-	bool _keepRunning = true;
-
-
-	this() {
-		this._window = initThree();
-		this._window.onKey.connect!"_onKey"(this);
-		this._window.onClose.connect!"_onClose"(this);
-	}
+void setupTweakbar() {
+	writeln("creating TweakBar");
+	double time        = 0, dt;              // Current time and enlapsed time
+	double turn        = 0;                  // Model turn counter
+	double speed       = 0.3;                // Model rotation speed
+	int wire           = 0;                  // Draw model in wireframe?
+	uint frameCount    = 0;
+	double fps    = 0;
+	float bgColor[3]   = [0.1f, 0.2f, 0.4f]; // Background color
+	ubyte cubeColor[4] = [255, 0, 0, 128];   // Model color (32bits RGBA)
 	
-	~this() {
-		//_window.destroy();
-		deinitThree();
-	}
-
-	void run() {
-		auto mesh = new Mesh("C:/Coding/models/Collada/duck.dae");
-
-		//-----------------
-		// Create TweakBar
-		writeln("creating TweakBar");
-		double time        = 0, dt;              // Current time and enlapsed time
-		double turn        = 0;                  // Model turn counter
-		double speed       = 0.3;                // Model rotation speed
-		int wire           = 0;                  // Draw model in wireframe?
-		uint frameCount    = 0;
-		double fps    = 0;
-		float bgColor[3]   = [0.1f, 0.2f, 0.4f]; // Background color
-		ubyte cubeColor[4] = [255, 0, 0, 128];   // Model color (32bits RGBA)
-
-		auto quat = Quaternion(0,0,0,1);
-
-//		auto w = this._window.getBounds()[2];
-//		auto h = this._window.getBounds()[3];
-		TwWindowSize(1600, 900);
-
-		// Create a tweak bar
-		auto bar = TwNewBar("TweakBar");
-		TwDefine(" GLOBAL help='This example shows how to integrate AntTweakBar with GLFW and OpenGL.' "); // Message added to the help bar.		
-		// Add 'speed' to 'bar': it is a modifable (RW) variable of type TW_TYPE_DOUBLE. Its key shortcuts are [s] and [S].
-		TwAddVarRW(bar, "speed", TW_TYPE_DOUBLE, &speed, " label='Rot speed' min=0 max=2 step=0.01 keyIncr=s keyDecr=S help='Rotation speed (turns/second)' ");		
-		// Add 'wire' to 'bar': it is a modifable variable of type TW_TYPE_BOOL32 (32 bits boolean). Its key shortcut is [w].
-		TwAddVarRW(bar, "wire", TW_TYPE_BOOL32, &wire, " label='Wireframe mode' key=CTRL+w help='Toggle wireframe display mode.' ");		
-		// Add 'time' to 'bar': it is a read-only (RO) variable of type TW_TYPE_DOUBLE, with 1 precision digit
-		TwAddVarRO(bar, "time", TW_TYPE_DOUBLE, &time, " label='Time' precision=1 help='Time (in seconds).' ");
-		//
-		TwAddVarRO(bar, "frameCount", TW_TYPE_UINT32, &frameCount, " label='FrameCount' precision=1 help='FrameCount (in counts).' ");
-		TwAddVarRO(bar, "fps", TW_TYPE_DOUBLE, &fps, " label='fps' precision=1 help='fps (in fps).' ");		
-		// Add 'bgColor' to 'bar': it is a modifable variable of type TW_TYPE_COLOR3F (3 floats color)
-		TwAddVarRW(bar, "bgColor", TW_TYPE_COLOR3F, &bgColor, " label='Background color' ");		
-		// Add 'cubeColor' to 'bar': it is a modifable variable of type TW_TYPE_COLOR32 (32 bits color) with alpha
-		TwAddVarRW(bar, "cubeColor", TW_TYPE_COLOR32, &cubeColor, " label='Cube color' alpha help='Color and transparency of the cube.' ");
-		//
-//		TwAddVarRW(bar, "quaternion", TW_TYPE_QUAT4F, &quat, " label='Cube color' alpha help='Color and transparency of the cube.' ");
-
-
-		//-----------------
-		// Create Mesh
-		Vector3f vertices[3] = [
-			Vector3f(-1.0f, -1.0f, 0.0f),
-			Vector3f( 1.0f, -1.0f, 0.0f),
-			Vector3f( 0.0f,  1.0f, 0.0f)
-		];
-
-
+	auto quat = Quaternion(0,0,0,1);
+	
+	//		auto w = this._window.getBounds()[2];
+	//		auto h = this._window.getBounds()[3];
+	TwWindowSize(1600, 900);
+	
+	// Create a tweak bar
+	auto bar = TwNewBar("TweakBar");
+	TwDefine(" GLOBAL help='This example shows how to integrate AntTweakBar with GLFW and OpenGL.' "); // Message added to the help bar.		
+	// Add 'speed' to 'bar': it is a modifable (RW) variable of type TW_TYPE_DOUBLE. Its key shortcuts are [s] and [S].
+	TwAddVarRW(bar, "speed", TW_TYPE_DOUBLE, &speed, " label='Rot speed' min=0 max=2 step=0.01 keyIncr=s keyDecr=S help='Rotation speed (turns/second)' ");		
+	// Add 'wire' to 'bar': it is a modifable variable of type TW_TYPE_BOOL32 (32 bits boolean). Its key shortcut is [w].
+	TwAddVarRW(bar, "wire", TW_TYPE_BOOL32, &wire, " label='Wireframe mode' key=CTRL+w help='Toggle wireframe display mode.' ");		
+	// Add 'time' to 'bar': it is a read-only (RO) variable of type TW_TYPE_DOUBLE, with 1 precision digit
+	TwAddVarRO(bar, "time", TW_TYPE_DOUBLE, &time, " label='Time' precision=1 help='Time (in seconds).' ");
+	//
+	TwAddVarRO(bar, "frameCount", TW_TYPE_UINT32, &frameCount, " label='FrameCount' precision=1 help='FrameCount (in counts).' ");
+	TwAddVarRO(bar, "fps", TW_TYPE_DOUBLE, &fps, " label='fps' precision=1 help='fps (in fps).' ");		
+	// Add 'bgColor' to 'bar': it is a modifable variable of type TW_TYPE_COLOR3F (3 floats color)
+	TwAddVarRW(bar, "bgColor", TW_TYPE_COLOR3F, &bgColor, " label='Background color' ");		
+	// Add 'cubeColor' to 'bar': it is a modifable variable of type TW_TYPE_COLOR32 (32 bits color) with alpha
+	TwAddVarRW(bar, "cubeColor", TW_TYPE_COLOR32, &cubeColor, " label='Cube color' alpha help='Color and transparency of the cube.' ");
+	//
+	//		TwAddVarRW(bar, "quaternion", TW_TYPE_QUAT4F, &quat, " label='Cube color' alpha help='Color and transparency of the cube.' ");
+}
 
-		//-----------------
-		// Create Shaders
-		enum vertexShaderSource = "
+//-----------------
+// Create Shaders
+enum vertexShaderSource = "
 			#version 420 core
 
 			layout(location = 0) in vec3 in_position;
@@ -220,7 +197,7 @@ class Tester {
 			}
 		";
 
-		enum fragmentShaderSource = "
+enum fragmentShaderSource = "
 			#version 420 core
 			in vec4 v_color;
 
@@ -232,25 +209,64 @@ class Tester {
 			}
 		";
 
-		Unique!(Shader!(ShaderType.Vertex)) vertexShader = new Shader!(ShaderType.Vertex)(vertexShaderSource);
-		Unique!(Shader!(ShaderType.Fragment)) fragmentShader = new Shader!(ShaderType.Fragment)(fragmentShaderSource);
 
-		assert(vertexShader.isLinked, vertexShader.infoLog());
-		assert(fragmentShader.isLinked, fragmentShader.infoLog());
+auto createShaderPipeline(Shader!(ShaderType.Vertex) vertexShader, Shader!(ShaderType.Fragment) fragmentShader) {	
+	assert(vertexShader.isLinked, vertexShader.infoLog());
+	assert(fragmentShader.isLinked, fragmentShader.infoLog());
+
+	writeln("a: ");
+	auto shaderPipeline = new ShaderPipeline();
+	shaderPipeline.bind();
+	shaderPipeline.use(vertexShader);
+	shaderPipeline.use(fragmentShader);
+	writeln("b: ");
+	assert(shaderPipeline.isValidProgramPipeline, shaderPipeline.infoLog());
+	shaderPipeline.unbind();
+	writeln("c: ");
+	return shaderPipeline;
+}
 
-		Unique!(ShaderPipeline) shaderPipeline = new ShaderPipeline();
-		shaderPipeline.bind();
-		shaderPipeline.use(vertexShader);
-		shaderPipeline.use(fragmentShader);
-		assert(shaderPipeline.isValidProgramPipeline, shaderPipeline.infoLog());
-		shaderPipeline.unbind();
+class Tester {
+	Window _window;
+	bool _keepRunning = true;
 
+	this() {
+		this._window = initThree();
+		this._window.onKey.connect!"_onKey"(this);
+		this._window.onClose.connect!"_onClose"(this);
+	}
+	
+	~this() {
+		import core.memory;
+		writeln("GC.collect: ");
+		GC.collect();
+		//Collect window _AFTER_ everything else
+		this._window = null;
+		GC.collect();
+		deinitThree();
+	}
+
+	void run() {
+
+		auto mesh = new Mesh("C:/Coding/models/Collada/duck.dae");
+
+
+		setupTweakbar();
 
+		writeln("creating shaders: ");
+		auto vertexShader = new Shader!(ShaderType.Vertex)(vertexShaderSource);
+		auto fragmentShader = new Shader!(ShaderType.Fragment)(fragmentShaderSource);
+		writeln("creating shader pipeline: ");
+		auto shaderPipeline = createShaderPipeline(vertexShader, fragmentShader);
+
+
+		writeln("connectiong window callbacks: ");
 		this._window.onSize.connect!"onSize"(this);
 		this._window.onPosition.connect!"onPosition"(this);
 		this._window.onButton.connect!"onButton"(this);
 		this._window.onCursorPos.connect!"onCursorPos"(this);
 
+		writeln("begin render loop: ");
 		//-----------------
 		// Render Loop
 		glfwSetTime(0);
@@ -269,42 +285,39 @@ class Tester {
 			TwDraw();
 
 			this._window.swapBuffers();
+			updateWindows();
 
-			++frameCount;
-			//if(frameCount % 100 == 0) {
-				updateWindows();
-				time = glfwGetTime();
-				fps = cast(double)frameCount / time;
-			//}
+
+//			++frameCount;
+//			//if(frameCount % 100 == 0) {
+//				time = glfwGetTime();
+//				fps = cast(double)frameCount / time;
+//			//}
 		}
 	}
 
-
-	void onSize(Window w, int x, int y) {
+	void onSize(Window window, int width, int height) {
+		TwWindowSize(width, height);
 	}
 
-	void onPosition(Window w, int x, int y) {
+	void onPosition(Window window, int x, int y) {
 	}
 
+	void onButton(Window window , int button, ButtonAction action) {
+		TwMouseAction twaction = action == ButtonAction.Pressed ? TW_MOUSE_PRESSED : TW_MOUSE_RELEASED;
+		TwMouseButtonID twbutton;
 
-//#define GLFW_MOUSE_BUTTON_LAST      GLFW_MOUSE_BUTTON_8
-//#define GLFW_MOUSE_BUTTON_LEFT      GLFW_MOUSE_BUTTON_1
-//#define GLFW_MOUSE_BUTTON_RIGHT     GLFW_MOUSE_BUTTON_2
-//#define GLFW_MOUSE_BUTTON_MIDDLE    GLFW_MOUSE_BUTTON_3
-//
-//
-//	TW_MOUSE_LEFT   = 1,
-//		TW_MOUSE_MIDDLE = 2,
-//		TW_MOUSE_RIGHT  = 3
-
-	void onButton(Window w , int b, ButtonAction a) {
-		TwMouseAction action = a == ButtonAction.Pressed ? TW_MOUSE_PRESSED : TW_MOUSE_RELEASED;
-		TwMouseButtonID button = b;
-		TwMouseButton(action, button);
-	}
+		switch(button) {
+			default:
+			case GLFW_MOUSE_BUTTON_LEFT: twbutton = TW_MOUSE_LEFT; break;
+			case GLFW_MOUSE_BUTTON_RIGHT: twbutton = TW_MOUSE_RIGHT; break;
+			case GLFW_MOUSE_BUTTON_MIDDLE: twbutton = TW_MOUSE_MIDDLE; break;			
+		}
 
-	void onCursorPos(Window w, double x, double y) {
+		TwMouseButton(twaction, twbutton);
+	}
 
+	void onCursorPos(Window window, double x, double y) {
 		TwMouseMotion(cast(int)x, this._window.getBounds()[3] - cast(int)y);
 	}
 	
@@ -313,7 +326,7 @@ class Tester {
 	}
 	
 	void _onKey(Window window, Key key, ScanCode scanCode, KeyAction action, KeyMod keyMod) {
-		if(window is this._window.opDot() && action == KeyAction.Pressed) {
+		if(window is this._window && action == KeyAction.Pressed) {
 			if(key == Key.Escape) {
 				this.stop();
 			}

+ 4 - 0
source/three/gl/framebuffer.d

@@ -11,6 +11,8 @@ module three.gl.framebuffer;
 import derelict.opengl3.gl3;
 import three.gl.util;
 
+import std.stdio;
+
 
 //==============================================================================
 ///
@@ -56,11 +58,13 @@ public:
 	///
 	this() {
 		check!glGenFramebuffers(1, &this._id); 
+		writeln("Framebuffer created: ", this._id);
 	}
 
 	///
 	~this() {
 		check!glDeleteFramebuffers(1, &this._id);
+		writeln("Framebuffer destroyed: ", this._id);
 	}
 	
 public:	   

+ 3 - 0
source/three/gl/renderbuffer.d

@@ -11,6 +11,7 @@ module three.gl.renderbuffer;
 import derelict.opengl3.gl3;
 import three.gl.util;
 
+import std.stdio;
 
 //==============================================================================
 ///
@@ -22,11 +23,13 @@ public:
 	///
 	this() {
 		check!glGenRenderbuffers(1, &this._id);
+		writeln("Renderbuffer created: ", this._id);
 	}
 
 	///
 	~this() {
 		check!glDeleteRenderbuffers(1, &this._id); 
+		writeln("Renderbuffer destroxed: ", this._id);
 	}
 	
 public:	  

+ 7 - 2
source/three/gl/shader.d

@@ -12,6 +12,7 @@ import derelict.opengl3.gl3;
 import three.gl.util;
 
 import std.string;
+import std.stdio;
 
 //==============================================================================
 ///
@@ -44,11 +45,13 @@ public:
 	this(string source) {
 		auto szSource = [source.toStringz()];
 		this._id = check!glCreateShaderProgramv(TYPE, 1, szSource.ptr);
+		writeln("Shader created: ", this._id);
 	}
 
 	///
 	~this() {
 		check!glDeleteProgram(this._id);
+		writeln("Shader deleted: ", this._id);
 	}
 
 public:	  
@@ -81,7 +84,7 @@ public:
 	}	
 
 	///
-	string infoLog() {
+	string infoLog() const {
 		int len;
 		check!glGetProgramiv(this._id, GL_INFO_LOG_LENGTH , &len);
 		if (len > 1) {
@@ -105,11 +108,13 @@ public:
 	///
 	this() { 
 		glGenProgramPipelines(1, &this._id); 
+		writeln("ShaderPipeline created: ", this._id);
 	}
 
 	///
 	~this() { 
 		glDeleteProgramPipelines(1, &this._id);
+		writeln("ShaderPipeline deleted: ", this._id);
 	}
 	
 public:			
@@ -151,7 +156,7 @@ public:
 	}
 
 	///
-	string infoLog() {
+	string infoLog() const {
 		int len;
 		glGetProgramiv(this._id, GL_INFO_LOG_LENGTH , &len);
 		if (len > 1) {

+ 4 - 0
source/three/gl/texture.d

@@ -11,6 +11,8 @@ module three.gl.texture;
 import derelict.opengl3.gl3;
 import three.gl.util;
 
+import std.stdio;
+
 //==============================================================================
 enum TextureTarget : GLenum {		
 	Texture1D = GL_TEXTURE_1D, 
@@ -323,11 +325,13 @@ public:
 	///
 	this() {
 		check!glGenTextures(1, &this._id);
+		writeln("Texture created: ", this._id);
 	}
 
 	///
 	~this() {
 		check!glDeleteTextures(1, &this._id);
+		writeln("Texture destroyed: ", this._id);
 	}
 	
 public:				

+ 4 - 1
source/three/gl/vao.d

@@ -11,7 +11,7 @@ module three.gl.vao;
 import derelict.opengl3.gl3;
 import three.gl.util;
 
-
+import std.stdio;
 //==============================================================================
 ///
 final class VertexArrayObject {
@@ -22,11 +22,14 @@ public:
 	///
 	this() {
 		check!glGenVertexArrays(1, &this._id);
+		writeln("vao created: ", this._id);
 	}
 
 	///
 	~this() {
+		writeln("vao destroxing..: ", this._id);
 		check!glDeleteVertexArrays(1, &this._id);
+		writeln("vao destroyed: ", this._id);
 	}
 	
 public:		   

+ 3 - 1
source/three/gl/vbo.d

@@ -45,7 +45,7 @@ enum BufferUsageHint {
 	DynamicCopy = GL_DYNAMIC_COPY
 }
 
-
+import std.stdio;
 //==============================================================================
 ///
 final class VertexBufferObject(VertexBufferObjectTarget target) {
@@ -56,11 +56,13 @@ public:
 	///
 	this() {
 		check!glGenBuffers(1, &this._id);
+		writeln("vbo created: ", this._id);
 	}
 
 	///
 	~this() {
 		check!glDeleteBuffers(1, &this._id);
+		writeln("vbo destroyed: ", this._id);
 	}
 	
 public:		 

+ 3 - 0
source/three/glfw/window.d

@@ -15,6 +15,7 @@ import std.string;
 import stdx.signals;
 
 import three.gfx.color;
+import std.stdio;
 
 //==============================================================================
 ///
@@ -72,6 +73,7 @@ public:
 
 		this.makeAktiveRenderWindow();
 		this.clear(1,0,0,1);
+		writeln("Window created: ", this._glfwWindow);
 	}
 
 	///
@@ -89,6 +91,7 @@ public:
 		glfwSetCharCallback(this._glfwWindow, null);
 
 		glfwDestroyWindow(this._glfwWindow);
+		writeln("Window destroyed: ", this._glfwWindow);
 	}
 
 public:		  

+ 3 - 4
source/three/init.d

@@ -11,12 +11,11 @@ import three.glfw.window;
 
 import std.stdio;
 import std.conv;
-import std.typecons;
 
 
 private static FT_Library _s_freeTypeLibrary;
 
-Unique!(Window) initThree() {
+Window initThree() {
 	"Starting Three.d".writeln();
 
 	"Loading OpenGL".writeln();
@@ -41,7 +40,7 @@ Unique!(Window) initThree() {
 	if(!glfwInit()) throw new Exception("Initialising GLFW failed");
 
 	"Creating Window".writeln();
-	Unique!(Window) window = new Window("Fray", 1600, 900);
+	auto window = new Window("Fray", 1600, 900);
 	
 	"ReLoading OpenGL".writeln();
 	try {
@@ -57,7 +56,7 @@ Unique!(Window) initThree() {
 	"Initialising AntTweakBar".writeln();
 	if(TwInit(TW_OPENGL_CORE, null) == 0) throw new Exception("Initialising AntTweakBar failed");
 
-	return window.release();
+	return window;
 }
 
 void deinitThree() {