Sie sind auf Seite 1von 13

OpenGl Lab

Dr. Khalid Shaker Alubidi

1 Starting OpenGl
Before we can start

1. Windows comes with OpenGL, and Visual Studio comes with the

OpenGL libraries, but neither of them comes with GLUT. Get the newest version of GLUT here: GLUT 3.7.6 for Windows.

2. lnstall Glut into the following directories:


glut32.dll -> C:\Windows\System or C:\WinNT\System glut32.lib -> C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\lib glut.h -> C:\Program Files\Microsoft Visual Studio 8\VC\PlatformSDK\include\gl Creating a new Project
1. From START menu, select:

Programs -> Microsoft Visual Studio 2005 -> Microsoft Visual Studio 2005
2. You are going to see this page (refer Figure 1).

Create Project

Figure 1
3. Go to Create: ->Click Project. 4. New Project window will pop-out (refer Figure 2).

Adapted from http://www.cecs.csulb.edu/~pnguyen/Using OpenGL in Visual Studio 2005.htm

OpenGl Lab

Dr. Khalid Shaker Alubidi

(b)

(a)

(c) (d) (e)

Figure 2
5. Choose Win32 for Project types: (refer Figure 2-(a)). For Templates:

choose Win32 Console Application (refer Figure 2- (b)). In Name: text box, type MyProject (refer Figure 2- (c)). Choose the location where you want to save your file it is advisable for you to save your file in My Document or Desktop for easy access later by browsing the location. Click OK.
6. In the next window, click Next (refer Figure 3).

Figure 3
7. Tick Empty Project for the Additional options: (refer Figure 4). Then

click Finish.

Adapted from http://www.cecs.csulb.edu/~pnguyen/Using OpenGL in Visual Studio 2005.htm

OpenGl Lab

Dr. Khalid Shaker Alubidi

Tick here

Figure 4
8. Now you have a project called MyProject.

Writing a File
1. After you create a project, you need to create a source file in order to

write your C program.


2. In the Solution Explorer, right-click on Source File->Add->New

Item (refer Figure 5).

Figure 5

Adapted from http://www.cecs.csulb.edu/~pnguyen/Using OpenGL in Visual Studio 2005.htm

OpenGl Lab

Dr. Khalid Shaker Alubidi

3. Add New Item Window will appear (refer Figure 6). Click on C++

File (.cpp) refer Figure 6 (a). In the Name: textbox, type Lab1 (refer Figure 6 (b)). DO NOT change the location of the file because automatically, the file will be saved in the same directory as the Project (in your case it is MyProject)

(a)

(b)

Figure 6
4. You will see 3 windows Files and Classes Information, Editor and

Output. Files and Classes Information window will tell you the structure of you project. Editor window is the place where you put/type your codes. Message window will tell you the message from the compilation and building process. Refer Figure 7.

Editor Files and Classes Information

Message Window: Compilation & Error Message

Adapted from http://www.cecs.csulb.edu/~pnguyen/Using OpenGL in Visual Studio 2005.htm

OpenGl Lab

Dr. Khalid Shaker Alubidi

Figure 7
5. You are now ready to write your C program. You can copy and paste

the program below to you Editor window.

int {

main( int argc, char **argv)

return 0; } // end of main()


6. Your screen should look something like this (Figure 8):

Figure 8
7. To compile the program, from the menu bar (Figure 9), select Build-

>Compile. Alternatively, you can press Ctrl+F7 to compile the program.

Adapted from http://www.cecs.csulb.edu/~pnguyen/Using OpenGL in Visual Studio 2005.htm

OpenGl Lab

Dr. Khalid Shaker Alubidi

Figure 9
8. Your Message window will tell the output of the compilation process.

For this example you will see that you have 0 errors and 0 warning (refer Figure 10).

Figure 10

Adapted from http://www.cecs.csulb.edu/~pnguyen/Using OpenGL in Visual Studio 2005.htm

OpenGl Lab

Dr. Khalid Shaker Alubidi

9. Since you do not have any error you can link the program by building

it. To link the program, from the menu bar (Figure 11), select Build>Build Solution. Alternatively, you can press F7 to link the program.

Figure 11
10. Your Message window will tell the output of the linking process. For this

example you will see that you have 0 errors and 0 warning (refer Figure 12).

Adapted from http://www.cecs.csulb.edu/~pnguyen/Using OpenGL in Visual Studio 2005.htm

OpenGl Lab

Dr. Khalid Shaker Alubidi

Figure 12
11. To run (execute) the program, from the menu bar, select Debug-

>Start Without Debugging. Alternatively, you can press Ctrl+F5 (refer Figure 13). Try running the above program and you should have produce something like the following Output window (Figure 14)

Adapted from http://www.cecs.csulb.edu/~pnguyen/Using OpenGL in Visual Studio 2005.htm

OpenGl Lab

Dr. Khalid Shaker Alubidi

Figure 13

Figure 14 12.Now we are going to dabble in some of the more advanced features of Visual C++. We need to play with some compiler/linker settings and add some runtime libraries. Many people are initially nervous about playing with this aspect of their compiler since they are worried it will never work the same again. Things to remember, all settings you change only affect the current project. All of the changes we are about to make will not affect any other programs you have made in the past.

13. Use Visual Studio's menu Project options ( Project

--> MyProject

Properties). Refer Figure 15.

Adapted from http://www.cecs.csulb.edu/~pnguyen/Using OpenGL in Visual Studio 2005.htm

OpenGl Lab

Dr. Khalid Shaker Alubidi

Figure 15 14.The Draw Property Page dialog will open. Once it appears, do the following:
a. Select the Configuration combo box, select All Configuration

(Figure 16)

Figure 16

Adapted from http://www.cecs.csulb.edu/~pnguyen/Using OpenGL in Visual Studio 2005.htm

OpenGl Lab

Dr. Khalid Shaker Alubidi

b. In the left pane, select the linker subtree and then click the Input option. Add the following code to the Additional Dependencies text in the right pane (Figure 17) Copy and Paste: opengl32.lib glu32.lib glut32.lib

Figure 17 c. Now Visual Studio knows where to find GLUT. Click OK button.

Writing an openGl Program 1. A openGL program comprises of 4 main section. They are as stated below : i) include command i. comprises of the important header files that needs to be used in the program ii) MyInit() function i. This is where you will set the coordinate system, point size, background color and drawing color iii) Display function i. This is where you will create the image there are many types of images such as polygon, dots. Lines etc iv) Main Function i. This is where you will set the event management feature

Adapted from http://www.cecs.csulb.edu/~pnguyen/Using OpenGL in Visual Studio 2005.htm

OpenGl Lab

Dr. Khalid Shaker Alubidi

ii. Set the size of the window iii. Position the window iv. Provide the name for the window 2. Include commands
// automatically includes GL.h and glu.h // so we can do some output to the console

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

3.

MyInit Function

void myinit(void) { /* Set up attributes */ glClearColor(1.0, 1.0, 1.0, 0.0); /* black background */ /* set up viewing projection*/ /* 500 x 500 window with origin lower left */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 500.0, 0.0, 500.0); glMatrixMode(GL_MODELVIEW);

}
4. Display Function
void display( void ) { glClear(GL_COLOR_BUFFER_BIT); //clear the window glColor3f(1.0, 0.0, 0.0); // draw in red // Set up model/viewing transformation glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // Draw whatever primitives are desired glBegin(GL_POINTS); glVertex2f(250, 250); glVertex2f(100,100); glVertex2f(375, 56); glEnd(); printf("executing display"); /*just to show when display() is called*/ glFlush(); /* clear buffers */

}
5. Main Function

Adapted from http://www.cecs.csulb.edu/~pnguyen/Using OpenGL in Visual Studio 2005.htm

OpenGl Lab

Dr. Khalid Shaker Alubidi

void main(int argc, char** argv) { /* Standard GLUT initialization */ glutInit(&argc,argv); //initialize GLUT glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /* default, not needed */ glutInitWindowSize(500,500); /* 500 x 500 pixel window */ glutInitWindowPosition(0,0); /* place window top left on display */ glutCreateWindow("Hello World OpenGL Program"); /* window title */ glutDisplayFunc(display); /* display callback invoked when window opened */ myinit(); /* set attributes, viewing parameters */ glutMainLoop(); /* enter event loop */

Adapted from http://www.cecs.csulb.edu/~pnguyen/Using OpenGL in Visual Studio 2005.htm

Das könnte Ihnen auch gefallen