Drawing text

Started by
7 comments, last by Zakwayda 15 years, 6 months ago
I am supposed to be drawing text in open gl using lines, quads, triangles or mixtures of the lot. Then i have to perform actions on this text like rotation etc. I have gotten a bit confused, to start i have drawn the letter N and i have done the following. glBegin(GL_POLYGON); //LETTER N, MADE FROM 3 SHAPES glVertex2f(50.0, 50.0); glVertex2f(50.0, 325.0); glVertex2f(50.0, 50.0); glVertex2f(85.0, 50.0); glVertex2f(85.0, 50.0); glVertex2f(85.0, 135.0); glVertex2f(85.0, 135.0); glVertex2f(50.0, 325.0); glEnd(); First off, this isnt 3d is it? How would i make it 3d? 2nd of all, if i perform a rotation on this letter, wont it rotate 3 seperate shapes as the letter is made up of 3 shapes. Is there anyway i can bind the 3 seperate polygons used in making the letter N, into 1 whole shape? Any advice would be greatly appreciated, or if you want to show me how you would draw this letter using polygons or triangles, please dont hesistate. Thanks very much
Advertisement
First of all, it sounds like this homework (correct me if I'm wrong). If that is the case, we can certainly give you some hints, but we can't do your work for you (nor should you want us to - how would that benefit you, exactly?).
Quote:
I have gotten a bit confused, to start i have drawn the letter N and i have done the following.

glBegin(GL_POLYGON); //LETTER N, MADE FROM 3 SHAPES

glVertex2f(50.0, 50.0);
glVertex2f(50.0, 325.0);

glVertex2f(50.0, 50.0);
glVertex2f(85.0, 50.0);

glVertex2f(85.0, 50.0);
glVertex2f(85.0, 135.0);

glVertex2f(85.0, 135.0);
glVertex2f(50.0, 325.0);

glEnd();
This is not a valid polygon (although you may accidentally be getting results that look somewhat correct). Take another look at whatever OpenGL reference you're using, and review how the different rendering primitives are defined in OpenGL.
Quote:First off, this isnt 3d is it? How would i make it 3d?
It is 3-d, but the vertices all lie in the XY plane (and therefore so will the rendered geometry, at least in local space). If you want fully 3-d geometry, you'll want to use 3-element vertices rather than 2-element vertices.

If you're referring to the 'dimension' of the shape (e.g. a plane is 2-d while a box is 3-d), that's a different subject entirely.
Quote:2nd of all, if i perform a rotation on this letter, wont it rotate 3 seperate shapes as the letter is made up of 3 shapes. Is there anyway i can bind the 3 seperate polygons used in making the letter N, into 1 whole shape?
All geometry submitted to OpenGL is transformed by the transform matrices that are currently at the top of the various matrix stacks. In this sense, all geometry rendered between changes to the matrix stack is implicitly 'bound together'; you don't need to do anything special to make this happen.
Its not homework, i am trying to get into open gl using c++ as i have a strong background in java and i want to try and start a new language.
I am a bit confused as to why it is not a valid polygon or what rendering primatives are (never had to deal with this in java). On looking at the code again, i was thinking its because i dont keep the polygon line going in a constant direction?
If you was to make the letter N, by drawing it yourself, would you be using polygons?
Quote:Original post by nick2price
I am a bit confused as to why it is not a valid polygon or what rendering primatives are (never had to deal with this in java). On looking at the code again, i was thinking its because i dont keep the polygon line going in a constant direction?
If you was to make the letter N, by drawing it yourself, would you be using polygons?
You need lines, not polygons. For one thing, polygons are closed, and for another, any number of vertices submited between glBegin and glEnd form a single polygon. Using lines will cause each pair of vertices to form a single line.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

For my letter k, i have used lines as so
glBegin(GL_LINES); //LETTER K

glVertex2f(165.0, 50.0);
glVertex2f(165.0, 305.0);

glVertex2f(165.0, 305.0);
glVertex2f(185.0, 305.0);

glVertex2f(185.0, 305.0);
glVertex2f(185.0, 195.0);

glVertex2f(185.0, 195.0);
glVertex2f(225.0, 305.0);

glVertex2f(225.0, 305.0);
glVertex2f(245.0, 305.0);

glVertex2f(245.0, 305.0);
glVertex2f(205.0, 175.0);

glVertex2f(205.0, 175.0);
glVertex2f(245.0, 50.0);

glVertex2f(245.0, 50.0);
glVertex2f(225.0, 50.0);

glVertex2f(225.0, 50.0);
glVertex2f(185.0, 160.0);

glVertex2f(185.0, 160.0);
glVertex2f(185.0, 50.0);

glVertex2f(185.0, 50.0);
glVertex2f(165.0, 50.0);

glEnd();

So the above is creating the letter k as a like "bubble" character (not just a stick k). So in turn it is basically a line loop. But then doing it this way, i cant fill in the area created with a colour, can i?
Quote:Original post by nick2price
So the above is creating the letter k as a like "bubble" character (not just a stick k). So in turn it is basically a line loop. But then doing it this way, i cant fill in the area created with a colour, can i?
Right, sorry - I didn't realise you were trying to create filled characters. If you aren't doing this for a homework assignment, there are many font rendering solutions already out there, which would avoid all this laborious hand coding.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Notice that in order to create a non convex polygons (as in many letter in the alphabet), you'll need to tesselate it to convex parts. You can use glu for this, but beware, as it is rather confusing (or you can triangulate it using a standard triangulation algorithm).
wow, too advanced wording for me. I am only a week into open gl. If i create a letter with a line loop, can i fill it in with a colour and make it 3d?
Quote:If i create a letter with a line loop, can i fill it in with a colour and make it 3d?
Not really. (Also, you still haven't clarified what you mean by '3d'.)

OpenGL doesn't have a concept of 'filling' (in the sense of, say, the 'fill' tool in Photoshop, or a filled path in Illustrator). OpenGL just has a few primitives that it understands. These are (more or less):

1. Points
2. Lines
3. Triangles

The different rendering primitives available (line strips, line loops, quads, polygons, etc.) are made up of the above three primitive types.

So no, you can't render a line loop and then fill it in to create a solid character. You can render the characters using triangles (or quads, or polygons), but it'll probably be kind of pain to define them manually.

There are other approaches you could take though. For example, you could define the characters using lines (line segments, actually), and then procedurally generate boxes or capsules built around the lines to give your characters a 'filled' look. If you're new to 3-d math though, this might prove difficult.

As has been mentioned, if the goal here is just to get text on the screen, then you should take a less painful approach (e.g. using a bitmapped font). If your goal is just to play around with OpenGL and to get familiar with the API, then I would suggest the following. Start by just rendering some simple geometry (e.g. a triangle, a square) using the different primitives OpenGL makes available (line strips, line loops, quads, etc.) to see how they are specified and how they look. Once you've done that, it'll probably be easier for you to figure out how to render a character and get it looking the way you want it to look.

This topic is closed to new replies.

Advertisement