Selaa lähdekoodia

revised model loading code

Zoadian 11 vuotta sitten
vanhempi
sitoutus
570ff59716
5 muutettua tiedostoa jossa 78 lisäystä ja 141 poistoa
  1. 3 2
      source/app.d
  2. 66 115
      source/three/mesh.d
  3. 4 23
      source/three/renderer.d
  4. 5 0
      source/three/renderer_.d
  5. 0 1
      source/three/scene.d

+ 3 - 2
source/app.d

@@ -67,8 +67,9 @@ void main() {
 	//------------------------------------------------
 	// Create Scene
 	//------------------------------------------------
-	scene.mesh.loadModel("C:/Coding/models/Collada/duck.dae");
-	
+	ModelData modelData;
+	loadModelData(modelData, "C:/Coding/models/Collada/duck.dae");
+	renderer.uploadModelData(modelData);
 	
 	//------------------------------------------------
 	// Generate TweakBar

+ 66 - 115
source/three/mesh.d

@@ -2,142 +2,93 @@
 
 import three.common;
 
-struct DrawArraysIndirectCommand {
-	GLuint vertexCount;
-	GLuint instanceCount;
-	GLuint firstVertex;
-	GLuint baseInstance;
+
+
+struct Position {
+	float x, y, z;
+}
+
+struct Normal {
+	float x, y, z;
+}
+
+struct Color {
+	float r, g, b, a;
+}
+
+struct TextureCoordinate {
+	float u, v;
+}
+
+struct Matrix4 {
+	float[4*4] data;
 }
 
-struct DrawElementsIndirectCommand {
-	GLuint count;
-	GLuint instanceCount;
-	GLuint firstIndex;
-	GLuint baseVertex;
-	GLuint baseInstance;
+struct VertexData {
+	Position position;
+	Normal normal;
+	Color color;
+	TextureCoordinate textureCoordinate;
 }
 
-struct SOAMesh {
-	SoA!GLuint vao;
-	SoA!GLuint vboVertices;
-	SoA!GLuint vboNormals;
-	SoA!GLuint vboTexcoords;
-	SoA!GLuint vboColors;
-	SoA!GLuint vboIndices;
-	SoA!GLuint cntIndices;
-	size_t cnt;
+alias IndexData = uint;
+
+struct MeshData {
+	VertexData[] vertexData;
+	IndexData[] indexData;
 }
 
-void loadModel(ref SOAMesh mesh, string filePath) {
+struct ModelData {
+	MeshData[] meshData;
+}
+
+void loadModelData(out ModelData modelData, string filePath) {
 	import std.traits;
 	import std.string : toStringz;
 	auto scene = aiImportFile(filePath.toStringz(),	aiProcess_Triangulate); scope(exit) aiReleaseImport(scene);
-	
+
+	modelData.meshData.length = scene.mNumMeshes;
+
 	for(uint m = 0; m < scene.mNumMeshes; ++m) {	
 		const(aiMesh*) meshData = scene.mMeshes[m];
 		assert(meshData !is null);
-		
-		//-----------------------------
-		// create mesh
-		auto meshIdx = mesh.cnt;
-		++mesh.cnt;
-		mesh.vao.length = mesh.cnt;
-		mesh.vboVertices.length = mesh.cnt;
-		mesh.vboNormals.length = mesh.cnt;
-		mesh.vboTexcoords.length = mesh.cnt;
-		mesh.vboIndices.length = mesh.cnt;
-		mesh.cntIndices.length = mesh.cnt;
-		
-		//-----------------------------
-		// upload data
-		glCheck!glGenVertexArrays(1, &mesh.vao[meshIdx]);
-		glCheck!glBindVertexArray(mesh.vao[meshIdx]); scope(exit) glCheck!glBindVertexArray(0);
-		
-		alias Vertex = float[3];
-		alias Normal = float[3];
-		alias TexCoord = float[2];
-		alias Color = float[4];
-		alias Index = uint[1];
 
 		size_t cntIndices = 0;
 		foreach(f; 0..meshData.mNumFaces) {
-			cntIndices += meshData.mFaces[f].mNumIndices;				
+			cntIndices += meshData.mFaces[f].mNumIndices;
 		}
-		mesh.cntIndices[meshIdx] = cntIndices;
-		
-		{// upload vertex data
-			Vertex[] vertexData;
-			vertexData.length = meshData.mNumVertices;
-			foreach(v; 0..meshData.mNumVertices) {
-				vertexData[v] = [meshData.mVertices[v].x, meshData.mVertices[v].y, meshData.mVertices[v].z];
+
+		modelData.meshData[m].vertexData.length = meshData.mNumVertices;
+		modelData.meshData[m].indexData.length = cntIndices;
+
+		foreach(v; 0..meshData.mNumVertices) {
+			modelData.meshData[m].vertexData[v].position = Position(meshData.mVertices[v].x, meshData.mVertices[v].y, meshData.mVertices[v].z);
+
+			modelData.meshData[m].vertexData[v].normal = Normal(meshData.mNormals[v].x, meshData.mNormals[v].y, meshData.mNormals[v].z);
+
+			if(meshData.mColors[0] !is null) {
+				modelData.meshData[m].vertexData[v].color = Color(meshData.mColors[0][v].r, meshData.mColors[0][v].g, meshData.mColors[0][v].b, meshData.mColors[0][v].a);
 			}
-			glCheck!glGenBuffers(1, &mesh.vboVertices[meshIdx]);
-			glCheck!glBindBuffer(GL_ARRAY_BUFFER, mesh.vboVertices[meshIdx]); scope(exit) glCheck!glBindBuffer(GL_ARRAY_BUFFER, 0); 
-			glCheck!glBufferData(GL_ARRAY_BUFFER, cast(ptrdiff_t)(Vertex.sizeof * vertexData.length) , vertexData.ptr, GL_STATIC_DRAW);	
-			GLuint attribIndex = 0;	
-			glCheck!glEnableVertexAttribArray(attribIndex);
-			glCheck!glVertexAttribPointer(attribIndex, Vertex.sizeof / ForeachType!Vertex.sizeof, GL_FLOAT, GL_FALSE, 0, cast(void*)0);
-		}
-		
-		{// upload normal data
-			Normal[] normalData;
-			normalData.length = meshData.mNumVertices;
-			foreach(v; 0..meshData.mNumVertices) {
-				normalData[v] = [meshData.mNormals[v].x, meshData.mNormals[v].y, meshData.mNormals[v].z];
+			else {
+				modelData.meshData[m].vertexData[v].color = Color(0, 0, 0, 1);
 			}
-			glCheck!glGenBuffers(1, &mesh.vboNormals[meshIdx]);
-			glCheck!glBindBuffer(GL_ARRAY_BUFFER, mesh.vboNormals[meshIdx]); scope(exit) glCheck!glBindBuffer(GL_ARRAY_BUFFER, 0); 
-			glCheck!glBufferData(GL_ARRAY_BUFFER, cast(ptrdiff_t)(Normal.sizeof * normalData.length) , normalData.ptr, GL_STATIC_DRAW);	
-			GLuint attribIndex = 1;	
-			glCheck!glEnableVertexAttribArray(attribIndex);
-			glCheck!glVertexAttribPointer(attribIndex, Normal.sizeof / ForeachType!Normal.sizeof, GL_FLOAT, GL_FALSE, 0, cast(void*)0);
-		}
-		
-		if(meshData.mTextureCoords[0] !is null) {// upload texture data
-			TexCoord[] textureData;
-			textureData.length = meshData.mNumVertices;
-			foreach(v; 0..meshData.mNumVertices) {
-				textureData[v] = [meshData.mTextureCoords[0][v].x, meshData.mTextureCoords[0][v].y];
+
+			if(meshData.mTextureCoords[0] !is null) {
+				modelData.meshData[m].vertexData[v].textureCoordinate = TextureCoordinate(meshData.mTextureCoords[0][v].x, meshData.mTextureCoords[0][v].y);
 			}
-			glCheck!glGenBuffers(1, &mesh.vboTexcoords[meshIdx]);
-			glCheck!glBindBuffer(GL_ARRAY_BUFFER, mesh.vboTexcoords[meshIdx]); scope(exit) glCheck!glBindBuffer(GL_ARRAY_BUFFER, 0); 
-			glBufferData(GL_ARRAY_BUFFER, cast(ptrdiff_t)(TexCoord.sizeof * textureData.length) , textureData.ptr, GL_STATIC_DRAW);		
-			GLuint attribIndex = 2;
-			glEnableVertexAttribArray(attribIndex);
-			glVertexAttribPointer(attribIndex, TexCoord.sizeof / ForeachType!TexCoord.sizeof, GL_FLOAT, GL_FALSE, 0, cast(void*)0);
-		}
-		
-		if(meshData.mColors[0] !is null) {// upload color data
-			Color[] colorData;
-			colorData.length = meshData.mNumVertices;
-			foreach(v; 0..meshData.mNumVertices) {
-				colorData[v] = [meshData.mColors[0][v].r, meshData.mColors[0][v].g, meshData.mColors[0][v].b, meshData.mColors[0][v].a];
+			else {
+				modelData.meshData[m].vertexData[v].textureCoordinate = TextureCoordinate(0, 0);
 			}
-			glCheck!glGenBuffers(1, &mesh.vboTexcoords[meshIdx]);
-			glCheck!glBindBuffer(GL_ARRAY_BUFFER, mesh.vboTexcoords[meshIdx]); scope(exit) glCheck!glBindBuffer(GL_ARRAY_BUFFER, 0); 
-			glBufferData(GL_ARRAY_BUFFER, cast(ptrdiff_t)(Color.sizeof * colorData.length) , colorData.ptr, GL_STATIC_DRAW);	
-			GLuint attribIndex = 3;	
-			glEnableVertexAttribArray(attribIndex);
-			glVertexAttribPointer(attribIndex, Color.sizeof / ForeachType!Color.sizeof, GL_FLOAT, GL_FALSE, 0, cast(void*)0);
 		}
-		
-		{// upload index data
-			Index[] indexData;
-			indexData.length = cntIndices;
-			size_t curIndexDataIdx = 0;
-			foreach(f; 0..meshData.mNumFaces) {
-				assert(meshData.mFaces !is null);
-				foreach(i; 0..meshData.mFaces[f].mNumIndices) {
-					assert(meshData.mFaces[f].mIndices !is null);
-					indexData[curIndexDataIdx++] = meshData.mFaces[f].mIndices[i];		
-				}
+
+		size_t curIndexDataIdx = 0;
+		foreach(f; 0..meshData.mNumFaces) {
+			assert(meshData.mFaces !is null);
+			foreach(i; 0..meshData.mFaces[f].mNumIndices) {
+				assert(meshData.mFaces[f].mIndices !is null);
+				modelData.meshData[m].indexData[curIndexDataIdx++] = meshData.mFaces[f].mIndices[i];		
 			}
-			glCheck!glGenBuffers(1, &mesh.vboIndices[meshIdx]);
-			glCheck!glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh.vboIndices[meshIdx]); scope(exit) glCheck!glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); 
-			glCheck!glBufferData(GL_ELEMENT_ARRAY_BUFFER, cast(ptrdiff_t)(Index.sizeof * indexData.length) , indexData.ptr, GL_STATIC_DRAW);	
-//			GLuint attribIndex = 4;	
-//			glCheck!glEnableVertexAttribArray(attribIndex);
-//			glCheck!glVertexAttribPointer(attribIndex, Index.sizeof / ForeachType!Index.sizeof, GL_UNSIGNED_INT, GL_FALSE, 0, cast(void*)0);
 		}
+
 	}
-}
+}

+ 4 - 23
source/three/renderer.d

@@ -5,6 +5,7 @@ import three.scene;
 import three.camera;
 import three.renderTarget;
 import three.viewport;
+import three.mesh;
 
 import std.string : toStringz;
 import std.exception : collectException;
@@ -80,29 +81,6 @@ struct DrawElementsIndirectCommand {
 
 
 
-struct Position {
-	float x, y, z;
-}
-
-struct Normal {
-	float x, y, z;
-}
-
-struct Color {
-	float r, g, b, a;
-}
-
-struct Matrix4 {
-	float[4*4] data;
-}
-
-struct VertexData {
-	Position position;
-	Normal normal;
-	Color color;
-}
-
-alias IndexData = uint;
 
 struct DrawParameter {
 	Matrix4 transformationMatrix;
@@ -138,6 +116,9 @@ void destruct(ref Renderer renderer) {
 	renderer = renderer.init;
 }
 
+void uploadModelData(ref Renderer renderer, ModelData modelData) {
+}
+
 void renderOneFrame(ref Renderer renderer, ref Scene scene, ref Camera camera, ref RenderTarget renderTarget, ref Viewport viewport) {
 	
 	/*

+ 5 - 0
source/three/renderer_.d

@@ -1,5 +1,7 @@
 module three.rendererx;
 
+version(none) {
+
 import three.common;
 import three.scene;
 import three.camera;
@@ -280,4 +282,7 @@ debug {
 		glCheck!glReadBuffer(GL_COLOR_ATTACHMENT0 + 2);
 		glCheck!glBlitFramebuffer(0, 0, width, height, width-400, height-300, width, height, GL_COLOR_BUFFER_BIT, GL_LINEAR);
 	}
+}
+
+
 }

+ 0 - 1
source/three/scene.d

@@ -4,7 +4,6 @@ import three.common;
 import three.mesh;
 
 struct Scene {
-	SOAMesh mesh;
 }
 
 void construct(out Scene scene) pure @safe nothrow @nogc {