Example #1
0
        /// <summary>
        /// This method is called by <code>Render</code>, directly before
        /// <code>GL.drawElements</code>. It activates the program and sets up
        /// the GL context with the following constants and attributes:
        ///
        /// <code>uMvpMatrix</code> — MVP matrix
        /// <code>aPosition</code> — vertex position (xy)
        /// </summary>
        protected virtual void BeforeDraw()
        {
            Program.Activate(); // create, upload, use program

            //is this the best place for this?
            Gl.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferName);
            Gl.BindBuffer(BufferTarget.ElementArrayBuffer, IndexBufferName);

            uint attribPosition = (uint)Program.Attributes["aPosition"];

            Gl.EnableVertexAttribArray(attribPosition);
            Gl.VertexAttribPointer(attribPosition, 2, VertexAttribType.Float, false, Vertex.Size, (IntPtr)Vertex.PositionOffset);

            int uMvpMatrix = Program.Uniforms["uMvpMatrix"];

            Gl.UniformMatrix4(uMvpMatrix, false, MvpMatrix3D.RawData);

            if (Texture != null)
            {
                uint aTexCoords = (uint)Program.Attributes["aTexCoords"];
                Gl.EnableVertexAttribArray(aTexCoords);
                Gl.VertexAttribPointer(aTexCoords, 2, VertexAttribType.Float, false, Vertex.Size, (IntPtr)Vertex.TextureOffset);
                Gl.ActiveTexture(TextureUnit.Texture0);

                RenderUtil.SetSamplerStateAt(Texture.Base, Texture.NumMipMaps > 0, TextureSmoothing, TextureRepeat);
            }
            // color & alpha are set in subclasses
        }
Example #2
0
        public uint LoadCubeMap(string[] textureFiles)
        {
            uint textureID = Gl.GenTexture();

            Gl.ActiveTexture(TextureUnit.Texture0);
            Gl.BindTexture(TextureTarget.TextureCubeMap, textureID);

            for (int i = 0; i < textureFiles.Length; i++)
            {
                Bitmap bitmap = LoadBitmap(textureFiles[i]);
                Gl.TexImage2D(TextureTarget.TextureCubeMapPositiveX + i
                              , 0
                              , InternalFormat.Rgba
                              , bitmap.Width
                              , bitmap.Height
                              , 0
                              , PixelFormat.Bgra
                              , PixelType.UnsignedByte
                              , BitmapToIntPtr(bitmap));
            }
            Gl.TexParameteri(TextureTarget.TextureCubeMap, TextureParameterName.TextureMinFilter, Gl.LINEAR);
            Gl.TexParameteri(TextureTarget.TextureCubeMap, TextureParameterName.TextureMagFilter, Gl.LINEAR);
            Gl.TexParameteri(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapS, Gl.CLAMP_TO_EDGE);
            Gl.TexParameteri(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapT, Gl.CLAMP_TO_EDGE);
            loadedTextureList.Add(textureID);
            return(textureID);
        }
Example #3
0
 public void Render(List <GUITexture> guiList)
 {
     if (guiList == null || guiList.Count > 0 == false)
     {
         return;
     }
     this.shader.Start();
     Gl.BindVertexArray(this.quadModel.VaoID);
     Gl.EnableVertexAttribArray(0);// Position
     Gl.Enable(EnableCap.Blend);
     Gl.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
     Gl.Disable(EnableCap.DepthTest);
     for (int i = 0; i < guiList.Count; i++)
     {
         Gl.ActiveTexture(TextureUnit.Texture0);
         Gl.BindTexture(TextureTarget.Texture2d, guiList[i].TextureID);
         Matrix4x4f transformationMatrix = Maths.CreateTransformationMatrix(guiList[i].Position, guiList[i].Scale);
         this.shader.LoadTransformationMatrix(transformationMatrix);
         Gl.DrawArrays(PrimitiveType.TriangleStrip, 0, this.quadModel.VertexCount);
     }
     Gl.Enable(EnableCap.DepthTest);
     Gl.Disable(EnableCap.Blend);
     Gl.DisableVertexAttribArray(0);
     Gl.BindVertexArray(0);
     this.shader.Stop();
 }
Example #4
0
        public static (Cubemap, Cubemap, Cubemap) Convert(string hdriPath)
        {
            Gl.Disable(EnableCap.CullFace);
            var equirectangularToCubemapShader = new Shader("Content/Shaders/EquirectangularToCubemap/EquirectangularToCubemap.frag", "Content/Shaders/EquirectangularToCubemap/EquirectangularToCubemap.vert");
            var convolutionShader = new Shader("Content/Shaders/Convolution/convolution.frag", "Content/Shaders/Convolution/convolution.vert");
            var prefilterShader   = new Shader("Content/Shaders/Prefilter/prefilter.frag", "Content/Shaders/Prefilter/prefilter.vert");
            var skyHdri           = HdriTexture.LoadFromFile(hdriPath);

            var envMap = new Cubemap(RenderToCubemap(equirectangularToCubemapShader, 512, () =>
            {
                equirectangularToCubemapShader.SetInt("equirectangularMap", 0);
                Gl.ActiveTexture(TextureUnit.Texture0);
                Gl.BindTexture(TextureTarget.Texture2d, skyHdri.Id);
            }));
            var convMap = new Cubemap(RenderToCubemap(convolutionShader, 64, () =>
            {
                convolutionShader.SetInt("environmentMap", 0);
                Gl.ActiveTexture(TextureUnit.Texture0);
                Gl.BindTexture(TextureTarget.TextureCubeMap, envMap.Id);
            }));
            var prefMap = new Cubemap(CreatePrefilteredEnvironmentMap(prefilterShader, () =>
            {
                prefilterShader.SetInt("environmentMap", 0);
                Gl.ActiveTexture(TextureUnit.Texture0);
                Gl.BindTexture(TextureTarget.TextureCubeMap, envMap.Id);
            }));

            Gl.Enable(EnableCap.CullFace);

            return(
                envMap,
                convMap,
                prefMap
                );
        }
Example #5
0
        public override void Draw()
        {
            base.Draw();
            Gl.ActiveTexture(TextureUnit.Texture0);
            Gl.BindTexture(this.bitmapFont.FontTexture);
            int num = 0;

            if (this.Size.Y > this.TextSize.Y)
            {
                num = (this.Size.Y - this.TextSize.Y) / 2;
            }
            Gl.Enable(EnableCap.Blend);
            this.Program.Use();
            if (this.Justification == BMFont.Justification.Center)
            {
                this.Program["position"].SetValue(new Vector2((float)(this.CorrectedPosition.X + this.Padding.X + this.Size.X / 2), (float)(this.CorrectedPosition.Y + this.Padding.Y + num)));
            }
            else
            {
                this.Program["position"].SetValue(new Vector2((float)(this.CorrectedPosition.X + this.Padding.X), (float)(this.CorrectedPosition.Y + this.Padding.Y + num)));
            }
            this.Program["color"].SetValue(this.color);
            this.VAO.Draw();
            Gl.Disable(EnableCap.Blend);
        }
Example #6
0
        public override void Draw()
        {
            base.Draw();

            Gl.ActiveTexture(TextureUnit.Texture0);
            Gl.BindTexture(bitmapFont.FontTexture);

            int yoffset = 0;

            if (this.Size.Y > TextSize.Y)
            {
                yoffset = (Size.Y - TextSize.Y) / 2;
            }

            Gl.Enable(EnableCap.Blend);
            Program.Use();
            if (this.Justification == BMFont.Justification.Center)
            {
                Program["position"].SetValue(new Vector2(CorrectedPosition.X + Padding.X + Size.X / 2, CorrectedPosition.Y + Padding.Y + yoffset));
            }
            else
            {
                Program["position"].SetValue(new Vector2(CorrectedPosition.X + Padding.X, CorrectedPosition.Y + Padding.Y + yoffset));
            }
            Program["color"].SetValue(color);
            VAO.Draw();
            Gl.Disable(EnableCap.Blend);
        }
Example #7
0
        private void PrepareRender(List <Light> lightList, Camera camera, float frameTimeSec)
        {
            this.shader.Start();
            this.shader.LoadViewMatrix(camera);
            this.shader.LoadShineVariables(this.shineDamper, this.reflectivity);
            this.shader.LoadLights(lightList);

            this.moveFactor += WAVE_SPEED * frameTimeSec;
            this.moveFactor %= 1;
            this.shader.LoadMoveFactor(this.moveFactor);

            Gl.BindVertexArray(quad.VaoID);
            Gl.EnableVertexAttribArray(0);

            Gl.ActiveTexture(TextureUnit.Texture0);
            Gl.BindTexture(TextureTarget.Texture2d, this.fbos.ReflectionTexture);

            Gl.ActiveTexture(TextureUnit.Texture1);
            Gl.BindTexture(TextureTarget.Texture2d, this.fbos.RefractionTexture);

            Gl.ActiveTexture(TextureUnit.Texture2);
            Gl.BindTexture(TextureTarget.Texture2d, this.dudvTexture);

            Gl.ActiveTexture(TextureUnit.Texture3);
            Gl.BindTexture(TextureTarget.Texture2d, this.normalMap);

            Gl.ActiveTexture(TextureUnit.Texture4);
            Gl.BindTexture(TextureTarget.Texture2d, this.fbos.RefractionDepthTexture);

            Gl.Enable(EnableCap.Blend);
            Gl.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
        }
Example #8
0
        /// <summary>
        /// 保存した状態を復帰する。
        /// </summary>
        public void RestoreState()
        {
            Gl.UseProgram((uint)LastProgram);

            SetEnabledVertexAttribArray(0, LastVertexAttribArrayEnabled[0] != 0);
            SetEnabledVertexAttribArray(1, LastVertexAttribArrayEnabled[1] != 0);
            SetEnabledVertexAttribArray(2, LastVertexAttribArrayEnabled[2] != 0);
            SetEnabledVertexAttribArray(3, LastVertexAttribArrayEnabled[3] != 0);

            SetEnabled(EnableCap.ScissorTest, LastScissorTest);
            SetEnabled(EnableCap.StencilTest, LastStencilTest);
            SetEnabled(EnableCap.DepthTest, LastDepthTest);
            SetEnabled(EnableCap.CullFace, LastCullFace);
            SetEnabled(EnableCap.Blend, LastBlend);

            Gl.FrontFace((FrontFaceDirection)LastFrontFace);

            Gl.ColorMask(LastColorMask[0] != 0, LastColorMask[1] != 0, LastColorMask[2] != 0, LastColorMask[3] != 0);

            Gl.BindBuffer(BufferTarget.ArrayBuffer, (uint)LastArrayBufferBinding);
            Gl.BindBuffer(BufferTarget.ElementArrayBuffer, (uint)LastElementArrayBufferBinding);

            Gl.ActiveTexture(TextureUnit.Texture1);
            Gl.BindTexture(TextureTarget.Texture2d, (uint)LastTexture1Binding2D);

            Gl.ActiveTexture(TextureUnit.Texture0);
            Gl.BindTexture(TextureTarget.Texture2d, (uint)LastTexture0Binding2D);

            Gl.ActiveTexture((TextureUnit)LastActiveTexture);

            Gl.BlendFuncSeparate((BlendingFactor)LastBlending[0], (BlendingFactor)LastBlending[1], (BlendingFactor)LastBlending[2], (BlendingFactor)LastBlending[3]);

            RestoreViewport();
            RestoreFrameBuffer();
        }
Example #9
0
        public static Texture LoadCubeMap(string file)
        {
            file = file.Replace("n!", defaultNormal);
            file = file.Replace("s!", defaultSpecular);
            file = file.Replace("!", defaultDiffuse);


            string name = dictionaryFunction(file);

            if (!textureMap.ContainsKey(name))
            {
                Gl.ActiveTexture(0);
                Gl.Enable(EnableCap.TextureCubeMap);
                Texture t;
                textureMap.Add(name, t = new Texture());
                Gl.BindTexture(TextureTarget.TextureCubeMap, t.ID);
                for (int i = 0; i < 6; i++)
                {
                    Texture.TextureGl(TextureTarget.TextureCubeMapPositiveX + i, file + "/" + nameMap[i] + ".png", System.Drawing.RotateFlipType.RotateNoneFlipNone);
                }
                Gl.TexParameteri(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapS, TextureParameter.ClampToEdge);
                Gl.TexParameteri(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapT, TextureParameter.ClampToEdge);
                Gl.TexParameteri(TextureTarget.TextureCubeMap, TextureParameterName.TextureMagFilter, TextureParameter.Linear);
                Gl.TexParameteri(TextureTarget.TextureCubeMap, TextureParameterName.TextureMinFilter, TextureParameter.Linear);
                //Gl.TexParameteri(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapR, TextureParameter.ClampToEdge);
            }
            return(textureMap[name]);
        }
Example #10
0
        public void Activate()
        {
            Gl.ActiveTexture(ActiveTextureTarget.Texture0);
            Gl.CheckForError();

            Gl.BindTexture(TextureTarget.Texture2D, textureObject);
            Gl.CheckForError();
        }
Example #11
0
 public static void Bind(Texture t, int textureUnit = 0, TextureTarget tr = TextureTarget.Texture2D)
 {
     if (t == null)
     {
         return;
     }
     Gl.ActiveTexture(textureUnit);
     Gl.BindTexture(tr, t.ID);
 }
Example #12
0
        public GpuSpaceTextureContext(uint texture, uint buffer)
        {
            Texture = texture;
            _buffer = buffer;

            Gl.ActiveTexture(TextureUnit.Texture1);
            Gl.BindTexture((TextureTarget)Gl.TEXTURE_BUFFER, Texture);
            Gl.TexBuffer((TextureTarget)Gl.TEXTURE_BUFFER, InternalFormat.Rgba32f, _buffer);
        }
 /// <summary>
 /// Binds this texture as uniform into texture slot 0. Texture must be bound.
 /// </summary>
 /// <param name="pipeline">The shader used for drawing</param>
 public void Set(ShaderPipeline pipeline)
 {
     if (pipeline == null)
     {
         throw new ArgumentNullException(nameof(pipeline));
     }
     Gl.ActiveTexture(TextureUnit.Texture0);
     Bind();
     Gl.Uniform1(pipeline.GetUniformLocation(name), 0);
     Unbind();
 }
Example #14
0
 private bool BindTexture(int index, Texture texture)
 {
     if (texture == null)
     {
         Gl.BindTexture(TextureTarget.Texture2d, 0);
         return(false);
     }
     Gl.ActiveTexture((TextureUnit)((int)TextureUnit.Texture0 + index));
     Gl.BindTexture(TextureTarget.Texture2d, textureCache.TryRegister(texture));
     return(true);
 }
        public override void OnRenderFrame(float deltaTime)
        {
            // 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);
//            program["textureArray"].SetValue(textureId);


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

            int tempStride = sizeof(Single) * stride; // *****

            Gl.EnableVertexAttribArray(locationPosition);
            Gl.BindBuffer(gVertexBuffer);
            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)));


            Gl.BindBuffer(gIndirectBuffer);


            Gl.EnableVertexAttribArray(locationDrawid);
            Gl.BindBuffer(gDrawIdBuffer);
//            Gl.VertexAttribPointer(locationDrawid, 1, VertexAttribPointerType.Int, false, Marshal.SizeOf(typeof(int)),
//                IntPtr.Zero);
            Gl.VertexAttribPointer(locationDrawid, 1, VertexAttribPointerType.Float, false, sizeof(Single), IntPtr.Zero);
            Gl.VertexAttribDivisor(locationDrawid, 1);
            // This needs to be implemented.
            // https://www.opengl.org/wiki/GLAPI/glMultiDrawElementsIndirect
            //  glClear( GL_COLOR_BUFFER_BIT );


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

//            Gl.BindTexture(TextureTarget.Texture2DArray, 0);
//
//            Gl.BindBuffer(BufferTarget.ArrayBuffer, 0);
//            Gl.BindBuffer(BufferTarget.DrawIndirectBuffer, 0);
//            Gl.BindBuffer(BufferTarget.ElementArrayBuffer, 0);
//            Gl.UseProgram(0);
        }
        /// <inheritdoc />
        public override bool BindTexture(uint pointer, uint slot = 0)
        {
            GLThread.ExecuteGLThread(() =>
            {
                Gl.ActiveTexture(TextureUnit.Texture0 + (int)slot);
                Gl.BindTexture(TextureTarget.Texture2d, pointer);

                CheckError("after binding texture");
            });

            return(true);
        }
Example #17
0
        public void Draw(Shader shader, uint diffuseTexture)
        {
            shader.Use();

            Gl.ActiveTexture(TextureUnit.Texture0);
            Gl.BindTexture(TextureTarget.Texture2d, diffuseTexture);
            shader.SetInt("diffuseTexture", 0);

            Gl.ActiveTexture(TextureUnit.Texture0);

            DrawRaw();
        }
Example #18
0
        public void DrawWithCharacterCount(int count)
        {
            int count1 = Math.Min(count * 6, this.VAO.VertexCount);

            Gl.ActiveTexture(TextureUnit.Texture0);
            Gl.BindTexture(this.bitmapFont.FontTexture);
            Gl.Enable(EnableCap.Blend);
            this.Program.Use();
            this.Program["position"].SetValue(new Vector2((float)(this.CorrectedPosition.X + this.Padding.X), (float)(this.CorrectedPosition.Y + this.Padding.Y)));
            this.Program["color"].SetValue(this.color);
            this.VAO.BindAttributes(this.Program);
            Gl.DrawElements(BeginMode.Triangles, count1, DrawElementsType.UnsignedInt, IntPtr.Zero);
            Gl.Disable(EnableCap.Blend);
        }
Example #19
0
 public void PrepareDrawCall()
 {
     // Bind OpenGL objects.
     Gl.UseProgram(ShaderProgram);
     for (int i = 0; i < Renderer.TextureSlotIndex; i++)
     {
         Gl.ActiveTexture((TextureUnit)(TextureUnit.Texture0 + i));
         Gl.BindTexture(TextureTarget.Texture2d, (uint)Renderer.TextureSlots[i]);
     }
     Gl.BindVertexArray(VertexArray);
     Gl.BindBuffer(BufferTarget.ArrayBuffer, VertexBuffer);
     // Upload the new vertex buffer data to VRAM.
     Gl.BufferSubData(BufferTarget.ArrayBuffer, IntPtr.Zero, (uint)(Renderer.VertexIndex * Marshal.SizeOf <Vertex>()), Renderer.Vertices);
 }
Example #20
0
        // Draw the Image
        public void draw()
        {
            Gl.UseProgram(shaderImage.it.getProgramId());
            Gl.ActiveTexture(TextureUnit.Texture0);
            Gl.BindTexture(TextureTarget.Texture2d, imageTex);
            Gl.Uniform1(texID, 0);

            Gl.EnableVertexAttribArray(0);
            Gl.BindBuffer(BufferTarget.ArrayBuffer, quad_vb);
            Gl.VertexAttribPointer(0, 3, VertexAttribType.Float, false, 0, IntPtr.Zero);
            Gl.DrawArrays(PrimitiveType.Triangles, 0, 6);
            Gl.DisableVertexAttribArray(0);
            Gl.UseProgram(0);
        }
Example #21
0
        private void prepareTexturedModel(TexturedModel model)
        {
            RawModel rawModel = model.rawModel;

            Gl.BindVertexArray(rawModel.vaoID);
            Gl.EnableVertexAttribArray(0);
            Gl.EnableVertexAttribArray(1);
            Gl.EnableVertexAttribArray(2);
            ModelTexture texture = model.modelTexture;

            shader.loadVariables(texture.shineDamper, texture.reflectivity);
            Gl.ActiveTexture(TextureUnit.Texture0);                                // activate texture
            Gl.BindTexture(TextureTarget.Texture2d, model.modelTexture.textureId); // pass coords
        }
Example #22
0
        private void prepareTerrain(Terrain terrain)
        {
            RawModel rawModel = terrain.model;

            Gl.BindVertexArray(rawModel.vaoID);
            Gl.EnableVertexAttribArray(0);
            Gl.EnableVertexAttribArray(1);
            Gl.EnableVertexAttribArray(2);
            ModelTexture texture = terrain.texture;

            shader.loadVariables(texture.shineDamper, texture.reflectivity);
            Gl.ActiveTexture(TextureUnit.Texture0);
            Gl.BindTexture(TextureTarget.Texture2d, texture.textureId);
        }
Example #23
0
        public void render(TexturedModel texturedModel)
        {
            RawModel model = texturedModel.rawModel;

            Gl.BindVertexArray(model.vaoID);
            Gl.EnableVertexAttribArray(0);
            Gl.EnableVertexAttribArray(1);
            Gl.ActiveTexture(TextureUnit.Texture0);
            Gl.BindTexture(TextureTarget.Texture2d, texturedModel.modelTexture.textureId);
            Gl.DrawElements(PrimitiveType.Triangles, model.vertexCount, DrawElementsType.UnsignedInt, IntPtr.Zero);
            Gl.DisableVertexAttribArray(0);
            Gl.DisableVertexAttribArray(1);
            Gl.BindVertexArray(0);
        }
Example #24
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(pyramid, program, "vertexPosition");
            Gl.BindBuffer(pyramidTriangles);

            // draw the pyramid
            Gl.DrawElements(BeginMode.Triangles, pyramidTriangles.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);

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

            Glut.glutSwapBuffers();
        }
        private void bindTextures(Terrain terrain)
        {
            TerrainTexturePack texturePack = terrain.texturePack;

            Gl.ActiveTexture(TextureUnit.Texture0);
            Gl.BindTexture(TextureTarget.Texture2d, texturePack.backgroundTexture.textureID);
            Gl.ActiveTexture(TextureUnit.Texture1);
            Gl.BindTexture(TextureTarget.Texture2d, texturePack.rTexture.textureID);
            Gl.ActiveTexture(TextureUnit.Texture2);
            Gl.BindTexture(TextureTarget.Texture2d, texturePack.gTexture.textureID);
            Gl.ActiveTexture(TextureUnit.Texture3);
            Gl.BindTexture(TextureTarget.Texture2d, texturePack.bTexture.textureID);
            Gl.ActiveTexture(TextureUnit.Texture4);
            Gl.BindTexture(TextureTarget.Texture2d, terrain.blendMap.textureID);
        }
Example #26
0
 public override void Draw(int viewPortWidth, int viewPortHeight)
 {
     UserProgram(_program);
     Gl.ClearColor(0.0f, 0.5f, 1.0f, 1.0f);
     Gl.Clear(ClearBufferMask.ColorBufferBit);
     base.Draw(viewPortWidth, viewPortHeight);
     Gl.ActiveTexture(TextureUnit.Texture0);
     Gl.BindTexture(TextureTarget.Texture2d, _texture2);
     Gl.Uniform1(Gl.GetUniformLocation(_currentProgram, "texture2"), 0);
     Gl.ActiveTexture(TextureUnit.Texture1);
     Gl.BindTexture(TextureTarget.Texture2d, _texture1);
     Gl.Uniform1(Gl.GetUniformLocation(_currentProgram, "texture1"), 1);
     Gl.ActiveTexture(TextureUnit.Texture0);
     Gl.BindVertexArray(_vertexAttrObject);
     Gl.DrawElements(PrimitiveType.Triangles, indices.Length, DrawElementsType.UnsignedInt, indices);
 }
Example #27
0
        private static void InitializeTexture()
        {
            var crateTexture = new Texture("textures/crate1.png");

            Gl.ActiveTexture(0);
            Gl.BindTexture(crateTexture);

            // Scale up (magnify)
            Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter,
                             TextureParameter.Nearest); // Linear
            // Scale down (minify)
            Gl.TexParameteri(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter,
                             TextureParameter.Nearest); // Linear

            Gl.Enable(EnableCap.CullFace);
            Gl.CullFace(CullFaceMode.Back);
        }
Example #28
0
        private void BindTextures(float frameTimeSec)
        {
            /*time += frameTimeSec * 1000;
             * time %= 24000;
             * uint texture1;
             * uint texture2;
             * float blendFactor;
             *
             * float time1 = 5000;
             * float time2 = 8000;
             * float time3 = 21000;
             * float time4 = 24000;
             *
             * if (time >= 0 && time < time1)
             * {
             *  texture1 = textureID_night;
             *  texture2 = textureID_night;
             *  blendFactor = (time - 0) /  (time1 - 0);
             * }
             * else if (time >= time1 && time < time2)
             * {
             *  texture1 = textureID_night;
             *  texture2 = textureID_day;
             *  blendFactor = (time - time1) / (time2 - time1);
             * }
             * else if (time >= time2 && time < time3)
             * {
             *  texture1 = textureID_day;
             *  texture2 = textureID_day;
             *  blendFactor = (time - time2) / (time3 - time2);
             * }
             * else
             * {
             *  texture1 = textureID_day;
             *  texture2 = textureID_night;
             *  blendFactor = (time - time3) / (time4 - time3);
             * }*/

            Gl.ActiveTexture(TextureUnit.Texture0);
            Gl.BindTexture(TextureTarget.TextureCubeMap, textureID_day);

            Gl.ActiveTexture(TextureUnit.Texture1);
            Gl.BindTexture(TextureTarget.TextureCubeMap, textureID_night);

            this.shader.LoadBendFactor(0);
        }
Example #29
0
        public void render(TexturedModel texturedModel, StaticShader shader)
        {
            RawModel model = texturedModel.rawModel;

            Gl.BindVertexArray(model.vaoID);
            Gl.EnableVertexAttribArray(0);
            Gl.EnableVertexAttribArray(1);
            // uncomment lines to see shape
            //Matrix4f mat = Maths.createTransformationMatrix(new Vertex3f(0, 0, 0), 0, 0, 0, 1);
            //shader.loadTransformationMatrix(mat);
            Gl.ActiveTexture(TextureUnit.Texture0);                                        // activate texture
            Gl.BindTexture(TextureTarget.Texture2d, texturedModel.modelTexture.textureId); // pass coords
            Gl.DrawElements(PrimitiveType.Triangles, model.vertexCount, DrawElementsType.UnsignedInt, IntPtr.Zero);
            Gl.DisableVertexAttribArray(0);
            Gl.DisableVertexAttribArray(1);
            Gl.BindVertexArray(0);
        }
Example #30
0
        // Render the mesh
        public void Draw(Shader shader)
        {
            // Bind appropriate textures
            uint diffuseNr  = 1;
            uint specularNr = 1;

            for (int i = 0; i < this.textures.Count; i++)
            {
                Gl.ActiveTexture(Gl.TEXTURE0 + i); // Active proper texture unit before binding
                                                   // Retrieve texture number (the N in diffuse_textureN)
                StringBuilder stringBuilder = new StringBuilder();
                string        number;
                string        name = this.textures[i].type;
                if (name == "texture_diffuse")
                {
                    stringBuilder.Append(diffuseNr); // Transfer GLuint to stream
                    diffuseNr++;
                }
                else if (name == "texture_specular")
                {
                    stringBuilder.Append(specularNr); // Transfer GLuint to stream
                    specularNr++;
                }
                number = stringBuilder.ToString();
                // Now set the sampler to the correct texture unit
                Gl.Uniform1(Gl.GetUniformLocation(shader.Program, name + number), i);
                // And finally bind the texture
                Gl.BindTexture(TextureTarget.Texture2d, this.textures[i].id);
            }

            // Also set each mesh's shininess property to a default value (if you want you could extend this to another mesh property and possibly change this value)
            Gl.Uniform1(Gl.GetUniformLocation(shader.Program, "material.shininess"), 16.0f);

            // Draw mesh
            Gl.BindVertexArray(this.VAO);
            Gl.DrawElements(OpenGL.PrimitiveType.Triangles, this.indices.Count, DrawElementsType.UnsignedInt, (IntPtr)0);
            Gl.BindVertexArray(0);

            // Always good practice to set everything back to defaults once configured.
            for (int i = 0; i < this.textures.Count; i++)
            {
                Gl.ActiveTexture(Gl.TEXTURE0 + i);
                Gl.BindTexture(TextureTarget.Texture2d, 0);
            }
        }