www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Loading Textures in OpenGL

reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
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?
Jul 23 2023
parent reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Sunday, 23 July 2023 at 16:21:05 UTC, Ruby The Roobster wrote:
 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.

 [...]
DCV uses gamut for image input, and opengl for displaying stuff. You can take a look at the sources: https://github.com/libmir/dcv/blob/master/source/dcv/imageio/image.d#L399 https://github.com/libmir/dcv/blob/master/source/dcv/plot/drawprimitives.d#L126
Jul 23 2023
parent reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
On Sunday, 23 July 2023 at 17:02:40 UTC, Ferhat Kurtulmuş wrote:
 On Sunday, 23 July 2023 at 16:21:05 UTC, Ruby The Roobster 
 wrote:
 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.

 [...]
DCV uses gamut for image input, and opengl for displaying stuff. You can take a look at the sources: https://github.com/libmir/dcv/blob/master/source/dcv/imageio/image.d#L399 https://github.com/libmir/dcv/blob/master/source/dcv/plot/drawprimitives.d#L126
Thank you. I'm still trying to work out how it works. It seems as if the creator tried the same thing that I did, and then commented it out. Perhaps for the same reason?
Jul 23 2023
next sibling parent Ruby The Roobster <michaeleverestc79 gmail.com> writes:
On Sunday, 23 July 2023 at 17:35:03 UTC, Ruby The Roobster wrote:
 On Sunday, 23 July 2023 at 17:02:40 UTC, Ferhat Kurtulmuş wrote:
 On Sunday, 23 July 2023 at 16:21:05 UTC, Ruby The Roobster 
 wrote:
 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.

 [...]
DCV uses gamut for image input, and opengl for displaying stuff. You can take a look at the sources: https://github.com/libmir/dcv/blob/master/source/dcv/imageio/image.d#L399 https://github.com/libmir/dcv/blob/master/source/dcv/plot/drawprimitives.d#L126
Thank you. I'm still trying to work out how it works. It seems as if the creator tried the same thing that I did, and then commented it out. Perhaps for the same reason?
I think I found the problem: It IS the flags I set. Saving the image gives a blank PNG file, though it doesn't explain why I get noise at the bottom of the rectangle.
Jul 23 2023
prev sibling parent reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Sunday, 23 July 2023 at 17:35:03 UTC, Ruby The Roobster wrote:
 On Sunday, 23 July 2023 at 17:02:40 UTC, Ferhat Kurtulmuş wrote:
 On Sunday, 23 July 2023 at 16:21:05 UTC, Ruby The Roobster 
 wrote:
 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.

 [...]
DCV uses gamut for image input, and opengl for displaying stuff. You can take a look at the sources: https://github.com/libmir/dcv/blob/master/source/dcv/imageio/image.d#L399 https://github.com/libmir/dcv/blob/master/source/dcv/plot/drawprimitives.d#L126
Thank you. I'm still trying to work out how it works. It seems as if the creator tried the same thing that I did, and then commented it out. Perhaps for the same reason?
It is me who did it. I was getting some weirdly distorted images. And you see my final solution. I am writing on my mobil so cannot help you further.
Jul 23 2023
parent reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
On Sunday, 23 July 2023 at 17:45:53 UTC, Ferhat Kurtulmuş wrote:
 On Sunday, 23 July 2023 at 17:35:03 UTC, Ruby The Roobster 
 [SNIP]
 Thank you.  I'm still trying to work out how it works.  It 
 seems as if the creator tried the same thing that I did, and 
 then commented it out.  Perhaps for the same reason?
It is me who did it. I was getting some weirdly distorted images. And you see my final solution. I am writing on my mobil so cannot help you further.
Ah. Anyhow, I fixed it. I had to use .scanline to fill the Image data, but in the end it worked. Thank you very much.
Jul 23 2023
parent Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Sunday, 23 July 2023 at 18:06:46 UTC, Ruby The Roobster wrote:
 On Sunday, 23 July 2023 at 17:45:53 UTC, Ferhat Kurtulmuş wrote:
 On Sunday, 23 July 2023 at 17:35:03 UTC, Ruby The Roobster 
 [SNIP]
 Thank you.  I'm still trying to work out how it works.  It 
 seems as if the creator tried the same thing that I did, and 
 then commented it out.  Perhaps for the same reason?
It is me who did it. I was getting some weirdly distorted images. And you see my final solution. I am writing on my mobil so cannot help you further.
Ah. Anyhow, I fixed it. I had to use .scanline to fill the Image data, but in the end it worked. Thank you very much.
Nice to hear that.
Jul 23 2023