public DemoMesh(Demo demo, Shape shape, DemoTexture texture, Vector2 textureScale, bool indices, bool indices16Bit, bool flipTriangles, bool flipNormals, bool smoothNormals, CullFaceMode cullMode, bool dynamic, bool scaleNormals) { this.demo = demo; Render = new RenderMeshDeferredPNT(); meshVertexBuffer = -1; meshIndexBuffer = -1; meshVertices = null; meshIndices16Bit = null; meshIndices32Bit = null; meshIndicesType = 0; this.dynamic = dynamic; this.scaleNormals = scaleNormals; SetCullMode(cullMode); SetTexture(texture); if (indices) { meshVertices = new VertexPositionNormalTexture[shape.VertexCount]; shape.GetMeshVertices(textureScale.X, textureScale.Y, flipNormals, smoothNormals, meshVertices); CreateVertexBuffer(meshVertices, dynamic); if (indices16Bit) { meshIndices16Bit = new ushort[shape.IndexCount]; shape.GetMesh16BitIndices(flipTriangles, meshIndices16Bit); CreateIndexBuffer(meshIndices16Bit, false); } else { meshIndices32Bit = new int[shape.IndexCount]; shape.GetMesh32BitIndices(flipTriangles, meshIndices32Bit); CreateIndexBuffer(meshIndices32Bit, false); } } else { meshVertices = new VertexPositionNormalTexture[shape.TriangleVertexCount]; shape.GetMesh(textureScale.X, textureScale.Y, flipTriangles, flipNormals, smoothNormals, meshVertices); CreateVertexBuffer(meshVertices, dynamic); } }
public DemoMesh(Demo demo, TriangleMesh triangleMesh, DemoTexture texture, Vector2 textureScale, bool indices, bool indices16Bit, bool flipTriangles, bool flipNormals, bool smoothNormals, CullFaceMode cullMode, bool dynamic, bool scaleNormals) { this.demo = demo; Render = new RenderMeshDeferredPNT(); meshVertexBuffer = -1; meshIndexBuffer = -1; meshVertices = null; meshIndices16Bit = null; meshIndices32Bit = null; meshIndicesType = 0; this.dynamic = dynamic; this.scaleNormals = scaleNormals; SetCullMode(cullMode); SetTexture(texture); if (indices) { triangleMesh.GetMeshVertices(flipNormals, smoothNormals, out meshVertices); CreateVertexBuffer(meshVertices, dynamic); if (indices16Bit) { triangleMesh.GetMesh16BitIndices(flipTriangles, out meshIndices16Bit); CreateIndexBuffer(meshIndices16Bit, false); } else { triangleMesh.GetMesh32BitIndices(flipTriangles, out meshIndices32Bit); CreateIndexBuffer(meshIndices32Bit, false); } } else { triangleMesh.GetMesh(textureScale, flipTriangles, flipNormals, smoothNormals, out meshVertices); CreateVertexBuffer(meshVertices, dynamic); } }
public void Draw(ref Matrix4 world, ref Matrix4 view, ref Matrix4 projection, PhysicsLight light, PhysicsMaterial material, PhysicsCamera camera, bool sleep, bool wireframe) { Render.SetWorld(ref world); Render.SetView(ref view); Render.SetProjection(ref projection); Render.EnableScaleNormals = scaleNormals; light.GetDirection(ref lightDirection); light.GetDiffuse(ref lightDiffuse); light.GetSpecular(ref lightSpecular); if (meshCullMode == CullFaceMode.FrontAndBack) { GL.Disable(EnableCap.CullFace); } GL.CullFace(meshCullMode); DemoTexture currentTexture = meshTexture; if (material.UserDataStr != null) { currentTexture = demo.Textures[material.UserDataStr]; } material.GetAmbient(ref ambient); material.GetDiffuse(ref diffuse); material.GetEmission(ref emission); material.GetSpecular(ref specular); if (sleep) { diffuse.X = 0.7f; diffuse.Y = 1.0f; diffuse.Z = 1.0f; } Render.SetAmbient(ref ambient); Render.SetSpecular(ref specular); Render.SetEmission(ref emission); Render.SpecularPower = material.SpecularPower; Render.Alpha = material.TransparencyFactor; Render.EnableTwoSidedNormals = material.TwoSidedNormals; if (!wireframe) { Render.SetDiffuse(ref diffuse); if (currentTexture != null) { Render.EnableTexture = true; Render.Texture = currentTexture.Handle; } else { Render.EnableTexture = false; } } else { GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line); diffuse.X *= 4.0f; diffuse.Y *= 4.0f; diffuse.Z *= 4.0f; Render.SetDiffuse(ref diffuse); Render.EnableTexture = false; } Render.Apply(); if (demo.EnableVertexBuffer) { if (meshVertexBuffer != -1) { if (meshIndexBuffer != -1) { GL.EnableClientState(ArrayCap.VertexArray); GL.EnableClientState(ArrayCap.NormalArray); GL.EnableClientState(ArrayCap.TextureCoordArray); GL.BindBuffer(BufferTarget.ArrayBuffer, meshVertexBuffer); GL.BindBuffer(BufferTarget.ElementArrayBuffer, meshIndexBuffer); GL.VertexPointer(3, VertexPointerType.Float, VertexPositionNormalTexture.SizeInBytes, IntPtr.Zero); GL.NormalPointer(NormalPointerType.Float, VertexPositionNormalTexture.SizeInBytes, (IntPtr)Vector3.SizeInBytes); GL.TexCoordPointer(2, TexCoordPointerType.Float, VertexPositionNormalTexture.SizeInBytes, (IntPtr)(Vector3.SizeInBytes + Vector3.SizeInBytes)); GL.DrawElements(PrimitiveType.Triangles, indexCount, meshIndicesType, 0); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); GL.VertexPointer(3, VertexPointerType.Float, 0, IntPtr.Zero); GL.NormalPointer(NormalPointerType.Float, 0, IntPtr.Zero); GL.TexCoordPointer(2, TexCoordPointerType.Float, 0, IntPtr.Zero); GL.DisableClientState(ArrayCap.VertexArray); GL.DisableClientState(ArrayCap.NormalArray); GL.DisableClientState(ArrayCap.TextureCoordArray); } else { GL.EnableClientState(ArrayCap.VertexArray); GL.EnableClientState(ArrayCap.NormalArray); GL.EnableClientState(ArrayCap.TextureCoordArray); GL.BindBuffer(BufferTarget.ArrayBuffer, meshVertexBuffer); GL.VertexPointer(3, VertexPointerType.Float, VertexPositionNormalTexture.SizeInBytes, IntPtr.Zero); GL.NormalPointer(NormalPointerType.Float, VertexPositionNormalTexture.SizeInBytes, (IntPtr)Vector3.SizeInBytes); GL.TexCoordPointer(2, TexCoordPointerType.Float, VertexPositionNormalTexture.SizeInBytes, (IntPtr)(Vector3.SizeInBytes + Vector3.SizeInBytes)); GL.DrawArrays(PrimitiveType.Triangles, 0, vertexCount); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.VertexPointer(3, VertexPointerType.Float, 0, IntPtr.Zero); GL.NormalPointer(NormalPointerType.Float, 0, IntPtr.Zero); GL.TexCoordPointer(2, TexCoordPointerType.Float, 0, IntPtr.Zero); GL.DisableClientState(ArrayCap.VertexArray); GL.DisableClientState(ArrayCap.NormalArray); GL.DisableClientState(ArrayCap.TextureCoordArray); } } } else { VertexPositionNormalTexture v1, v2, v3; if (meshIndices16Bit != null) { GL.Begin(PrimitiveType.Triangles); for (int i = 0; i < indexCount; i += 3) { v1 = meshVertices[meshIndices16Bit[i]]; v2 = meshVertices[meshIndices16Bit[i + 1]]; v3 = meshVertices[meshIndices16Bit[i + 2]]; GL.TexCoord2(v1.TextureCoordinate); GL.Normal3(v1.Normal); GL.Vertex3(v1.Position); GL.TexCoord2(v2.TextureCoordinate); GL.Normal3(v2.Normal); GL.Vertex3(v2.Position); GL.TexCoord2(v3.TextureCoordinate); GL.Normal3(v3.Normal); GL.Vertex3(v3.Position); } GL.End(); } else if (meshIndices32Bit != null) { GL.Begin(PrimitiveType.Triangles); for (int i = 0; i < indexCount; i += 3) { v1 = meshVertices[meshIndices32Bit[i]]; v2 = meshVertices[meshIndices32Bit[i + 1]]; v3 = meshVertices[meshIndices32Bit[i + 2]]; GL.TexCoord2(v1.TextureCoordinate); GL.Normal3(v1.Normal); GL.Vertex3(v1.Position); GL.TexCoord2(v2.TextureCoordinate); GL.Normal3(v2.Normal); GL.Vertex3(v2.Position); GL.TexCoord2(v3.TextureCoordinate); GL.Normal3(v3.Normal); GL.Vertex3(v3.Position); } GL.End(); } else { GL.Begin(PrimitiveType.Triangles); for (int i = 0; i < vertexCount; i += 3) { v1 = meshVertices[i]; v2 = meshVertices[i + 1]; v3 = meshVertices[i + 2]; GL.TexCoord2(v1.TextureCoordinate); GL.Normal3(v1.Normal); GL.Vertex3(v1.Position); GL.TexCoord2(v2.TextureCoordinate); GL.Normal3(v2.Normal); GL.Vertex3(v2.Position); GL.TexCoord2(v3.TextureCoordinate); GL.Normal3(v3.Normal); GL.Vertex3(v3.Position); } GL.End(); } } if (meshCullMode == CullFaceMode.FrontAndBack) { GL.Enable(EnableCap.CullFace); } if (wireframe) { GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill); } }
public void SetTexture(DemoTexture texture) { meshTexture = texture; }