private static void DrawToGL(Mesh meshToRender, bool isTransparent, Matrix4X4?meshToViewTransform, bool blendTexture = true, bool allowBspRendering = true) { if (!blendTexture) { // Turn off default GL_MODULATE mode GL.TexEnv(TextureEnvironmentTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, GL_REPLACE); } if (meshToViewTransform != null && isTransparent && meshToRender.FaceBspTree != null && meshToRender.Faces.Count > 0 && allowBspRendering) { var invMeshToViewTransform = meshToViewTransform.Value; invMeshToViewTransform.Invert(); DrawToGLZSorted(meshToRender, meshToViewTransform.Value, invMeshToViewTransform); if (!blendTexture) { // Restore default GL_MODULATE mode GL.TexEnv(TextureEnvironmentTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, GL_MODULATE); } return; } var glMeshPlugin = GLMeshTrianglePlugin.Get(meshToRender); for (int i = 0; i < glMeshPlugin.subMeshs.Count; i++) { SubTriangleMesh subMesh = glMeshPlugin.subMeshs[i]; // Make sure the GLMeshPlugin has a reference to hold onto the image so it does not go away before this. if (subMesh.texture != null) { if (subMesh.texture.HasTransparency) { GL.Enable(EnableCap.Blend); } var glPlugin = ImageGlPlugin.GetImageGlPlugin(subMesh.texture, true); GL.Enable(EnableCap.Texture2D); GL.BindTexture(TextureTarget.Texture2D, glPlugin.GLTextureHandle); GL.EnableClientState(ArrayCap.TextureCoordArray); } else { GL.Disable(EnableCap.Texture2D); GL.DisableClientState(ArrayCap.TextureCoordArray); } if (subMesh.UseVertexColors) { GL.EnableClientState(ArrayCap.ColorArray); } GL.EnableClientState(ArrayCap.NormalArray); GL.EnableClientState(ArrayCap.VertexArray); unsafe { fixed(VertexTextureData *pTextureData = subMesh.textureData.Array) { fixed(VertexColorData *pColorData = subMesh.colorData.Array) { fixed(VertexNormalData *pNormalData = subMesh.normalData.Array) { fixed(VertexPositionData *pPosition = subMesh.positionData.Array) { GL.VertexPointer(3, VertexPointerType.Float, 0, new IntPtr(pPosition)); GL.NormalPointer(NormalPointerType.Float, 0, new IntPtr(pNormalData)); GL.TexCoordPointer(2, TexCordPointerType.Float, 0, new IntPtr(pTextureData)); if (pColorData != null) { GL.ColorPointer(3, ColorPointerType.UnsignedByte, 0, new IntPtr(pColorData)); } GL.DrawArrays(BeginMode.Triangles, 0, subMesh.positionData.Count); } } } } } GL.DisableClientState(ArrayCap.NormalArray); GL.DisableClientState(ArrayCap.VertexArray); GL.DisableClientState(ArrayCap.TextureCoordArray); GL.DisableClientState(ArrayCap.ColorArray); GL.TexCoordPointer(2, TexCordPointerType.Float, 0, new IntPtr(0)); GL.ColorPointer(3, ColorPointerType.UnsignedByte, 0, new IntPtr(0)); GL.NormalPointer(NormalPointerType.Float, 0, new IntPtr(0)); GL.VertexPointer(3, VertexPointerType.Float, 0, new IntPtr(0)); if (subMesh.texture != null) { GL.DisableClientState(ArrayCap.TextureCoordArray); } } if (!blendTexture) { // Restore default GL_MODULATE mode GL.TexEnv(TextureEnvironmentTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, GL_MODULATE); } }
private void CreateRenderData(Mesh meshToBuildListFor, Func <Vector3Float, Color> getColorFunc) { subMeshs = new List <SubTriangleMesh>(); SubTriangleMesh currentSubMesh = null; VectorPOD <VertexTextureData> textureData = null; VectorPOD <VertexColorData> colorData = null; VectorPOD <VertexNormalData> normalData = null; VectorPOD <VertexPositionData> positionData = null; // first make sure all the textures are created for (int faceIndex = 0; faceIndex < meshToBuildListFor.Faces.Count; faceIndex++) { FaceTextureData faceTexture; meshToBuildListFor.FaceTextures.TryGetValue(faceIndex, out faceTexture); if (faceTexture != null) { ImageGlPlugin.GetImageGlPlugin(faceTexture.image, true); } // don't compare the data of the texture but rather if they are just the same object if (subMeshs.Count == 0 || (faceTexture != null && (object)subMeshs[subMeshs.Count - 1].texture != (object)faceTexture.image)) { SubTriangleMesh newSubMesh = new SubTriangleMesh(); newSubMesh.texture = faceTexture == null ? null : faceTexture.image; subMeshs.Add(newSubMesh); if (getColorFunc != null) { newSubMesh.UseVertexColors = true; } currentSubMesh = subMeshs[subMeshs.Count - 1]; textureData = currentSubMesh.textureData; colorData = currentSubMesh.colorData; normalData = currentSubMesh.normalData; positionData = currentSubMesh.positionData; } VertexColorData color = new VertexColorData(); if (getColorFunc != null) { var faceColor = getColorFunc(meshToBuildListFor.Faces[faceIndex].normal); color = new VertexColorData { red = faceColor.red, green = faceColor.green, blue = faceColor.blue }; } VertexTextureData tempTexture; VertexNormalData tempNormal; VertexPositionData tempPosition; tempTexture.textureU = faceTexture == null ? 0 : (float)faceTexture.uv0.X; tempTexture.textureV = faceTexture == null ? 0 : (float)faceTexture.uv0.Y; var normal = meshToBuildListFor.Faces[faceIndex].normal; tempNormal.normalX = normal.X; tempNormal.normalY = normal.Y; tempNormal.normalZ = normal.Z; int vertexIndex = meshToBuildListFor.Faces[faceIndex].v0; tempPosition.positionX = (float)meshToBuildListFor.Vertices[vertexIndex].X; tempPosition.positionY = (float)meshToBuildListFor.Vertices[vertexIndex].Y; tempPosition.positionZ = (float)meshToBuildListFor.Vertices[vertexIndex].Z; textureData.Add(tempTexture); normalData.Add(tempNormal); positionData.Add(tempPosition); colorData.add(color); tempTexture.textureU = faceTexture == null ? 0 : (float)faceTexture.uv1.X; tempTexture.textureV = faceTexture == null ? 0 : (float)faceTexture.uv1.Y; vertexIndex = meshToBuildListFor.Faces[faceIndex].v1; tempPosition.positionX = (float)meshToBuildListFor.Vertices[vertexIndex].X; tempPosition.positionY = (float)meshToBuildListFor.Vertices[vertexIndex].Y; tempPosition.positionZ = (float)meshToBuildListFor.Vertices[vertexIndex].Z; textureData.Add(tempTexture); normalData.Add(tempNormal); positionData.Add(tempPosition); colorData.add(color); tempTexture.textureU = faceTexture == null ? 0 : (float)faceTexture.uv2.X; tempTexture.textureV = faceTexture == null ? 0 : (float)faceTexture.uv2.Y; vertexIndex = meshToBuildListFor.Faces[faceIndex].v2; tempPosition.positionX = (float)meshToBuildListFor.Vertices[vertexIndex].X; tempPosition.positionY = (float)meshToBuildListFor.Vertices[vertexIndex].Y; tempPosition.positionZ = (float)meshToBuildListFor.Vertices[vertexIndex].Z; textureData.Add(tempTexture); normalData.Add(tempNormal); positionData.Add(tempPosition); colorData.add(color); } }
private static void DrawToGL(Mesh meshToRender) { GLMeshTrianglePlugin glMeshPlugin = GLMeshTrianglePlugin.Get(meshToRender); for (int i = 0; i < glMeshPlugin.subMeshs.Count; i++) { SubTriangleMesh subMesh = glMeshPlugin.subMeshs[i]; // Make sure the GLMeshPlugin has a reference to hold onto the image so it does not go away before this. if (subMesh.texture != null) { ImageGlPlugin glPlugin = ImageGlPlugin.GetImageGlPlugin(subMesh.texture, true); GL.Enable(EnableCap.Texture2D); GL.BindTexture(TextureTarget.Texture2D, glPlugin.GLTextureHandle); GL.EnableClientState(ArrayCap.TextureCoordArray); } else { GL.Disable(EnableCap.Texture2D); GL.DisableClientState(ArrayCap.TextureCoordArray); } #if true GL.EnableClientState(ArrayCap.NormalArray); GL.EnableClientState(ArrayCap.VertexArray); unsafe { fixed(VertexTextureData *pTextureData = subMesh.textrueData.Array) { fixed(VertexNormalData *pNormalData = subMesh.normalData.Array) { fixed(VertexPositionData *pPosition = subMesh.positionData.Array) { GL.TexCoordPointer(2, TexCordPointerType.Float, 0, new IntPtr(pTextureData)); GL.NormalPointer(NormalPointerType.Float, 0, new IntPtr(pNormalData)); GL.VertexPointer(3, VertexPointerType.Float, 0, new IntPtr(pPosition)); GL.DrawArrays(BeginMode.Triangles, 0, subMesh.positionData.Count); } } } } #else GL.InterleavedArrays(InterleavedArrayFormat.T2fN3fV3f, 0, subMesh.vertexDatas.Array); if (subMesh.texture != null) { //GL.TexCoordPointer(2, TexCoordPointerType.Float, VertexData.Stride, subMesh.vertexDatas.Array); //GL.EnableClientState(ArrayCap.TextureCoordArray); } else { GL.DisableClientState(ArrayCap.TextureCoordArray); } #endif GL.DisableClientState(ArrayCap.NormalArray); GL.DisableClientState(ArrayCap.VertexArray); GL.DisableClientState(ArrayCap.TextureCoordArray); GL.TexCoordPointer(2, TexCordPointerType.Float, 0, new IntPtr(0)); GL.NormalPointer(NormalPointerType.Float, 0, new IntPtr(0)); GL.VertexPointer(3, VertexPointerType.Float, 0, new IntPtr(0)); if (subMesh.texture != null) { GL.DisableClientState(ArrayCap.TextureCoordArray); } } }
private void CreateRenderData(Mesh meshToBuildListFor) { subMeshs = new List <SubTriangleMesh>(); SubTriangleMesh currentSubMesh = null; VectorPOD <VertexTextureData> textureData = new VectorPOD <VertexTextureData>(); VectorPOD <VertexNormalData> normalData = new VectorPOD <VertexNormalData>(); VectorPOD <VertexPositionData> positionData = new VectorPOD <VertexPositionData>(); // first make sure all the textures are created foreach (Face face in meshToBuildListFor.Faces) { ImageBuffer faceTexture = face.GetTexture(0); if (faceTexture != null) { ImageGlPlugin.GetImageGlPlugin(faceTexture, true); } // don't compare the data of the texture but rather if they are just the same object if (subMeshs.Count == 0 || (object)subMeshs[subMeshs.Count - 1].texture != (object)faceTexture) { SubTriangleMesh newSubMesh = new SubTriangleMesh(); newSubMesh.texture = faceTexture; subMeshs.Add(newSubMesh); currentSubMesh = subMeshs[subMeshs.Count - 1]; textureData = currentSubMesh.textrueData; normalData = currentSubMesh.normalData; positionData = currentSubMesh.positionData; } Vector2[] textureUV = new Vector2[2]; Vector3[] position = new Vector3[2]; int vertexIndex = 0; foreach (FaceEdge faceEdge in face.FaceEdges()) { if (vertexIndex < 2) { textureUV[vertexIndex] = faceEdge.GetUVs(0); position[vertexIndex] = faceEdge.firstVertex.Position; } else { VertexTextureData tempTexture; VertexNormalData tempNormal; VertexPositionData tempPosition; tempTexture.textureU = (float)textureUV[0].x; tempTexture.textureV = (float)textureUV[0].y; tempNormal.normalX = (float)face.normal.x; tempNormal.normalY = (float)face.normal.y; tempNormal.normalZ = (float)face.normal.z; tempPosition.positionX = (float)position[0].x; tempPosition.positionY = (float)position[0].y; tempPosition.positionZ = (float)position[0].z; textureData.Add(tempTexture); normalData.Add(tempNormal); positionData.Add(tempPosition); tempTexture.textureU = (float)textureUV[1].x; tempTexture.textureV = (float)textureUV[1].y; tempNormal.normalX = (float)face.normal.x; tempNormal.normalY = (float)face.normal.y; tempNormal.normalZ = (float)face.normal.z; tempPosition.positionX = (float)position[1].x; tempPosition.positionY = (float)position[1].y; tempPosition.positionZ = (float)position[1].z; textureData.Add(tempTexture); normalData.Add(tempNormal); positionData.Add(tempPosition); Vector2 textureUV2 = faceEdge.GetUVs(0); Vector3 position2 = faceEdge.firstVertex.Position; tempTexture.textureU = (float)textureUV2.x; tempTexture.textureV = (float)textureUV2.y; tempNormal.normalX = (float)face.normal.x; tempNormal.normalY = (float)face.normal.y; tempNormal.normalZ = (float)face.normal.z; tempPosition.positionX = (float)position2.x; tempPosition.positionY = (float)position2.y; tempPosition.positionZ = (float)position2.z; textureData.Add(tempTexture); normalData.Add(tempNormal); positionData.Add(tempPosition); textureUV[1] = faceEdge.GetUVs(0); position[1] = faceEdge.firstVertex.Position; } vertexIndex++; } } }
private static void CreateVBOForSubMesh(VectorPOD<TriangleVertexData> vertexDatas, SubTriangleMesh currentSubMesh) { #if USE_VBO currentSubMesh.count = vertexDatas.Count; currentSubMesh.vboHandle = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, currentSubMesh.vboHandle); GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(currentSubMesh.count * VertexData.Stride), vertexDatas.Array, BufferUsageHint.StaticDraw); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); #endif }
private void CreateRenderData(Mesh meshToBuildListFor) { subMeshs = new List<SubTriangleMesh>(); SubTriangleMesh currentSubMesh = null; VectorPOD<TriangleVertexData> vertexDatas = new VectorPOD<TriangleVertexData>(); // first make sure all the textures are created foreach (Face face in meshToBuildListFor.Faces) { ImageBuffer faceTexture = face.GetTexture(0); if (faceTexture != null) { ImageGlPlugin.GetImageGlPlugin(faceTexture, true); } // don't compare the data of the texture but rather if they are just the same object if (subMeshs.Count == 0 || (object)subMeshs[subMeshs.Count - 1].texture != (object)faceTexture) { SubTriangleMesh newSubMesh = new SubTriangleMesh(); newSubMesh.texture = faceTexture; subMeshs.Add(newSubMesh); #if USE_VBO if (currentSubMesh != null) { CreateVBOForSubMesh(vertexDatas, currentSubMesh); vertexDatas.Clear(); } currentSubMesh = subMeshs[subMeshs.Count - 1]; #else currentSubMesh = subMeshs[subMeshs.Count - 1]; vertexDatas = currentSubMesh.vertexDatas; #endif } Vector2[] textureUV = new Vector2[2]; Vector3[] position = new Vector3[2]; int vertexIndex = 0; foreach (FaceEdge faceEdge in face.FaceEdges()) { if (vertexIndex < 2) { textureUV[vertexIndex] = faceEdge.GetUVs(0); position[vertexIndex] = faceEdge.firstVertex.Position; } else { TriangleVertexData tempVertex; tempVertex.textureU = (float)textureUV[0].x; tempVertex.textureV = (float)textureUV[0].y; tempVertex.positionsX = (float)position[0].x; tempVertex.positionsY = (float)position[0].y; tempVertex.positionsZ = (float)position[0].z; tempVertex.normalsX = (float)face.normal.x; tempVertex.normalsY = (float)face.normal.y; tempVertex.normalsZ = (float)face.normal.z; vertexDatas.Add(tempVertex); tempVertex.textureU = (float)textureUV[1].x; tempVertex.textureV = (float)textureUV[1].y; tempVertex.positionsX = (float)position[1].x; tempVertex.positionsY = (float)position[1].y; tempVertex.positionsZ = (float)position[1].z; tempVertex.normalsX = (float)face.normal.x; tempVertex.normalsY = (float)face.normal.y; tempVertex.normalsZ = (float)face.normal.z; vertexDatas.Add(tempVertex); Vector2 textureUV2 = faceEdge.GetUVs(0); Vector3 position2 = faceEdge.firstVertex.Position; tempVertex.textureU = (float)textureUV2.x; tempVertex.textureV = (float)textureUV2.y; tempVertex.positionsX = (float)position2.x; tempVertex.positionsY = (float)position2.y; tempVertex.positionsZ = (float)position2.z; tempVertex.normalsX = (float)face.normal.x; tempVertex.normalsY = (float)face.normal.y; tempVertex.normalsZ = (float)face.normal.z; vertexDatas.Add(tempVertex); textureUV[1] = faceEdge.GetUVs(0); position[1] = faceEdge.firstVertex.Position; } vertexIndex++; } } CreateVBOForSubMesh(vertexDatas, currentSubMesh); }
private void CreateRenderData(Mesh meshToBuildListFor, Func <Vector3, Color> getColorFunc) { subMeshs = new List <SubTriangleMesh>(); SubTriangleMesh currentSubMesh = null; VectorPOD <VertexTextureData> textureData = null; VectorPOD <VertexColorData> colorData = null; VectorPOD <VertexNormalData> normalData = null; VectorPOD <VertexPositionData> positionData = null; // first make sure all the textures are created foreach (Face face in meshToBuildListFor.Faces) { ImageBuffer faceTexture = face.GetTexture(0); if (faceTexture != null) { ImageGlPlugin.GetImageGlPlugin(faceTexture, true); } // don't compare the data of the texture but rather if they are just the same object if (subMeshs.Count == 0 || (object)subMeshs[subMeshs.Count - 1].texture != (object)faceTexture) { SubTriangleMesh newSubMesh = new SubTriangleMesh(); newSubMesh.texture = faceTexture; subMeshs.Add(newSubMesh); if (getColorFunc != null) { newSubMesh.UseVertexColors = true; } currentSubMesh = subMeshs[subMeshs.Count - 1]; textureData = currentSubMesh.textureData; colorData = currentSubMesh.colorData; normalData = currentSubMesh.normalData; positionData = currentSubMesh.positionData; } Vector2[] textureUV = new Vector2[2]; Vector3[] position = new Vector3[2]; VertexColorData color = new VertexColorData(); if (getColorFunc != null) { var faceColor = getColorFunc(face.Normal); color = new VertexColorData { red = faceColor.red, green = faceColor.green, blue = faceColor.blue }; } int vertexIndex = 0; foreach (FaceEdge faceEdge in face.FaceEdges()) { if (vertexIndex < 2) { textureUV[vertexIndex] = faceEdge.GetUv(0); position[vertexIndex] = faceEdge.FirstVertex.Position; } else { VertexTextureData tempTexture; VertexNormalData tempNormal; VertexPositionData tempPosition; tempTexture.textureU = (float)textureUV[0].X; tempTexture.textureV = (float)textureUV[0].Y; tempNormal.normalX = (float)face.Normal.X; tempNormal.normalY = (float)face.Normal.Y; tempNormal.normalZ = (float)face.Normal.Z; tempPosition.positionX = (float)position[0].X; tempPosition.positionY = (float)position[0].Y; tempPosition.positionZ = (float)position[0].Z; textureData.Add(tempTexture); normalData.Add(tempNormal); positionData.Add(tempPosition); colorData.add(color); tempTexture.textureU = (float)textureUV[1].X; tempTexture.textureV = (float)textureUV[1].Y; tempNormal.normalX = (float)face.Normal.X; tempNormal.normalY = (float)face.Normal.Y; tempNormal.normalZ = (float)face.Normal.Z; tempPosition.positionX = (float)position[1].X; tempPosition.positionY = (float)position[1].Y; tempPosition.positionZ = (float)position[1].Z; textureData.Add(tempTexture); normalData.Add(tempNormal); positionData.Add(tempPosition); colorData.add(color); Vector2 textureUV2 = faceEdge.GetUv(0); Vector3 position2 = faceEdge.FirstVertex.Position; tempTexture.textureU = (float)textureUV2.X; tempTexture.textureV = (float)textureUV2.Y; tempNormal.normalX = (float)face.Normal.X; tempNormal.normalY = (float)face.Normal.Y; tempNormal.normalZ = (float)face.Normal.Z; tempPosition.positionX = (float)position2.X; tempPosition.positionY = (float)position2.Y; tempPosition.positionZ = (float)position2.Z; textureData.Add(tempTexture); normalData.Add(tempNormal); positionData.Add(tempPosition); colorData.add(color); textureUV[1] = faceEdge.GetUv(0); position[1] = faceEdge.FirstVertex.Position; } vertexIndex++; } } }
private void CreateRenderData(Mesh meshToBuildListFor) { subMeshs = new List<SubTriangleMesh>(); SubTriangleMesh currentSubMesh = null; VectorPOD<VertexTextureData> textureData = new VectorPOD<VertexTextureData>(); VectorPOD<VertexNormalData> normalData = new VectorPOD<VertexNormalData>(); VectorPOD<VertexPositionData> positionData = new VectorPOD<VertexPositionData>(); // first make sure all the textures are created foreach (Face face in meshToBuildListFor.Faces) { ImageBuffer faceTexture = face.GetTexture(0); if (faceTexture != null) { ImageGlPlugin.GetImageGlPlugin(faceTexture, true); } // don't compare the data of the texture but rather if they are just the same object if (subMeshs.Count == 0 || (object)subMeshs[subMeshs.Count - 1].texture != (object)faceTexture) { SubTriangleMesh newSubMesh = new SubTriangleMesh(); newSubMesh.texture = faceTexture; subMeshs.Add(newSubMesh); currentSubMesh = subMeshs[subMeshs.Count - 1]; textureData = currentSubMesh.textrueData; normalData = currentSubMesh.normalData; positionData = currentSubMesh.positionData; } Vector2[] textureUV = new Vector2[2]; Vector3[] position = new Vector3[2]; int vertexIndex = 0; foreach (FaceEdge faceEdge in face.FaceEdges()) { if (vertexIndex < 2) { textureUV[vertexIndex] = faceEdge.GetUVs(0); position[vertexIndex] = faceEdge.firstVertex.Position; } else { VertexTextureData tempTexture; VertexNormalData tempNormal; VertexPositionData tempPosition; tempTexture.textureU = (float)textureUV[0].x; tempTexture.textureV = (float)textureUV[0].y; tempNormal.normalX = (float)face.normal.x; tempNormal.normalY = (float)face.normal.y; tempNormal.normalZ = (float)face.normal.z; tempPosition.positionX = (float)position[0].x; tempPosition.positionY = (float)position[0].y; tempPosition.positionZ = (float)position[0].z; textureData.Add(tempTexture); normalData.Add(tempNormal); positionData.Add(tempPosition); tempTexture.textureU = (float)textureUV[1].x; tempTexture.textureV = (float)textureUV[1].y; tempNormal.normalX = (float)face.normal.x; tempNormal.normalY = (float)face.normal.y; tempNormal.normalZ = (float)face.normal.z; tempPosition.positionX = (float)position[1].x; tempPosition.positionY = (float)position[1].y; tempPosition.positionZ = (float)position[1].z; textureData.Add(tempTexture); normalData.Add(tempNormal); positionData.Add(tempPosition); Vector2 textureUV2 = faceEdge.GetUVs(0); Vector3 position2 = faceEdge.firstVertex.Position; tempTexture.textureU = (float)textureUV2.x; tempTexture.textureV = (float)textureUV2.y; tempNormal.normalX = (float)face.normal.x; tempNormal.normalY = (float)face.normal.y; tempNormal.normalZ = (float)face.normal.z; tempPosition.positionX = (float)position2.x; tempPosition.positionY = (float)position2.y; tempPosition.positionZ = (float)position2.z; textureData.Add(tempTexture); normalData.Add(tempNormal); positionData.Add(tempPosition); textureUV[1] = faceEdge.GetUVs(0); position[1] = faceEdge.firstVertex.Position; } vertexIndex++; } } }