0
votes

I think I have problem with the shader. It is supposed to print 1 color texture. It doesn't work. If I bind instead 3 color(GL_RGB) texture it displays it like original picture. I bind texture like this:

    glTexImage2D(GL_TEXTURE_2D, 0, GL_R, freetype_letters[i]->glyph->bitmap.width, freetype_letters[i]->glyph->bitmap.rows, 0, GL_R, GL_UNSIGNED_BYTE, freetype_letters[i]->glyph->bitmap.buffer);

Array is now a test version of constant rectangle size:

float letter_quad[]={
0.5f, 0.5f,     0.5f, 0.0f,     0.0f, 0.0f,      0.0f, 0.5f
};
glGenVertexArrays(1, &letter_box_a[i]);
glGenBuffers(1, &letter_box_b[i]);
glBindVertexArray(letter_box_a[i]);
glBindBuffer(GL_ARRAY_BUFFER, letter_box_b[i]);
glBufferData(GL_ARRAY_BUFFER, 4 * 2 * sizeof(float), letter_quad, GL_STATIC_DRAW);
glVertexAttribPointer(0,2,GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(0);

Then I call function responsible for choosing the letter to draw and drawing it:

draw_letter(0.0f, 0.0f, 'K', Text_shader, letter_texture, letter_box_a, letter_box_b, glm::vec3(1.0f, 1.0f, 1.0f));

Here is the declaration:

void draw_letter(float PosX, float PosY, unsigned char letter, unsigned int Text_shader, unsigned int *letter_texture, unsigned int *letter_box_a, unsigned int *letter_box_b, glm::vec3 Color){
unsigned int i = (unsigned int)letter;

glBindBuffer(GL_ARRAY_BUFFER, letter_box_b[i]);
glBindTexture(GL_TEXTURE_2D, letter_texture[i]);
glBindVertexArray(letter_box_a[i]);
glUseProgram(Text_shader);
int color_shader = glGetUniformLocation(Text_shader, "InColor");
glUniform3fv(color_shader, 1, glm::value_ptr(Color));
glm::vec2 Position = glm::vec2(PosX, PosY);
int pos_shader = glGetUniformLocation(Text_shader, "InPosition");
glUniform2fv(pos_shader, 1, glm::value_ptr(Position));
glDrawArrays(GL_QUADS,0,4);
}

Vertex shader. Watch that "InPosition" is not a position of texture! It is position on screen where letter should be displayed sent from uniform. I am not sure if I can do the trick with sending coordinate of textures from vertex data. Ofc array is 0.0 - 0.5 on x and 0.0 to 0.5 on y. This test version scalling it by 2 should give texture cordinates 0.0 to 1.0. InPos changed 2->3 coordinate:

#version 330 core
layout (location=0) in vec3 aPos;
uniform vec3 InColor;
uniform vec2 InPosition;
out vec2 TexCoord;
out vec3 OutColor;
void main(){
TexCoord= vec2(aPos.x*2, aPos.y*2);
gl_Position= vec4(aPos.x + InPosition.x, aPos.y +     InPosition.y, 0.0, 1.0);
OutColor= InColor;
}

Fragment shader:

#version 330 core
out vec4 FragColor;
in vec2 TexCoord;
in vec3 OutColor;

If it helps I sometimes get error in console: Assertion failed! func_exponential.inl, Line 166 Expression x > genType(0) This is function in glm library. Some dividing by 0. But it probably isn't related.

1

1 Answers

3
votes

GL_R is not a valid internal format, nor is it a valid pixel transfer format. Indeed, if you had been using a core-only OpenGL header, it wouldn't even be a valid enumerator. Even so, you should be checking your OpenGL errors, because that would have given you an error.

The internal format you're looking for is GL_R8, and the pixel transfer format would be GL_RED.