Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

glTexImage2D throws System.AccessViolationException: #47

Closed
theLemon505 opened this issue Feb 20, 2022 · 3 comments
Closed

glTexImage2D throws System.AccessViolationException: #47

theLemon505 opened this issue Feb 20, 2022 · 3 comments

Comments

@theLemon505
Copy link

no clue why this is happening but when i convert a System.Drawing image to a byte[] then to a IntPtr and pass it to this function, i get a System.AccessViolationException:

@theLemon505
Copy link
Author

here the code

using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using static OpenGL.GL;

namespace TheRaft.Core.Graphics
{
internal class Texture
{
public uint id;

    public unsafe Texture(string name)
    {
        string path = "C:/.TheRaft/Textures/" + name;
        Image i = Image.FromFile(path);
        byte[] data = (byte[])(new ImageConverter()).ConvertTo(i, typeof(byte[]));
        fixed (byte* p = data)
        {
            IntPtr ptr = (IntPtr)p;
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
            float[] borderColor = { 1.0f, 1.0f, 0.0f, 1.0f };
            fixed (float* pt = &borderColor[0]) {
                glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, pt);
            }
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            id = glGenTexture();
            glBindTexture(GL_TEXTURE_2D, id);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, i.Width, i.Height, 0, GL_RGB, GL_UNSIGNED_BYTE, ptr);
            glGenerateMipmap(GL_TEXTURE_2D);
        };
    }
}

}

@theLemon505
Copy link
Author

fixed, was not allocating the memory for the texture

@ForeverZer0
Copy link
Owner

Glad you got it figured out, but one word of warning if you are using the OpenGL bindings from the example project:

They are not thoroughly tested nor even "officially" part of this project, were simply generated via a dirty script I wrote on a fixed GL version using the OpenGL registry, and were merely provided as convenience to test this project with. While they will likely suffice for most cases without any issue, I would not personally consider them production-ready, nor exactly the most user-friendly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants