【问题标题】:Vala OpenGL glGenTextureVala OpenGL glGenTexture
【发布时间】:2012-09-18 14:53:46
【问题描述】:
colors = surface->format->BytesPerPixel;
if (colors == 4) {   // alpha
    if (surface->format->Rmask == 0x000000ff)
        texture_format = GL_RGBA;
    else
        texture_format = GL_BGRA;
} else {             // no alpha
    if (surface->format->Rmask == 0x000000ff)
        format = GL_RGB;
    else
        format = GL_BGR;
}

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture); 
glTexImage2D(GL_TEXTURE_2D, 0, colors, surface->w, surface->h, 0,
                    texture_format, GL_UNSIGNED_BYTE, surface->pixels);

我正在尝试将在 Stack Overflow 上找到的代码转换为 Vala。但是,我遇到了 glGenTextures 的问题。

第一部分很简单:

    int texture_format;
    uchar colors = screen.format.BytesPerPixel;
    if (colors == 4) {
        if (screen.format.Rmask == 0x000000ff) {
            texture_format = GL_RGBA;
        } else {
            texture_format = GL_BGRA;
        }
    } else {
        if (screen.format.Rmask == 0x000000ff) {
            texture_format = GL_RGB;
        } else {
            texture_format = GL_BGR;
        }
    }

不过,这部分我不知道如何转换: glGenTextures(1, &texture);

我试过了:

    GL.GLuint[] texture = {};
    glGenTextures (1, texture);

我明白了:

.vala:46.27-46.33: error: Argument 2: Cannot pass value to reference or output parameter

我也试过了:

GL.GLuint[] texture = {};
glGenTextures (1, out texture);

我明白了:

ERROR:valaccodearraymodule.c:1183:vala_ccode_array_module_real_get_array_length_cvalue: assertion failed: (_tmp48_)

我试过了:

glGenTextures (1, &texture);

我明白了:

.vala:46.27-46.34: error: Argument 2: Cannot pass value to reference or output parameter

我尝试了各种其他类似的东西,有什么想法吗?

【问题讨论】:

    标签: opengl sdl vala


    【解决方案1】:

    听起来绑定是错误的。基于the glGenTextures man page,Vala 绑定应该是这样的:

    public static void glGenTextures ([CCode (array_length_pos = 0.9)] GL.GLuint[] textures);
    

    调用它的正确方法是:

    GL.GLuint[] textures = new GL.GLuint[1];
    glGenTextures (textures);
    // your texture is textures[0]
    

    当然,对于您只需要单个纹理的用例来说,这不是很好,但这没什么大不了的。问题是你必须在堆上分配一个数组......我不怀疑这是一个对性能敏感的代码区域,所以我可能不会打扰,但你总是可以创建一个替代版本:

    [CCode (cname = "glGenTextures")]
    public static void glGenTexture (GL.GLsizei n, out GL.GLuint texture);
    

    然后你可以调用它

    GL.GLuint texture;
    glGenTexture (1, out texture);
    

    尽管您只能使用此版本生成单个纹理,但您仍然必须手动传递第一个参数。

    【讨论】:

    • 效果很好。现在唯一的问题是surface.pixels 返回void*,但glTexImage2D(); 调用GL.GLvoid[]?。关于如何在 gl.vapi 上解决这个问题的任何想法?谢谢!
    • 铸造应该可以工作。 glTexImage2D (..., (GL.GLvoid[]) surface.pixels);。也就是说,GL.GLvoid[]? 的参数可能是错误的——它可能应该是 void*、GL.GLvoid(并且 GL.GLvoid 应该是一个类而不是结构)、uint8[](用于二进制数据),或者如果像素格式是明确定义的更具体的东西(例如,某种像素类型的二维数组)。除非您在某处看到过此操作,否则您可能希望找到一些有关 SDL 和 OpenGL 的详细文档,以确保格式兼容……尝试将 RGBA 放入 BGR 缓冲区不会很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多