lab 1-getting started opengl (1)

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). 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 Create Project

Upload: shazwan-hanis

Post on 06-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lab 1-Getting Started OpenGl (1)

8/3/2019 Lab 1-Getting Started OpenGl (1)

http://slidepdf.com/reader/full/lab-1-getting-started-opengl-1 1/13

OpenGl Lab Dr. Khalid Shaker Alubidi

1 Starting OpenGl

Before we can start

1. Windows comes with OpenGL, and Visual Studio comes with theOpenGL libraries, but neither of them comes with GLUT. Get the newestversion of GLUT here: GLUT 3.7.6 for Windows. 

2. lnstall Glut into the following directories:

glut32.dll -> C:\Windows\System or C:\WinNT\Systemglut32.lib -> C:\Program Files\Microsoft Visual Studio8\VC\PlatformSDK\libglut.h -> C:\Program Files\Microsoft Visual Studio8\VC\PlatformSDK\include\gl

Creating a new Project

1. From START menu, select:Programs -> Microsoft Visual Studio 2005 -> Microsoft VisualStudio 2005

2.  You are going to see this page (refer Figure 1).

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

CreateProject

Page 2: Lab 1-Getting Started OpenGl (1)

8/3/2019 Lab 1-Getting Started OpenGl (1)

http://slidepdf.com/reader/full/lab-1-getting-started-opengl-1 2/13

OpenGl Lab Dr. Khalid Shaker Alubidi

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 locationwhere you want to save your file – it is advisable for you to save yourfile in My Document or Desktop for easy access later by browsingthe 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). Thenclick Finish.

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

(a)

(b)

(c)

(d)

(e)

Page 3: Lab 1-Getting Started OpenGl (1)

8/3/2019 Lab 1-Getting Started OpenGl (1)

http://slidepdf.com/reader/full/lab-1-getting-started-opengl-1 3/13

OpenGl Lab Dr. Khalid Shaker Alubidi

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 towrite your C program.

2. In the Solution Explorer, right-click on Source File->Add->NewItem (refer Figure 5).

Figure 5

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

Tick here

Page 4: Lab 1-Getting Started OpenGl (1)

8/3/2019 Lab 1-Getting Started OpenGl (1)

http://slidepdf.com/reader/full/lab-1-getting-started-opengl-1 4/13

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 becauseautomatically, the file will be saved in the same directory as the Project(in your case it is MyProject)

Figure 6

4.  You will see 3 windows – Files and Classes Information, Editor andOutput. Files and Classes Information window will tell you thestructure of you project. Editor window is the place where you put/typeyour codes. Message window will tell you the message from thecompilation and building process. Refer Figure 7.

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

Editor 

Files andClasses

Information

Message Window: Compilation & Error Message

(a)

(b)

Page 5: Lab 1-Getting Started OpenGl (1)

8/3/2019 Lab 1-Getting Started OpenGl (1)

http://slidepdf.com/reader/full/lab-1-getting-started-opengl-1 5/13

OpenGl Lab Dr. Khalid Shaker Alubidi

Figure 7

5.  You are now ready to write your C program. You can copy and pastethe program below to you Editor window.

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 theprogram.

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

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

return 0;} // end of main()

Page 6: Lab 1-Getting Started OpenGl (1)

8/3/2019 Lab 1-Getting Started OpenGl (1)

http://slidepdf.com/reader/full/lab-1-getting-started-opengl-1 6/13

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

Page 7: Lab 1-Getting Started OpenGl (1)

8/3/2019 Lab 1-Getting Started OpenGl (1)

http://slidepdf.com/reader/full/lab-1-getting-started-opengl-1 7/13

OpenGl Lab Dr. Khalid Shaker Alubidi

9. Since you do not have any error you can link the program by buildingit. 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 thisexample you will see that you have 0 errors and 0 warning (referFigure 12).

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

Page 8: Lab 1-Getting Started OpenGl (1)

8/3/2019 Lab 1-Getting Started OpenGl (1)

http://slidepdf.com/reader/full/lab-1-getting-started-opengl-1 8/13

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

Page 9: Lab 1-Getting Started OpenGl (1)

8/3/2019 Lab 1-Getting Started OpenGl (1)

http://slidepdf.com/reader/full/lab-1-getting-started-opengl-1 9/13

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 andadd some runtime libraries. Many people are initially nervous aboutplaying with this aspect of their compiler since they are worried it willnever work the same again.

 Things to remember, all settings you change only affect the currentproject. All of the changes we are about to make will not affect anyother programs you have made in the past.

13. Use Visual Studio's menu Project options ( Project --> MyProjectProperties). Refer Figure 15.

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

Page 10: Lab 1-Getting Started OpenGl (1)

8/3/2019 Lab 1-Getting Started OpenGl (1)

http://slidepdf.com/reader/full/lab-1-getting-started-opengl-1 10/13

OpenGl Lab Dr. Khalid Shaker Alubidi

Figure 15

14.The Draw Property Page dialog will open. Once it appears, do thefollowing:

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

Page 11: Lab 1-Getting Started OpenGl (1)

8/3/2019 Lab 1-Getting Started OpenGl (1)

http://slidepdf.com/reader/full/lab-1-getting-started-opengl-1 11/13

OpenGl Lab Dr. Khalid Shaker Alubidi

b. In the left pane, select the linker subtree and then click the Input option. Addthe following code to the Additional Dependencies text in the right pane (Figure17)

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 asstated below :

i) include command

i. comprises of the important header files that needs tobe used in the program

ii) MyInit() function

i. This is where you will set the coordinate system, pointsize, background color and drawing color

iii) Display function

i. This is where you will create the image – there aremany types of images such as polygon, dots. Lines etc

iv) Main Function

i. This is where you will set the event managementfeature

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

Page 12: Lab 1-Getting Started OpenGl (1)

8/3/2019 Lab 1-Getting Started OpenGl (1)

http://slidepdf.com/reader/full/lab-1-getting-started-opengl-1 12/13

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

3. MyInit Function

4. Display Functionvoid display( void ){

glClear(GL_COLOR_BUFFER_BIT); //clear the windowglColor3f(1.0, 0.0, 0.0); // draw in red// Set up model/viewing transformationglMatrixMode(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() iscalled*/

glFlush(); /* clear buffers */

}

5. Main Function

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

#include <GL/glut.h>  // automatically includes GL.h and glu.h

#include <stdio.h>  // so we can do some output to the console

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);

}

Page 13: Lab 1-Getting Started OpenGl (1)

8/3/2019 Lab 1-Getting Started OpenGl (1)

http://slidepdf.com/reader/full/lab-1-getting-started-opengl-1 13/13

OpenGl Lab Dr. Khalid Shaker Alubidi

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

/* Standard GLUT initialization */glutInit(&argc,argv); //initialize GLUTglutInitDisplayMode (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