Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        private void GenerateGeometry()
        {
            Stopwatch watch = Stopwatch.StartNew();


            Dictionary <Tile.TileIds, Bitmap> tempTiletypeList =
                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];

//            float xOffset = 0.0f;
//            float yOffset =0.0f;
//            int index = 0;
//
//            for (UInt32 i = 0; i != 10; ++i)
//            {
//                for (UInt32 j = 0; j != 10; ++j)
//                {
//                    for (UInt32 k = 0; k != 5; ++k)
//                    {
//                        vVertex[index].x = gQuad[k].x + xOffset;
//                        vVertex[index].y = gQuad[k].y + yOffset;
//                        vVertex[index].z = gQuad[k].z;
//                        vVertex[index].u = gQuad[k].u;
//                        vVertex[index].v = gQuad[k].v;
//                        index++;
//                    }
//                    xOffset += 1.0f;
//                }
//                yOffset += 1.0f;
//                xOffset = 0.0f;
//            }


            // 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);

//                in vec3 position;
//              in vec2 texCoord;


            uint location = (uint)Gl.GetAttribLocation(program.ProgramID, "position");
//            int tempStride = stride;
            int tempStride = sizeof(Single) * stride;

//            int tempStride = sizeof(float)*stride;
//            int tempStride = Marshal.SizeOf(typeof (float))*stride;
            Gl.EnableVertexAttribArray(location);
            Gl.BindBuffer(gVertexBuffer);
            Gl.VertexAttribPointer(location, 3, VertexAttribPointerType.Float, false, tempStride,
                                   IntPtr.Zero);

            location = (uint)Gl.GetAttribLocation(program.ProgramID, "texCoord");

            Gl.EnableVertexAttribArray(location);
            Gl.BindBuffer(gVertexBuffer);
            Gl.VertexAttribPointer(location, 2, VertexAttribPointerType.Float, false, tempStride,
//                (IntPtr)(3 ));
//                (IntPtr)(3 * sizeof()));
                                   new IntPtr(3 * sizeof(Single)));


//            const uint loc0 = 0;
//            const uint loc1 = 1;
//            Gl.EnableVertexAttribArray(loc0);
//            Gl.VertexAttribPointer(loc0, 3, gVertexBuffer.PointerType, false, Marshal.SizeOf(typeof (float))*stride,
//                IntPtr.Zero);
//
//            Gl.EnableVertexAttribArray(loc1);
//            Gl.VertexAttribPointer(loc1, 2, gVertexBuffer.PointerType, false, Marshal.SizeOf(typeof (float))*stride,
//                (IntPtr) (3*sizeof (float)));


            //Specify vertex attributes for the shader
//            glEnableVertexAttribArray(0);
//            glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(SVertex2D), (GLvoid*)0);
//            glEnableVertexAttribArray(1);
//            glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(SVertex2D), (GLvoid*)2);


            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*3);
                vDrawCommand[i].baseVertex = (uint)(i * 4); //****
//                vDrawCommand[i].baseVertex = (uint) (i*stride);
                vDrawCommand[i].baseInstance = i;
            }


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

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

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

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


            //Generate an instanced vertex array to identify each draw call in the shader
            int[] vDrawId = new int[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 <int>(vDrawId, VertexAttribPointerType.Int, BufferTarget.ArrayBuffer,
                                          BufferUsageHint.StaticDraw);
            Gl.BindBuffer(gDrawIdBuffer);


            location = (uint)Gl.GetAttribLocation(program.ProgramID, "drawid");

            Gl.EnableVertexAttribArray(location);
//            Gl.VertexAttribPointer(location, 1, VertexAttribPointerType.UnsignedInt, true, Marshal.SizeOf(typeof(UInt32)),
//                IntPtr.Zero);
            Gl.VertexAttribPointer(location, 1, VertexAttribPointerType.UnsignedInt, true, 0,
                                   IntPtr.Zero);
            Gl.VertexAttribDivisor(location, 1);
//            Gl.VertexAttribDivisor(2,1);


//            const uint loc2 = 2;
//            Gl.EnableVertexAttribArray(loc2);
//            Gl.VertexAttribPointer(loc2, 1, gDrawIdBuffer.PointerType, true, Marshal.SizeOf(typeof (UInt32)),
//                IntPtr.Zero);
//            Gl.VertexAttribDivisor(2,1);

//
//  glGenBuffers( 1, &gDrawIdBuffer );
//  glBindBuffer( GL_ARRAY_BUFFER, gDrawIdBuffer );
//  glBufferData( GL_ARRAY_BUFFER, sizeof(vDrawId), vDrawId, GL_STATIC_DRAW );
//
//  glEnableVertexAttribArray(2);
//  glVertexAttribIPointer(2, 1, GL_UNSIGNED_INT, 0, (GLvoid*)0 );
//  glVertexAttribDivisor(2, 1);

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