View Full Version : Drawing the letter N
nick2price
10-09-2008, 07:44 AM
I am a total newbie, 1 week in, so please ignore the lack of knowledge. I am trying to draw the letter N made out of either lines, polygons or triangles. I didnt want to do the easy 3 straight lines, so i am instead attempting to do it more fancy. So i decided to try polygons, but the problem is my N doesnt look very good and the code is problably all wrong. I would be so greatful if anyone could take a look at my code and advise or help me in making improvements to both the code and the letter N. Thanks
#include "stdafx.h"
#include "glut.h"
#include <stdlib.h>
void init(void)
{
glClearColor (0.0, 0.0, 0.85, 0.0);
glMatrixMode (GL_PROJECTION); //Viewport
gluOrtho2D( 0.0,400.0,0.0,400.0); //Viewport, i.e., camera position.
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (0.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glLineWidth (3.0);
glVertex2f(50.0, 50.0);
glVertex2f(50.0, 325.0);
glColor3f (1.0, 0.0, 0.0);
glVertex2f(50.0, 50.0);
glVertex2f(85.0, 50.0);
glColor3f (2.0, 0.0, 0.0);
glVertex2f(85.0, 50.0);
glVertex2f(85.0, 135.0);
glColor3f (3.0, 0.0, 0.0);
glVertex2f(85.0, 135.0);
glVertex2f(50.0, 325.0);
glColor3f (4.0, 0.0, 0.0);
glEnd();
glColor3f (0.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex2f(100.0, 50.0);
glVertex2f(50.0, 325.0);
glColor3f (1.0, 0.0, 0.0);
glVertex2f(50.0, 325.0);
glVertex2f(145.0, 50.0);
glColor3f (2.0, 0.0, 0.0);
glVertex2f(145.0, 50.0);
glVertex2f(100.0, 50.0);
glEnd();
glColor3f (0.0, 0.0, 0.0);
glBegin(GL_POLYGON);
glVertex2f(145.0, 50.0);
glVertex2f(145.0, 325.0);
glColor3f (3.0, 0.0, 0.0);
glVertex2f(145.0, 325.0);
glVertex2f(145.0, 325.0);
glColor3f (2.0, 0.0, 0.0);
glVertex2f(145.0, 325.0);
glVertex2f(105.0, 166.0);
glColor3f (1.0, 0.0, 0.0);
glVertex2f(105.0, 166.0);
glVertex2f(145.0, 50.0);
glEnd();
glFlush ();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (700, 400);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
A. Masserann
10-09-2008, 08:42 AM
screenshot please ?
glLineWidth (3.0); has nothing to do here since you are rendering polygons, and moreonver it's illegal to put it between glBegin/glEnd.
You shoud rather use triangles strips, it's more difficult to "draw by hand" but it's faster
While you're at it, use Display Lists, arrays or VBOs ( avoid glBegin/glEnd, they are handful but slow )
And I believe that in glut you have some utility functions to draw letters. Or is it in wgl ?
glFlush is useless, SwapBuffers will do it anyway.
:)
trinitrotoluene
10-09-2008, 10:06 AM
The values for glColor3f must be between 0.0 and 1.0 . Here is a sample code which use texturing. The letter N appear a little better than your (can be arguable). Play with the code (change texture coordinates, change texture image ... etc.)
#include <GL/glut.h>
#include <stdlib.h>
GLuint texID;
GLubyte texdata[] =
{
255,0,0,255, 255,0,0,255,
0,0,0,255, 0,0,0,255
};
GLfloat vertices[] =
{
50.0,50.0,
50.0,325.0,
85.0,50.0,
85.0,135.0,
100.0,50.0,
145.0,50.0,
145.0,325.0,
100.0,175.0
};
GLfloat texture_coords[] =
{
0.8,0.8,
0.2,0.2,
0.8,0.0,
0.6,0.6,
0.8,0.8,
0.8,0.0,
0.5,0.5,
0.8,0.2
};
GLfloat colors[] =
{
0.0,0.0,0.0,
1.0,0.0,0.0,
1.0,0.0,0.0,
0.33,0.0,0.0,
0.0,0.0,0.0,
1.0,0.0,0.0,
0.5,0.0,0.0,
1.0,0.0,0.0
};
GLuint indices[] = {0,2,1,2,1,3,1,3,7,4,3,7,4,7,5,7,6,5};
void init(void)
{
glEnable(GL_DEPTH_TEST);
glClearColor (0.0, 0.0, 0.85, 0.0);
glMatrixMode (GL_PROJECTION); //Viewport
gluOrtho2D( 0.0,400.0,0.0,400.0); //Viewport, i.e., camera position.
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// glEnableClientState(GL_COLOR_ARRAY);
glGenTextures(1,&texID);
glBindTexture(GL_TEXTURE_2D,texID);
glTexEnvi(GL_TEXTURE_2D,GL_TEXTURE_ENV_MODE,GL_REP LACE);
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_MAG_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,2,2,0,GL_RGB A,GL_UNSIGNED_BYTE,texdata);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//glColor4f(1.0,1.0,0.0,1.0);
glEnable(GL_TEXTURE_2D);
glVertexPointer(2,GL_FLOAT,0,vertices);
glTexCoordPointer(2,GL_FLOAT,0,texture_coords);
//glColorPointer(3,GL_FLOAT,0,colors);
glDrawElements(GL_TRIANGLES,18,GL_UNSIGNED_INT,ind ices);
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize (700, 400);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Have fun :) .
nick2price
10-09-2008, 11:17 AM
Is there any easy way of getting the vertext points or is it just a case of trial and error?
trinitrotoluene
10-09-2008, 12:09 PM
One solution is to use a 3D modeler like Blender but you will need to find or write code to load the the resulting model data (letter_N.obj,letter_N.3ds or whatever format the 3D modeler is able to write) in plain OpenGL. If you use a scene graph like OpenSceneGraph, loading 3d models is easy because OSG support it via osgDB. An other solution is the obvious pencil and paper.
A. Masserann
10-09-2008, 12:23 PM
nick2price : yes there is, just copy'n paste Nehe's code :
http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=14
nick2price
10-09-2008, 01:48 PM
I have done lines for the letter K.
glColor3f (0.0, 0.0, 0.0);
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();
If i change it to polygon to much of the K is coloured in. (The bottom right point of the k seems to create an area with the bottom left point of the k. Is there anyway to colour in the area which is created by lines?
A. Masserann
10-09-2008, 02:06 PM
OpenGL only handles convex polygons ( check wikipedia if you're unsure about convexity )
You'll have to split your polygon into a few more triangles, no options.
glut provides utility functions for that, but they are a bit tricky to play with.
PS : will the next one be a '2' ? :p
nick2price
10-09-2008, 04:32 PM
I have decided to start very basic now, and just draw my letters with lines. This is what i have done for the letter N
glBegin(GL_LINES); //LETTER N
glVertex3f(50.0, 50.0, 0.0);
glVertex3f(50.0, 250.0, 0.0);
glVertex3f(50.0, 250.0, 0.0);
glVertex3f(80.0, 50.0, 0.0);
glVertex3f(80.0, 50.0, 0.0);
glVertex3f(80.0, 250.0, 0.0);
glEnd();
Now can i still do stuff with lines for example make them all fly about the place and on a button click the word will form. How is somthing like this done? or could anyone give me an example of somthing fun i could do with the lines above.
Powered by vBulletin® Version 4.2.0 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.