A look at the Open GL Code Open






















![Specifying vertices Syntax void gl. Vertex {234} {sifd} [v] (TYPEcoords); number of arguments Specifying vertices Syntax void gl. Vertex {234} {sifd} [v] (TYPEcoords); number of arguments](https://slidetodoc.com/presentation_image_h2/d4d92edb21cb68203a924b92e0f6f956/image-23.jpg)
![Specifying vertices Syntax void gl. Vertex {234} {sifd} [v] (TYPEcoords); type of arguments Specifying vertices Syntax void gl. Vertex {234} {sifd} [v] (TYPEcoords); type of arguments](https://slidetodoc.com/presentation_image_h2/d4d92edb21cb68203a924b92e0f6f956/image-24.jpg)
![Specifying vertices Syntax void gl. Vertex {234} {sifd} [v] (TYPEcoords); if vector Specifying vertices Syntax void gl. Vertex {234} {sifd} [v] (TYPEcoords); if vector](https://slidetodoc.com/presentation_image_h2/d4d92edb21cb68203a924b92e0f6f956/image-25.jpg)
![Specifying vertices Syntax void gl. Vertex {234} {sifd} [v] (TYPEcoords); the arguments Specifying vertices Syntax void gl. Vertex {234} {sifd} [v] (TYPEcoords); the arguments](https://slidetodoc.com/presentation_image_h2/d4d92edb21cb68203a924b92e0f6f956/image-26.jpg)
![Specifying vertices Syntax void gl. Vertex {234} {sifd} [v] (TYPEcoords); Examples gl. Vertex 2 Specifying vertices Syntax void gl. Vertex {234} {sifd} [v] (TYPEcoords); Examples gl. Vertex 2](https://slidetodoc.com/presentation_image_h2/d4d92edb21cb68203a924b92e0f6f956/image-27.jpg)







- Slides: 34

A look at the Open. GL Code Open. GL Programming Guide: Chapter 1: Introduction to Open. GL http: //www. opengl. org/resources/code/basics. html

Sample code (simplistic) #include <whatever. You. Need. h> main() { Initialize. AWindow. Please(); gl. Clear. Color (0. 0, 0. 0); gl. Clear (GL_COLOR_BUFFER_BIT); gl. Color 3 f (1. 0, 1. 0); gl. Ortho(0. 0, 1. 0, -1. 0, 1. 0); gl. Begin(GL_POLYGON); gl. Vertex 3 f (0. 25, 0. 0); gl. Vertex 3 f (0. 75, 0. 0); gl. Vertex 3 f (0. 25, 0. 75, 0. 0); gl. End(); gl. Flush(); Update. The. Window. And. Check. For. Events(); }

Sample code (simplistic) #include <whatever. You. Need. h> main() { Initialize. AWindow. Please(); gl. Clear. Color (0. 0, 0. 0); gl. Clear (GL_COLOR_BUFFER_BIT); gl. Color 3 f (1. 0, 1. 0); gl. Ortho(0. 0, 1. 0, -1. 0, 1. 0); gl. Begin(GL_POLYGON); gl. Vertex 3 f (0. 25, 0. 0); gl. Vertex 3 f (0. 75, 0. 0); gl. Vertex 3 f (0. 25, 0. 75, 0. 0); gl. End(); gl. Flush(); Update. The. Window. And. Check. For. Events(); } Open. GL does not do it!

Sample code (simplistic) #include <whatever. You. Need. h> main() { Initialize. AWindow. Please(); Clear color = black gl. Clear. Color (0. 0, 0. 0); gl. Clear (GL_COLOR_BUFFER_BIT); gl. Color 3 f (1. 0, 1. 0); gl. Ortho(0. 0, 1. 0, -1. 0, 1. 0); gl. Begin(GL_POLYGON); gl. Vertex 3 f (0. 25, 0. 0); gl. Vertex 3 f (0. 75, 0. 0); gl. Vertex 3 f (0. 25, 0. 75, 0. 0); gl. End(); gl. Flush(); Update. The. Window. And. Check. For. Events(); }

Sample code (simplistic) #include <whatever. You. Need. h> main() { Initialize. AWindow. Please(); Clear color = black gl. Clear. Color (0. 0, 0. 0); gl. Clear (GL_COLOR_BUFFER_BIT); Do the clearing gl. Color 3 f (1. 0, 1. 0); gl. Ortho(0. 0, 1. 0, -1. 0, 1. 0); gl. Begin(GL_POLYGON); gl. Vertex 3 f (0. 25, 0. 0); gl. Vertex 3 f (0. 75, 0. 0); gl. Vertex 3 f (0. 25, 0. 75, 0. 0); gl. End(); gl. Flush(); Update. The. Window. And. Check. For. Events(); }

Sample code (simplistic) #include <whatever. You. Need. h> main() { Initialize. AWindow. Please(); gl. Clear. Color (0. 0, 0. 0); gl. Clear (GL_COLOR_BUFFER_BIT); gl. Color 3 f (1. 0, 1. 0); Current color = white gl. Ortho(0. 0, 1. 0, -1. 0, 1. 0); gl. Begin(GL_POLYGON); gl. Vertex 3 f (0. 25, 0. 0); gl. Vertex 3 f (0. 75, 0. 0); gl. Vertex 3 f (0. 25, 0. 75, 0. 0); gl. End(); gl. Flush(); Update. The. Window. And. Check. For. Events(); }

Sample code (simplistic) #include <whatever. You. Need. h> main() { Initialize. AWindow. Please(); gl. Clear. Color (0. 0, 0. 0); gl. Clear (GL_COLOR_BUFFER_BIT); gl. Color 3 f (1. 0, 1. 0); gl. Ortho(0. 0, 1. 0, -1. 0, 1. 0); Specify 3 D view gl. Begin(GL_POLYGON); gl. Vertex 3 f (0. 25, 0. 0); gl. Vertex 3 f (0. 75, 0. 0); gl. Vertex 3 f (0. 25, 0. 75, 0. 0); gl. End(); gl. Flush(); Update. The. Window. And. Check. For. Events(); }

Sample code (simplistic) #include <whatever. You. Need. h> main() { Initialize. AWindow. Please(); gl. Clear. Color (0. 0, 0. 0); gl. Clear (GL_COLOR_BUFFER_BIT); gl. Color 3 f (1. 0, 1. 0); gl. Ortho(0. 0, 1. 0, -1. 0, 1. 0); gl. Begin(GL_POLYGON); Start polygon definition gl. Vertex 3 f (0. 25, 0. 0); gl. Vertex 3 f (0. 75, 0. 0); gl. Vertex 3 f (0. 25, 0. 75, 0. 0); gl. End(); gl. Flush(); Update. The. Window. And. Check. For. Events(); }

Sample code (simplistic) #include <whatever. You. Need. h> main() { Initialize. AWindow. Please(); gl. Clear. Color (0. 0, 0. 0); gl. Clear (GL_COLOR_BUFFER_BIT); gl. Color 3 f (1. 0, 1. 0); gl. Ortho(0. 0, 1. 0, -1. 0, 1. 0); gl. Begin(GL_POLYGON); Start polygon definition gl. Vertex 3 f (0. 25, 0. 0); gl. Vertex 3 f (0. 75, 0. 25, 0. 0); List vertices gl. Vertex 3 f (0. 75, 0. 0); gl. Vertex 3 f (0. 25, 0. 75, 0. 0); gl. End(); gl. Flush(); Update. The. Window. And. Check. For. Events(); }

Sample code (simplistic) #include <whatever. You. Need. h> main() { Initialize. AWindow. Please(); gl. Clear. Color (0. 0, 0. 0); gl. Clear (GL_COLOR_BUFFER_BIT); gl. Color 3 f (1. 0, 1. 0); gl. Ortho(0. 0, 1. 0, -1. 0, 1. 0); gl. Begin(GL_POLYGON); Start polygon definition gl. Vertex 3 f (0. 25, 0. 0); gl. Vertex 3 f (0. 75, 0. 25, 0. 0); List vertices gl. Vertex 3 f (0. 75, 0. 0); gl. Vertex 3 f (0. 25, 0. 75, 0. 0); gl. End(); End polygon definition gl. Flush(); Update. The. Window. And. Check. For. Events(); }

Sample code (simplistic) #include <whatever. You. Need. h> main() { Initialize. AWindow. Please(); gl. Clear. Color (0. 0, 0. 0); gl. Clear (GL_COLOR_BUFFER_BIT); gl. Color 3 f (1. 0, 1. 0); gl. Ortho(0. 0, 1. 0, -1. 0, 1. 0); gl. Begin(GL_POLYGON); gl. Vertex 3 f (0. 25, 0. 0); gl. Vertex 3 f (0. 75, 0. 0); gl. Vertex 3 f (0. 25, 0. 75, 0. 0); gl. End(); gl. Flush(); Flush the buffer Update. The. Window. And. Check. For. Events(); }

Sample code (simplistic) #include <whatever. You. Need. h> main() { Initialize. AWindow. Please(); gl. Clear. Color (0. 0, 0. 0); gl. Clear (GL_COLOR_BUFFER_BIT); gl. Color 3 f (1. 0, 1. 0); gl. Ortho(0. 0, 1. 0, -1. 0, 1. 0); gl. Begin(GL_POLYGON); gl. Vertex 3 f (0. 25, 0. 0); gl. Vertex 3 f (0. 75, 0. 0); gl. Vertex 3 f (0. 25, 0. 75, 0. 0); gl. End(); gl. Flush(); Update. The. Window. And. Check. For. Events(); } E. g. , wait a while

Sample code (simplistic) #include <whatever. You. Need. h> main() { Initialize. AWindow. Please(); Open. GL does not do it! Clear color = black gl. Clear. Color (0. 0, 0. 0); gl. Clear (GL_COLOR_BUFFER_BIT); Do the clearing gl. Color 3 f (1. 0, 1. 0); Current color = white gl. Ortho(0. 0, 1. 0, -1. 0, 1. 0); Specify 3 D view gl. Begin(GL_POLYGON); Start polygon definition gl. Vertex 3 f (0. 25, 0. 0); gl. Vertex 3 f (0. 75, 0. 25, 0. 0); List vertices gl. Vertex 3 f (0. 75, 0. 0); gl. Vertex 3 f (0. 25, 0. 75, 0. 0); gl. End(); End polygon definition gl. Flush(); Flush the buffer Update. The. Window. And. Check. For. Events(); } E. g. , wait a while

Callbacks A callback is a reference to executable code, or a piece of executable code, that is passed as an argument to other code. This allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer. (Wikipedia)

Sample code (2) Uses GLUT (GL utility toolkit) /* * Declare initial window size, position, and display mode * (single buffer and RGBA). Open window with "hello" * in its title bar. Call initialization routines. * Register callback function to display graphics. * Enter main loop and process events. */ int main(int argc, char** argv) { glut. Init(&argc, argv); Initialize GLUT + Open. GL stuff glut. Init. Display. Mode (GLUT_SINGLE | GLUT_RGB); glut. Init. Window. Size (250, 250); glut. Init. Window. Position (100, 100); glut. Create. Window ("hello"); gl. Clear. Color (0. 0, 0. 0); gl. Clear (GL_COLOR_BUFFER_BIT); gl. Ortho(0. 0, 1. 0, -1. 0, 1. 0); glut. Display. Func(display); Register callback glut. Main. Loop(); Enter main loop, process events return 0; /* ANSI C requires main to return int. */ }

Sample code (1) /* hello. c: introductory Open. GL program. */ #include <GL/glut. h> void display(void) { /* clear all pixels */ gl. Clear (GL_COLOR_BUFFER_BIT); /* draw white polygon (rectangle) with corners at (0. 25, 0. 0) and (0. 75, 0. 0) */ gl. Color 3 f (1. 0, 1. 0); gl. Begin(GL_POLYGON); gl. Vertex 3 f (0. 25, 0. 0); gl. Vertex 3 f (0. 75, 0. 0); gl. Vertex 3 f (0. 25, 0. 75, 0. 0); gl. End(); /* don't wait! */ gl. Flush (); }

Introducing more callbacks /* * Introduce callbacks on reshape and mouse events */ int main(int argc, char** argv) { glut. Init(&argc, argv); glut. Init. Display. Mode (GLUT_SINGLE | GLUT_RGB); glut. Init. Window. Size (250, 250); glut. Init. Window. Position (100, 100); glut. Create. Window ("hello"); gl. Clear. Color (0. 0, 0. 0); gl. Clear (GL_COLOR_BUFFER_BIT); gl. Ortho(0. 0, 1. 0, -1. 0, 1. 0); glut. Reshape. Func(reshape); Register reshape callback glut. Mouse. Func(mouse); Register mouse event callback glut. Display. Func(display); Register display callback glut. Main. Loop(); Enter main loop, process events return 0; }

Modifying the display() function void display(void) { /* clear all pixels */ gl. Clear (GL_COLOR_BUFFER_BIT); /* draw white polygon (rectangle) with corners at (0. 25, 0. 0) and (0. 75, 0. 0) */ gl. Color 3 f (1. 0, 1. 0); gl. Begin(GL_POLYGON); gl. Vertex 3 f (0. 25, 0. 0); gl. Vertex 3 f (0. 75, 0. 0); gl. Vertex 3 f (0. 25, 0. 75, 0. 0); gl. End(); printf("I am drawing!n"); gl. Flush (); }

Defining the mouse() function void mouse(int button, int state, int x, int y) { if (state == GLUT_DOWN) printf("Button %d pressed at x=%d, y=%dn", button, x, y); if (state == GLUT_UP) printf("Button %d released at x=%d, y=%dn", button, x, y); }

Defining the reshape() function void reshape(int w, int h) { gl. Viewport(0, 0, w, h); printf("New window size w=%d, h=%dn", w, h); }

callbacks. c • Show code • Run demo

Fundamentals of Open. GL programming Open. GL Programming Guide: Chapter 2
![Specifying vertices Syntax void gl Vertex 234 sifd v TYPEcoords number of arguments Specifying vertices Syntax void gl. Vertex {234} {sifd} [v] (TYPEcoords); number of arguments](https://slidetodoc.com/presentation_image_h2/d4d92edb21cb68203a924b92e0f6f956/image-23.jpg)
Specifying vertices Syntax void gl. Vertex {234} {sifd} [v] (TYPEcoords); number of arguments
![Specifying vertices Syntax void gl Vertex 234 sifd v TYPEcoords type of arguments Specifying vertices Syntax void gl. Vertex {234} {sifd} [v] (TYPEcoords); type of arguments](https://slidetodoc.com/presentation_image_h2/d4d92edb21cb68203a924b92e0f6f956/image-24.jpg)
Specifying vertices Syntax void gl. Vertex {234} {sifd} [v] (TYPEcoords); type of arguments
![Specifying vertices Syntax void gl Vertex 234 sifd v TYPEcoords if vector Specifying vertices Syntax void gl. Vertex {234} {sifd} [v] (TYPEcoords); if vector](https://slidetodoc.com/presentation_image_h2/d4d92edb21cb68203a924b92e0f6f956/image-25.jpg)
Specifying vertices Syntax void gl. Vertex {234} {sifd} [v] (TYPEcoords); if vector
![Specifying vertices Syntax void gl Vertex 234 sifd v TYPEcoords the arguments Specifying vertices Syntax void gl. Vertex {234} {sifd} [v] (TYPEcoords); the arguments](https://slidetodoc.com/presentation_image_h2/d4d92edb21cb68203a924b92e0f6f956/image-26.jpg)
Specifying vertices Syntax void gl. Vertex {234} {sifd} [v] (TYPEcoords); the arguments
![Specifying vertices Syntax void gl Vertex 234 sifd v TYPEcoords Examples gl Vertex 2 Specifying vertices Syntax void gl. Vertex {234} {sifd} [v] (TYPEcoords); Examples gl. Vertex 2](https://slidetodoc.com/presentation_image_h2/d4d92edb21cb68203a924b92e0f6f956/image-27.jpg)
Specifying vertices Syntax void gl. Vertex {234} {sifd} [v] (TYPEcoords); Examples gl. Vertex 2 s(2, 3); gl. Vertex 3 d(0. 0, 3. 1415926535898); gl. Vertex 4 f(2. 3, 1. 0, -2. 2, 2. 0); GLdouble dvect[3] = {5. 0, 9. 0, 1992. 0}; gl. Vertex 3 dv(dvect);

Open. GL drawing primitives gl. Begin(GL_POLYGON); gl. Vertex 2 f(0. 0, 0. 0); gl. Vertex 2 f(0. 0, 3. 0); gl. Vertex 2 f(4. 0, 3. 0); gl. Vertex 2 f(6. 0, 1. 5); gl. Vertex 2 f(4. 0, 0. 0); gl. End(); end of vertex-data list gl. Begin(GL_POINTS); gl. Vertex 2 f(0. 0, 0. 0); gl. Vertex 2 f(0. 0, 3. 0); gl. Vertex 2 f(4. 0, 3. 0); gl. Vertex 2 f(6. 0, 1. 5); gl. Vertex 2 f(4. 0, 0. 0); gl. End();

Open. GL drawing primitives

Valid and invalid polygons

A shortcut for rectangles void gl. Rect{sifd}(TYPEx 1, TYPEy 1, TYPEx 2, TYPEy 2); void gl. Rect{sifd}v(TYPE*v 1, TYPE*v 2);

Valid commands between gl. Begin() and gl. End() gl. Vertex*() set vertex coordinates gl. Color*() set current color gl. Index*() set current color index gl. Material*() set material properties gl. Edge. Flag*() control drawing of edges gl. Array. Element() extract vertex array data gl. Normal*() set normal vector coordinates gl. Tex. Coord*() set texture coordinates gl. Eval. Coord*() generate coordinates gl. Eval. Point*() generate coordinates gl. Call. List() execute display list gl. Call. Lists() execute display lists

A sample valid construct #define PI 3. 1415926535898 GLint circle_points = 100; gl. Begin(GL_LINE_LOOP); for (i = 0; i < circle_points; i++) { angle = 2*PI*i/circle_points; gl. Vertex 2 f(cos(angle), sin(angle)); } gl. End();

The end