Page 2 of 4 FirstFirst 1234 LastLast
Results 11 to 20 of 31

Thread: OpenGL 3.2 and bitmap fonts

  1. #11

    Re: OpenGL 3.2 and bitmap fonts

    Could you use a TRIANGLE_STRIP? That way you would still manage it with 4 vertices.

    Code:
    0---2
    | / |
    1/_3

  2. #12

    Re: OpenGL 3.2 and bitmap fonts

    Triangle strips is not the best way as they are continues over more then 2 triangles. Better to draw using index buffer.

    In other words draw the verticies as you would normaly do but into a VBO with GL_STREAM_DRAW and then generate a index buffer:

    [code=pascal]

    0---2 4---6
    | / | | / |
    1---3 5---7

    // Triangle 1
    indicies[0]:= 0;
    indicies[0]:= 2;
    indicies[0]:= 1;
    // Triangle 2
    indicies[0]:= 2;
    indicies[0]:= 3;
    indicies[0]:= 1;

    // Triangle 3
    indicies[0]:= 4;
    indicies[0]:= 6;
    indicies[0]:= 5;
    // Triangle 4
    indicies[0]:= 6;
    indicies[0]:= 7;
    indicies[0]:= 5;
    [/code]

    and so on...

    Amnoxx

    Oh, and this code appears to be an approximate replacement for return(random() & 0x01);

    Phoenix Wiki
    http://www.phoenixlib.net/

    Phoenix Forum
    http://www.pascalgamedevelopment.com/viewforum.php?f=71

  3. #13

    Re: OpenGL 3.2 and bitmap fonts

    I don't know OpenGL 3 that well but figures described here (http://openbve.trainsimcentral.co.uk/common/faces.png) are basics in all graphics API's? However you can use indexes with triangle_strips too. Only point of indexes is to save memory by reducing vertex calls. But if you compare rendering speed of GL_TRIANGLES and GL_TRIANGLE_STRIP, then strip is always faster.

    Now that i think about it, it depends if you render the font characters continuous or 1 by 1. You actually need unique texture coordinate per vertex so continuous is out of question. I guess in this case indexes wouldn't do any good.

  4. #14

    Re: OpenGL 3.2 and bitmap fonts

    I'm still trying to convert a quad into a triangle. I got this code (it's being worked on, hence it looks so ugly):
    [code=pascal]
    { .: DrawQuad :. }
    procedure DrawQuad(X, Y, Wid, Hgt, Lev, Tu, Tu2, Tv, Tv2: Single);
    var
    V: array[0..3] of TBrainVector;
    begin
    Tv := 1 - Tv;
    Tv2 := 1 - Tv2;

    TexCoords.Add(TexCoord(Tu, Tv));
    TexCoords.Add(TexCoord(Tu2, Tv));
    TexCoords.Add(TexCoord(Tu2, Tv2));
    TexCoords.Add(TexCoord(Tu, Tv2));
    V[0] := Vec3(X, Y, -Lev);
    V[1] := Vec3(X + Wid, Y, -Lev);
    V[2] := Vec3(X + Wid, Y - Hgt, -Lev);
    V[3] := Vec3(X, Y - Hgt, -Lev);

    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;
    [/code]
    The quad is drawn correctly, but there's something wrong with its texture coordinates. Can you point me out, what's wrong?

  5. #15

    Re: OpenGL 3.2 and bitmap fonts

    I don't know if it helps, but here is how I draw a quad using a triangle strip:

    [pascal]Procedure TParticleSystem.Draw;
    Var
    i : Integer;
    Particle : TParticle;
    Begin
    glPushAttrib(GL_ALL_ATTRIB_BITS);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_LIGHTING);
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glEnable(GL_ALPHA);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    For i := FActiveParticles.Count - 1 Downto 0 Do
    Begin
    Particle := TParticle(FActiveParticles.Items[i]);
    glPushMatrix;
    glTranslatef(Particle.X,Particle.Y,Particle.Z);
    glScalef(FScale,FScale,1);
    glColor4f(Particle.R,Particle.G,Particle.B,Particl e.A);
    glBegin(GL_TRIANGLE_STRIP);
    glTexCoord2fv(@FImageRect.tRect.t3); glVertex3f(+Particle.Size/2,+Particle.Size/2,0); // Top Right
    glTexCoord2fv(@FImageRect.tRect.t2); glVertex3f(-Particle.Size/2,+Particle.Size/2,0); // Top Left
    glTexCoord2fv(@FImageRect.tRect.t4); glVertex3f(+Particle.Size/2,-Particle.Size/2,0); // Bottom Right
    glTexCoord2fv(@FImageRect.tRect.t1); glVertex3f(-Particle.Size/2,-Particle.Size/2,0); // Bottom Left
    glEnd;
    glPopMatrix;
    End;
    glPopAttrib;
    End;[/pascal]

    cheers,
    Paul
    Games:
    Seafox


    Pages:
    Syntax Error Software itch.io page

    Online Chess
    http://gameknot.com/#paul_nicholls

  6. #16

    Re: OpenGL 3.2 and bitmap fonts

    Quote Originally Posted by Brainer
    The quad is drawn correctly, but there's something wrong with its texture coordinates. Can you point me out, what's wrong?
    You are adding only 4 texture coordinates (instead of 6) but you use 6 vertices. If you used index buffer you would manage with only 4 texture and vertex coordinates.

  7. #17

    Re: OpenGL 3.2 and bitmap fonts

    Ok, I got over the problem with rendering quads.

    But now, I'd like to know, how do I put the text in front of the camera in the 2D space? I'm using this code to set the projection matrix:
    [code=delphi]
    FProjMat := Mat4CreateOrtho(0, FWidth, 0, FHeight, -1.0, 1.0);
    [/code]
    And this snippet to render the font:
    [code=delphi]
    glEnable(GL_BLEND);
    glDisable(GL_DEPTH_TEST);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    Camera.ProjectionType := ptOrthogonal;

    Texture.Bind(0);
    Self.DoDefaultRender(Camera);
    Texture.UnBind();

    Camera.ProjectionType := ptPerspective;

    glDisable(GL_BLEND);
    glEnable(GL_DEPTH_TEST);
    [/code]
    But the text is not visible in the orthogonal view. It is in the perspective, tho'. What am I doing wrong? Maybe it's something wrong with the shader?
    Code:
    #version 150
    
    precision highp float;
    
    uniform mat4 proj;
    uniform mat4 modelview;
    
    in vec4 vertex;
    in vec3 normal;
    in vec2 texCoord;
    
    out vec3 fragmentNormal;
    out vec2 textureCoord;
    
    void main(void) {
    	
    	fragmentNormal = (modelview * vec4(normal, 0.0)).xyz;
    	textureCoord = texCoord;
    
    	gl_Position = proj * modelview * vertex;
    }

  8. #18

    Re: OpenGL 3.2 and bitmap fonts

    Could be a few things:

    - You are not using the right Z value
    - You are rendering the letters in the wrong position (try (0,0) )
    - You are not using the right scale of the letters
    - You are rendering the triangles in the wrong order (unlikely though, since it works in perspective view).

    WILL asked me to write a little article for PascalGamer that explains how to do both 2D and 3D rendering at the same time. I might do that in the future, if I find the time.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  9. #19

    Re: OpenGL 3.2 and bitmap fonts

    If your triangles are rendered 1-sided (GL_CULL_FACE enabled) then by default ortho is like the backside of perspective. If you turn camera 180 degrees or draw vertices in opposite order it may help. (or coordinates just aren't right...)

  10. #20

    Re: OpenGL 3.2 and bitmap fonts

    I tried both chronozphere and User137 solutions, but none worked.

    Hm, I can't understand why it doesn't work. The quads are not displayed. You said the coords may not be right. Here's how I specify them:
    [code=delphi]
    procedure TBrain2DFont.BuildObject;
    const
    Txt: String = 'BrainEngine';
    Size: Single = 0.025;
    Y: Single = 0.0;
    var
    CurX: single;
    Ch: Widechar;
    Chaar, I, Ind: integer;

    { .: DrawQuad :. }
    procedure DrawQuad(X, Y, Wid, Hgt, Lev, 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, Lev);
    V[1] := Vec3(-(X + Wid), Y, Lev);
    V[2] := Vec3(-(X + Wid), Y - Hgt, Lev);
    V[3] := Vec3(-X, Y - Hgt, Lev);

    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
    CurX := 0.0;
    if (Length(F) = 0) then
    exit;

    for I := 1 to length(Txt) do
    begin
    Ch := Txt[I];
    Chaar := integer(ch);

    if Chaar = 32 then
    begin
    Ind := -1;
    CurX := CurX + SpaceWidth*Size;
    end
    else
    begin
    Ind := CharLookup[Chaar];
    end;

    if ind > -1 then
    begin
    CurX := CurX + F[Ind].A*Size;


    DrawQuad(CurX, Y, F[ind].Wid*Size, F[ind].Hgt*Size, 0.0, F[ind].x1,
    F[ind].x2, F[ind].y1, F[ind].y2);

    CurX := CurX + F[Ind].C*Size;
    end;
    end;
    end;
    [/code]
    This code works, but only in the perspective view. The font is displayed correctly: http://i44.tinypic.com/25sara0.png

    So what's wrong?

Page 2 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
  •