Sunteți pe pagina 1din 3

SCHOOL of ELECTRICAL ENGINEERING & COMPUTER SCIENCE FACULTY of ENGINEERING & BUILT ENVIRONMENT The UNIVERSITY of NEWCASTLE Comp3320/Comp6370

Computer Graphics, Semester 2, 2011 Introduction to OpenGL - Tutorial Week 4


Prepared by Shashank Bhatia: shashank.bhatia@uon.edu.au

Outcome:
Know how to setup your OpenGL programming environment Compile and run sample code Understand basic building blocks of the code Modify sample code Some exercises to try out

What is OpenGL?
OpenGL is a hardware-independent software interface to graphics hardware. It consists of various commands that the user can use to generate graphical objects, and perform operations on them. However, this does not mean that one can use OpenGL to directly render highlevel models of three-dimensional objects (such as parts of body, airplanes, cars etc.). Instead, OpenGL allows one to build up desired model from a small set of geometric primitives (such as lines, points, curves, polygons etc.)

Before you begin


To start programming in OpenGL the user would require the following setup: Select a programming language (eg. Java/C++/Python) Obtain corresponding OpenGL libraries and headers suitable for selected language Setup the programming environment This tutorial will further dwell into the setup of the environment using Java and VC++

Environment setup and Sample program


Java On of the interface libraries that can be used to bundle OpenGL with Java is JoGL (Java OpenGL). To setup the environment to be used with Java, follow the steps provided:

1. Download the JoglEnvironment.zip le from blackboard website. (Tutorial Week 4 Programming Resources ) 2. Unzip the le to your U: drive The zip le will expand out to provide a few .bat les compile.bat is a windows shell script to compile the OpenGL program source run.bat will execute the compiled program To ensure that the right compiler is used, and right source is being compiled, make sure that these bat les contain correct paths, and source names. 3. Search for the absolute path of the java compiler located on the machine. You can do this by searching for javac.exe le. Usually, the path would be C:\JAVAJDK\bin 4. Modify the bat les and ensure they have correct path for the Java compiler 5. Download SampleCodeJava.zip le from blackboard, and unzip them in the same folder containing bat les. 6. Modify compile.bat and run.bat les to ensure they have the correct name of sample program that you would like to try (eg. SampleJOGL) 7. compile.bat will generate the java bytecode, and run.bat will execute the program This completes the environment setup for Java. Note that in order to have the same setup working on your own personal computers, you must have Java and OpenGL installed on them before trying to compile this code. C++ Standard Visual C++ would usually have all OpenGL libraries pre-installed and setup in them. On all machines in this lab, you will not have to do anything special to compile the code. Just download the SampleCodeC++.zip from blackboard website. Unzip the code in some folder. Open the project using VC++. If there is a warning about migrating the project to new version editor, accept it. Build and run the code. This is enough for Visual Studio licensed version. On the other hand, if you happen to use Visual Studio Express Edition, there is a little amount of work required to be done. Follow the following steps: 1. Download the VSEELibraries.zip le from blackboard website and unzip them into the Visual Studio directory. 2. Copy the glud.dll le into the system folder. 3. If you are unable to copy these les due to administrative privileges, then unzip the les into a separate folder under the C: drive, and make sure that Visual studio compiler is able to see them. To do this, goto Tools>Options>Projects and Solutions> VC++ Directories. Then add the lib and include directories here. You will also have to make sure that the glut.dll is in same directory as the executable generated by the compiler.

Understanding the code


There are three sections in which the entire code can be divided: 1. Arrangement of a display window 2. OpenGL initialisation 3. OpenGL drawing Arranging Display Window OpenGL is responsible for drawing the necessary graphics. But it does not handle any window related functions. Some of these include creating a window, handing window resize, starting and quitting from window etc. To this end, the header le windows.h is used. In the code, following functions do the necessary window creation, resize handling and clean up: 1. GLvoid ReSizeGLScene(GLsizei width, GLsizei height) 2. GLvoid KillGLWindow(GLvoid) 3. BOOL CreateGLWindow(char* title, int width, int height, int bits, bool fullscreenag) 4. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 5. int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,int nCmdShow) OpenGL Initialisation This section of code is mostly included in the int InitGL(GLvoid) function. This function sets the initial parameters of GL renderer. For example, it can be used to specify what color to use to clear the screen, turning on/o depth buer, enabling smooth shading etc. This routine is called once the OpenGL window is created by the windows related code. OpenGL Drawing Finally, in this section all drawing is done. Whatever you plan to display on the screen should be included in the int DrawGLScene(GLvoid) function. You can try and modify this section.

NEHE
More explanation of these functions can be derived from the comments provided in the source code, and NEHE Lesson 01 and Lesson 02. NEHE has an excellent collection of tutorials to get you going in programming your group project. The current VC++ sample programs was adapted from NEHE website. I suggest that you experiment with a few of the tutorials and get a grasp of the essential concepts that you want incorporated into your group project early on in the semester. Follow this link for Lesson 01 to 05. The website will provide detailed explanation of each line of code. http://nehe.gamedev.net/tutorial/lessons_01__05/22004/ Once again, remember to reference particular tutorials on NEHE or any other sites which your programming codes are based on.

S-ar putea să vă placă și