1
Vote

Showing BitMap Image under Shaders ?

description

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.

Thanks

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,
                                      0,2,3
                                       };
          
          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.Enable(OpenGL.GL_TEXTURE_2D);
                gl.ActiveTexture(OpenGL.GL_TEXTURE0);
                  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");
                shaderProgram.AssertValid(gl);
    
                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));
    
                CreateVerticesForSquare(gl);
        }
        
             public void Draw(OpenGL gl)
             {
                   gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_STENCIL_BUFFER_BIT);
                   
                shaderProgram.Bind(gl);
                vertexBufferArray.Bind(gl);

                gl.DrawElements(OpenGL.GL_TRIANGLES, indices.Length, indices);
                
                vertexBufferArray.Unbind(gl);
                shaderProgram.Unbind(gl);
              }
             
            private void CreateVerticesForSquare(OpenGL gl)
            {
                vertexBufferArray = new VertexBufferArray();
                vertexBufferArray.Create(gl);
                vertexBufferArray.Bind(gl);
    
                var vertexDataBuffer = new VertexBuffer();
                vertexDataBuffer.Create(gl);
                vertexDataBuffer.Bind(gl);
                vertexDataBuffer.SetData(gl, attributeIndexPosition, vertices, false, 3);
                                
                textureCoord = new VertexBuffer();
                textureCoord.Create(gl);
                textureCoord.Bind(gl);
                textureCoord.SetData(gl, attributeIndexTexture, textureCoords, false, 2);
                
                vertexBufferArray.Unbind(gl);
            }
    }
}
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;
}

comments

D4Water wrote Jan 24 at 10: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 at 10:42 PM

D4Water wrote Jan 24 at 10: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 at 10:47 PM