Visual C++ and OpenGL Programming Tutorial: Creating a Simple Window, Study notes of Computer Graphics

A step-by-step tutorial on creating a simple opengl window using visual c++ and the opengl utility toolkit (glut). It covers setting up the visual c++ environment, entering and compiling the code, and executing the program. The tutorial also includes instructions on how to handle errors and execute the program.

Typology: Study notes

Pre 2010

Uploaded on 03/28/2010

koofers-user-7bd-1
koofers-user-7bd-1 🇺🇸

5

(1)

9 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Visua l C+ + Tut o r i a l
CSE 1 6 7 , W in t er 2 0 03
Fa r h an a Ba n d u kw al a
One of t h e best way s t o begi n u n der st andin g t he st r u ct u r e and st yle o f
a t y pica l Op en g GL program is t o en t er, com pile, and r u n a sm all
ex am ple. Th e l ist in g belo w cont ains a sim ple p r ogr am w h ich u ses
Ope nGL Utilit y Toolk it ( GLUT) t o o pen a w ind ow. I f t hi s is t he fir st
tim e y ou’ v e seen t his sor t of p r ogr am , it pr obably w ill n ot m ake a lot
of sen se in itially. Don t wor r y abo ut t hat . For now , t h e g oal i s t o u se
the Visual C+ + en v iron m en t t o create, com p il e and ex ecu t e t hi s
sim ple p r ogr am .
//simplewindow.cpp
#include <windows.h>
#include <gl\glut.h>
// Called to draw scene
void renderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//drawing code here
glFlush();
}
void initState(void)
{
glClearColor(0.0f,0.0f,0.0f,1.0f);
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("Simple Window");
glutDisplayFunc(renderScene);
initState();
glutMainLoop();
pf3
pf4
pf5

Partial preview of the text

Download Visual C++ and OpenGL Programming Tutorial: Creating a Simple Window and more Study notes Computer Graphics in PDF only on Docsity!

Visua l C+ + Tut or ia l CSE 1 6 7 , W int e r 2 0 0 3 Fa r ha na Ba nduk w a la

One of t he best ways t o begin underst anding t he st ruct ure and st yle of a t ypical OpengGL program is t o ent er, com pile, and run a sm all exam ple. The list ing below cont ains a sim ple program which uses OpenGL Ut ilit y Toolkit ( GLUT) t o open a window. I f t his is t he first t im e you’ve seen t his sort of program , it probably will not m ake a lot of sense init ially. Don’t worry about t hat. For now, t he goal is t o use t he Visual C+ + environm ent t o creat e, com pile and execut e t his sim ple program.

//simplewindow.cpp #include <windows.h> #include <gl\glut.h>

// Called to draw scene void renderScene(void) { // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//drawing code here

glFlush(); }

void initState(void) { glClearColor(0.0f,0.0f,0.0f,1.0f); }

int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutCreateWindow("Simple Window"); glutDisplayFunc(renderScene);

initState();

glutMainLoop();

This sm all program does t hree t hings. First it init ializes t he glut library, and creat es a window. Next , it set s up t he display callback ( renderScene) and last it init iat es t he m ain event loop ( glut MainLoop). We will discuss t he det ails of t his program st ruct ure in subsequent classes. For now, t his helps you set up t he Visual C+ + environm ent and get a piece of OpenGL code com piled.

Tut orial St eps:

  1. St art VC+ + by double clicking on it s icon in t he Visual C+ + group of t he Program Manager.
  2. I f VC+ + has been used before on t his m achine, it is possible for it t o com e up in several different st at es because VC+ + rem em bers and aut om at ically reopens t he proj ect and files in use t he last t im e it exit ed. What we want right now is a st at e where it has no proj ect or code loaded. I f t he program st art s wit h a dialog t hat says it was unable t o find a cert ain file, clear t he dialog by clicking t he " No" but t on.
  3. Go t o t he W indow m enu and select t he Close All opt ion if it is available. Go t o t he File m enu and select t he Close opt ion if it is available t o close any rem aining windows. Now you are at t he proper st art ing point.

What you see now is " norm al". Along t he t op is t he m enu bar and several t oolbars. Along t he bot t om is a st at us window where various m essages will be displayed.

  1. To creat e a new proj ect for sim plewindow.cpp, choose t he N e w opt ion in t he File m enu. Under t he Pr oj e ct s t ab, highlight W in3 2 Console Applica t ion. I n t he Locat ion field t ype an appropriat e pat h nam e or click t he Browse but t on. Type t he word " sim plewindow" in for t he proj ect nam e, and you will see t hat word echoed in t he Locat ion field as well. Click t he OK but t on.
  2. I n t he next window, use t he default select ion " An em pt y proj ect " , click " Finish" , t hen click " OK" once m ore in t he next window.
  3. The area along t he left side of t he screen will now change so t hat t hree t abs are available. The I nfoView t ab is st ill t here, but t here is now also a ClassView and a FileView t ab. The ClassView t ab
  1. Having creat ed t he proj ect file and adj ust ed t he set t ings, you are ready t o com pile t he sim plewindow.cpp program. I n t he Build m enu you will find t hree different com pile opt ions:
    1. Com pile sim plewindow.cpp ( only available if t he t ext window for sim plewindow.cpp has focus)
    2. Build sim plewindow.exe
    3. Rebuild All

The first opt ion sim ply com piles t he source file list ed and form s t he obj ect file for it. This opt ion does not perform a link, so it is useful only for quickly com piling a file t o check for errors. The second opt ion com piles all of t he source files in t he proj ect t hat have been m odified since t he last build, and t hen links t hem t o form an execut able. The t hird opt ion recom piles all of t he source files in t he proj ect and relinks t hem. I t is a " com pile and link from scrat ch" opt ion t hat is useful aft er you change cert ain com piler opt ions or m ove t o a different plat form.

  1. I n t his case, choose t he Build sim ple w indow .e x e opt ion in t he Build m enu t o com pile and link t he code. Visual C+ + will creat e a new subdirect ory nam ed Debug and place t he execut able nam ed sim plewindow.exe in t hat new subdirect ory. This subdirect ory holds all disposable ( easily recreat ed) files generat ed by t he com piler, so you can delet e t his direct ory when you run short on disk space wit hout fear of losing anyt hing im port ant.
  2. I f you see com piler errors, sim ply double click on t he error m essage in t he out put window. The edit or will t ake you t o t hat error. Com pare your code against t he code above and fix t he problem. I f you see a m ass of linker errors, it probably m eans t hat you specified t he proj ect t ype incorrect ly in t he dialog used t o creat e t he proj ect. You m ay want t o sim ply delet e your new direct ory and recreat e it again following t he inst ruct ions given above exact ly.
  3. To execut e t he program , choose t he Ex e cut e sim ple w indow .e x e opt ion in t he Build m enu. A window appears. Not e t hat t he program is com plet e. You can m ove t he window, re- size it , m inim ize it , and cover and uncover it wit h ot her windows.
  4. To t erm inat e t he program , click on it s syst em m enu ( t he sm all box t o t he left of t he t it le bar) and select t he Close opt ion.