Updating shader from 120 to 330

Hello,

I’ve tried to update
http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Text_Rendering_01

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

old vert:


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

new vert:


#version 330 core

in vec4 coord;
out vec2 texcoord;


void main()
{
  gl_Position = vec4(coord.xy, 0, 1);
  texcoord=coord.zw;
}

old frag:


#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:


#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

 glGenBuffers(1, &vbo);

and

glGenTextures(1, &tex); 

were done earlier in the
program so you don’t see them here.


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?

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.

[QUOTE=Dan Bartlett;1248649]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.[/QUOTE]

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.

I pulled my hair out over this for weeks. For some reason I clicked on the ‘Comment on this page’ link and someone had discovered how to make this work properly.
Just thought I’d share.

http://en.wikibooks.org/wiki/Talk:OpenGL_Programming/Modern_OpenGL_Tutorial_Text_Rendering_01