Beispiel #1
0
        public static void Render()
        {
            if (Player.Instance == null)
            {
                return;
            }
            Gl.UseProgram(shader.ProgramID);

            int minx, maxx, miny, maxy;

            Terrain.Range(out minx, out maxx, out miny, out maxy);
            int mingx = (int)Math.Floor((float)minx / GridX);
            int mingy = (int)Math.Floor((float)miny / GridY);
            int maxgx = (int)Math.Ceiling((float)maxx / GridX);
            int maxgy = (int)Math.Ceiling((float)maxy / GridY);

            MathUtil.Clamp(ref mingx, 0, EntityGrid.GetLength(0) - 1);
            MathUtil.Clamp(ref mingy, 0, EntityGrid.GetLength(1) - 1);
            MathUtil.Clamp(ref maxgx, 0, EntityGrid.GetLength(0) - 1);
            MathUtil.Clamp(ref maxgy, 0, EntityGrid.GetLength(1) - 1);

            Dictionary <EntityID, HashSet <Entity> > EntitiesMap = new Dictionary <EntityID, HashSet <Entity> >();

            for (int i = mingx; i <= maxgx; i++)
            {
                for (int j = mingy; j <= maxgy; j++)
                {
                    HashSet <Entity> set = EntityGrid[i, j];
                    foreach (Entity e in set)
                    {
                        HashSet <Entity> setbatch;
                        if (EntitiesMap.TryGetValue(e.entityId, out setbatch))
                        {
                            setbatch.Add(e);
                        }
                        else
                        {
                            setbatch = new HashSet <Entity>();
                            setbatch.Add(e);
                            EntitiesMap.Add(e.entityId, setbatch);
                        }
                    }
                }
            }

            LoadedEntities = 0;

            Gl.Enable(EnableCap.Blend);
            Gl.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
            Gl.BindTexture(Assets.Textures.EntityTexture.TextureTarget, Assets.Textures.EntityTexture.TextureID);
            foreach (EntityID entityId in EntitiesMap.Keys)
            {
                EntityModel model = Assets.Models.GetModel(entityId);
                Gl.BindVertexArray(model.vao.ID);


                foreach (Entity e in EntitiesMap[entityId])
                {
                    LoadedEntities++;
                    shader["modelMatrix"].SetValue(e.ModelMatrix());
                    if (e.data.recentDmg > 0)
                    {
                        float offsetval = 1 - e.data.recentDmg / EntityData.maxRecentDmgTime;
                        offsetval /= 2;
                        Vector4 colouroffset = TextureUtil.ToVec4(Color.DarkGoldenrod) * new Vector4(offsetval, offsetval, offsetval, 1);
                        colouroffset *= e.data.colour;
                        shader["clr"].SetValue(colouroffset);
                    }
                    else
                    {
                        shader["clr"].SetValue(e.data.colour);
                    }

                    Gl.DrawElements(model.drawmode, model.vao.count, DrawElementsType.UnsignedInt, IntPtr.Zero);
                }
            }

            if (GameLogic.RenderHitboxes)
            {
                var model = Assets.Models.GetModel(EntityID.HitboxOutline);
                Gl.BindVertexArray(model.vao.ID);
                foreach (EntityID entityId in EntitiesMap.Keys)
                {
                    foreach (Entity e in EntitiesMap[entityId])
                    {
                        var h = e.hitbox;
                        shader["modelMatrix"].SetValue(MathUtil.ModelMatrix(h.Size, 0, h.Position));
                        Gl.DrawElements(model.drawmode, model.vao.count, DrawElementsType.UnsignedInt, IntPtr.Zero);
                    }
                }
                Gl.BindVertexArray(0);
            }

            Gl.BindTexture(Assets.Textures.EntityTexture.TextureTarget, 0);
            Gl.Disable(EnableCap.Blend);
            Gl.BindVertexArray(0);
            Gl.UseProgram(0);
        }
Beispiel #2
0
        private static void OnRenderFrame()
        {
            watch.Stop();
            float deltaTime = (float)watch.ElapsedTicks / System.Diagnostics.Stopwatch.Frequency;

            watch.Restart();

            // perform rotation of the scene depending on keyboard input
            if (right)
            {
                phi += deltaTime;
            }
            if (left)
            {
                phi -= deltaTime;
            }
            if (up)
            {
                theta += deltaTime;
            }
            if (down)
            {
                theta -= deltaTime;
            }
            if (theta < 0)
            {
                theta += (float)Math.PI * 2;
            }

            // set up the viewport and clear the previous depth and color buffers
            Gl.Viewport(0, 0, width, height);
            Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            // make sure the shader program and texture are being used
            Gl.UseProgram(program.ProgramID);
            Gl.BindTexture(flagTexture);

            // calculate the camera position using some fancy polar co-ordinates
            Vector3 position = 20 * new Vector3((float)(Math.Cos(phi) * Math.Sin(theta)), (float)Math.Cos(theta), (float)(Math.Sin(phi) * Math.Sin(theta)));
            Vector3 upVector = ((theta % (Math.PI * 2)) > Math.PI) ? new Vector3(0, 1, 0) : new Vector3(0, -1, 0);

            program["view_matrix"].SetValue(Matrix4.LookAt(position, Vector3.Zero, upVector));

            program["time"].SetValue(flagTime);
            flagTime += deltaTime;

            Gl.BindBufferToShaderAttribute(flagVertices, program, "vertexPosition");
            Gl.BindBufferToShaderAttribute(flagUVs, program, "vertexUV");
            Gl.BindBuffer(flagTriangles);

            Gl.DrawElements(BeginMode.Triangles, flagTriangles.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);

            // bind the font program as well as the font texture
            Gl.UseProgram(fontProgram.ProgramID);
            Gl.BindTexture(font.FontTexture);

            // build this string every frame, since theta and phi can change
            FontVAO vao = font.CreateString(fontProgram, string.Format("Theta:   {0:0.000},  Phi:   {1:0.000},  Time:   {2:0.000}", theta, phi, flagTime), BMFont.Justification.Right);

            vao.Position = new Vector2(width / 2 - 10, height / 2 - font.Height - 10);
            vao.Draw();
            vao.Dispose();

            // draw the tutorial information, which is static
            information.Draw();

            Glut.glutSwapBuffers();
        }
Beispiel #3
0
        public void Draw()
        {
            // apply our camera view matrix to the shader view matrix (this can be used for all objects in the scene)
//            Gl.UseProgram(program);
            program.Use();
            program["view_matrix"].SetValue(theCamera.ViewMatrix);
            program["projection_matrix"].SetValue(projectionMatrix);


            Gl.ActiveTexture(TextureUnit.Texture0);
            Gl.BindTexture(TextureTarget.Texture2DArray, textureId);


//            Gl.BindBuffer(gVertexBuffer);

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


            uint location = (uint)Gl.GetAttribLocation(program.ProgramID, "position");
            //            int tempStride = stride;
//            int tempStride = sizeof(Single) * 4;
            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);

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

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


            Gl.BindBuffer(gIndirectBuffer);


            location = (uint)Gl.GetAttribLocation(program.ProgramID, "drawid");
            Gl.EnableVertexAttribArray(location);
            Gl.BindBuffer(gDrawIdBuffer);
            Gl.VertexAttribPointer(location, 1, VertexAttribPointerType.Int, true, Marshal.SizeOf(typeof(int)),
                                   IntPtr.Zero);
//            Gl.VertexAttribPointer(location, 1, VertexAttribPointerType.UnsignedInt, true, 0, IntPtr.Zero);
            Gl.VertexAttribDivisor(location, 1);
//            Gl.VertexAttribDivisor(2, 1);


            // This needs to be implemented.
            // https://www.opengl.org/wiki/GLAPI/glMultiDrawElementsIndirect
//  glClear( GL_COLOR_BUFFER_BIT );

//            Gl.MultiDrawElements(BeginMode.Triangles, );

            Gl.MultiDrawElementsIndirect(BeginMode.Triangles, DrawElementsType.UnsignedInt, IntPtr.Zero, numberOfTiles,
                                         0);
//  glMultiDrawElementsIndirect( GL_TRIANGLES,
//			       GL_UNSIGNED_INT,
//			       (GLvoid*)0,
//			       100,
//			       0 );

//  glutSwapBuffers();
            Gl.BindTexture(TextureTarget.Texture2DArray, 0);

//            Gl.UnmapBuffer()
            Gl.BindBuffer(BufferTarget.ArrayBuffer, 0);
            Gl.BindBuffer(BufferTarget.DrawIndirectBuffer, 0);
            Gl.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
            Gl.UseProgram(0);
        }
 public static TextureBinding <T> Bind(T texture)
 {
     Gl.BindTexture(TextureTarget, texture.Handle);
     return(new TextureBinding <T>());
 }
Beispiel #5
0
 public Texture(TextureTarget t, string file) : this()
 {
     Gl.BindTexture(t, ID);
     TextureGl(t, file);
 }
Beispiel #6
0
 /// <summary>
 /// Unbind the current texture array
 /// </summary>
 public void Unbind()
 {
     Gl.BindTexture(TextureTarget.Texture2dArray, 0);
 }
Beispiel #7
0
        private static void OnRenderFrame()
        {
            //calculate how much time has elapsed since the last frame
            watch.Stop();
            float deltaTime = (float)watch.ElapsedTicks / System.Diagnostics.Stopwatch.Frequency;

            watch.Restart();

            //use the deltaTime to adjust the angle of the cube and pyramid
            if (autoRotate)
            {
                yangle += deltaTime / 2;
                xangle += deltaTime;
            }

            if (up)
            {
                xangle -= deltaTime;
            }
            if (down)
            {
                xangle += deltaTime;
            }
            if (left)
            {
                yangle += deltaTime;
            }
            if (right)
            {
                yangle -= deltaTime;
            }


            //set up the OpenGl viewport and clear both the color and depth bits
            Gl.Viewport(0, 0, width, height);
            Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            //use our shader program
            program.Use();
            Gl.BindTexture(glassTexture);


            uint vertexPositionIndex = (uint)Gl.GetAttribLocation(program.ProgramID, "vertexPosition");

            Gl.EnableVertexAttribArray(vertexPositionIndex);

            //enable disable lighting
            program["enable_lighting"].SetValue(lighting);

            //bind the vertex position, colors and elemnets of the cube
            program["model_matrix"].SetValue(Matrix4.CreateRotationY(yangle) * Matrix4.CreateRotationX(xangle));
            //           System.Diagnostics.Debug.WriteLine("{0} {1} {2} {3}",yangle, xangle, up, down);
            Gl.BindBufferToShaderAttribute(cube, program, "vertexPosition");
            Gl.BindBufferToShaderAttribute(cubeNormals, program, "vertexNormal");
            Gl.BindBufferToShaderAttribute(cubeUV, program, "vertexUV");
            Gl.BindBuffer(cubeElements);

            // draw my cube
            Gl.DrawElements(BeginMode.Quads, cubeElements.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);

            Glut.glutSwapBuffers();
        }
Beispiel #8
0
 private void BindFrameBuffer(uint frameBuffer, int width, int height)
 {
     Gl.BindTexture(TextureTarget.Texture2d, 0); //To make sure the texture isn't bound
     Gl.BindFramebuffer(FramebufferTarget.Framebuffer, frameBuffer);
     Gl.Viewport(0, 0, width, height);
 }
Beispiel #9
0
        private static void OnRenderFrame()
        {
            watch.Stop();
            float deltaTime = (float)watch.ElapsedTicks / System.Diagnostics.Stopwatch.Frequency;

            watch.Restart();

            if (msaa)
            {
                Gl.Enable(EnableCap.Multisample);
            }
            else
            {
                Gl.Disable(EnableCap.Multisample);
            }

            // update our camera by moving it up to 5 units per second in each direction
            if (down)
            {
                camera.MoveRelative(Vector3.UnitZ * deltaTime * 5);
            }
            if (up)
            {
                camera.MoveRelative(-Vector3.UnitZ * deltaTime * 5);
            }
            if (left)
            {
                camera.MoveRelative(-Vector3.UnitX * deltaTime * 5);
            }
            if (right)
            {
                camera.MoveRelative(Vector3.UnitX * deltaTime * 5);
            }
            if (space)
            {
                camera.MoveRelative(Vector3.UnitY * deltaTime * 3);
            }

            // set up the viewport and clear the previous depth and color buffers
            Gl.Viewport(0, 0, width, height);
            Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            // apply our camera view matrix to the shader view matrix (this can be used for all objects in the scene)
            Gl.UseProgram(program);
            program["view_matrix"].SetValue(camera.ViewMatrix);

            // now draw the object file
            if (wireframe)
            {
                Gl.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
            }
            objectFile.Draw();
            Gl.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);

            // bind the font program as well as the font texture
            Gl.UseProgram(fontProgram.ProgramID);
            Gl.BindTexture(font.FontTexture);

            // draw the tutorial information, which is static
            information.Draw();

            Glut.glutSwapBuffers();
        }
Beispiel #10
0
        public void TestGenTexture()
        {
            if (!HasVersion(1, 1) && !HasEsVersion(1, 0))
            {
                Assert.Inconclusive("OpenGL 1.1 or OpenGL ES 1.0");
            }

            uint[] textureObjects = new uint[3];
            uint   textureObject;

            textureObject = Gl.GenTexture();
            Assert.AreNotEqual(0, textureObject);
            Assert.IsFalse(Gl.IsTexture(textureObject));

            try {
                Gl.BindTexture(TextureTarget.Texture2d, textureObject);
                Assert.IsTrue(Gl.IsTexture(textureObject));
            } finally {
                Gl.DeleteTextures(textureObject);
                Assert.IsFalse(Gl.IsTexture(textureObject));
            }

            Gl.GenTextures(textureObjects);
            try {
                for (int i = 0; i < textureObjects.Length; i++)
                {
                    Assert.AreNotEqual(0, textureObjects[i]);
                    Assert.IsFalse(Gl.IsTexture(textureObjects[i]));

                    Gl.BindTexture(TextureTarget.Texture2d, textureObjects[i]);
                    Assert.IsTrue(Gl.IsTexture(textureObjects[i]));
                }
            } finally {
                for (int i = 0; i < textureObjects.Length; i++)
                {
                    if (Gl.IsTexture(textureObjects[i]))
                    {
                        Gl.DeleteTextures(textureObjects[i]);
                        Assert.IsFalse(Gl.IsTexture(textureObjects[i]));
                    }
                }
            }

            Gl.GenTextures(textureObjects);
            try {
                for (int i = 0; i < textureObjects.Length; i++)
                {
                    Assert.AreNotEqual(0, textureObjects[i]);
                    Assert.IsFalse(Gl.IsTexture(textureObjects[i]));

                    Gl.BindTexture(TextureTarget.Texture2d, textureObjects[i]);
                    Assert.IsTrue(Gl.IsTexture(textureObjects[i]));
                }
            } finally {
                Gl.DeleteTextures(textureObjects);
                for (int i = 0; i < textureObjects.Length; i++)
                {
                    Assert.IsFalse(Gl.IsTexture(textureObjects[i]));
                }
            }

            Gl.GenTextures(textureObjects);
            try {
                for (int i = 0; i < textureObjects.Length; i++)
                {
                    Assert.AreNotEqual(0, textureObjects[i]);
                    Assert.IsFalse(Gl.IsTexture(textureObjects[i]));

                    Gl.BindTexture(TextureTarget.Texture2d, textureObjects[i]);
                    Assert.IsTrue(Gl.IsTexture(textureObjects[i]));
                }
            } finally {
                Gl.DeleteTextures(textureObjects[0], textureObjects[1], textureObjects[2]);
                for (int i = 0; i < textureObjects.Length; i++)
                {
                    Assert.IsFalse(Gl.IsTexture(textureObjects[i]));
                }
            }
        }
Beispiel #11
0
        private static void OnRenderFrame()
        {
            watch.Stop();
            float deltaTime = (float)watch.ElapsedTicks / System.Diagnostics.Stopwatch.Frequency;

            watch.Restart();
            Console.WriteLine("FPS : " + 1f / deltaTime);
            time += deltaTime * timedir;
            // perform rotation of the cube depending on the keyboard state
            if (autoRotate)
            {
                xangle += deltaTime / 2;
                yangle += deltaTime;
            }
            if (right)
            {
                yangle += deltaTime;
            }
            if (left)
            {
                yangle -= deltaTime;
            }
            if (up)
            {
                xangle -= deltaTime;
            }
            if (down)
            {
                xangle += deltaTime;
            }

            // set up the viewport and clear the previous depth and color buffers
            Gl.Viewport(0, 0, width, height);
            Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            //Gl.ClearColor(255f/255,255f/255,255f/255,255f/255);
            // make sure the shader program and texture are being used
            Gl.UseProgram(program);
            Gl.BindTexture(CarModel.bodyTexture);
            Gl.Disable(EnableCap.Blend);
            Gl.Enable(EnableCap.DepthTest);

            // set up the model matrix and draw the cube
            program["model_matrix"].SetValue(Matrix4.CreateRotationY(yangle) * Matrix4.CreateRotationX(xangle));
            program["enable_lighting"].SetValue(lighting);

            Gl.BindBufferToShaderAttribute(CarModel.cube, program, "vertexPosition");
            Gl.BindBufferToShaderAttribute(CarModel.cubeNormals, program, "vertexNormal");
            Gl.BindBufferToShaderAttribute(CarModel.cubeUV, program, "vertexUV");
            Gl.BindBuffer(CarModel.cubeQuads);

            Gl.DrawElements(BeginMode.Quads, CarModel.cubeQuads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);


            //Draw window
            Gl.BindTexture(CarModel.kacaTexture);

            program["max_diffuse"].SetValue(maxdiffuse);
            program["ambient"].SetValue(ambient);

            Gl.BindBufferToShaderAttribute(CarModel.window, program, "vertexPosition");
            Gl.BindBufferToShaderAttribute(CarModel.windowNormals, program, "vertexNormal");
            Gl.BindBufferToShaderAttribute(CarModel.windowUV, program, "vertexUV");
            Gl.BindBuffer(CarModel.windowQuads);

            Gl.DrawElements(BeginMode.Quads, CarModel.windowQuads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);

            //drawwheel
            Gl.BindTexture(CarModel.rodaTexture);

            Gl.BindBufferToShaderAttribute(CarModel.wheel1, program, "vertexPosition");
            Gl.BindBufferToShaderAttribute(CarModel.wheel1Normals, program, "vertexNormal");
            Gl.BindBufferToShaderAttribute(CarModel.wheel1UV, program, "vertexUV");
            Gl.BindBuffer(CarModel.wheel1Quads);
            Gl.DrawElements(BeginMode.TriangleStrip, CarModel.wheel1Quads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
            Gl.BindBufferToShaderAttribute(CarModel.wheel2, program, "vertexPosition");
            Gl.DrawElements(BeginMode.TriangleStrip, CarModel.wheel1Quads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
            Gl.BindBufferToShaderAttribute(CarModel.wheel2Normals, program, "vertexNormal");

            Gl.BindBufferToShaderAttribute(CarModel.wheel3, program, "vertexPosition");
            Gl.DrawElements(BeginMode.TriangleStrip, CarModel.wheel1Quads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
            Gl.BindBufferToShaderAttribute(CarModel.wheel4, program, "vertexPosition");
            Gl.DrawElements(BeginMode.TriangleStrip, CarModel.wheel1Quads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);

            //Gl.BindTexture(velgTexture);
            Gl.BindBufferToShaderAttribute(CarModel.wheel1Normals, program, "vertexNormal");

            Gl.BindBufferToShaderAttribute(CarModel.velg1, program, "vertexPosition");
            Gl.DrawElements(BeginMode.TriangleFan, CarModel.wheel1Quads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
            Gl.BindBufferToShaderAttribute(CarModel.velg2, program, "vertexPosition");

            Gl.DrawElements(BeginMode.TriangleFan, CarModel.wheel1Quads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
            Gl.BindBufferToShaderAttribute(CarModel.wheel2Normals, program, "vertexNormal");


            Gl.BindBufferToShaderAttribute(CarModel.velg3, program, "vertexPosition");
            Gl.DrawElements(BeginMode.TriangleFan, CarModel.wheel1Quads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
            Gl.BindBufferToShaderAttribute(CarModel.velg4, program, "vertexPosition");
            Gl.DrawElements(BeginMode.TriangleFan, CarModel.wheel1Quads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
            Gl.Enable(EnableCap.Blend);
            Gl.Disable(EnableCap.DepthTest);

            //Draw Rain
            // make sure the shader program and texture are being used
            Gl.UseProgram(Rain.program.ProgramID);
            Gl.BindTexture(Rain.particleTexture);
            Rain.program["model_matrix"].SetValue(Matrix4.CreateRotationY(yangle) * Matrix4.CreateRotationX(xangle));

            // update our particle list
            for (int i = 0; i < Rain.particles.Count; i++)
            {
                Rain.particles[i].Update(deltaTime);
                //if (particles[i].Life < 0) particles[i] = new Particle(Vector3.Zero);
                if (Rain.particles[i].Life < 0)
                {
                    Rain.particles[i] = new Rain.Particle(new Vector3((float)Rain.generator.NextDouble() * 50 - 25, 20, (float)Rain.generator.NextDouble() * 50 - 25));
                }
                Rain.particlePositions[i] = Rain.particles[i].Position;
            }

            // delete our previous particle positions (if applicable) and then create a new VBO
            if (Rain.particleVertices != null)
            {
                Rain.particleVertices.Dispose();
            }
            Rain.particleVertices = new VBO <Vector3>(Rain.particlePositions);

            // bind the VBOs to their shader attributes
            Gl.BindBufferToShaderAttribute(Rain.particleVertices, Rain.program, "vertexPosition");
            Gl.BindBufferToShaderAttribute(Rain.particleColors, Rain.program, "vertexColor");
            Gl.BindBuffer(Rain.particlePoints);

            // enable point sprite mode (which enables the gl_PointCoord value)
            Gl.Enable(EnableCap.PointSprite);
            Gl.DrawElements(BeginMode.Points, Rain.particlePoints.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
            Gl.Disable(EnableCap.PointSprite);


            //Draw Smoke
            // make sure the shader program and texture are being used
            Gl.UseProgram(Smoke.program.ProgramID);
            Gl.BindTexture(Smoke.particleTexture);
            Smoke.program["model_matrix"].SetValue(Matrix4.CreateRotationY(yangle) * Matrix4.CreateRotationX(xangle));
            if (time > 5f)
            {
                timedir = -1;
            }
            else if (time < 0f)
            {
                timedir = 1;
            }
            Smoke.program["mixer"].SetValue(time / 2.5f);

            // update our particle list
            for (int i = 0; i < Smoke.particles.Count; i++)
            {
                Smoke.particles[i].Update(deltaTime);
                //if (particles[i].Life < 0) particles[i] = new Particle(Vector3.Zero);
                if (Smoke.particles[i].Life < 0)
                {
                    Smoke.particles[i] = new Smoke.Particle(new Vector3(1.8f, 0.2f, 0.3f));
                }
                Smoke.particlePositions[i] = Smoke.particles[i].Position;
            }

            // delete our previous particle positions (if applicable) and then create a new VBO
            if (Smoke.particleVertices != null)
            {
                Smoke.particleVertices.Dispose();
            }
            Smoke.particleVertices = new VBO <Vector3>(Smoke.particlePositions);

            // bind the VBOs to their shader attributes
            Gl.BindBufferToShaderAttribute(Smoke.particleVertices, Smoke.program, "vertexPosition");
            Gl.BindBufferToShaderAttribute(Smoke.particleColors, Smoke.program, "vertexColor");
            Gl.BindBuffer(Smoke.particlePoints);

            // enable point sprite mode (which enables the gl_PointCoord value)
            Gl.Enable(EnableCap.PointSprite);
            Gl.DrawElements(BeginMode.Points, Smoke.particlePoints.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
            Gl.Disable(EnableCap.PointSprite);


            Glut.glutSwapBuffers();
        }
Beispiel #12
0
 public static void Unbind() => Gl.BindTexture(TextureTarget.Texture2d, 0u);
Beispiel #13
0
 public void Bind()
 {
     // Lock();
     Gl.BindTexture(TextureTarget.Texture2d, TexturePointer);
     // Unlock();
 }
Beispiel #14
0
        public override void DrawMesh(ICubismTexture itexture, float[] vertex_buffer, float[] uv_buffer, short[] index_buffer, ICubismClippingMask iclipping_mask, Matrix clipping_matrix, BlendModeType blend_mode, bool use_culling, double opacity)
        {
            var  texture           = (CubismOpenGlNetTexture)itexture;
            var  clipping_mask     = iclipping_mask as CubismOpenGlNetClippingMask;
            bool use_clipping_mask = (clipping_mask != null);

            UseCulling = use_culling;
            BlendMode  = blend_mode;

            var shader = ShaderManager.ShaderForDrawMesh(use_clipping_mask, UsePremultipliedAlpha);

            Gl.UseProgram(shader.ProgramId);

            // 頂点バッファを設定する
            Gl.EnableVertexAttribArray((uint)shader.AttributePositionLocation);
            GCHandle pinned_vertex_buffer = GCHandle.Alloc(vertex_buffer, GCHandleType.Pinned);

            Gl.VertexAttribPointer((uint)shader.AttributePositionLocation, 2, VertexAttribType.Float, false, sizeof(float) * 2, pinned_vertex_buffer.AddrOfPinnedObject());

            // UVバッファを設定する
            Gl.EnableVertexAttribArray((uint)shader.AttributeTexCoordLocation);
            GCHandle pinned_uv_buffer = GCHandle.Alloc(uv_buffer, GCHandleType.Pinned);

            Gl.VertexAttribPointer((uint)shader.AttributeTexCoordLocation, 2, VertexAttribType.Float, false, sizeof(float) * 2, pinned_uv_buffer.AddrOfPinnedObject());

            if (use_clipping_mask == true)
            {
                Gl.ActiveTexture(TextureUnit.Texture1);
                Gl.BindTexture(TextureTarget.Texture2d, clipping_mask.TextureId);
                Gl.Uniform1(shader.SamplerTexture1Location, 1);

                // View座標をClippingContextの座標に変換するための行列を設定
                Gl.UniformMatrix4(shader.UniformClipMatrixLocation, false, clipping_matrix.AsColumnMajorArray());

                // 使用するカラーチャンネルを設定
                Gl.Uniform4(shader.UnifromChannelFlagLocation, 1.0f, 0.0f, 0.0f, 0.0f);
            }
            else
            {
                Gl.ActiveTexture(TextureUnit.Texture1);
                Gl.BindTexture(TextureTarget.Texture2d, 0);
            }

            //テクスチャ設定
            Gl.ActiveTexture(TextureUnit.Texture0);
            Gl.BindTexture(TextureTarget.Texture2d, texture.TextureId);
            Gl.Uniform1(shader.SamplerTexture0Location, 0);

            // 座標変換
            Gl.UniformMatrix4(shader.UniformMatrixLocation, false, MvpMatrix.AsColumnMajorArray());

            // モデルの色を設定する
            float[] color = new float[4];
            ModelColor.CopyTo(color, 0);
            color[3] *= (float)opacity;
            if (UsePremultipliedAlpha == true)
            {
                color[0] *= color[3];
                color[1] *= color[3];
                color[2] *= color[3];
            }
            Gl.Uniform4(shader.UniformBaseColorLocation, color[0], color[1], color[2], color[3]);

            // 描画する
            GCHandle pinned_index_buffer = GCHandle.Alloc(index_buffer, GCHandleType.Pinned);

            Gl.DrawElements(PrimitiveType.Triangles, index_buffer.Length, DrawElementsType.UnsignedShort, pinned_index_buffer.AddrOfPinnedObject());

            // バッファのアドレスの固定を解除する
            pinned_vertex_buffer.Free();
            pinned_uv_buffer.Free();
            pinned_index_buffer.Free();
        }
Beispiel #15
0
 public void Bind(Texture texture)
 {
     GlHelper.ThrowNullException(Target);
     Gl.BindTexture(Target, texture.Id);
     GlHelper.GetError();
 }
Beispiel #16
0
        private static void OnRenderFrame()
        {
            watch.Stop();
            float deltaTime = (float)watch.ElapsedTicks / System.Diagnostics.Stopwatch.Frequency;

            watch.Restart();

            // update our camera by moving it up to 5 units per second in each direction
            if (down)
            {
                camera.MoveRelative(Vector3.UnitZ * deltaTime * 5);
            }
            if (up)
            {
                camera.MoveRelative(-Vector3.UnitZ * deltaTime * 5);
            }
            if (left)
            {
                camera.MoveRelative(-Vector3.UnitX * deltaTime * 5);
            }
            if (right)
            {
                camera.MoveRelative(Vector3.UnitX * deltaTime * 5);
            }
            if (space)
            {
                camera.MoveRelative(Vector3.Up * deltaTime * 3);
            }

            // set up the viewport and clear the previous depth and color buffers
            Gl.Viewport(0, 0, width, height);
            Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            // make sure the shader program and texture are being used
            Gl.UseProgram(program);
            Gl.ActiveTexture(TextureUnit.Texture1);
            Gl.BindTexture(brickNormals);
            Gl.ActiveTexture(TextureUnit.Texture0);
            Gl.BindTexture(brickDiffuse);

            // apply our camera view matrix to the shader view matrix (this can be used for all objects in the scene)
            program["view_matrix"].SetValue(camera.ViewMatrix);

            // set up the model matrix and draw the cube
            program["model_matrix"].SetValue(Matrix4.Identity);
            program["enable_lighting"].SetValue(lighting);
            program["enable_mapping"].SetValue(normalMapping);

            Gl.BindBufferToShaderAttribute(cube, program, "vertexPosition");
            Gl.BindBufferToShaderAttribute(cubeNormals, program, "vertexNormal");
            Gl.BindBufferToShaderAttribute(cubeTangents, program, "vertexTangent");
            Gl.BindBufferToShaderAttribute(cubeUV, program, "vertexUV");
            Gl.BindBuffer(cubeTriangles);

            Gl.DrawElements(BeginMode.Triangles, cubeTriangles.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);

            // bind the font program as well as the font texture
            Gl.UseProgram(fontProgram.ProgramID);
            Gl.BindTexture(font.FontTexture);

            // draw the tutorial information, which is static
            information.Draw();

            Glut.glutSwapBuffers();
        }
Beispiel #17
0
 /// <summary>
 /// bind the curent texture array
 /// </summary>
 public void Bind()
 {
     Gl.BindTexture(TextureTarget.Texture2dArray, id);
 }
        private static void Main()
        {
            if (!Glfw.Init())
            {
                Environment.Exit(-1);
            }

            Glfw.WindowHint(Glfw.Hint.Resizable, false);
            Glfw.WindowHint(Glfw.Hint.Doublebuffer, true);
            Glfw.WindowHint(Glfw.Hint.Samples, 4);
            Glfw.Window window = Glfw.CreateWindow(1280, 720, "Hello World!");

            if (!window)
            {
                Glfw.Terminate();
                Environment.Exit(-1);
            }

            Gl.Initialize();
            Glfw.MakeContextCurrent(window);

            Gl.Enable(EnableCap.Multisample);
            Gl.Enable(EnableCap.Texture2d);
            Gl.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
            Gl.Enable(EnableCap.Blend);

            System.Diagnostics.Trace.TraceInformation($"Renderer: {Gl.GetString(StringName.Renderer)}");
            System.Diagnostics.Trace.TraceInformation($"Version:  {Gl.GetString(StringName.Version)}");

            uint vao = Gl.GenVertexArray();

            Gl.BindVertexArray(vao);

            float[] points =
            {
                // Pos              color           tex
                -0.5f,  0.5f, 0f, 1f, 1f, 1f, 0f, 0f,
                0.5f,   0.5f, 0f, 1f, 1f, 1f, 1f, 0f,
                0.5f,  -0.5f, 0f, 1f, 1f, 1f, 1f, 1f,
                -0.5f, -0.5f, 0f, 1f, 1f, 1f, 0f, 1f,

                0f,       0f, 0f, 1f, 1f, 1f, 0f, 0f,
                1f,       0f, 0f, 1f, 1f, 1f, 1f, 0f,
                1f,      -1f, 0f, 1f, 1f, 1f, 1f, 1f,
                0f,      -1f, 0f, 1f, 1f, 1f, 0f, 1f
            };
            uint buffer = Gl.GenBuffer();

            Gl.BindBuffer(BufferTarget.ArrayBuffer, buffer);
            Gl.BufferData(BufferTarget.ArrayBuffer, (uint)points.Length * 4, points, BufferUsage.StaticDraw);

            uint[] elements =
            {
                0, 1, 2,
                2, 3, 0,

                4, 5, 6,
                6, 7, 4
            };
            uint ebo = Gl.GenBuffer();

            Gl.BindBuffer(BufferTarget.ElementArrayBuffer, ebo);
            Gl.BufferData(BufferTarget.ElementArrayBuffer, (uint)elements.Length * 4, elements, BufferUsage.StaticDraw);

            uint texture = Gl.CreateTexture(TextureTarget.Texture2d);
            {
                Gl.BindTexture(TextureTarget.Texture2d, texture);
                using (var image = new System.Drawing.Bitmap(".\\resources\\textures\\dirt.png")) {
                    System.Drawing.Imaging.BitmapData data = image.LockBits(new System.Drawing.Rectangle(0, 0, image.Width, image.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
                    Gl.TexImage2D(TextureTarget.Texture2d, 0, InternalFormat.Rgba, data.Width, data.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0);
                    image.UnlockBits(data);
                }
                Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureMinFilter, Gl.NEAREST);
                Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureMagFilter, Gl.NEAREST);
            }

            ShaderProgram program;

            using (var vertexShader = Shader.FromFile(".\\src\\shaders\\SimpleVertexShader.vert", ShaderType.VertexShader))
                using (var fragmentShader = Shader.FromFile(".\\src\\shaders\\SimpleFragmentShader.frag", ShaderType.FragmentShader)) {
                    program = new ShaderProgram(
                        new[] { vertexShader, fragmentShader },
                        new string[] { "model", "projection" },
                        new[] { "position", "color", "texcoord" }
                        );
                }
            program.Use();

            var posAttrib = (uint)program.Attributes["position"];

            Gl.EnableVertexAttribArray(posAttrib);
            Gl.VertexAttribPointer(posAttrib, 3, VertexAttribType.Float, false, 8 * 4, IntPtr.Zero);
            var colAttrib = (uint)program.Attributes["color"];

            Gl.EnableVertexAttribArray(colAttrib);
            Gl.VertexAttribPointer(colAttrib, 3, VertexAttribType.Float, false, 8 * 4, new IntPtr(3 * 4));
            var texAttrib = (uint)program.Attributes["texcoord"];

            Gl.EnableVertexAttribArray(texAttrib);
            Gl.VertexAttribPointer(texAttrib, 2, VertexAttribType.Float, false, 8 * 4, new IntPtr(6 * 4));

            // var modelView = Matrix4x4f.Translated(0f, 0f, 0f);
            // var projection = Matrix4x4d.Ortho2D(-1f, 1f, -1f, 1f);
            // var modelAttrib = Gl.GetAttribLocation(programId, "model");

            //var viewAttrib = Gl.GetAttribLocation(programId, "view");
            //Gl.UniformMatrix4f(viewAttrib, 1, false, ref view);

            // var projectionAttrib = Gl.GetAttribLocation(programId, "projection");
            // Gl.UniformMatrix4f(projectionAttrib, 1, false, projection);

            while (!Glfw.WindowShouldClose(window))
            {
                Gl.ClearColor(1.0f, 0.0f, 1.0f, 1.0f);
                Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                // Gl.UniformMatrix4f(modelAttrib, 1, false, projection * modelView);
                Gl.DrawElements(PrimitiveType.Triangles, elements.Length, DrawElementsType.UnsignedInt, IntPtr.Zero);

                Glfw.SwapBuffers(window);

                Glfw.PollEvents();
                if (Glfw.GetKey(window, (int)Glfw.KeyCode.Escape))
                {
                    Glfw.SetWindowShouldClose(window, true);
                }
            }

            Console.WriteLine("Exiting...");

            Gl.DeleteBuffers(buffer);
            Gl.DeleteVertexArrays(vao);

            Glfw.Terminate();
        }
Beispiel #19
0
        private static void OnRenderFrame()
        {
            watch.Stop();
            float deltaTime = (float)watch.ElapsedTicks / System.Diagnostics.Stopwatch.Frequency;

            watch.Restart();

            // perform rotation of the scene depending on keyboard input
            if (right)
            {
                phi += deltaTime;
            }
            if (left)
            {
                phi -= deltaTime;
            }
            if (up)
            {
                theta += deltaTime;
            }
            if (down)
            {
                theta -= deltaTime;
            }
            if (theta < 0)
            {
                theta += (float)Math.PI * 2;
            }

            // set up the viewport and clear the previous depth and color buffers
            Gl.Viewport(0, 0, width, height);
            Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            // make sure the shader program and texture are being used
            Gl.UseProgram(program.ProgramID);
            Gl.BindTexture(starTexture);

            // calculate the camera position using some fancy polar co-ordinates
            Vector3 position = 20 * new Vector3((float)(Math.Cos(phi) * Math.Sin(theta)), (float)Math.Cos(theta), (float)(Math.Sin(phi) * Math.Sin(theta)));
            Vector3 upVector = ((theta % (Math.PI * 2)) > Math.PI) ? new Vector3(0, 1, 0) : new Vector3(0, -1, 0);

            program["view_matrix"].SetValue(Matrix4.LookAt(position, Vector3.Zero, upVector));

            // loop through the stars, drawing each one
            for (int i = 0; i < stars.Count; i++)
            {
                // set the position and color of this star
                program["model_matrix"].SetValue(Matrix4.CreateTranslation(new Vector3(stars[i].dist, 0, 0)) * Matrix4.CreateRotationZ(stars[i].angle));
                program["color"].SetValue(stars[i].color);

                Gl.BindBufferToShaderAttribute(star, program, "vertexPosition");
                Gl.BindBufferToShaderAttribute(starUV, program, "vertexUV");
                Gl.BindBuffer(starQuads);

                Gl.DrawElements(BeginMode.Triangles, starQuads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);

                // update the position of the star
                stars[i].angle += (float)i / stars.Count * deltaTime * 2;
                stars[i].dist  -= 0.2f * deltaTime;

                // if we've reached the center then move this star outwards and give it a new color
                if (stars[i].dist < 0f)
                {
                    stars[i].dist += 5f;
                    stars[i].color = new Vector3((float)generator.NextDouble(), (float)generator.NextDouble(), (float)generator.NextDouble());
                }
            }

            // bind the font program as well as the font texture
            Gl.UseProgram(fontProgram.ProgramID);
            Gl.BindTexture(font.FontTexture);

            // build this string every frame, since theta and phi can change
            FontVAO vao = font.CreateString(fontProgram, string.Format("Theta: {0:0.000}, Phi: {1:0.000}", theta, phi), BMFont.Justification.Right);

            vao.Position = new Vector2(width / 2 - 10, height / 2 - font.Height - 10);
            vao.Draw();
            vao.Dispose();

            // draw the tutorial information, which is static
            information.Draw();

            Glut.glutSwapBuffers();
        }
Beispiel #20
0
        private static void OnRenderFrame()
        {
            watch.Stop();
            float deltaTime = (float)watch.ElapsedTicks / System.Diagnostics.Stopwatch.Frequency;

            watch.Restart();

            // set up the viewport and clear the previous depth and color buffers
            Gl.Viewport(0, 0, width, height);
            Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            // perform rotation of the cube depending on the keyboard state
            if (autoRotate)
            {
                xangle += deltaTime / 2;
                yangle += deltaTime;
            }
            if (right)
            {
                yangle += deltaTime;
            }
            if (left)
            {
                yangle -= deltaTime;
            }
            if (up)
            {
                xangle -= deltaTime;
            }
            if (down)
            {
                xangle += deltaTime;
            }

            // make sure the shader program and texture are being used
            Gl.UseProgram(program);
            Gl.ActiveTexture(TextureUnit.Texture1);
            Gl.BindTexture(brickNormals);
            Gl.ActiveTexture(TextureUnit.Texture0);
            Gl.BindTexture(brickDiffuse);

            // set up the model matrix and draw the cube
            program["model_matrix"].SetValue(Matrix4.CreateRotationY(yangle) * Matrix4.CreateRotationX(xangle));
            program["enable_lighting"].SetValue(lighting);
            program["enable_mapping"].SetValue(normalMapping);

            Gl.BindBufferToShaderAttribute(cube, program, "vertexPosition");
            Gl.BindBufferToShaderAttribute(cubeNormals, program, "vertexNormal");
            Gl.BindBufferToShaderAttribute(cubeTangents, program, "vertexTangent");
            Gl.BindBufferToShaderAttribute(cubeUV, program, "vertexUV");
            Gl.BindBuffer(cubeTriangles);

            Gl.DrawElements(BeginMode.Triangles, cubeTriangles.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);

            // bind the font program as well as the font texture
            Gl.UseProgram(fontProgram.ProgramID);
            Gl.BindTexture(font.FontTexture);

            // draw the tutorial information, which is static
            information.Draw();

            Glut.glutSwapBuffers();
        }
 public TextureBinding(T texture)
 {
     Gl.BindTexture(TextureTarget, texture.Handle);
 }
Beispiel #22
0
 public void Bind()
 {
     Gl.BindTexture(TextureTarget.Texture2d, _id);
 }
 public void Dispose() => Gl.BindTexture(TextureTarget, 0u);
Beispiel #24
0
        private static void OnRenderFrame()
        {
            //Clock tick used for animation
            watch.Stop();
            float deltaTime = (float)watch.ElapsedTicks / System.Diagnostics.Stopwatch.Frequency;

            watch.Restart();
            angle += deltaTime;

            Gl.Viewport(0, 0, width, height);
            Gl.Clear(ClearBufferMask.ColorBufferBit
                     | ClearBufferMask.DepthBufferBit);

            //Use shader
            Gl.UseProgram(program);

            //Drawing cube
            #region
            Gl.BindTexture(crateTexture);
            program["model_matrix"].SetValue(
                Matrix4.CreateRotationZ(angle * speedS) *
                Matrix4.CreateRotationY(angle * speedS) *
                Matrix4.CreateRotationX(angle * speedS) *
                Matrix4.CreateScaling(new Vector3(0.5f, 0.5f, 0.5f)) *
                Matrix4.CreateTranslation(new Vector3(0, 0, -5)));
            Gl.BindBufferToShaderAttribute(cube, program, "vertexPosition");
            Gl.BindBufferToShaderAttribute(cubeUV, program, "vertexUV");
            Gl.BindBuffer(cubeElements);
            //Dibuja el cube
            Gl.DrawElements(BeginMode.Quads, cubeElements.Count,
                            DrawElementsType.UnsignedInt, IntPtr.Zero);
            #endregion

            //Drawing rect
            #region
            Gl.BindTexture(rectTexture);
            program["model_matrix"].SetValue(
                Matrix4.CreateRotationY(angle * speedS) *
                Matrix4.CreateScaling(new Vector3(0.5f, 0.5f, 0.5f)) *
                Matrix4.CreateTranslation(new Vector3(-3, 0, -2)));
            Gl.BindBufferToShaderAttribute(rect, program, "vertexPosition");
            Gl.BindBufferToShaderAttribute(rectUV, program, "vertexUV");
            Gl.BindBuffer(rectElements);
            Gl.DrawElements(BeginMode.Quads, rectElements.Count,
                            DrawElementsType.UnsignedInt, IntPtr.Zero);
            #endregion

            //Drawing pyramid
            #region
            Gl.BindTexture(pyTexture);
            program["model_matrix"].SetValue(
                Matrix4.CreateRotationZ(angle * speedS) *
                Matrix4.CreateTranslation(new Vector3(2, 0, -2)));
            Gl.BindBufferToShaderAttribute(pyramid, program, "vertexPosition");
            Gl.BindBufferToShaderAttribute(pyUV, program, "vertexUV");
            Gl.BindBuffer(pyramidElements);
            Gl.DrawElements(BeginMode.Triangles, pyramidElements.Count,
                            DrawElementsType.UnsignedInt, IntPtr.Zero);
            #endregion

            Glut.glutSwapBuffers();
        }
Beispiel #25
0
        private static uint CreatePrefilteredEnvironmentMap(Shader shader, Action preRender)
        {
            var cube       = new Cube();
            var captureFbo = Gl.GenFramebuffer();

            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, captureFbo);

            var captureRbo = Gl.GenRenderbuffer();

            Gl.BindRenderbuffer(RenderbufferTarget.Renderbuffer, captureRbo);
            Gl.RenderbufferStorage(RenderbufferTarget.Renderbuffer, InternalFormat.DepthComponent24, 128, 128);
            Gl.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, captureRbo);

            var prefilterMap = Gl.GenTexture();

            Gl.BindTexture(TextureTarget.TextureCubeMap, prefilterMap);
            for (int i = 0; i < 6; ++i)
            {
                Gl.TexImage2D(TextureTarget.TextureCubeMapPositiveX + i, 0, InternalFormat.Rgb16f, 128, 128, 0, PixelFormat.Rgb, PixelType.Float, IntPtr.Zero);
            }

            Gl.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.LinearMipmapLinear);
            Gl.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            Gl.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
            Gl.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
            Gl.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapR, (int)TextureWrapMode.ClampToEdge);

            Gl.GenerateMipmap(TextureTarget.TextureCubeMap);

            var projMatrix   = Matrix4x4f.Perspective(90f, 1.0f, 0.1f, 10.0f);
            var viewMatrices = new Matrix4x4f[]
            {
                Matrix4x4f.LookAt(new Vertex3f(0f, 0f, 0f), new Vertex3f(1f, 0f, 0f), new Vertex3f(0f, -1f, 0f)),
                Matrix4x4f.LookAt(new Vertex3f(0f, 0f, 0f), new Vertex3f(-1f, 0f, 0f), new Vertex3f(0f, -1f, 0f)),
                Matrix4x4f.LookAt(new Vertex3f(0f, 0f, 0f), new Vertex3f(0f, 1f, 0f), new Vertex3f(0f, 0f, 1f)),
                Matrix4x4f.LookAt(new Vertex3f(0f, 0f, 0f), new Vertex3f(0f, -1f, 0f), new Vertex3f(0f, 0f, -1f)),
                Matrix4x4f.LookAt(new Vertex3f(0f, 0f, 0f), new Vertex3f(0f, 0f, 1f), new Vertex3f(0f, -1f, 0f)),
                Matrix4x4f.LookAt(new Vertex3f(0f, 0f, 0f), new Vertex3f(0f, 0f, -1f), new Vertex3f(0f, -1f, 0f)),
            };

            shader.Use();
            preRender.Invoke();
            shader.SetMatrix("projectionMatrix", projMatrix);

            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, captureFbo);
            int mipLevels = 5;

            for (int mips = 0; mips < mipLevels; ++mips)
            {
                int mipSize = (int)(128 * (float)Math.Pow(0.5, mips));
                Gl.BindRenderbuffer(RenderbufferTarget.Renderbuffer, captureRbo);
                Gl.RenderbufferStorage(RenderbufferTarget.Renderbuffer, InternalFormat.DepthComponent24, mipSize, mipSize);
                Gl.Viewport(0, 0, mipSize, mipSize);
                float roughness = mips / (float)(mipLevels - 1);
                shader.SetFloat("roughness", roughness);
                for (int i = 0; i < 6; ++i)
                {
                    shader.SetMatrix("viewMatrix", viewMatrices[i]);
                    Gl.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.TextureCubeMapPositiveX + i, prefilterMap, mips);
                    Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                    cube.Draw();
                }
            }
            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            return(prefilterMap);
        }
Beispiel #26
0
        private static void OnRenderFrame()
        {
            watch.Stop();
            float deltaTime = (float)watch.ElapsedTicks / System.Diagnostics.Stopwatch.Frequency;

            watch.Restart();

            // perform rotation of the cube depending on the keyboard state
            if (autoRotate)
            {
                xangle += deltaTime / 2;
                yangle += deltaTime;
            }
            if (right)
            {
                yangle += deltaTime;
            }
            if (left)
            {
                yangle -= deltaTime;
            }
            if (up)
            {
                xangle -= deltaTime;
            }
            if (down)
            {
                xangle += deltaTime;
            }

            // set up the viewport and clear the previous depth and color buffers
            Gl.Viewport(0, 0, width, height);
            Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            Gl.ClearColor(30f / 255, 144f / 255, 255f / 255, 255f / 255);
            // make sure the shader program and texture are being used
            Gl.UseProgram(program);
            Gl.BindTexture(CarModel.bodyTexture);

            // set up the model matrix and draw the cube
            program["model_matrix"].SetValue(Matrix4.CreateRotationY(yangle) * Matrix4.CreateRotationX(xangle));
            program["enable_lighting"].SetValue(lighting);

            Gl.BindBufferToShaderAttribute(CarModel.cube, program, "vertexPosition");
            Gl.BindBufferToShaderAttribute(CarModel.cubeNormals, program, "vertexNormal");
            Gl.BindBufferToShaderAttribute(CarModel.cubeUV, program, "vertexUV");
            Gl.BindBuffer(CarModel.cubeQuads);

            Gl.DrawElements(BeginMode.Quads, CarModel.cubeQuads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);


            //Draw window
            Gl.BindTexture(CarModel.kacaTexture);

            program["max_diffuse"].SetValue(maxdiffuse);
            program["ambient"].SetValue(ambient);

            Gl.BindBufferToShaderAttribute(CarModel.window, program, "vertexPosition");
            Gl.BindBufferToShaderAttribute(CarModel.windowNormals, program, "vertexNormal");
            Gl.BindBufferToShaderAttribute(CarModel.windowUV, program, "vertexUV");
            Gl.BindBuffer(CarModel.windowQuads);

            Gl.DrawElements(BeginMode.Quads, CarModel.windowQuads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);

            //drawwheel
            Gl.BindTexture(CarModel.rodaTexture);

            Gl.BindBufferToShaderAttribute(CarModel.wheel1, program, "vertexPosition");
            Gl.BindBufferToShaderAttribute(CarModel.wheel1Normals, program, "vertexNormal");
            Gl.BindBufferToShaderAttribute(CarModel.wheel1UV, program, "vertexUV");
            Gl.BindBuffer(CarModel.wheel1Quads);
            Gl.DrawElements(BeginMode.TriangleStrip, CarModel.wheel1Quads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
            Gl.BindBufferToShaderAttribute(CarModel.wheel2, program, "vertexPosition");
            Gl.DrawElements(BeginMode.TriangleStrip, CarModel.wheel1Quads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
            Gl.BindBufferToShaderAttribute(CarModel.wheel2Normals, program, "vertexNormal");

            Gl.BindBufferToShaderAttribute(CarModel.wheel3, program, "vertexPosition");
            Gl.DrawElements(BeginMode.TriangleStrip, CarModel.wheel1Quads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
            Gl.BindBufferToShaderAttribute(CarModel.wheel4, program, "vertexPosition");
            Gl.DrawElements(BeginMode.TriangleStrip, CarModel.wheel1Quads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);

            //Gl.BindTexture(velgTexture);
            Gl.BindBufferToShaderAttribute(CarModel.wheel1Normals, program, "vertexNormal");

            Gl.BindBufferToShaderAttribute(CarModel.velg1, program, "vertexPosition");
            Gl.DrawElements(BeginMode.TriangleFan, CarModel.wheel1Quads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
            Gl.BindBufferToShaderAttribute(CarModel.velg2, program, "vertexPosition");

            Gl.DrawElements(BeginMode.TriangleFan, CarModel.wheel1Quads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
            Gl.BindBufferToShaderAttribute(CarModel.wheel2Normals, program, "vertexNormal");


            Gl.BindBufferToShaderAttribute(CarModel.velg3, program, "vertexPosition");
            Gl.DrawElements(BeginMode.TriangleFan, CarModel.wheel1Quads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
            Gl.BindBufferToShaderAttribute(CarModel.velg4, program, "vertexPosition");
            Gl.DrawElements(BeginMode.TriangleFan, CarModel.wheel1Quads.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
            Glut.glutSwapBuffers();
        }
Beispiel #27
0
        private void GenerateArrayTexture()
        {
            //Generate an array texture
//            Gl.GenTextures(1, gArrayTexture);
            textureId = Gl.GenTexture();

            Gl.ActiveTexture(TextureUnit.Texture0);
            Gl.BindTexture(TextureTarget.Texture2DArray, textureId);

            //Generate an array texture
//  glGenTextures( 1, &gArrayTexture );
//  glActiveTexture(GL_TEXTURE0);
//  glBindTexture(GL_TEXTURE_2D_ARRAY, gArrayTexture);

            Size textureSize = new Size(256, 256);

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

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

            // replace glTexStorage3D with following code
            // https://www.opengl.org/sdk/docs/man/html/glTexStorage3D.xhtml
            //http://stackoverflow.com/questions/17760193/correct-storage-allocation-for-textures-in-gl-texture-2d-array
            //No mipmaps as textures are 1x1
            int levels = 1; // Specify the number of texture levels

            int width  = textureSize.Width;
            int height = textureSize.Height;

            for (int i = 0; i < levels; i++)
            {
                Gl.TexImage3D(TextureTarget.Texture2DArray, i, PixelInternalFormat.Rgb8, width, height, numberOfTextures,
                              0, PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero);
                width  = Math.Max(1, (width / 2));
                height = Math.Max(1, (height / 2));
            }
            //Create storage for the texture. (100 layers of 1x1 texels)
//  glTexStorage3D( GL_TEXTURE_2D_ARRAY,
//                  1,                    //No mipmaps as textures are 1x1
//                  GL_RGB8,              //Internal format
//                  1, 1,                 //width,height
//                  100                   //Number of layers
//                );

            for (int i = 0; i != numberOfTextures; ++i)
            {
                //Choose a random color for the i-essim image
//     Gl.
//    GLubyte color[3] = {rand()%255,rand()%255,rand()%255};

                //Specify i-essim image

                Bitmap tempBmp = tempTiletypeList.Values.ToArray()[i];


                Size tempSize = tempBmp.Size;

                // must be Format32bppArgb file format, so convert it if it isn't in that format
                BitmapData bitmapData = tempBmp.LockBits(new Rectangle(0, 0, tempSize.Width, tempSize.Height),
                                                         ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);


                Gl.TexSubImage3D(TextureTarget.Texture2DArray,
                                 0,                                        //Mipmap number
                                 0, 0, i,                                  //xoffset, yoffset, zoffset
                                 textureSize.Width, textureSize.Height, 1, //width, height, depth
                                 PixelFormat.Bgra,                         //format
                                 PixelType.UnsignedByte,                   //type
                                 bitmapData.Scan0                          //pointer to data
                                 );


                tempBmp.UnlockBits(bitmapData);

//    glTexSubImage3D( GL_TEXTURE_2D_ARRAY,
//                     0,                     //Mipmap number
//                     0,0,i,                 //xoffset, yoffset, zoffset
//                     1,1,1,                 //width, height, depth
//                     GL_RGB,                //format
//                     GL_UNSIGNED_BYTE,      //type
//                     color);                //pointer to data
            }

            Gl.TexParameteri(TextureTarget.Texture2DArray, TextureParameterName.TextureMinFilter,
                             TextureParameter.Linear);
            Gl.TexParameteri(TextureTarget.Texture2DArray, TextureParameterName.TextureMagFilter,
                             TextureParameter.Linear);
            Gl.TexParameteri(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapS,
                             TextureParameter.ClampToEdge);
            Gl.TexParameteri(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapT,
                             TextureParameter.ClampToEdge);

//           glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
//  glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
//  glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
//  glTexParameteri(GL_TEXTURE_2D_ARRAY,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
        }
Beispiel #28
0
 internal Texture(uint handle, Vector2I size) : base(handle, tex => Gl.BindTexture(TextureTarget.Texture2d, tex), Gl.DeleteTextures)
 {
     Size = size;
     ContextManager.ContextChange += OnContextChange;
 }
Beispiel #29
0
 public void Use()
 {
     Gl.ActiveTexture(TextureUnit.Texture0);
     Gl.BindTexture(TextureTarget.Texture2DArray, TextureID);
 }
Beispiel #30
0
        public void DrawImage(GlImage image, float x, float y, float w, float h, float ix, float iy, float iw, float ih)
        {
            if (ix < 0)
            {
                x += -w * ix / iw;
                ix = 0;
            }

            if (ix + iw > image.Width)
            {
                w -= (ix + iw - image.Width) * w / iw;
                iw = image.Width - ix;
            }

            if (iy < 0)
            {
                y += -h * iy / ih;
                iy = 0;
            }

            if (iy + ih > image.Height)
            {
                h -= (iy + ih - image.Height) * h / ih;
                ih = image.Height - iy;
            }

            Gl.UseProgram(image.ShaderProgram);

            Gl.ActiveTexture(Gl.GL_TEXTURE0);
            Gl.BindTexture(Gl.GL_TEXTURE_2D, image.Id);
            Gl.Uniform1(Gl.GetUniformLocation(image.ShaderProgram, "tex"), 0);

            var vert  = (uint)Gl.GetAttribLocation(image.ShaderProgram, "vert");
            var tvert = (uint)Gl.GetAttribLocation(image.ShaderProgram, "vertTexCoord");

            var projectionMatrix = new float[16];

            Gl.GetFloat(Gl.GL_PROJECTION_MATRIX, projectionMatrix);
            Gl.UniformMatrix4(Gl.GetUniformLocation(image.ShaderProgram, "projectionMatrix"), 1, false,
                              projectionMatrix);

            Gl.PolygonMode(Gl.GL_FRONT, Gl.GL_FILL);
            Gl.Color(Color.Transparent);
            Gl.Enable(Gl.GL_TEXTURE_2D);

            Gl.Begin(Gl.GL_QUADS);

            Gl.VertexAttrib2(vert, x, y);
            Gl.VertexAttrib2(tvert, ix / image.Width, iy / image.Height);

            Gl.VertexAttrib2(vert, x, y + h);
            Gl.VertexAttrib2(tvert, ix / image.Width, (iy + ih) / image.Height);

            Gl.VertexAttrib2(vert, x + w, y + h);
            Gl.VertexAttrib2(tvert, (ix + iw) / image.Width, (iy + ih) / image.Height);

            Gl.VertexAttrib2(vert, x + w, y);
            Gl.VertexAttrib2(tvert, (ix + iw) / image.Width, iy / image.Height);

            Gl.End();

            Gl.Disable(Gl.GL_TEXTURE_2D);
            Gl.BindTexture(Gl.GL_TEXTURE_2D, 0);

            Gl.UseProgram(0);

            Gl.Flush();
        }