public QuadRenderer(IRenderer renderer) { mesh = new Mesh(); var vertexFormat = new VertexFormat(); position = vertexFormat.Add(new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3)); texcoord = vertexFormat.Add(new Attribute(VertexUsage.TexCoord, VertexAttribPointerType.Float, 0, 2)); color = vertexFormat.Add(new Attribute(VertexUsage.Color, VertexAttribPointerType.Float, 0, 4)); // Some gfx cards fail to show last vertex right if texcoord is set to have 3 components vertexBuffer = BufferFactory.Create(vertexFormat, BufferUsageHint.StaticDraw); indexBuffer = BufferFactory.Create(DrawElementsType.UnsignedInt, BufferUsageHint.StaticDraw); mesh.VertexBufferRange = vertexBufferRange = vertexBuffer.CreateVertexBufferRange(); indexBufferRange = mesh.FindOrCreateIndexBufferRange( MeshMode.PolygonFill, indexBuffer, BeginMode.Triangles ); vertexWriter = new VertexBufferWriter(mesh.VertexBufferRange); indexWriter = new IndexBufferWriter(indexBufferRange); }
public TreeTemplate( int sliceCount, int stackDivision, int coneCount, float height, float radius, float radAdd ) { var attributePosition = new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3); var attributeNormal = new Attribute(VertexUsage.Normal, VertexAttribPointerType.Float, 0, 3); /* content normals */ VertexFormat vertexFormat = new VertexFormat(); vertexFormat.Add(attributePosition); vertexFormat.Add(attributeNormal); this.sliceCount = sliceCount; this.stackDivision = stackDivision; this.coneCount = coneCount; this.height = height; this.radius = radius; this.radAdd = radAdd; float coneHeight = height / (float)coneCount; Matrix4 rotZ = Matrix4.CreateRotation( RenderStack.Math.Conversions.DegreesToRadians(90.0f), Vector3.UnitZ ); float cylHeight = coneHeight; float cylRadius = height / 20.0f; Geometry cylinderGeometry = new RenderStack.Geometry.Shapes.Cylinder(-cylHeight, cylHeight, cylRadius, sliceCount); cylinderGeometry.Transform(rotZ); GeometryMesh cylinderMesh = new GeometryMesh(cylinderGeometry, NormalStyle.CornerNormals, vertexFormat); Shape cylinderShape = new CylinderShape(cylHeight, cylRadius); cylinderMesh.GetMesh.Name = "cylinder"; meshes.Add(cylinderMesh); shapes.Add(cylinderShape); for (int c = 0; c < coneCount; c++) { float topRadius = (coneCount - 1 - c) * radius / (float)coneCount; float bottomRadius = topRadius + radAdd; float R = bottomRadius; float r = topRadius; float fullConeHeight = (R * coneHeight) / (R - r); float minX = -fullConeHeight / 3.0f; float maxX = 2.0f * fullConeHeight / 3.0f; float offset = -minX; Geometry coneGeometry = new RenderStack.Geometry.Shapes.Cone(minX, maxX, bottomRadius, 0.0f, true, true, sliceCount, stackDivision); coneGeometry.Transform(rotZ); GeometryMesh coneMesh = new GeometryMesh(coneGeometry, NormalStyle.CornerNormals, vertexFormat); Shape coneShape = new ConeShape(fullConeHeight, R); coneMesh.GetMesh.Name = "cone" + c.ToString(); meshes.Add(coneMesh); shapes.Add(coneShape); } }
//private BufferRange indexBufferRange; protected override void InitializeService() { //basic = materialManager["Basic"]; basic = materialManager.MakeMaterial("Basic"); Image terrainImage = new Image("res/images/terrain.png"); // \todo support material textures basic.Textures["t_surface_color"] = materialManager.Textures["terrain"] = new TextureGL(terrainImage); blockTypes[BlockType.Grass] = new BlockType(0, 0, 2, 0, 3, 0, 3, 0, 3, 0, 3, 0); blockTypes[BlockType.Stone] = new BlockType(1, 0); blockTypes[BlockType.Dirt] = new BlockType(2, 0); blockTypes[BlockType.Cobblestone] = new BlockType(0, 1); blockTypes[BlockType.Bedrock] = new BlockType(1, 1); blockTypes[BlockType.Wood] = new BlockType(5, 1, 5, 1, 4, 1, 4, 1, 4, 1, 4, 1); blockTypes[BlockType.Sand] = new BlockType(2, 1); // 0, 0 grass (in gray) // 1, 0 stone // 2, 0 dirt // 3, 0 grass left, right, front, back // 4, 0 wooden planks // 5, 0 stone slab sides // 6, 0 stone slab top, bottom // 7, 0 brick // 8, 0 tnt sides // 9, 0 tnt top // 10, 0 tnt bottom // 11, 0 spider web // 12, 0 red flower // 13, 0 yellow flower // 14, 0 water? // 15, 0 sapling // 0, 1 cobble stone // 1, 1 bedrock // 2, 1 sand // 3, 1 gravel // 4, 1 log sidem // 5, 1 log top, bottom // 6, 1 iron block // 7, 1 gold block // 8, 1 diamond block // 9, 1 chest top, bottom // 10, 1 chest front, left, right // 11, 1 chest back // 12, 1 var vertexFormat = new VertexFormat(); position = vertexFormat.Add(new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3)); texcoord = vertexFormat.Add(new Attribute(VertexUsage.TexCoord, VertexAttribPointerType.Float, 0, 2)); //color = vertexFormat.Add(new Attribute(VertexUsage.Color, VertexAttribPointerType.Byte, 0, 4, true)); color = vertexFormat.Add(new Attribute(VertexUsage.Color, VertexAttribPointerType.Float, 0, 4)); // Some gfx cards fail to show last vertex right if texcoord is set to have 3 components vertexBuffer = BufferFactory.Create(vertexFormat, BufferUsageHint.StaticDraw); indexBuffer = BufferFactory.Create(DrawElementsType.UnsignedInt, BufferUsageHint.StaticDraw); }
public void BeginPrint(Mesh.Mesh mesh) { Bounds.ResetForGrow(); var vertexFormat = mesh.VertexBufferRange.VertexFormat; position = vertexFormat.FindAttribute(VertexUsage.Position, 0); texCoord = vertexFormat.FindAttribute(VertexUsage.TexCoord, 0); color = vertexFormat.FindAttribute(VertexUsage.Color, 0); }
public void BuildMeshFromGeometry( BufferUsageHint bufferUsageHint, NormalStyle normalStyle ) { var attributePosition = new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3); var attributeNormal = new Attribute(VertexUsage.Normal, VertexAttribPointerType.Float, 0, 3); /* content normals */ var attributeNormalFlat = new Attribute(VertexUsage.Normal, VertexAttribPointerType.Float, 1, 3); /* flat normals */ var attributeNormalSmooth = new Attribute(VertexUsage.Normal, VertexAttribPointerType.Float, 2, 3); /* smooth normals */ var attributeColor = new Attribute(VertexUsage.Color, VertexAttribPointerType.Float, 0, 4); var attributeIdVec3 = new Attribute(VertexUsage.Id, VertexAttribPointerType.Float, 0, 3); var attributeIdUInt = (RenderStack.Graphics.Configuration.useIntegerPolygonIDs) ? new Attribute(VertexUsage.Id, VertexAttribPointerType.UnsignedInt, 0, 1) : null; VertexFormat vertexFormat = new VertexFormat(); vertexFormat.Add(attributePosition); vertexFormat.Add(attributeNormal); vertexFormat.Add(attributeNormalFlat); vertexFormat.Add(attributeNormalSmooth); Dictionary <Corner, Vector2> cornerTexcoords = null; Dictionary <Point, Vector2> pointTexcoords = null; if (Geometry.CornerAttributes.Contains <Vector2>("corner_texcoords")) { cornerTexcoords = Geometry.CornerAttributes.Find <Vector2>("corner_texcoords"); } if (Geometry.PointAttributes.Contains <Vector2>("point_texcoords")) { pointTexcoords = Geometry.PointAttributes.Find <Vector2>("point_texcoords"); } if (cornerTexcoords != null || pointTexcoords != null) { var attributeTexcoord = new Attribute(VertexUsage.TexCoord, VertexAttribPointerType.Float, 0, 2); vertexFormat.Add(attributeTexcoord); } // \todo When do we want color attribute and when we don't? //if(cornerColors != null || pointColors != null) //{ vertexFormat.Add(attributeColor); //} vertexFormat.Add(attributeIdVec3); if (RenderStack.Graphics.Configuration.useIntegerPolygonIDs) { vertexFormat.Add(attributeIdUInt); } BuildMeshFromGeometry(bufferUsageHint, normalStyle, vertexFormat); }
public void Cube(long x, long y, long z, byte blockType) { // B C // A D // F G // E H // float right = (float)x + 1.0f; float top = (float)y + 1.0f; float back = (float)z + 1.0f; float left = (float)x - 0.0f; float bottom = (float)y - 0.0f; float front = (float)z - 0.0f; Vector3 A = new Vector3(left, top, back); Vector3 B = new Vector3(left, top, front); Vector3 C = new Vector3(right, top, front); Vector3 D = new Vector3(right, top, back); Vector3 E = new Vector3(left, bottom, back); Vector3 F = new Vector3(left, bottom, front); Vector3 G = new Vector3(right, bottom, front); Vector3 H = new Vector3(right, bottom, back); BlockType block = Map.blockTypes[blockType]; UInt32 color = 0xffffff; if (blockType == BlockType.Grass || blockType == BlockType.Dirt) { color = 0xccff88; } #if false CubeFace(A, B, C, D, block.TopU, block.TopV, color, 1.0f); // top CubeFace(A, D, H, E, block.BackU, block.BackV, color, 0.8f); // back CubeFace(B, A, E, F, block.LeftU, block.LeftV, color, 0.5f); // left CubeFace(F, E, H, G, block.BottomU, block.BottomV, color, 0.3f); // bottom CubeFace(D, C, G, H, block.RightU, block.RightV, color, 0.5f); // right CubeFace(C, B, F, G, block.FrontU, block.FrontV, color, 0.8f); // front #else CubeFace(A, B, C, D, block.TopU, block.TopV, color, 1.0f); // top CubeFace(A, D, H, E, block.BackU, block.BackV, 0xffffff, 0.8f); // back CubeFace(B, A, E, F, block.LeftU, block.LeftV, 0xffffff, 0.6f); // left CubeFace(F, E, H, G, block.BottomU, block.BottomV, 0xffffff, 0.3f); // bottom CubeFace(D, C, G, H, block.RightU, block.RightV, 0xffffff, 0.6f); // right CubeFace(C, B, F, G, block.FrontU, block.FrontV, 0xffffff, 0.8f); // front #endif }
private void UseMap(Map map) { this.map = map; position = map.Position; texcoord = map.Texcoord; color = map.Color; mesh = new Mesh(); mesh.VertexBufferRange = vertexBufferRange = map.VertexBuffer.CreateVertexBufferRange(); indexBufferRange = mesh.FindOrCreateIndexBufferRange( MeshMode.PolygonFill, map.IndexBuffer, BeginMode.Triangles ); vertexWriter = new VertexBufferWriter(mesh.VertexBufferRange); indexWriter = new IndexBufferWriter(indexBufferRange); }
protected override void InitializeService() { if ( (RenderStack.Graphics.Configuration.canUseGeometryShaders) && (RenderStack.Graphics.Configuration.glslVersion >= 330) ) { material = new Material("", renderer.Programs["WideLine"], renderer.MaterialUB); } else { material = new Material("", renderer.Programs["ColorFill"], renderer.MaterialUB); } LineWidth = 1.0f; mesh = new Mesh(); var vertexFormat = new VertexFormat(); position = vertexFormat.Add(new Attribute(VertexUsage.Position, VertexAttribPointerType.Float, 0, 3)); edgeColor = vertexFormat.Add(new Attribute(VertexUsage.Color, VertexAttribPointerType.Float, 1, 4)); vertexBuffer = BufferFactory.Create(vertexFormat, BufferUsageHint.DynamicDraw); indexBuffer = BufferFactory.Create(DrawElementsType.UnsignedInt, BufferUsageHint.DynamicDraw); mesh.VertexBufferRange = vertexBufferRange = vertexBuffer.CreateVertexBufferRange(); indexBufferRange = mesh.FindOrCreateIndexBufferRange( MeshMode.EdgeLines, indexBuffer, BeginMode.Lines ); vertexWriter = new VertexBufferWriter(mesh.VertexBufferRange); indexWriter = new IndexBufferWriter(indexBufferRange); }
public virtual void UpdateTubeMesh() { var vertexFormat = TubeMesh.VertexBufferRange.VertexFormat; tubePosition = vertexFormat.FindAttribute(VertexUsage.Position, 0); tubeNormal = vertexFormat.FindAttribute(VertexUsage.Normal, 0); tubeTangent = vertexFormat.FindAttribute(VertexUsage.Tangent, 0); tubeColor = vertexFormat.FindAttribute(VertexUsage.Color, 0); tubeT = vertexFormat.FindAttribute(VertexUsage.Color, 1); tubeId = vertexFormat.FindAttribute(VertexUsage.Id, 0); tubeVertexWriter.BeginEdit(); tubeIndexWriter.BeginEdit(); // \todo hack fixme ((GenericCurve)(curve)).UpdateNURBS(); // Compute initial N Vector3 pos = curve.PositionAt(0.0f); Vector3 posNext = curve.PositionAt(1.0f / 512.0f); Vector3 d1 = posNext - pos; Vector3 T = Vector3.Normalize(d1); Vector3 N = d1.MinAxis; Vector3 B = Vector3.Normalize(Vector3.Cross(T, N)); LastN = Vector3.Normalize(Vector3.Cross(B, T)); UpdateTubeMeshWithAdaptiveSubdivision(); for (int stack = 1; stack < TubeStackCount - 2; ++stack) { int nextStack = stack + 1; for (int slice = 0; slice < tubeSliceCount; ++slice) { int nextSlice = (slice + 1) % tubeSliceCount; tubeIndexWriter.Quad( (uint)(stack * tubeSliceCount + nextSlice), (uint)(stack * tubeSliceCount + slice), (uint)(nextStack * tubeSliceCount + slice), (uint)(nextStack * tubeSliceCount + nextSlice) ); tubeIndexWriter.CurrentIndex += 6; } } for (int slice = 0; slice < tubeSliceCount; ++slice) { int nextSlice1 = (slice + 1) % tubeSliceCount; tubeIndexWriter.Triangle( (uint)(0 * tubeSliceCount), (uint)(0 * tubeSliceCount + slice), (uint)(0 * tubeSliceCount + nextSlice1) ); tubeIndexWriter.CurrentIndex += 3; } for (int slice = 0; slice < tubeSliceCount; ++slice) { int nextSlice1 = (slice + 1) % tubeSliceCount; tubeIndexWriter.Triangle( (uint)((TubeStackCount - 1) * tubeSliceCount + nextSlice1), (uint)((TubeStackCount - 1) * tubeSliceCount + slice), (uint)((TubeStackCount - 1) * tubeSliceCount) ); tubeIndexWriter.CurrentIndex += 3; } tubeVertexWriter.EndEdit(); tubeIndexWriter.EndEdit(); }