public static SVertex2D FromArray(float[] anArray)
        {
            if (anArray.Length != 5)
            {
                throw new ArgumentException("Needs to be an array of length 5");
            }
            SVertex2D tempSVertex2D = new SVertex2D()
            {
                x = anArray[0],
                y = anArray[1],
                z = anArray[2],
                u = anArray[3],
                v = anArray[4]
            };

            return(tempSVertex2D);
        }
Exemple #2
0
        private void GenerateGeometry()
        {
            numberOfVerticesPerTile = gQuad.Length;

            Stopwatch watch = Stopwatch.StartNew();


            Dictionary <Tile.TileIds, Bitmap> tempTiletypeList =
                RenderObjects.RenderObjects.CreateTileBitmaps(new Size(256, 256));
            List <ObjObject> tempObjList = new List <ObjObject>();

            Tile[] tempTiles = theMap.TheTileArray;

            numberOfTiles = tempTiles.Length;
            int numberOfTextures = tempTiletypeList.Count;

            // Number of values in SVertex2D
            stride = 5;
            SVertex2D[] vVertex = new SVertex2D[numberOfTiles * numberOfVerticesPerTile];

            // n tiles
            // 6 SVertex2D vectors per tile
            // 5 (stride) entries per SVertex2D

            int index = 0;

            foreach (Tile tempTile in tempTiles)
            {
                Vector tempLoc = tempTile.Location;
                for (int k = 0; k != numberOfVerticesPerTile; ++k)
                {
                    vVertex[index].x = gQuad[k].x + tempLoc.X;
                    vVertex[index].y = gQuad[k].y;
                    vVertex[index].z = gQuad[k].z + tempLoc.Y;
                    vVertex[index].u = gQuad[k].u;
                    vVertex[index].v = gQuad[k].v;
                    index++;
                }
            }

            // Copy into float array
            float[] verteses = new float[numberOfTiles * stride * numberOfVerticesPerTile];

            index = 0;
            for (int i = 0; i < vVertex.Count(); i++)
            {
                verteses[index]     = vVertex[i].x;
                verteses[index + 1] = vVertex[i].y;
                verteses[index + 2] = vVertex[i].z;
                verteses[index + 3] = vVertex[i].u;
                verteses[index + 4] = vVertex[i].v;
                index += stride;
            }

            // Bind the buffers
            gVertexBuffer = new VBO <float>(verteses, BufferTarget.ArrayBuffer);
            Gl.BindBuffer(gVertexBuffer);

            locationPosition = (uint)Gl.GetAttribLocation(program.ProgramID, "position");
            locationTexCoord = (uint)Gl.GetAttribLocation(program.ProgramID, "texCoord");

//            int tempStride = sizeof(Single) * stride; // *****
//
//            Gl.EnableVertexAttribArray(locationPosition);
//            Gl.VertexAttribPointer(locationPosition, 3, VertexAttribPointerType.Float, false, tempStride,
//                IntPtr.Zero);
//
//            Gl.EnableVertexAttribArray(locationTexCoord);
//            Gl.VertexAttribPointer(locationTexCoord, 2, VertexAttribPointerType.Float, false, tempStride,
//                new IntPtr(3 * sizeof(Single)));
//
//
            CreateTileIdVbo(tempTiles);

            watch.Stop();
            GameCore.TheGameCore.RaiseMessage(string.Format("CreateTiles() took {0}ms", watch.ElapsedMilliseconds));
        }
        private void GenerateGeometry()
        {
            Stopwatch watch = Stopwatch.StartNew();


            Dictionary <Tile.TileIds, Bitmap> tempTiletypeList =
                RenderObjects.RenderObjects.CreateTileBitmaps(new Size(256, 256));
            List <ObjObject> tempObjList = new List <ObjObject>();

            Tile[] tempTiles = theMap.TheTileArray;

            numberOfTiles = tempTiles.Count();
            int numberOfTextures = tempTiletypeList.Count;

            // Number of values in SVertex2D
            stride = 5;
            SVertex2D[] vVertex = new SVertex2D[numberOfTiles * 4];

            // n tiles
            // 4 SVertex2D vectors per tile
            // 5 (stride) entries per SVertex2D

            int index = 0;

            foreach (Tile tempTile in tempTiles)
            {
                Vector tempLoc = tempTile.Location;
                for (int k = 0; k != 4; ++k)
                {
                    vVertex[index].x = gQuad[k].x + tempLoc.X;
                    vVertex[index].y = gQuad[k].y;
                    vVertex[index].z = gQuad[k].z + tempLoc.Y;
                    vVertex[index].u = gQuad[k].u;
                    vVertex[index].v = gQuad[k].v;
                    index++;
                }
            }

            // Copy into float array
            float[] verteses = new float[numberOfTiles * stride * 4];

            index = 0;
            for (int i = 0; i < vVertex.Count(); i++)
            {
                verteses[index]     = vVertex[i].x;
                verteses[index + 1] = vVertex[i].y;
                verteses[index + 2] = vVertex[i].z;
                verteses[index + 3] = vVertex[i].u;
                verteses[index + 4] = vVertex[i].v;
                index += stride;
            }

            // Bind the buffers
            gVertexBuffer = new VBO <float>(verteses, BufferTarget.ArrayBuffer);
            Gl.BindBuffer(gVertexBuffer);

            locationPosition = (uint)Gl.GetAttribLocation(program.ProgramID, "position");
            locationTexCoord = (uint)Gl.GetAttribLocation(program.ProgramID, "texCoord");

            gElementBuffer = new VBO <int>(gIndex, BufferTarget.ElementArrayBuffer);
            Gl.BindBuffer(gElementBuffer);

            //Generate draw commands
            SDrawElementsCommand[] vDrawCommand = new SDrawElementsCommand[numberOfTiles];
            for (uint i = 0; i < numberOfTiles; ++i)
            {
                vDrawCommand[i].vertexCount   = 6;
                vDrawCommand[i].instanceCount = 1;
                vDrawCommand[i].firstIndex    = 0;
                vDrawCommand[i].baseVertex    = (uint)(i * 4); //****
                vDrawCommand[i].baseInstance  = i;
            }


            // Copy into float array
            //Generate draw commands
            int vDrawCommanStride = 5;

            int[] vDrawCommandArray = new int[numberOfTiles * vDrawCommanStride];

            index = 0;
            for (int i = 0; i < vDrawCommand.Count(); i++)
            {
                vDrawCommandArray[index]     = (int)vDrawCommand[i].vertexCount;
                vDrawCommandArray[index + 1] = (int)vDrawCommand[i].instanceCount;
                vDrawCommandArray[index + 2] = (int)vDrawCommand[i].firstIndex;
                vDrawCommandArray[index + 3] = (int)vDrawCommand[i].baseVertex;
                vDrawCommandArray[index + 4] = (int)vDrawCommand[i].baseInstance;
                index += vDrawCommanStride;
            }

            gIndirectBuffer = new VBO <int>(vDrawCommandArray, BufferTarget.DrawIndirectBuffer,
                                            BufferUsageHint.StaticDraw);
            Gl.BindBuffer(gIndirectBuffer);


            //Generate an instanced vertex array to identify each draw call in the shader
            float[] vDrawId = new float[numberOfTiles];

            index = 0;
            foreach (Tile aTile in tempTiles)
            {
                vDrawId[index] = (int)aTile.TheTileId;
                index++;
            }


            //            for (UInt32 i = 0; i < numberOfTiles; i++)
            //            {
            //                vDrawId[i] = i;
            //            }

            gDrawIdBuffer = new VBO <float>(vDrawId, VertexAttribPointerType.Float, BufferTarget.ArrayBuffer,
                                            BufferUsageHint.StaticDraw);
            Gl.BindBuffer(gDrawIdBuffer);

            locationDrawid = (uint)Gl.GetAttribLocation(program.ProgramID, "drawTexId");

            watch.Stop();
            GameCore.TheGameCore.RaiseMessage(string.Format("CreateTiles() took {0}ms", watch.ElapsedMilliseconds));
        }