Monday, March 19, 2012

First Introduction to OpenGL

OpenGL logo
picture taken from: Wikipedia: OpenGL_logo.jpg
OpenGL is one of the most powerful graphic library. OpenGL has a great rivalry with DirectX (also know as Direct3D) from Windows in Application Programming Interfaces (APIs). Both of them can be used in application to render 2D and 3D computer graphics and giving acceleration from hardware graphics card when available.
Here is some of differentiation between OpenGL and Direct3D
Direct3D OpenGL
Platform Microsoft Windows Cross-Platform
Mobile Platform Direct3D Mobile OpenGL ES
License Proprietary Open Source or Trademark
OpenGL is an Open Source and Cross-Platform so you can develop an application without border of Operating System. In this section we are using Visual C++ 2010 as IDE in programming OpenGL. It is said that it is more easier using Windows Toolkit to programming OpenGL in C++. So lets continue..
1. Download GLUT you can take it here.
2. Copy the glut32.dll in C:\Windows\System32 or you can make a custom folder include the path.
3. Header glut,h can be placed in Visual Studio standard include folder. In Visual C++ 2010 you can get in:
C:\Program Files\Microsoft SDKs\Windows\v7.0A\include\gl
you’ll be also found gl.h in this folder, then you can just include it in your code
#include <gl/glut.h>
4. Copy file “glut32.lib” to the standard Visual Studio library, you can get it in
C:\Program Files\Microsoft SDKs\Windows\v7.0A\lib
5. Add additional dependencies in the project
Project Properties>>Linker>>Input>>Additional Dependencies

glut32.lib

glu32.lib

opengl32.lib

6. Lets try to make a simple program
Lets make a new console application project
#include <stdio.h>
#include <stdlib.h>
#include <gl/glut.h>
//make sure you take standard library first before glut,h
void mydisplay(){
       glClear(GL_COLOR_BUFFER_BIT); // clear the console
       glBegin(GL_POLYGON); //we will create a square
               glVertex2f(-0.5, -0.5);
               glVertex2f(-0.5, 0.5);
               glVertex2f(0.5, 0.5);
               glVertex2f(0.5, -0.5); //can you find the coordinat (0.0)
        glEnd();
        glFlush();
}
int main(int argc, char** argv)
        {
                printf("Hello World.. this is my first OpenGL program\n");
                printf("I can draw 2D Rectangle");
                glutCreateWindow("my_simple_program");
                glutDisplayFunc(mydisplay);
                glutMainLoop();
        }
Okay you’ll be see a white square with black background color.
image
Now you can explore more the powerful OpenGL APIs. I think one of the best tutorial OpenGL to know more the feature of OpenGL, lighthouse3d: http://www.lighthouse3d.com/tutorials/glut-tutorial/
Happy coding???

No comments:

Post a Comment