Tutorial 1: Getting Started with Our Framework and OpenGL for 2D and 3D Drawing, Assignments of Computer Graphics

An overview of a software framework for drawing geometry in 2d or 3d using opengl. It covers downloading and using the framework, understanding relevant opengl commands, and drawing 2d geometry. Students will focus on geometry and graphics rather than software engineering. The framework simplifies opengl interactions and spares students from the messier details.

Typology: Assignments

Pre 2010

Uploaded on 08/18/2009

koofers-user-ul8-1
koofers-user-ul8-1 🇺🇸

5

(1)

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Tutorial 1 : Our Framework and OpenGL
Fall 2005
Overview
Linked from the Homework and Labs page of the class web site is a zip file containing a
code framework you can use for your labs. The framework is a general platform for drawing
geometry in 2D or 3D through OpenGL. It has facilities for opening a single OpenGL window,
detecting keystrokes and mouse events, and entering text commands. These facilities should
be more than sufficient for the projects assigned this semester. We want you to focus on
geometry and graphics, not software engineering.
Another purpose of the software framework is to spare you the messier details of inter-
acting with OpenGL. OpenGL is a large and powerful API, with hundreds of options and
function calls. For the first two programming labs (and also for lab 0), you will only need
to call about a half-dozen different GL calls. The second part of this tutorial will cover the
GL interfaces you should be familiar with.
Our Framework
Download the software framework from the class web site. Inside the archive are several
files with the name Demo, each with a different suffix. These are Visual Studio’s support
files. The one you want to open is Demo.sln, which the Windows GUI labels as “Microsoft
Visual Studio Solution Object.” This is the main project file.
There are two subdirectories that contain source code. You should not need to worry
about the code in the Common/ directory. The Rendering/ directory contains three files:
OpenGLFrame.h: The header for the class that provides an interface to the GLUT
windowing library. Note that all functions are static, and can be called from any
context without a pointer to the actual OpenGLFrame object. The functions in this
class you might find useful are:
OpenGLFrame::printString(): print a string to the screen at the specified screen
coordinates.
OpenGLFrame::setOthographicProjection() and OpenGLFrame::resetPerspectiveProjection():
change drawing mode from 2d to 3d and back (see also the discussion in the second
half of the tutorial, below).
OpenGLFrame::getWidth() and OpenGLFrame::getHeight(): retrieve the dimen-
sions in pixels of the program’s window.
OpenGLFrame.cpp: The implementation of the OpenGLFrame class. You are free to
modify this, but you shouldn’t need to.
1
pf3
pf4

Partial preview of the text

Download Tutorial 1: Getting Started with Our Framework and OpenGL for 2D and 3D Drawing and more Assignments Computer Graphics in PDF only on Docsity!

Tutorial 1 : Our Framework and OpenGL

Fall 2005

Overview

Linked from the Homework and Labs page of the class web site is a zip file containing a code framework you can use for your labs. The framework is a general platform for drawing geometry in 2D or 3D through OpenGL. It has facilities for opening a single OpenGL window, detecting keystrokes and mouse events, and entering text commands. These facilities should be more than sufficient for the projects assigned this semester. We want you to focus on geometry and graphics, not software engineering.

Another purpose of the software framework is to spare you the messier details of inter- acting with OpenGL. OpenGL is a large and powerful API, with hundreds of options and function calls. For the first two programming labs (and also for lab 0), you will only need to call about a half-dozen different GL calls. The second part of this tutorial will cover the GL interfaces you should be familiar with.

Our Framework

Download the software framework from the class web site. Inside the archive are several files with the name Demo, each with a different suffix. These are Visual Studio’s support files. The one you want to open is Demo.sln, which the Windows GUI labels as “Microsoft Visual Studio Solution Object.” This is the main project file.

There are two subdirectories that contain source code. You should not need to worry about the code in the Common/ directory. The Rendering/ directory contains three files:

  • OpenGLFrame.h: The header for the class that provides an interface to the GLUT windowing library. Note that all functions are static, and can be called from any context without a pointer to the actual OpenGLFrame object. The functions in this class you might find useful are: - OpenGLFrame::printString(): print a string to the screen at the specified screen coordinates. - OpenGLFrame::setOthographicProjection() and OpenGLFrame::resetPerspectiveProjection(): change drawing mode from 2d to 3d and back (see also the discussion in the second half of the tutorial, below). - OpenGLFrame::getWidth() and OpenGLFrame::getHeight(): retrieve the dimen- sions in pixels of the program’s window.
  • OpenGLFrame.cpp: The implementation of the OpenGLFrame class. You are free to modify this, but you shouldn’t need to.
  • Callbacks.cpp: The functions called for rendering and responding to user input. This is the main file of the program, from your perspective.

You should read through the Callbacks.cpp file to get an idea of what the program can do. The order in which functions are called, and the commands you will need to draw geometry, are discussed in the next section.

Relevant OpenGL Commands

You can learn as much about OpenGL as you need to succeed in Comp 360 in about an hour. If you’re interested in more details about the OpenGL API, I recommend spending a week reading through the OpenGL Programming Guide (the “Red Book”)– there is a link with details about this book on the class web site. For now, you can get by with just the functions covered here.

The framework we provide calls the callback functions in this order:

call the initCallback(); Loop forever: clear the old frame draw the new frame: call setGluLookAt(); call renderFunc(); draw a framerate counter handle new user input (keystrokes, etc.) by calling appropriate callbacks call idleFunc();

The idea is that the location in 3D space of OpenGL’s camera is set in the setGluLookAt() function, and then everything is rendered in the ensuing call to renderFunc(). In many of your labs, you will want to draw 2D geometry, not 3D. To override the 3D camera and draw in 2D, you call the static function OpenGLFrame::setOrthographicProjection(); to return to 3D, call OpenGLFrame::resetPerspectiveProjection(). (Later on in the course, Dr. Goldman will explain the math behind projections. For now, you can equate ”orthographic” to 2 dimensions, and ”perspective” to 3 dimensions.)

Since your first two labs will deal entirely with 2 dimensional geometry, the provided framework code already calls setOrthographicProjection() and resetPerspectiveProjection() in the body of renderFunc(). For now, all your drawing code should fall between these two function calls.

You will notice another line of GL code in the body of renderFunc()– specifically, the call glDisable(GL LIGHTING). This tells OpenGL that we don’t want it to make dynamic lighting computations when it draws our geometry on the screen. Generally, lighting only makes sense in a 3D environment; for our 2D programs we will leave it disabled.

Once we have set an orthographic projection and disabled lighting, we are ready to draw geometry. The general method for drawing in OpenGL is as follows:

sions as the pixel dimensions of the window. The origin will be in the upper left-hand corner of the window, and the y-dimension will grow downwards. Thus, drawing a point at coordinates (100.0, 100.0) will generate a point 100 pixels below and 100 pixels to the right of the window’s upper-left corner.

  • I will grade labs based on program function (and, in some cases, program performance), but not on the quality of your source code. It is not necessary to exhaustively comment your code. However, I will be looking at your code, and if something does not work properly, I will make an effort to determine why. I am likely to be more patient and forgiving if I encounter code that is readable.