Results 1 to 9 of 9

Thread: Drawing the letter N

  1. #1
    Member Contributor
    Join Date
    Oct 2008
    Posts
    63

    Drawing the letter N

    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
    Code :
    #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(&amp;argc, argv);
       glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
       glutInitWindowSize (700, 400); 
       glutInitWindowPosition (100, 100);
       glutCreateWindow (argv[0]);
       init ();
       glutDisplayFunc(display); 
     
       glutMainLoop();
       return 0;
    }

  2. #2
    Junior Member Newbie
    Join Date
    Sep 2008
    Location
    france
    Posts
    26

    Re: Drawing the letter N

    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.


  3. #3
    Senior Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362

    Re: Drawing the letter N

    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.)

    Code :
     
    #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,&amp;texID);
      glBindTexture(GL_TEXTURE_2D,texID);
      glTexEnvi(GL_TEXTURE_2D,GL_TEXTURE_ENV_MODE,GL_REPLACE);
      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_FILTER,GL_LINEAR);
      glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
      glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,2,2,0,GL_RGBA,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,indices);
      glDisable(GL_TEXTURE_2D);    
     
      glutSwapBuffers();
    }
     
     
     
    int main(int argc, char** argv)
    {
      glutInit(&amp;argc, argv);
      glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA);
      glutInitWindowSize (700, 400); 
      glutInitWindowPosition (100, 100);
      glutCreateWindow (argv[0]);
      init ();
      glutDisplayFunc(display); 
     
      glutMainLoop();
      return 0;
    }

    Have fun .

  4. #4
    Member Contributor
    Join Date
    Oct 2008
    Posts
    63

    Re: Drawing the letter N

    Is there any easy way of getting the vertext points or is it just a case of trial and error?

  5. #5
    Senior Member Regular Contributor trinitrotoluene's Avatar
    Join Date
    Sep 2008
    Location
    Montérégie,Québec
    Posts
    362

    Re: Drawing the letter N

    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.

  6. #6
    Junior Member Newbie
    Join Date
    Sep 2008
    Location
    france
    Posts
    26

    Re: Drawing the letter N

    nick2price : yes there is, just copy'n paste Nehe's code :

    http://nehe.gamedev.net/data/lessons....asp?lesson=14

  7. #7
    Member Contributor
    Join Date
    Oct 2008
    Posts
    63

    Re: Drawing the letter N

    I have done lines for the letter K.
    Code :
    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?

  8. #8
    Junior Member Newbie
    Join Date
    Sep 2008
    Location
    france
    Posts
    26

    Re: Drawing the letter N

    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' ?

  9. #9
    Member Contributor
    Join Date
    Oct 2008
    Posts
    63

    Re: Drawing the letter N

    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
    Code :
    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.

Similar Threads

  1. Simple Letter K with vertex
    By stonedunicorn in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 01-10-2018, 04:03 PM
  2. Drawing the letter A shape help
    By krazyb in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 02-24-2015, 11:24 AM
  3. Open Letter Update June 2012
    By webmaster in forum COLLADA
    Replies: 0
    Last Post: 06-06-2012, 09:57 AM
  4. Letter by letter text printing
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 03-30-2002, 05:57 AM
  5. How to draw 3d letter in Unix system?
    By lqiao in forum OpenGL: Advanced Coding
    Replies: 1
    Last Post: 10-15-2000, 09:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean