Loading Textures in OpenGL

Ruby The Roobster michaeleverestc79 at gmail.com
Sun Jul 23 16:21:05 UTC 2023


I'm currently trying to load two textures and apply them to a 
rectangle, following
[this](https://learnopengl.com/Getting-started/Textures) which is 
linked to from the README file of the bindbc OpenGL bindings.

I'm using Gamut to load the files, with the file:

```d
module texture;

import gamut;
import bindbc.opengl;
import bindbc.glfw;
debug import std.stdio;

private __gshared Image[] _textures;
private __gshared char[][] texData;
public __gshared uint[] textures;

void newTexture(in string filename, out int width, out int hight, 
out int nrChannels)
{
     auto k = ++_textures.length;
     _textures[k-1].loadFromFile(filename);
     if(_textures[k-1].isError)
         throw new Exception(_textures[$-1].errorMessage.idup);
     if(_textures[k-1].type != PixelType.rgb8 && 
_textures[k-1].type != PixelType.rgba8)
         throw new Exception("invalid pixel type");

     width = _textures[k-1].width;
     hight = _textures[k-1].height;
     nrChannels = 3; // May change later, we'll see
     _textures[k-1].setSize(width, hight, _textures[k-1].type, 
LAYOUT_GAPLESS | LAYOUT_VERT_STRAIGHT);
     auto u = ++texData.length;
     texData[u-1] = cast(char[])_textures[k-1].allPixelsAtOnce;
     debug writeln(texData[u-1]);
     textures.length++;
     glGenTextures(1, &textures[k-1]);
     glBindTexture(GL_TEXTURE_2D, textures[k-1]);
     // Set wrapping & filtering options
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
GL_NEAREST_MIPMAP_LINEAR);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, 
GL_LINEAR);
     // Bind the texture;
     if(texData[u-1])
     {
         switch(_textures[k-1].type)
         {
             case PixelType.rgb8:
                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, 
hight, 0, GL_RGB, GL_UNSIGNED_BYTE, texData[u-1].ptr);
                 glGenerateMipmap(GL_TEXTURE_2D);
                 break;
             case PixelType.rgba8:
                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, 
hight, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData[u-1].ptr);
                 glGenerateMipmap(GL_TEXTURE_2D);
                 break;
             default:

         }
     }
     else
     {
         throw new Exception("Failed to load texture: " ~ 
filename);
     }
     _textures[k-1] = Image.init;
     texData[u-1] = [][];
}
```

I presume that I am messing up with reading the pixels, but I 
don't see how.
Does LAYOUT_GAPLESS | LAYOUT_VERT_STRAGIHT really have that great 
of an effect?


More information about the Digitalmars-d-learn mailing list