glut in MS Visual Studio 2010 (MSVS10)


One of my earlier blogs, OpenGL and glut in C++ with MS Visual Studio 2008 (MSVS9) described how to set up your Visual Studio and Computer to work with glut, as well as OpenGL. Now with the new edition, Microsoft Visual Studio 2010, as the version shifted from v6.0A to v7.0A, we need to change some path.So, here goes, what we need to do for using glut in MSVS10:

  • Download the original GLUT library from Nate Robins’ site
  • Unzip the download and copy files as instructed below,
    • glut.h to the folder C:\Program Files\Microsoft SDKs\Windows\v7.0A\Include\gl\
    • glut32.lib to the folder C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib\
    • glut32.dll to the folder C:\Windows\System32\
  • You are now ready to use GLUT!

All other things remain same as described on http://wp.me/puVgg-j

happy gluting, nJoy!

Advertisements

OpenGL, Tree as a fractal, trigonometric Approach

Our goal is to,

  • Draw a tree using fractals with recursion
  • Take user input for:
    • Depth of tree/sub-trees
    • Angle between leftmost and rightmost branch in same depth

Fractals are useful way of approaching to infinity and for drawing some real life like objects where same pattern recurs. To do so, we are drawing single line and then recurring sub trees.

Recursive definition of such trees might be,

A tree is a line from root point to tree top point followed by two sub-trees, each having the root at the top of parent tree and own top at new calculated point.

The idea of Fractal tree is to build it from two initial points,

  • Root of tree p1(x1,y1)
  • Tree top p2(x2,y2)

1

Fig 1: Draw the basis of tree

We draw this part by,

glBegin(GL_LINE_STRIP);
glVertex2f(x1,y1);// root of tree glVertex2f(x2,y2);//tree top glEnd();

 

Now, we should find the two points for each of the sub-trees. To do so, let us first find a point p3 on the line p1p2. This will be done by changing the co-ordinate by placing new origin at p1 and multiplying x2 and y2 by the same multiplication factor. Hence, if the distance between p1 and p2 be l12 and if the multiplication factor is m then we need to get p3 at l23 distant from p1 such that

It is depicted below in figure 2

2 copy

Fig 2: Find point 3

This is done by the code segment shown below,

x3=(x2-x1)*treeRatio+x1, y3=(y2-y1)*treeRatio+y1;

 

After that, we can find the tree top points of the sub-trees, p’3right and p’3left by rotating p3 in y-axis with respect to p2 by θ angle (right sub-tree) and – θ angle (left sub-tree) as done by figure 3 and the pseudo code below,

3 copy Fig 3: Find the recurring points

x3=x3-x2; y3-=y2;
X’3right =  x3 * cos(angle) + y3 * sin(angle) + x2;
Y’3right = -x3 * sin(angle) + y3 * cos(angle) + y2;
X’3left =  x3 * cos(-angle) + y3 * sin(-angle) + x2;
Y’3left = -x3 * sin(-angle) + y3 * cos(-angle) + y2;

 

Now, we can recursively call the function for drawing sub-trees, by decreasing depth by one.

Figure 4 below depicts a right sub-tree being called4 copy

Fig 4: Recursive call to draw right sub-tree

The code segment below, includes the definition of this recursive function in action,

Listing A

 

void tree(GLfloat x1, GLfloat y1,        //root of tree GLfloat x2, GLfloat y2,    //tree top GLfloat angle,//angle/2 between outermost branches of same level GLint n                //depth of subtree )
{

    glBegin(GL_LINE_STRIP);
    glVertex2f(x1,y1);// root of tree glVertex2f(x2,y2);//tree top glEnd();
    if(n<1)return ;        //stop criteria of recursion int nn = n-1;        //decrement GLfloat        //find P3 by x3=(x2-x1)*treeRatio+x1-x2, //translating co-ordinate system to p1 y3=(y2-y1)*treeRatio+y1-y2; //then multiplying p2 by the factor //and position it back to real co-ord //right subtree tree(x2,        y2,
         x3 * cos(angle) + y3 * sin(angle) + x2,// P'3 -x3 * sin(angle) + y3 * cos(angle) + y2,// angle,nn,branchCount);
    //left subtree tree(x2,        y2,
         x3 * cos(-angle) + y3 * sin(-angle) + x2,// P'3 -x3 * sin(-angle) + y3 * cos(-angle) + y2,
        angle,nn,branchCount);
}

 

Enhanced Branches

If we want to use more than two sub-trees, we can do it by enhancing the function from listing A, as depicted in listing B. (Changes are highlighted)

Listing B
void tree(GLfloat x1, GLfloat y1,        //root of tree GLfloat x2, GLfloat y2,    //tree top GLfloat angle,//angle/2 between outermost branches of same level GLint n,        //depth of subtree GLint branchCount)        //number of sub-trees per level {

    glBegin(GL_LINE_STRIP);
    glVertex2f(x1,y1);// root of tree glVertex2f(x2,y2);//tree top glEnd();
    if(n<1)return ;        //stop criteria of recursion int nn = n-1;        //decrement GLfloat            //find P3 by x3=(x2-x1)*treeRatio+x1-x2, //translating co-ordinate system to p1 y3=(y2-y1)*treeRatio+y1-y2; //and then multiplying p2 by the factor //and position it back to real co-ord if(branchCount==2){
        //right subtree tree(x2,        y2,
             x3 * cos(angle) + y3 * sin(angle) + x2,// P'3 -x3 * sin(angle) + y3 * cos(angle) + y2,// angle,nn,branchCount);
        //left subtree tree(x2,        y2,
             x3 * cos(-angle) + y3 * sin(-angle) + x2,// P'3 -x3 * sin(-angle) + y3 * cos(-angle) + y2,
            angle,nn,branchCount);
    }
 else { GLfloat nowAngle = -angle; GLfloat angleTone = angle/(branchCount-1)*2; for(int i=0;i<branchCount;i++,nowAngle+=angleTone){ tree(x2, y2, x3 * cos(nowAngle) + y3 * sin(nowAngle) + x2, -x3 * sin(nowAngle) + y3 * cos(nowAngle) + y2, angle,nn,branchCount); } }
}

Many objects have self-similarity. A fractal is a rough or fragmented geometric shape that can be subdivided in parts, each of which is (at least approximately) a smaller copy of the whole. These fractals can be utilized to model such self-similar objects in computer graphics. Recursive functions can be nicely used to design fractals with its self-similar parts. Design of such objects depends on devising a recursion and thus care should be taken to limit the bound and base criteria should be evaluated nicely.

Thanks for your time with me 🙂

Beginning Device-independent Graphics Programming with OpenGL (using GL, GLU & GLUT)

We have already set up GLUT and OpenGL [↗]. Now we can start writing codes from the ground up.

Create an Empty C++ Win32 Console application and add a C++ source file.

Add necessary library headers as follow:

#include <stdlib.h>
#include <GL/glut.h>

Note» alteration of order will cause generation of error message.

Now let us write the main function that will perform the required initializations and start the event-processing loop. All the functions in GLUT have the prefix glut and those, which perform some kind of initialization, have the prefix glutInit (GL has gl and GLU has glu prefixes as well).

Note» Please do not start coding until I say it is time.

At first, we initialize glut using the function,

void glutInit//init GLUT lib & negotiate a session with the window system.
int *argc,  //pointer to unmodified argc var of main
char **argv //pointer to unmodified argv var of main
);

Then establish the window’s position:

int x,    //number of pixels from the left of the screen
int y     //number of pixels from the top of the screen
);            //for both deafult: -1 (positioned by system)

Now, choose the window size:

int width, //widht of window
int height //height of window
);

Unavoidable! Define the display mode:

void glutInitDisplayMode(unsigned int mode);

There are options for selecting modes. See documentation for more. We will use following for most of the cases,

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

Have completed above steps, the window can be created like this,

int glutCreateWindow(char *title);

» Here, title is a string that will be shown as the title of the displayed window.

`

Everything is set up; the kitchen is ready! Now start cooking. Yep, but you at least have to draw something before you really do the “hello world” of glut. So, we need the define a drawing function and call it from main, with the following,

void glutDisplayFunc//sets the display callback for the current window.
void (*func)(void) //name of function to be called when window is redrawn
);

» It is illegal to pass a Null as function and the return type of the display function should be void.

`

One last thing, tell glut to enter the event processing loop by calling,

void glutMainLoop(void);

Now, it is time we write some code, an example follows

#include <GL/glut.h>

void renderScene(void) {

glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3f(-0.5,-0.5,0.0);
glVertex3f(0.5,0.0,0.0);
glVertex3f(0.0,0.5,0.0);
glEnd();
glFlush();
}

void main(int argc, char **argv) {

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);

glutInitWindowPosition(100,100);

glutInitWindowSize(320,320);

glutCreateWindow(“nafSadh- GLUT Tutorial”);

glutDisplayFunc(renderScene);

glutMainLoop();

}

O.k., you have written your first (may be) program in glut, it is time you do some artisanship. If you run the code above, two windows will come, one the console window, another is OpneGL window. When the OpenGL window is resized, the triangle is distorted. To solve this, we need to add a functionality that handles the resizing. The glut function for this is,

void glutReshapeFunc(void (*func)(int width, int height));

This should be called just before the main loop, an example follow,

#include <GL/glut.h>

void renderScene(void) {

… … …

}

void changeSize(int w, int h) {

// Prevent a divide by zero, when window is too short (you cant make a window of zero width).
if(h == 0)
h = 1;
float ratio = 1.0* w / h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,5.0,
0.0,0.0,-1.0,
0.0f,1.0f,0.0f);
}

void main(int argc, char **argv) {

… … …

glutDisplayFunc(renderScene);

glutReshapeFunc(changeSize);

glutMainLoop();

}

Of course, what I included here is inadequately sufficient, but hopefully enough for a QUICK START. Regardless of anyone else’s effort, I must first mention, Lighthouse 3D’s tutorial was most helpful for me. Beside these, following are some other links I must share:

OpenGL and glut in C++ with MS Visual Studio 2008 (MSVS9)

OpenGL support come with many IDEs, programming  environments and compilers. Likewise MSVS9 comes with built in support for OpenGL so that you can use all features of gl and glu libraries simply. To use glut support we need to download and add the glut support. Let us do both one by one.

OpenGL (gl and glu)

Creating Project
Creating Project

Hopefully you have already installed MS Visual Studio 2008 or MS Visual C++ 2008. If so, let us do the following,

  • Create an empty C++ Win32 application.
    • From File menu select New → Project (Ctrl+Shift+N).
    • Select Win32 Project, enter a Name, and click OK.
    • In the Wizard click Next, then check the box next to Empty Project, and click Finish.
  • Add a new C++ source file
    • Under the Project menu select Add New Item (Ctrl+Shift+A).
    • Select C++ File (.cpp), enter a Name, and click OK.
  • Link to the OpenGL libraries
    Linking Libs
    Linking Libs
    1. Under the Project menu select Project Properties (Alt+F7) at the bottom.
    2. Select Configuration Properties Linker Input from the navigation panel on the left.
    3. Select All Configurations from the Configuration drop-down box at the top of the dialog. This ensures you are changing the settings for both the Debug and Release configurations.
    4. Type “opengl32.lib glu32.lib” in Additional Dependencies and click OK.
  • Now, you can start coding. You can download GLDemo.cpp to begin with a demo that, if executed(Cntrl+F5), will show a window with blue triangle.

GLUT with a Win32 console application

Download and Install GLUT

Download and Install GLUT

 

Assuming, you already have MSVS9 installed, you have OpneGL libraries installed and placed in exact places. But, if one need to use the functionality of glut library it is necessary to download and install the very library. Please, do the following:

  • Download the original GLUT library from Nate Robins’ site
  • Unzip the download and copy files as instructed below,
    • glut.h to the folder C:\Program Files\Microsoft SDKs\Windows\v6.0A\Include\gl\
    • glut32.lib to the folder C:\Program Files\Microsoft SDKs\Windows\v6.0A\Lib\
    • glut32.dll to the folder C:\Windows\System32\
  • You are now ready to use GLUT!

N.B.: For Visual Studio 2010 check later post, glut in MS Visual Studio 2010 (MSVS10)

Working With GLUT

Now, have your GLUT set up, you can work with GLUT and OpenGL in whatever way you like. Meanwhile, let me place a simple demo:

  • From File menu select NewProject (Ctrl+Shift+N).
  • Select Win32 Console Application, enter a Name, and click OK.
  • In the Wizard click Next, then check the box next to Empty Project, and click Finish.
  • Add new source file (*.cpp) from the project menu [Source File → Add → New Item → Visual C++ → Code → C++ File (.cpp)] enter a Name and click add
  • You can use a demo file from here (GLUTdemo.cpp). If execute, you will see a triangle to appear; you can move it left[j] and right[l] ward and rotate/stop rotate[r] and exit app[Esc].
  • In a later post we will discuss coding in OpenGL and GLUT and make a simpler start-up demo, with short description on each line. [⇗]

It is all in short for now; I hope you this has been helpful. For introductory tutorial on programming OpenGL in C/C++ please try my next post in this category » Beginning Device-independent Graphics Programming with OpenGL (using GL, GLU & GLUT)


References
I would like to place some links that I found helpful, and also thank the authors hereby, as follow:
http://thoughtsfrommylife.com/article-748-OpenGL_and_Visual_Studio_Express_2008
http://www.opengl.org/
http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=220142
http://sourceforge.net/projects/freeglut/files/freeglut/2.4.0/freeglut-2.4.0.tar.gz/download
http://www.xmission.com/~nate/glut.html


I faced some problem in using OpenGL libraries and GLUT library and eventually found some solution from the web. Hence I am posting this blog to share some help in one place that may help other guys and girls.