private unsafe void InitVertexBuffers() { { VR02PositionBuffer positionBuffer = new VR02PositionBuffer(strin_Position); positionBuffer.Alloc(xFrameCount * yFrameCount * zFrameCount); vec3 *array = (vec3 *)positionBuffer.FirstElement(); int index = 0; for (int i = 0; i < xFrameCount; i++) { for (int j = 0; j < yFrameCount; j++) { for (int k = 0; k < zFrameCount; k++) { array[index++] = new vec3( (float)i / (float)xFrameCount - 0.5f, (float)j / (float)yFrameCount - 0.5f, ((float)k / (float)zFrameCount - 0.5f) * 109.0f / 256.0f ); } } } this.positionBufferRenderer = positionBuffer.GetRenderer(); positionBuffer.Dispose(); } { VR02UVBuffer uvBuffer = new VR02UVBuffer(strin_uv); uvBuffer.Alloc(xFrameCount * yFrameCount * zFrameCount); vec3 *array = (vec3 *)uvBuffer.FirstElement(); int index = 0; for (int i = 0; i < xFrameCount; i++) { for (int j = 0; j < yFrameCount; j++) { for (int k = 0; k < zFrameCount; k++) { array[index++] = new vec3( (float)i / (float)xFrameCount, (float)j / (float)yFrameCount, (float)k / (float)zFrameCount ); } } } this.uvBufferRenderer = uvBuffer.GetRenderer(); uvBuffer.Dispose(); } { var indexBuffer = new ZeroIndexBuffer(DrawMode.Points, 0, xFrameCount * yFrameCount * zFrameCount); indexBuffer.Alloc(xFrameCount * yFrameCount * zFrameCount);// this actually does nothing. this.indexBufferRenderer = indexBuffer.GetRenderer() as IndexBufferPointerBase; indexBuffer.Dispose(); } this.vao = new VertexArrayObject( this.indexBufferRenderer, this.positionBufferRenderer, this.uvBufferRenderer); }
protected override void DoInitialize() { // init shader program ShaderProgram program = new ShaderProgram(); string vertexShaderCode = null, geometryShaderCode = null, fragmentShaderCode = null; FillShaderCodes(this.allShaderCodes, ref vertexShaderCode, ref geometryShaderCode, ref fragmentShaderCode); program.Create(vertexShaderCode, fragmentShaderCode, geometryShaderCode, null); this.shaderProgram = program; // init all uniform variables this.uniformVariables = GetAllUniformVariables(this.allShaderCodes); // init property buffer objects' renderer var propertyBufferRenderers = new BufferPointer[propertyNameMap.Count()]; int index = 0; foreach (var item in propertyNameMap) { BufferPointer bufferRenderer = this.model.GetBufferRenderer( item.NameInIConvert2BufferRenderer, item.VarNameInShader); if (bufferRenderer == null) { throw new Exception(); } propertyBufferRenderers[index++] = bufferRenderer; } this.propertyBufferRenderers = propertyBufferRenderers; // init index buffer object's renderer this.indexBufferRenderer = this.model.GetIndexBufferRenderer(); this.model = null; this.allShaderCodes = null; this.propertyNameMap = null; //this.uniformNameMap = null; { IndexBufferPointer renderer = this.indexBufferRenderer as IndexBufferPointer; if (renderer != null) { this.elementCount = renderer.ElementCount; } } { ZeroIndexBufferPointer renderer = this.indexBufferRenderer as ZeroIndexBufferPointer; if (renderer != null) { this.elementCount = renderer.VertexCount; } } }
private unsafe void InitVertexBuffers() { { VR01PositionBuffer positionBuffer = new VR01PositionBuffer(strin_Position); positionBuffer.Alloc(zFrameCount); QuadPosition *array = (QuadPosition *)positionBuffer.FirstElement(); for (int i = 0; i < zFrameCount; i++) { array[i] = new QuadPosition( new vec3(-xLength, -yLength, (float)i / (float)zFrameCount - 0.5f), new vec3(xLength, -yLength, (float)i / (float)zFrameCount - 0.5f), new vec3(xLength, yLength, (float)i / (float)zFrameCount - 0.5f), new vec3(-xLength, yLength, (float)i / (float)zFrameCount - 0.5f) ); } this.positionBufferRenderer = positionBuffer.GetRenderer(); positionBuffer.Dispose(); } { VR01UVBuffer uvBuffer = new VR01UVBuffer(strin_uv); uvBuffer.Alloc(zFrameCount); QuadUV *array = (QuadUV *)uvBuffer.FirstElement(); for (int i = 0; i < zFrameCount; i++) { array[i] = new QuadUV( new vec3(0, 0, (float)i / (float)zFrameCount), new vec3(1, 0, (float)i / (float)zFrameCount), new vec3(1, 1, (float)i / (float)zFrameCount), new vec3(0, 1, (float)i / (float)zFrameCount) ); } this.uvBufferRenderer = uvBuffer.GetRenderer(); uvBuffer.Dispose(); } { var indexBuffer = new ZeroIndexBuffer(DrawMode.Quads, 0, zFrameCount * 4); indexBuffer.Alloc(zFrameCount);// this actually does nothing. this.indexBufferRenderer = indexBuffer.GetRenderer() as IndexBufferPointerBase; indexBuffer.Dispose(); } this.vao = new VertexArrayObject( this.indexBufferRenderer, this.positionBufferRenderer, this.uvBufferRenderer); }
protected void InitializeVAO() { this.positionBufferRenderer = model.GetPositionBufferRenderer(strposition); this.colorBufferRenderer = model.GetColorBufferRenderer(strcolor); //this.texCoordBufferRenderer = ???(strtexCoord); this.texCoordBufferRenderer = model.GetTexCoordBufferRenderer(strtexCoord); this.indexBufferRenderer = model.GetIndexes() as IndexBufferPointerBase; { IndexBufferPointer renderer = this.indexBufferRenderer as IndexBufferPointer; if (renderer != null) { this.elementCount = renderer.ElementCount; } } { ZeroIndexBufferPointer renderer = this.indexBufferRenderer as ZeroIndexBufferPointer; if (renderer != null) { this.elementCount = renderer.VertexCount; } } }
protected void InitializeVAO() { IModel model = this.model; this.positionBufferRenderer = model.GetPositionBufferRenderer(strin_Position); //this.colorBufferRenderer = model.GetColorBufferRenderer(strin_Color); using (var colorBuffer = new ColorBuffer(strin_Color)) { colorBuffer.Alloc(4 * 6); for (int i = 0; i < 6; i++) { colorBuffer[i * 4 + 0] = new vec2(0, 0); colorBuffer[i * 4 + 1] = new vec2(0, 1); colorBuffer[i * 4 + 2] = new vec2(1, 1); colorBuffer[i * 4 + 3] = new vec2(1, 0); } this.colorBufferRenderer = colorBuffer.GetRenderer(); } //this.normalBufferRenderer = model.GetNormalBufferRenderer(strin_Normal); this.indexBufferRenderer = model.GetIndexes() as IndexBufferPointerBase; { IndexBufferPointer renderer = this.indexBufferRenderer as IndexBufferPointer; if (renderer != null) { this.elementCount = renderer.ElementCount; } } { ZeroIndexBufferPointer renderer = this.indexBufferRenderer as ZeroIndexBufferPointer; if (renderer != null) { this.elementCount = renderer.VertexCount; } } }
protected void InitializeVAO() { IModel model = this.model; this.positionBufferRenderer = model.GetPositionBufferRenderer(strin_Position); //this.colorBufferRenderer = model.GetColorBufferRenderer(strin_Color); this.normalBufferRenderer = model.GetNormalBufferRenderer(strin_Normal); this.indexBufferRenderer = model.GetIndexes() as IndexBufferPointerBase; { IndexBufferPointer renderer = this.indexBufferRenderer as IndexBufferPointer; if (renderer != null) { this.elementCount = renderer.ElementCount; } } { ZeroIndexBufferPointer renderer = this.indexBufferRenderer as ZeroIndexBufferPointer; if (renderer != null) { this.elementCount = renderer.VertexCount; } } }
unsafe void InitializeVAO() { vec3[] colors = new vec3[] { new vec3(1, 0, 0), new vec3(0, 1, 0), new vec3(0, 0, 1) }; // 计算三个坐标轴 for (int axisIndex = 0; axisIndex < 3; axisIndex++) { var axisVertexCount = faceCount * 2; // Create a vertex buffer for the vertex data. using (var positionBuffer = new AxisPositionBuffer(strin_Position, BufferUsage.StaticDraw)) { positionBuffer.Alloc(axisVertexCount); vec3 *positionArray = (vec3 *)positionBuffer.FirstElement(); for (int i = 0; i < axisVertexCount; i++) { int face = i / 2; float[] components = new float[] { i % 2 == 1 ? 0 : this.axisLength, (float)(this.radius * Math.Cos(face * (Math.PI * 2) / faceCount)), (float)(this.radius * Math.Sin(face * (Math.PI * 2) / faceCount)) }; positionArray[i] = new vec3( components[(0 + axisIndex) % 3], components[(2 + axisIndex) % 3], components[(4 + axisIndex) % 3]); } this.positionBufferRenderers[axisIndex] = positionBuffer.GetRenderer(); } // Now do the same for the color data. using (var colorBuffer = new AxisColorBuffer(strin_Color, BufferUsage.StaticDraw)) { colorBuffer.Alloc(axisVertexCount); vec3 *colorArray = (vec3 *)colorBuffer.FirstElement(); for (int i = 0; i < axisVertexCount; i++) { colorArray[i] = colors[axisIndex]; } this.colorBufferRenderers[axisIndex] = colorBuffer.GetRenderer(); } // Now for the index data. using (var indexBuffer = new AxisIndexBuffer()) { int indexLength = axisVertexCount + 2; indexBuffer.Alloc(indexLength); byte *cylinderIndex = (byte *)indexBuffer.FirstElement(); for (int i = 0; i < indexLength - 2; i++) { cylinderIndex[i] = (byte)i; } cylinderIndex[indexLength - 2] = 0; cylinderIndex[indexLength - 1] = 1; this.indexBufferRenderers[axisIndex] = indexBuffer.GetRenderer() as IndexBufferPointerBase; } } // 计算XZ平面 { int planVertexCount = 4; // Create a vertex buffer for the vertex data. using (var positionBuffer = new AxisPositionBuffer(strin_Position, BufferUsage.StaticDraw)) { positionBuffer.Alloc(planVertexCount); vec3 *plan = (vec3 *)positionBuffer.FirstElement(); { float length = this.axisLength; plan[0] = new vec3(-length, 0, -length); plan[1] = new vec3(-length, 0, length); plan[2] = new vec3(length, 0, length); plan[3] = new vec3(length, 0, -length); } this.planPositionBufferRenderer = positionBuffer.GetRenderer(); } // Now do the same for the colour data. using (var colorBuffer = new AxisColorBuffer(strin_Color, BufferUsage.StaticDraw)) { colorBuffer.Alloc(planVertexCount); vec3 *colorArray = (vec3 *)colorBuffer.FirstElement(); { for (int i = 0; i < planVertexCount; i++) { colorArray[i] = this.planColor; } } this.planColorBufferRenderer = colorBuffer.GetRenderer(); } using (var indexBuffer = new ZeroIndexBuffer(DrawMode.LineLoop, 0, planVertexCount)) { indexBuffer.Alloc(planVertexCount);//这句话实际上什么都没有做。 this.planIndexBufferRenderer = indexBuffer.GetRenderer() as IndexBufferPointerBase; } } }
protected override void DoInitialize() { // init shader program ShaderProgram program = new ShaderProgram(); string vertexShaderCode = null, geometryShaderCode = null, fragmentShaderCode = null; FillShaderCodes(this.allShaderCodes, ref vertexShaderCode, ref geometryShaderCode, ref fragmentShaderCode); program.Create(vertexShaderCode, fragmentShaderCode, geometryShaderCode, null); this.shaderProgram = program; // init all uniform variables UniformVariableBase[] uniformVariables = GetAllUniformVariables(this.allShaderCodes); if (uniformVariables.Length != this.uniformNameMap.Count()) { throw new Exception(String.Format("uniforms' count not match! [{0}] in shaders but [{1}] in map.", uniformVariables.Length, this.uniformNameMap.Count())); } var pairs = from variable in uniformVariables join mappingPair in this.uniformNameMap on variable.VarName equals mappingPair.UniformNameInShader select new { variable, mappingPair }; if (pairs.Count() != uniformVariables.Length) { StringBuilder builder = new StringBuilder(); builder.AppendLine(string.Format("Only {0} of {1} uniforms are paired:", pairs.Count(), uniformVariables.Length)); foreach (var item in pairs) { builder.AppendLine(string.Format("{0}", item.mappingPair)); } throw new Exception(builder.ToString()); } //var samplers = from variable in uniformVariables // where variable.GetValue().GetType() == typeof(samplerValue) // select variable; //this.uniformSamplers = samplers.ToArray(); this.uniformVariables = uniformVariables; // init property buffer objects' renderer var propertyBufferRenderers = new BufferPointer[propertyNameMap.Count()]; int index = 0; foreach (var item in propertyNameMap) { BufferPointer bufferRenderer = this.model.GetBufferRenderer( item.NameInIConvert2BufferRenderer, item.VarNameInShader); if (bufferRenderer == null) { throw new Exception(); } propertyBufferRenderers[index++] = bufferRenderer; } this.propertyBufferPointers = propertyBufferRenderers; // init index buffer object's renderer this.indexBufferPointer = this.model.GetIndexBufferRenderer(); this.model = null; this.allShaderCodes = null; this.propertyNameMap = null; //this.uniformNameMap = null; { IndexBufferPointer renderer = this.indexBufferPointer as IndexBufferPointer; if (renderer != null) { this.elementCount = renderer.ElementCount; } } { ZeroIndexBufferPointer renderer = this.indexBufferPointer as ZeroIndexBufferPointer; if (renderer != null) { this.elementCount = renderer.VertexCount; } } }
private unsafe void InitVertexBuffers() { // http://images.cnblogs.com/cnblogs_com/bitzhuwei/482613/o_Cube-small.jpg { VR03PositionBuffer positionBuffer = new VR03PositionBuffer(strin_Position); positionBuffer.Alloc(xFrameCount * yFrameCount * zFrameCount); HexahedronPosition *array = (HexahedronPosition *)positionBuffer.FirstElement(); int index = 0; for (int i = 0; i < xFrameCount; i++) { for (int j = 0; j < yFrameCount; j++) { for (int k = 0; k < zFrameCount; k++) { var x = ((float)i / (float)xFrameCount - 0.5f) * factor; var y = ((float)j / (float)yFrameCount - 0.5f) * factor; var z = (((float)k / (float)zFrameCount - 0.5f) * 109.0f / 256.0f) * factor; var hexahedron = new HexahedronPosition(); hexahedron.v0 = new vec3(x + hexahedronHalfLength, y + hexahedronHalfLength, z + hexahedronHalfLength); hexahedron.v1 = new vec3(x - hexahedronHalfLength, y + hexahedronHalfLength, z + hexahedronHalfLength); hexahedron.v2 = new vec3(x + hexahedronHalfLength, y - hexahedronHalfLength, z + hexahedronHalfLength); hexahedron.v3 = new vec3(x - hexahedronHalfLength, y - hexahedronHalfLength, z + hexahedronHalfLength); hexahedron.v4 = new vec3(x + hexahedronHalfLength, y + hexahedronHalfLength, z - hexahedronHalfLength); hexahedron.v5 = new vec3(x - hexahedronHalfLength, y + hexahedronHalfLength, z - hexahedronHalfLength); hexahedron.v6 = new vec3(x + hexahedronHalfLength, y - hexahedronHalfLength, z - hexahedronHalfLength); hexahedron.v7 = new vec3(x - hexahedronHalfLength, y - hexahedronHalfLength, z - hexahedronHalfLength); array[index++] = hexahedron; } } } this.positionBufferRenderer = positionBuffer.GetRenderer(); positionBuffer.Dispose(); } { VR03UVBuffer uvBuffer = new VR03UVBuffer(strin_uv); uvBuffer.Alloc(xFrameCount * yFrameCount * zFrameCount); HexahedronUV *array = (HexahedronUV *)uvBuffer.FirstElement(); int index = 0; for (int i = 0; i < xFrameCount; i++) { for (int j = 0; j < yFrameCount; j++) { for (int k = 0; k < zFrameCount; k++) { var x = (float)i / (float)xFrameCount; var y = (float)j / (float)yFrameCount; var z = (float)k / (float)zFrameCount; var color = new vec3(x, y, z); var uv = new HexahedronUV(); uv.v0 = color; uv.v1 = color; uv.v2 = color; uv.v3 = color; uv.v4 = color; uv.v5 = color; uv.v6 = color; uv.v7 = color; array[index++] = uv; } } } this.uvBufferRenderer = uvBuffer.GetRenderer(); uvBuffer.Dispose(); } { var indexBuffer = new VR03IndexBuffer(); indexBuffer.Alloc(xFrameCount * yFrameCount * zFrameCount); HexahedronIndex *array = (HexahedronIndex *)indexBuffer.FirstElement(); int index = 0; for (int i = 0; i < xFrameCount; i++) { for (int j = 0; j < yFrameCount; j++) { for (int k = 0; k < zFrameCount; k++) { uint centerIndex = (uint)((i * yFrameCount * zFrameCount + j * zFrameCount + k) * 8); array[index++] = new HexahedronIndex(centerIndex); } } } this.indexBufferRenderer = indexBuffer.GetRenderer() as IndexBufferPointerBase; indexBuffer.Dispose(); } this.vao = new VertexArrayObject( this.indexBufferRenderer, this.positionBufferRenderer, this.uvBufferRenderer); }
private unsafe void InitVertexBuffers() { { VR04PositionBuffer positionBuffer = new VR04PositionBuffer(strin_Position); positionBuffer.Alloc(zFrameCount); QuadPosition *array = (QuadPosition *)positionBuffer.FirstElement(); for (int i = 0; i < zFrameCount; i++) { array[i] = new QuadPosition( new vec3(-xLength, -yLength, (float)i / (float)zFrameCount - 0.5f), new vec3(xLength, -yLength, (float)i / (float)zFrameCount - 0.5f), new vec3(xLength, yLength, (float)i / (float)zFrameCount - 0.5f), new vec3(-xLength, yLength, (float)i / (float)zFrameCount - 0.5f) ); } this.positionBufferRenderer = positionBuffer.GetRenderer(); positionBuffer.Dispose(); } { VR04UVBuffer uvBuffer = new VR04UVBuffer(strin_uv); uvBuffer.Alloc(zFrameCount); QuadUV *array = (QuadUV *)uvBuffer.FirstElement(); for (int i = 0; i < zFrameCount; i++) { array[i] = new QuadUV( new vec3(0, 0, (float)i / (float)zFrameCount), new vec3(1, 0, (float)i / (float)zFrameCount), new vec3(1, 1, (float)i / (float)zFrameCount), new vec3(0, 1, (float)i / (float)zFrameCount) ); } this.uvBufferRenderer = uvBuffer.GetRenderer(); uvBuffer.Dispose(); } { var indexBuffer = new VR04IndexBuffer(); indexBuffer.Alloc(zFrameCount); QuadIndex *array = (QuadIndex *)indexBuffer.FirstElement(); for (uint i = 0; i < zFrameCount; i++) { if (this.reverSide) { array[i] = new QuadIndex((zFrameCount - i - 1) * 4 + 0, (zFrameCount - i - 1) * 4 + 1, (zFrameCount - i - 1) * 4 + 2, (zFrameCount - i - 1) * 4 + 3 ); } else { array[i] = new QuadIndex(i * 4 + 0, i * 4 + 1, i * 4 + 2, i * 4 + 3); } } this.indexBufferRenderer = indexBuffer.GetRenderer() as IndexBufferPointerBase; indexBuffer.Dispose(); } //if (!this.reverSide) //{ // var indexBuffer = new ZeroIndexBuffer(DrawMode.Quads, zFrameCount * 4); // indexBuffer.Alloc(zFrameCount);// this actually does nothing. // this.indexBufferRenderer = indexBuffer.GetRenderer(); // indexBuffer.Dispose(); //} //else //{ // var indexBuffer = new VR04IndexBuffer(); // indexBuffer.Alloc(zFrameCount); // QuadIndex* array = (QuadIndex*)indexBuffer.FirstElement(); // for (uint i = 0; i < zFrameCount; i++) // { // //array[i] = new QuadIndex(i * 4 + 0, i * 4 + 1, i * 4 + 2, i * 4 + 3); // array[i] = new QuadIndex((zFrameCount - i - 1) * 4 + 0, // (zFrameCount - i - 1) * 4 + 1, // (zFrameCount - i - 1) * 4 + 2, // (zFrameCount - i - 1) * 4 + 3 // ); // } // this.indexBufferRenderer = indexBuffer.GetRenderer(); // indexBuffer.Dispose(); //} this.vao = new VertexArrayObject( this.indexBufferRenderer, this.positionBufferRenderer, this.uvBufferRenderer); }