This project is read-only.

Showing BitMap Image under Shaders ?


OK so I can see how to load a texture while under non shader support.. but I can't find any example or any way to support textures under shaders.

Anyone have any ideas ? I tried ActiveTexture(GL_TEXTURE0) and it accepts that, I have a few other settings setup, even the textureCoords attribute is set to 1, but for some reason, the shader doesn't show the texture. It compiles with no errors.. so not sure how to go about fixing this.

So far, it compiles and I get my blue background but with a black box. I can't get the texture to show up in that spot where the black box is supposed to be at. That black box is my vertices.


Here is the CODE that I currently have...
using System;
using System.Drawing;
using System.Drawing.Imaging;
using GlmNet;
using SharpGL;
using SharpGL.Shaders;
using SharpGL.VertexBuffers;

namespace OGLEngine
    public class Scene
          mat4 projectionMatrix;
          mat4 viewMatrix;
          mat4 modelMatrix;
          const uint attributeIndexPosition = 0;
          const uint attributeIndexTexture = 1;
          VertexBufferArray vertexBufferArray;
          private ShaderProgram shaderProgram;
          uint[] unsignedIntTextures = new uint[1];

            float[] vertices = { -0.5f, 0.5f, 0,
                                    -0.5f, -0.5f, 0,
                                     0.5f, -0.5f, 0,
                                     0.5f, 0.5f, 0
            uint[] indices = { 0,1,2,
          float[] textureCoords = { 0, 0,
                                                 0, 1,
                                                 1, 1,
                                                 1, 0
          Bitmap textureImage;
          VertexBuffer textureCoord ;
        public void Initialise(OpenGL gl, float width, float height)
              gl.ClearColor(0.4f, 0.6f, 0.9f, 0.0f);

               textureImage = new Bitmap("Crate.bmp");
                  gl.GenTextures(1, unsignedIntTextures);

                gl.BindTexture(OpenGL.GL_TEXTURE_2D, unsignedIntTextures[0]);
                  gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, 3, textureImage.Width, textureImage.Height, 0, OpenGL.GL_BGR, OpenGL.GL_UNSIGNED_BYTE,
                  textureImage.LockBits(new Rectangle(0, 0, textureImage.Width, textureImage.Height),
                  ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb).Scan0);
                gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR);
                gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR);
              ShaderLoader sl = new ShaderLoader();
                var vertexShaderSource = sl.LoadShader("defaultVertex.glsl");
                var fragmentShaderSource = sl.LoadShader("defaultFragment.glsl");
                shaderProgram = new ShaderProgram();
                shaderProgram.Create(gl, vertexShaderSource, fragmentShaderSource, null);
                shaderProgram.BindAttributeLocation(gl, attributeIndexPosition, "position");
                shaderProgram.BindAttributeLocation(gl, attributeIndexTexture, "textureCoords");
                const float rads = (60.0f / 360.0f) * (float)Math.PI * 2.0f;
                projectionMatrix = glm.perspective(rads, width / height, 0.1f, 100.0f);
                viewMatrix = glm.translate(new mat4(1.0f), new vec3(0.0f, 0.0f, -5.0f));
                modelMatrix = glm.scale(new mat4(1.0f), new vec3(2.5f));
             public void Draw(OpenGL gl)

                gl.DrawElements(OpenGL.GL_TRIANGLES, indices.Length, indices);
            private void CreateVerticesForSquare(OpenGL gl)
                vertexBufferArray = new VertexBufferArray();
                var vertexDataBuffer = new VertexBuffer();
                vertexDataBuffer.SetData(gl, attributeIndexPosition, vertices, false, 3);
                textureCoord = new VertexBuffer();
                textureCoord.SetData(gl, attributeIndexTexture, textureCoords, false, 2);
My Fragment Shader
#version 400 core

in vec2 pass_textureCoords;

out vec4 out_Color;

uniform sampler2D textureSampler;

void main(void) {
    out_Color = texture(textureSampler, pass_textureCoords);
My Vertext Shader
#version 400 core

in vec3 position;
in vec2 textureCoords;

out vec2 pass_textureCoords;

void main(void) {
    gl_Position = vec4(position, 1.0);
        pass_textureCoords = textureCoords;


D4Water wrote Jan 24, 2017 at 11:16 PM

I believe I solved what the problem is...

SharpGL ONLY uses BitMap images. It will not allow any other kind. So you are forced to use BMP files. So if you want to use the standards, like PNG, you have to write your own IMAGE to BITMAP convertor.

I'll have to do that and see if I can finally display a texture using shaders.

There needs to be an example added to the others, so you can display textures when using shaders. Right now the only texture example there is, uses the old fixed pipeline method which is outdated.

wrote Jan 24, 2017 at 11:42 PM

D4Water wrote Jan 24, 2017 at 11:46 PM

This site doesn't allow you to edit comments, so sorry for the multiple posts.

I fixed the code in the OP to reflect BitMap files now. But, still same problem. It compiles, but only shows a black box. No actual image is showing. So hopefully someone can spot my error.

Thank you.

wrote Jan 24, 2017 at 11:47 PM