+ Reply to Thread
Results 1 to 2 of 2

Thread: Graphic Programming using Glut32 and C++

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    34

    Graphic Programming using Glut32 and C++

    Hi guys , can someone provide graphic programming example code using glut32 to draw graphical shapes?

  2. #2
    Junior Member
    Join Date
    Dec 2011
    Posts
    7
    First you must download glut32 libraries

    Then follow these steps after download the libraries to correctly import and use the glut32 libraries
    1- File >> New project >> Visual C++ (from project types) >> Win32 Console Application (from Templates) >> (Enter name of project) >> OK.
    2- A wizard will open , click next
    3- Choose Console Application (from Application Type) >> Empty project (from Additional options) >> Finish.
    4- From Solution Explorer: Right click on Source files >> Add >> New Item >> C++ file (.cpp) >> (Enter name of C++ file) >> Add.
    5- Right click on project name >> properties >> C/C++ >> Additional Include Directories >> import GL folder which is located inside Include from the
    files you just downloaded.
    6- Linker >> Additional Library Directories >> import lib folder which is located inside GL Files from the
    files you just downloaded >> Use Library Dependency Inputs >> (Yes) >> Ok
    7- Finally you must put glut32.dll, glut32.lib, glut.h in your project folder.

    After you have done all that , lets start with examples and codes.

    Example 1 : [Drawing a Point]
    -----------
    Code:
    #include "glut.h"
    void myDisplay();
    void myInit();
    void main(int ag,char** argv)
    {
        glutInit(& ag,argv);
        glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);    glutInitWindowSize(600,400);
        glutInitWindowPosition(100,100);
        glutCreateWindow("my window");
        glutDisplayFunc(myDisplay);
        myInit();
        glutMainLoop();
    }
    
    void myInit()
    {
        glClearColor(1.0,1.0,1.0,0.0);
        glLoadIdentity();    
    gluOrtho2D(0,600,0,400);
    }
    
    void myDisplay()
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glPointSize(7.0);
        glBegin(GL_POINTS);
        //First Point
        glColor3f(1.0,0.0,0.0);//Red
        glVertex2i(100,100);
        //Second Point
        glColor3f(0.0,1.0,0.0);//Green
        glVertex2i(300,300);
        glEnd();    
        glFlush();
    }
    Example 2 : [Drawing a Line]
    -----------
    Code:
    #include "glut.h" // to call open GL        
    void myDisplay(); // header
    void myInit();  // header
    void main(int ag,char** argv)
    {
        glutInit(& ag,argv);
        glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);    glutInitWindowSize(600,400);
        glutInitWindowPosition(100,100);
        glutCreateWindow("my window");
        glutDisplayFunc(myDisplay);
        myInit();
        glutMainLoop();
    }
    
    void myInit()
    {
        glClearColor(1.0,1.0,1.0,0.0);
        glLoadIdentity();
        gluOrtho2D(0,600,0,400);
    }
    
    void myDisplay()
    {
        glClear(GL_COLOR_BUFFER_BIT); // to clear the buffer
        glPointSize(7.0);
        glBegin(GL_LINES);
        //First Line
        glColor3f(1.0,0.0,0.0);//Red
        glVertex2i(100,300); // top
        glVertex2i(200,200); //botton
        //Second Line
        glColor3f(0.0,1.0,0.0);//Green
        glVertex2i(500,200); //top
        glVertex2i(400,100); //botton
        glEnd();
        glFlush();
    }
    Example 3 : [Draw Line Strip]
    -----------
    Code:
    #include "glut.h"
    void myDisplay();
    void myInit();
    void main(int ag,char** argv)
    {
        glutInit(& ag,argv);
        glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
        glutInitWindowSize(600,400);
        glutInitWindowPosition(100,100);
        glutCreateWindow("my window");
        glutDisplayFunc(myDisplay);
        myInit();
        glutMainLoop();
    }
    
    void myInit()
    {
        glClearColor(1.0,1.0,1.0,0.0);
        glLoadIdentity();
        gluOrtho2D(0,600,0,400);
    }
    
    void myDisplay()
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glColor3f(1.0,0.0,0.0);
        glPointSize(7.0);
        glBegin(GL_LINE_STRIP);
        glVertex2i(100,100);//A    
        glVertex2i(100,200);//B
        glVertex2i(200,300);//C
        glVertex2i(300,200);//D
        glVertex2i(200,100);//E
        glEnd();
        glFlush();
    }
    Example 4 : [Draw Line Loop]
    -----------
    Code:
    #include "glut.h"
    void myDisplay();
    void myInit();
    
    void main(int ag,char** argv)
    {
        glutInit(& ag,argv);
        glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
        glutInitWindowSize(600,400);
        glutInitWindowPosition(100,100);
        glutCreateWindow("my window");
        glutDisplayFunc(myDisplay);
        myInit();
        glutMainLoop();
    }
    
    void myInit()
    {
        glClearColor(1.0,1.0,1.0,0.0);
        glLoadIdentity();
        gluOrtho2D(0,600,0,400);
    }
    
    void myDisplay()
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glColor3f(1.0,0.0,0.0);
        glPointSize(7.0);
        glBegin(GL_LINE_LOOP);
        glVertex2i(100,300);
        glVertex2i(300,200);
        glVertex2i(200,400);
        glVertex2i(400,200);
        glVertex2i(200,100);    
        glEnd();
        glFlush();
    }

+ Reply to Thread

Tags for this Thread

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