OpenSource For You

The ‘Hello World’ first step

-

1Rw IRr WKH FOassLF firsW-WLPH SrRgraP. 2SHn WKH HGLWRr, anG enter the following code:

@include<diLglut.h> void main(int argc, char**argv) { glutfnit(&argc, argv); glutfnitti­ndowmositi­on(1MM,1MM); glutfnitti­ndowpize(5MM,5MM); glutCreate­tindow(“eello torld”); glutMainio­op();

}

SavH WKH fiOH as ‘basic.c’, and to compile it, run ‘gcc basic.c -l glut’ at the terminal. Run the compiled binary with ‘./a.out’. Here, we have imported glut.h (in the GL folder). To locate it, run ‘whereis GL’ and it will give you the installed location, like GL: /usr/include/GL. TKHrH arH MusW fivH basLF IunFWLRns usHG WR FrHaWH a wLnGRw IRr GrawLng. TKH firsW, glutInit(), initialise­s the display. Then glutInitWi­ndowPositi­on (int x, int y) sSHFLfiHs WKH sFrHHn ORFaWLRn (uSSHr-OHIW FRrnHr) of the window. Next, glutInitWi­ndowSize(int x, int y) sSHFLfiHs WKH sLzH RI WKH wLnGRw; glutCreate­Window(char *string) naPHs WKH wLnGRw IRr LGHnWLfiFa­WLRn, anG FrHaWHs LW; andGlutMai­nLoop() is used to display the window and begin event processing. Figure 1 shows you how it appears.

TKH FRPSLOaWLR­n FRPPanG sSHFLfiHG -l glut, which means, a OLnN wLWK WKH gOuW OLbrary fiOH. By GHIauOW, GCC naPHs WKH RuWSuW compiled binary ‘a.out’; to change it, specify the -o (RuWSuW fiOH name) switch—for example, ‘gcc begin.c -l glut -o hello’. @include<diLglut.h> @include<diLgl.h> void display() { glClearCol­or(1,M,M,M); glClear(di_Clilo__rccbo__fT); glclush(); } void main(int argc, char**argv) { glutfnit(&argc, argv); glutfnitti­ndowmositi­on(1MM,1MM); glutfnitti­ndowpize(5MM,5MM); glutCreate­tindow(“eello torld”); glutaispla­ycunc(display); glutMainio­op();

}

Save it as ‘basic2.c’, compile and run it as in the earlier example. The result is shown in Figure 2.

The three GL functions we used to colour the window background are: glClearCol­or(R, G, B, alpha): This is used to set the colour for a window. The numbers you can use for each colour are bHWwHHn 0 anG 1; yRu Fan usH flRaW nuPbHrs OLNH 0.1, 0.11, HWF. glClear(GL_COLOR_BUFFER_BIT): This clears the screen to the desired colour set by glClearCol­or. glFlush() executes the commands to the screen rather than storing it in a buffer. In the next part of the series, I plan to explore how to draw lines and triangles, and use keyboard inputs.

 ??  ??

Newspapers in English

Newspapers from India