OpenGL - HelloWorld

Discussion in 'Tutorials' started by SWH.PVT, Jan 29, 2012.

  1. SWH.PVT Lance Corporal

    Contributions:
    18
    This is a brief tutorial that will show you how to create a basic C program that utlizes OpenGL. If you want to follow along,
    I'll be using Microsoft Visual C++ (VC++) for this tutorial.

    I'm actually going to be using freeglut, which is an open source alternative to the OpenGL Utility Toolkit (GLUT) library,
    since GLUT is no longer maintained and there are distribution issues as well. So GLUT is for all intents and purposes
    dead! Fortunately, freeglut is open source and is intended to be a full replacement for GLUT. freeglut, similar to GLUT,
    allows the programmer to create and manage windows and to read keyboard and mouse input amongst other things.

    The latest stable release of freeglut was just released on January 02, and it is this version which I will be using. You can
    go ahead and download the zipped file now and save it to your computer. Extract the contents of the file as well.
    You will want to download the freeglut 2.8.0 MSVC Package.

    Now on with the example code. Below is a C program that will be used to utilize freeglut. You can copy this file and save it
    as something like HelloWorld.C

    Code:
    [SIZE=2][FONT=Consolas]#include <stdlib.h>[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]#include <GL/glut.h>[/FONT][/SIZE]
     
    [SIZE=2][FONT=Consolas]void display(void);[/FONT][/SIZE]
     
    [SIZE=2][FONT=Consolas]int main(int argc, char **argv)[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]{[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glutInit(&argc, argv);[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glutInitWindowSize(500, 400);[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glutInitWindowPosition(300, 100);[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glutCreateWindow("Hello, World!");[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glutDisplayFunc(&display);[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glutMainLoop();[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]return 0;[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]}[/FONT][/SIZE]
     
    [SIZE=2][FONT=Consolas]void display()[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]{[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glEnable(GL_DEPTH_TEST);[/FONT][/SIZE]
     
    [SIZE=2][FONT=Consolas]glMatrixMode(GL_PROJECTION);[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glLoadIdentity();[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glOrtho(-3.0, 3.0, -3.0, 3.0, -3.0, 3.0);[/FONT][/SIZE]
     
    [SIZE=2][FONT=Consolas]glMatrixMode(GL_MODELVIEW);[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glLoadIdentity();[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]gluLookAt(0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glScalef(.005,.005,.005);[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glTranslatef(-300, 0, 0);[/FONT][/SIZE]
     
    [SIZE=2][FONT=Consolas]glColor3f(255,255,0);[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glutStrokeCharacter(GLUT_STROKE_ROMAN, 'H');[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glutStrokeCharacter(GLUT_STROKE_ROMAN, 'e');[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glutStrokeCharacter(GLUT_STROKE_ROMAN, 'l');[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glutStrokeCharacter(GLUT_STROKE_ROMAN, 'l');[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glutStrokeCharacter(GLUT_STROKE_ROMAN, 'o');[/FONT][/SIZE]
     
    [SIZE=2][FONT=Consolas]glutStrokeCharacter(GLUT_STROKE_ROMAN, ',');[/FONT][/SIZE]
     
    [SIZE=2][FONT=Consolas]glutStrokeCharacter(GLUT_STROKE_ROMAN, 'W');[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glutStrokeCharacter(GLUT_STROKE_ROMAN, 'o');[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glutStrokeCharacter(GLUT_STROKE_ROMAN, 'r');[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glutStrokeCharacter(GLUT_STROKE_ROMAN, 'l');[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glutStrokeCharacter(GLUT_STROKE_ROMAN, 'd');[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glutStrokeCharacter(GLUT_STROKE_ROMAN, '!');[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]glutSwapBuffers();[/FONT][/SIZE]
    [SIZE=2][FONT=Consolas]}[/FONT][/SIZE]
    
    Now, open VC++ and start a new project. You'll want to create a Win32 console application starting from an empty project.
    Next, add the above program file to your project. If you created a text file out of the above code, then you can add the program
    by right-clicking the project name and adding an existing item.

    Now, locate the zipped file that you previously downloaded. We are going to use this now. Look inside and you will find three
    main sub-directories:

    1.) The necessary header files are located in the include directory.
    2.) 32 bit and 64 bit versions of the object file library are located in the lib directory.
    3.) 32 bit and 64 bit versions of the DLL's are provided in the bin folder.

    We need to include the apporiate directory for our header files. Follow these steps: Right click project name. Choose properties.
    Click the down arrow for the include directories option. Click edit and select a new line to find the above include sub-diretory.
    Select the include directory and then click the select folder button.

    Do the same thing for the library directories option but this time find and select the lib sub-directory.

    If you try to debug and execute the program now you'll find that you can't runt the program because of an error about a
    missing freeglut.dll file.

    We fix this by making sure that the appropriate DLL file from the bin directory is located in the same directory that the
    application (executable) is located. Move the 32 bit version into the directory where your application is located.

    For me my program is located here:

    C:\Users\James\Documents\Visual Studio 2010\Projects\HW\Debug

    So this is the directory where I'll be placing my DLL file.
    You should move the DLL file into whatever directory you have your application.

    For more information on freeglut and its API and what functions are available and what not, check this site out.
  2. cardevsan El Presidente

    Contributions:
    324
    Specialties:
    Scripting, Programming
    Processing:
    Graphics:
    PREMIUM
    I Donated
    Where is my tutorial for Basic??
    :)
    Or COBOL??
    I remember COBOL did have 5 divisions I think??
  3. SWH.PVT Lance Corporal

    Contributions:
    18
    HAHA! To be honest I've never used COBOL before.

    Maybe cards will write a tutorial? :cool:

    I have more experience with basic tho.
  4. cardevsan El Presidente

    Contributions:
    324
    Specialties:
    Scripting, Programming
    Processing:
    Graphics:
    PREMIUM
    I Donated
    From memory there is Identification Division
    Procedure,Data and two more.

    From what I remember(it has been 1983 since I took it in High School)
    Each Division you put the info in.

    Data goes itno Data
    Then you call your Procedure's from the Procedure one.

    For the life of me I can not remember the other 2??
    I can Google it,But that would cheating...LOL
  5. SWH.PVT Lance Corporal

    Contributions:
    18
    Have you used cobol since high school?

    Apparently cobol supports object-oriented programming now according to wikipedia.

    i just cheated :) and it looks like there are 4 divisions when i looked. the ones you mentioned plus the environment division.

Share This Page