Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Updating shader from 120 to 330

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    5

    Updating shader from 120 to 330

    Hello,

    I've tried to update
    http://en.wikibooks.org/wiki/OpenGL_...t_Rendering_01

    to use 3.3 shaders. Can someone tell me if I translated them correctly?

    old vert:

    Code :
    #version 120
     
    attribute vec4 coord;
    varying vec2 texcoord;
     
    void main(void) {
      gl_Position = vec4(coord.xy, 0, 1);
      texcoord = coord.zw;
    }

    new vert:

    Code :
    #version 330 core
     
    in vec4 coord;
    out vec2 texcoord;
     
     
    void main()
    {
      gl_Position = vec4(coord.xy, 0, 1);
      texcoord=coord.zw;
    }
    old frag:
    Code :
    #version 120
     
    varying vec2 texcoord;
    uniform sampler2D tex;
    uniform vec4 color;
     
    void main(void) {
      gl_FragColor = vec4(1, 1, 1, texture2D(tex, texcoord).a) * color;
    }

    new frag:


    Code :
    #version 330 core
     
     
    in vec2 texcoord;
    out vec4 color;
     
    uniform sampler2D tex;
    uniform vec4 c;
     
     
    void main()
    {
      color = vec4(1, 1, 1, texture2D(tex, texcoord).a) * c;
    }

    Nothing is appearing for some reason the glyphs are transparent I've even tried hardcoding the color to vec4(1,1,1,1) and they show up black.


    Here is the render text function updated to use GL_RGBA for format since apparently GL_ALPHA is deprecated ( note that
    Code :
     glGenBuffers(1, &vbo);
    and
    Code :
    glGenTextures(1, &tex);
    were done earlier in the
    program so you don't see them here.

    Code :
    void render_text(const char *text, float x, float y, float sx, float sy) {
    {
     
      FT_Set_Pixel_Sizes(face, 0, 48);
     
     
     
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     
      glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
     
     
      glActiveTexture(GL_TEXTURE0);
      glBindTexture(GL_TEXTURE_2D, tex);
      glUniform1i(uniform_tex, 0);
     
      glEnableVertexAttribArray(0);
      glBindBuffer(GL_ARRAY_BUFFER, vbo);
      glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
     
     
      glEnable(GL_BLEND);
      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
      //glBlendFunc(GL_SRC_ALPHA, GL_ZERO);
     
     
      const char *p;
     
      for(p = text; *p; p++) {
        if(FT_Load_Char(face, *p, FT_LOAD_RENDER))
            continue;
     
        glTexImage2D(
          GL_TEXTURE_2D,
          0,
          GL_RGBA, //GL_ALPHA
          g->bitmap.width,
          g->bitmap.rows,
          0,
          GL_RGBA,
          GL_UNSIGNED_BYTE,
          g->bitmap.buffer
        );
     
        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;
     
        GLfloat box[4][4] = {
            {x2,     -y2    , 0, 0},
            {x2 + w, -y2    , 1, 0},
            {x2,     -y2 - h, 0, 1},
            {x2 + w, -y2 - h, 1, 1},
        };
     
        glBufferData(GL_ARRAY_BUFFER, sizeof box, box, GL_DYNAMIC_DRAW);
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
     
        x += (g->advance.x >> 6) * sx;
        y += (g->advance.y >> 6) * sy;
      }
     
      glDisable(GL_BLEND);
     
    }


    Any ideas on this?
    Last edited by abdd0e77; 03-02-2013 at 11:11 PM.

  2. #2
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    373
    I assume you've accidentally labelled your vertex shader as fragment shader and vice versa?

    One thing that won't work in a core OpenGL 3.3 profile is using the texture2D() function, which is only available in compatibility profile. You should rather use the overloaded texture() function instead. If you query the shader info log after compiling the shader, it should report this function not being supported/found.

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2012
    Posts
    5
    Quote Originally Posted by Dan Bartlett View Post
    I assume you've accidentally labelled your vertex shader as fragment shader and vice versa?

    One thing that won't work in a core OpenGL 3.3 profile is using the texture2D() function, which is only available in compatibility profile. You should rather use the overloaded texture() function instead. If you query the shader info log after compiling the shader, it should report this function not being supported/found.
    Oops, yes; I just fixed my original post.
    Hmm, well, I did swap the function and didn't notice a difference I'll try interrogating the shader info log. For some reason I guessed only errors were reported.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •