Ver código fonte

render simple triangle and make use of openGL vao + vbo + shader + shaderPipeline

Zoadian 11 anos atrás
pai
commit
ac715e3910

+ 2 - 3
package.json

@@ -6,13 +6,12 @@
 	"authors": ["Felix 'Zoadian' Hufnagel"],
 	
 	"targetPath": "bin",
-	"targetType": "library",
 	
 	"dependencies": {
-		"derelict-util" : "~master",
 		"derelict-gl3" : "~master",
 		"derelict-glfw3" : "~master",
 		"derelict-ft" : "~master",
-		"stdx" : "~master"
+		"stdx" : "~master",
+		"nitro" : "~master"
 	}
 }

+ 84 - 1
source/app.d

@@ -3,10 +3,14 @@ import std.stdio;
 import three;
 import std.typecons;
 
+import derelict.opengl3.gl3;
+import three.gl.util;
+
 class Tester {
 	Unique!(Window) _window;
 	bool _keepRunning = true;
-	
+
+
 	this() {
 		this._window = initThree();
 		this._window.onKey.connect!"_onKey"(this);
@@ -19,8 +23,87 @@ class Tester {
 	}
 	
 	void run() {
+		//-----------------
+		// 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)
+		];
+
+		Unique!(VertexArrayObject) vao = new VertexArrayObject();
+		vao.bind();
+		Unique!(VertexBufferObject!(VertexBufferObjectTarget.Array)) vbo = new VertexBufferObject!(VertexBufferObjectTarget.Array);
+		vbo.bind();
+
+		GLuint attribIndex = 0;
+		glBufferData(GL_ARRAY_BUFFER, cast(ptrdiff_t)(vertices.sizeof) , vertices.ptr, GL_STATIC_DRAW);		
+		glEnableVertexAttribArray(attribIndex);
+		glVertexAttribPointer(attribIndex, 3, GL_FLOAT, GL_FALSE, 0, cast(void*)0);
+		vbo.unbind();
+		vao.unbind();
+
+		//-----------------
+		// Create Shaders
+		enum vertexShaderSource = "
+			#version 420 core
+
+			layout(location = 0) in vec3 in_position;
+			//layout(location = 1) in vec3 in_normal;
+			//layout(location = 2) in vec2 in_texcoord;
+			//layout(location = 3) in vec3 in_color;
+
+			out gl_PerVertex 
+			{
+		    	vec4 gl_Position;
+		 	};
+
+			void main()
+			{
+				gl_Position = vec4(0.5 * in_position.x, 0.5 * in_position.y, in_position.z, 1.0);
+			}
+		";
+
+		enum fragmentShaderSource = "
+			#version 420 core
+
+			out vec4 FragColor;
+
+			void main()
+			{
+				FragColor = vec4(1.0, 0.0, 0.0, 1.0);
+			}
+		";
+
+		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());
+
+		Unique!(ShaderPipeline) shaderPipeline = new ShaderPipeline();
+		shaderPipeline.bind();
+		shaderPipeline.use(vertexShader);
+		shaderPipeline.use(fragmentShader);
+		assert(shaderPipeline.isValidProgramPipeline, shaderPipeline.infoLog());
+		shaderPipeline.unbind();
+
+		//-----------------
+		// Render Loop
 		while(this._keepRunning) {
 			updateWindows();
+					
+			this._window.clear(0, 0, 0.5, 1);
+
+			shaderPipeline.bind();
+			vao.bind();
+			vbo.bind();
+			glDrawArrays(GL_TRIANGLES, 0, 3);
+			vbo.unbind();
+			vao.unbind();
+			shaderPipeline.unbind();
+
+			this._window.swapBuffers();			
 		}
 	}
 	

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

@@ -66,6 +66,7 @@ public:
 public:	   
 	///
 	void bind(FramebufferTarget target) { 
+		assert(this.isValid);
 		check!glBindFramebuffer(target, this._id);
 	}    
 

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

@@ -32,6 +32,7 @@ public:
 public:	  
 	///
 	void bind() { 
+		assert(this.isValid);
 		check!glBindRenderbuffer(GL_RENDERBUFFER, this._id);
 	}
 

+ 14 - 3
source/three/gl/shader.d

@@ -11,6 +11,7 @@ module three.gl.shader;
 import derelict.opengl3.gl3;
 import three.gl.util;
 
+import std.string;
 
 //==============================================================================
 ///
@@ -22,13 +23,22 @@ enum ShaderType {
 	TesselationEvaluation 	= GL_TESS_EVALUATION_SHADER
 }
 
+private template shaderTypeBitIdentifier(ShaderType TYPE)
+{
+	static if(TYPE == ShaderType.Vertex) enum shaderTypeBitIdentifier = GL_VERTEX_SHADER_BIT;
+	else static if(TYPE == ShaderType.Fragment) enum shaderTypeBitIdentifier = GL_FRAGMENT_SHADER_BIT;
+	else static if(TYPE == ShaderType.Geometry) enum shaderTypeBitIdentifier = GL_GEOMETRY_SHADER_BIT;
+	else static if(TYPE == ShaderType.TesselationControl) enum shaderTypeBitIdentifier = GL_TESS_CONTROL_SHADER_BIT;
+	else static if(TYPE == ShaderType.TesselationEvaluation) enum shaderTypeBitIdentifier = GL_TESS_EVALUATION_SHADER_BIT;
+}
 
 //==============================================================================
 ///
 final class Shader(ShaderType TYPE) {		
 private:
 	uint _id;
-
+	int[string] _uniformLocationCache;
+	alias type = TYPE;
 public:	   
 	///
 	this(string source) {
@@ -62,8 +72,8 @@ public:
 		auto szName = name.toStringz();
 		assert(this._id > 0);
 		int x = check!glGetUniformLocation(this._id, &szName[0]);
-		//assert(x != -1, "wrong uniform location : " ~ name);
-		try{if(x == -1) "wrong uniform location : ".writeln(name);}catch(Exception e){}
+		assert(x != -1, "wrong uniform location : " ~ name);
+		//try{if(x == -1) "wrong uniform location : ".writeln(name);}catch(Exception e){}
 		
 		this._uniformLocationCache[name] = x;
 		//if(x == -1) throw new Exception("uniform location "~name~" not found");
@@ -119,6 +129,7 @@ public:
 public:    
 	/// 
 	void bind()  { 
+		assert(this.isValid);
 		glBindProgramPipeline(this._id); 
 	}
 

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

@@ -32,6 +32,7 @@ public:
 public:		   
 	///
 	void bind() { 
+		assert(this.isValid);
 		check!glBindVertexArray(this._id);
 	}
 

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

@@ -66,6 +66,7 @@ public:
 public:		 
 	///
 	void bind() { 
+		assert(this.isValid);
 		check!glBindBuffer(target, this._id);
 	}
 

+ 2 - 1
source/three/glfw/window.d

@@ -14,6 +14,7 @@ import derelict.glfw3.glfw3;
 import std.string;
 import stdx.signals;
 
+import three.gfx.color;
 
 //==============================================================================
 ///
@@ -101,7 +102,7 @@ public:
 		return _buttonStates.get(button, ButtonAction.Released);
 	}
 
-public:			
+public:		
 	///
 	void clear(float r, float g, float b, float a, float depth = 1.0f, GLenum bits = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) {
 		glClearColor(r, g, b, a);

+ 4 - 0
source/three/primitives/point.d

@@ -15,6 +15,10 @@ public:
 	T[D] data;
 	
 	alias data this;
+
+	this(T[] data...) {
+		this.data = data;
+	}
 	
 	static if(D >= 1) {
 		T x() @safe @property const {