转换GLSL现代OpenGL 3.2 [英] Converting GLSL modern OpenGL 3.2

查看:287
本文介绍了转换GLSL现代OpenGL 3.2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在wikibooks上的wikibooks上运行一个freetype教程,运行10.9和Xcode 5.我使用着色器版本120运行,但我想使用一些现代功能,所以我设置SDL提示到OpenGL 3.2,并转换我的着色器到版本150,问题是版本150,使用texture2D将阻止着色器编译。

I'm following a freetype tutorial on wikibooks on a mac running 10.9 with Xcode 5. I have it running with shader version 120 but I want to use some modern features so I set the SDL hints to OpenGL 3.2 and convert my shaders to 150. The problem is in version 150, the use of texture2D will prevent the shader from compiling.

这里是着色器的版本120:

Here is the version 120 of the shaders:

const GLchar* vTextSource =
"#version 120\n"
"attribute vec4 coord;"
"varying vec2 texcoord;"
"void main(void) {"
"  gl_Position = vec4(coord.xy, 0, 1);"
"  texcoord = coord.zw;"
"}";

const GLchar* fTextSource =
"#version 120\n"
"varying vec2 texcoord;"
"uniform sampler2D tex;"
"uniform vec4 color;"
"void main(void) {"
"  gl_FragColor = vec4(1,1,0, texture2D(tex, texcoord).a) * color;"
"}";

这是我对于版本150的。顶点着色器构建,但片段着色器失败,除非我删除texture2D的任何使用。所有的CPU代码是相同的。

And this is what I have for version 150. The vertex shader builds but the fragment shader fails unless I remove any uses of texture2D. All the CPU code is the same between them.

const GLchar* vTextSource =
"#version 150 \n"
"in vec4 coord;"
"out vec2 texcoord;"
"void main(void) {"
"  gl_Position = vec4(coord.xy, 0, 1);"
"  texcoord = coord.zw;"
"}";

const GLchar* fTextSource =
"#version 150\n"
"uniform sampler2D tex;"
"in vec2 texcoord;"
"uniform vec4 color;"
"out vec4 outColor;"
"void main(void) {"
"  outColor = vec4(1,1,1, texture2D(tex, texcoord).a) * color;"
"}";

我遗漏了什么?设置纹理采样器的核心配置文件不同于兼容模式?

Is there something I am missing? Is setting up the texture sampler different in core profile than in compatibility mode?

编辑:我已经从texture2D(...)更改为纹理片段着色器。它现在编译,但什么也没有。我不知道如何去调试这个。我已经包括纹理初始化例程:

I have changed from texture2D(...) to texture(...) in the fragment shader. It compiles now but shows nothing. I'm not sure how to go about debugging this. I've included the texture initialization routine:

void SdlApplication::render_text(const char *text, float x, float y, float sx, float sy) {
    const char *p;
    FT_GlyphSlot g = face->glyph;

    /* Create a texture that will be used to hold one "glyph" */
    GLuint tex;

    glActiveTexture(GL_TEXTURE0);
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);
    glUniform1i(ogl.uniform_tex, 0);

    /* We require 1 byte alignment when uploading texture data */
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    /* Clamping to edges is important to prevent artifacts when scaling */
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    /* Linear filtering usually looks best for text */
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    /* Set up the VBO for our vertex data */
    glEnableVertexAttribArray(ogl.attribute_coord);
    glBindBuffer(GL_ARRAY_BUFFER, vboText);
    glVertexAttribPointer(ogl.attribute_coord, 4, GL_FLOAT, GL_FALSE, 0, 0);

    /* Loop through all characters */
    for (p = text; *p; p++) {
        /* Try to load and render the character */
        if (FT_Load_Char(face, *p, FT_LOAD_RENDER))
            continue;

        /* Upload the "bitmap", which contains an 8-bit grayscale image, as an alpha texture */
        glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, g->bitmap.width, g->bitmap.rows, 0, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap.buffer);

        /* Calculate the vertex and texture coordinates */
        float x2 = x + g->bitmap_left * sx;
        float y2 = -y - g->bitmap_top * sy;
        float w = g->bitmap.width * sx;
        float h = g->bitmap.rows * sy;

        point box[4] = {
            {x2, -y2, 0, 0},
            {x2 + w, -y2, 1, 0},
            {x2, -y2 - h, 0, 1},
            {x2 + w, -y2 - h, 1, 1},
        };

        /* Draw the character on the screen */
        glBufferData(GL_ARRAY_BUFFER, sizeof box, box, GL_DYNAMIC_DRAW);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

        /* Advance the cursor to the start of the next character */
        x += (g->advance.x >> 6) * sx;
        y += (g->advance.y >> 6) * sy;
    }

    glDisableVertexAttribArray(ogl.attribute_coord);
    glDeleteTextures(1, &tex);
}



编辑2:添加vao到我的顶点设置。我现在得到的文本应该是纯色的正方形。所以看起来像纹理坐标再次搞砸了。

Edit 2: Added vao to my vertices setup. I now get squares of solid colors where the text should be. So it seems like the texture coordinates are messed up again.

我在每次调用后添加了检查,发现我在glewInit之后但是之前没有获得1280代码...

I added checks after each call and found out I get the 1280 code right after glewInit but not before...

推荐答案

除了 texture2D()的问题,你的GLSL代码更新到最新的标准看起来不错。正如已经指出的,纹理采样函数现在重载,需要使用 texture(),而不是 texture2D()

The update of your GLSL code to the latest standards looks fine, except for the problem with texture2D(). As was already pointed out, the texture sampling functions are now overloaded, and texture() needs to be used instead of texture2D().

其余的问题主要是更新代码以使用Core配置文件,这会淘汰许多旧版功能。查看发布的代码,包括:

The remaining problems are mostly with updating the code to use the Core Profile, which deprecates many legacy features. Looking at the posted code, this includes:


  • 使用VAO(顶点数组对象)是设置顶点状态的必要条件。使用 glGenVertexArrays() glBindVertexArray() glVertexAttribPointer() glEnableVertexAttribArray()

  • Using VAOs (Vertex Array Objects) is mandatory for setting up vertex state. Use functions like glGenVertexArrays() and glBindVertexArray() to set up a VAO, and make sure that you have it bound while using vertex state setup functions like glVertexAttribPointer() and glEnableVertexAttribArray().

不再支持 GL_ALPHA 纹理格式。对于使用单个8位组件的纹理格式,对于内部格式使用 GL_R8 ,对于内部格式使用 GL_RED 格式:

The GL_ALPHA texture format is not supported anymore. For using a texture format with a single 8-bit component, use GL_R8 for the internal format, and GL_RED for the format:

glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, g->bitmap.width, g->bitmap.rows, 0,
             GL_RED, GL_UNSIGNED_BYTE, g->bitmap.buffer);

这也需要对着色器代码进行细微的更改,因为1分量纹理现在是红色组件:

This will also require a minor change in the shader code, since the sampling value for the 1-component texture is now in the red component:

outColor = vec4(1,1,1, texture2D(tex, texcoord).r) * color;


这篇关于转换GLSL现代OpenGL 3.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆