Page 3 of 4 FirstFirst 1234 LastLast
Results 21 to 30 of 31

Thread: OpenGL 3.2 and bitmap fonts

  1. #21

    Re: OpenGL 3.2 and bitmap fonts

    I'm guessing you are drawing it so small it's hardly 1 pixel. Ortho screen coordinates go from 0 to window's width etc. You are scaling the render by 0.025, try leave it out for ortho. Each character should be at least 6x10 in numeric coordinates.

  2. #22

    Re: OpenGL 3.2 and bitmap fonts

    Quote Originally Posted by User137
    Each character should be at least 6x10 in numeric coordinates.
    What do you mean?

    I tried leaving it out, but still no success.

  3. #23

    Re: OpenGL 3.2 and bitmap fonts

    [pascal]Vec3(-X, ...[/pascal]
    X is always negative? This would draw it outside the screen.

    By 6x10 i mean that it would be roughly the size of font on this forum page, in pixels. You just need to take into account that perspective view goes by default from ~ -2 to 2 when ortho is 0 to 800 or something, that is hundreds of times bigger number scale.

  4. #24

    Re: OpenGL 3.2 and bitmap fonts

    I use a negative X to draw the triangles in reverse order. Is that a bad idea?

  5. #25

    Re: OpenGL 3.2 and bitmap fonts

    Quote Originally Posted by Brainer
    I use a negative X to draw the triangles in reverse order. Is that a bad idea?
    It doesn't really work like that. If you want to render text at (300, 300), you'll end up with text being rendered at (-300, 300) which is out of the screen.

    If you want to reverse the triangles, you should alter the order of your vertices:

    Code:
      Vertices.Add(V[0]); 
      Vertices.Add(V[2]); 
      Vertices.Add(V[1]); 
      Vertices.Add(V[3]); 
      Vertices.Add(V[2]); 
      Vertices.Add(V[0]);
    This is not tested. I hope you get what I mean.

    P.S: You are right about the fact that flipping an axis changes the rotation order. However, flipping X has some undesirable effects in this case.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  6. #26

    Re: OpenGL 3.2 and bitmap fonts

    Quote Originally Posted by chronozphere
    P.S: You are right about the fact that flipping an axis changes the rotation order. However, flipping X has some undesirable effects in this case.
    Even if I use positive values on the X axis, the text is not rendered.

    Hm, I've heard there's a need to map the text coordinates to the screen's coordinates before drawing in 2D mode. So it seems my current matrix calculations lead to no good, since there's a need to, I dunno how to call it, "project" the matrix onto a 2D plane? Maybe that's what should be done, what do you think?

    Here's the quote:
    if you were using a shader, you could just send vec2 with the screen size, then (vertex.x/(screen.x*0.5))-1 would scale the screen pixel to the correct place.. eg. (300/(600*0.5))-1 = 0 which would be the center of the screen.. in other words, you just need to send the vertex in screen coordinates and apply this scale instead of transforming it with the matrix in the shader.. it should work that way..

  7. #27

    Re: OpenGL 3.2 and bitmap fonts

    Can you do this first. Go in ortho mode and just draw a line from 50,50 to 100,50. If it is not showing then something is wrong with the setup. (GL_LINES)

  8. #28

    Re: OpenGL 3.2 and bitmap fonts

    Ok.

    I tried it and the line is not shown in orthographic projection.

  9. #29

    Re: OpenGL 3.2 and bitmap fonts

    I figured it out!

    I tried once again to turn the camera by 180 degrees and it worked. I had to change the matrix I used to create the final model-view matrix so that it could work. Here's how I do it.

    Calculating the projection matrix
    [code=delphi]
    FProjMat := Mat4CreateOrtho(0, FWidth, 0, FHeight, -1.0, 1.0);
    [/code]
    Calculating the view matrix
    [code=delphi]
    CachedMatrix := Mat4Rotate(Mat4Identity(), Euler(0.0, 180.0, 0.0))
    [/code]
    Rendering the text
    [code=delphi]
    procedure TBrain2DText.RenderText(const Camera: TBrainCamera;
    const AText: String; const ATextSize: Single; const AFont: TBrainFont);
    var
    CurX: Single;
    Ch: Char;
    Chaar, I, Ind: Integer;
    ProjMat, ModelMat, ModelView: TBrainMatrix;

    { .: AddQuad :. }
    procedure AddQuad(X, Y, Wid, Hgt, Tu, Tu2, Tv, Tv2: Single);
    var
    V: array [0 .. 3] of TBrainVector;
    begin
    TexCoords.Add(TexCoord(Tu, Tv));
    TexCoords.Add(TexCoord(Tu2, Tv));
    TexCoords.Add(TexCoord(Tu2, Tv2));
    TexCoords.Add(TexCoord(Tu2, Tv2));
    TexCoords.Add(TexCoord(Tu, Tv2));
    TexCoords.Add(TexCoord(Tu, Tv));

    V[0] := Vec3(-X, Y, 0.0);
    V[1] := Vec3(-(X + Wid), Y, 0.0);
    V[2] := Vec3(-(X + Wid), Y - Hgt, 0.0);
    V[3] := Vec3(-X, Y - Hgt, 0.0);

    Vertices.Add(V[0]);
    Vertices.Add(V[1]);
    Vertices.Add(V[2]);
    Vertices.Add(V[2]);
    Vertices.Add(V[3]);
    Vertices.Add(V[0]);
    end;

    begin
    // Before we begin, check if we have everything we need
    if not Assigned(Camera) then
    exit;
    if not Assigned(Shader) then
    exit;
    if (AText = '') then
    exit;
    if not Assigned(AFont) then
    exit;
    if (ATextSize = 0.0) then
    exit;

    // If there are any leftovers from previous text, remove it
    Vertices.Clear();
    TexCoords.Clear();

    // We are provided with parameters, so it's time to render
    // the text
    CurX := -Position.X;
    if not (AFont.FontLoaded) then
    exit;

    // Iterate through the text
    for I := 1 to Length(AText) do
    begin
    // Get the current character
    Ch := AText[I];
    Chaar := Integer(Ch);

    // Check if the current character is a legal one
    // (i.e. neither #10 nor #13). If it is, also check
    // if it's not the Space character. If so, move the cursor
    Ind := -1;
    case Chaar of
    32:
    CurX := CurX + AFont.SpaceWidth * ATextSize;
    10, 13: // No line-breaks supported
    break;
    else
    Ind := AFont.CharacterLookup[Chaar];
    end;

    // If we have a valid character...
    if (Ind > -1) then
    begin
    // Setup the cursor
    CurX := CurX + AFont.CharacterInfo[Ind].A * ATextSize;

    // Add a new quad which contains the character
    AddQuad(CurX, Position.Y, AFont.CharacterInfo[Ind].Width * ATextSize,
    AFont.CharacterInfo[Ind].Height * ATextSize, AFont.CharacterInfo[Ind].x1,
    AFont.CharacterInfo[Ind].x2, AFont.CharacterInfo[Ind].y1,
    AFont.CharacterInfo[Ind].y2);

    // Move the cursor to the next character
    CurX := CurX + AFont.CharacterInfo[Ind].C * ATextSize;
    end;
    end;
    // Notify the object that its structure has changed
    Self.StructureChanged();

    // Enable blending
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    // Switch to the orthographic projection, enable the shader,
    // and bind the texture
    Camera.ProjectionType := ptOrthogonal2D;
    Camera.ForceMatrixRecalc();

    Shader.Enable();
    AFont.FontTexture.Bind(0);
    AFont.FontTexture.Apply();

    // Calculate the matrices
    ProjMat := Camera.ProjectionMatrix;
    ModelMat := Mat4Translate(Mat4Identity(), Vec3(Position.X, Position.Y, 0.0));
    ModelView := Mat4Multiply(ModelMat, Camera.Matrix);

    // Update the shader
    Shader.SetMatrix(ProjectionMatrixUniformName, @ProjMat);
    Shader.SetMatrix(ModelViewMatrixUniformName, @ModelView);

    // Finally, render the text using triangle strips
    glBindVertexArray(GetVAO());
    glDrawArrays(GL_TRIANGLE_STRIP, 0, Vertices.Count);
    glBindVertexArray(0);

    // Restore to defaults
    Camera.ProjectionType := ptPerspective;
    Camera.ForceMatrixRecalc();

    glDisable(GL_BLEND);
    end;
    [/code]

    Hope this helps.

  10. #30
    PGD Staff / News Reporter phibermon's Avatar
    Join Date
    Sep 2009
    Location
    England
    Posts
    524

    Re: OpenGL 3.2 and bitmap fonts

    *sigh* it was so much easier with display lists
    When the moon hits your eye like a big pizza pie - that's an extinction level impact event.

Page 3 of 4 FirstFirst 1234 LastLast

Bookmarks

Posting Permissions

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