OpenGL-01

parakeetincurableΛογισμικό & κατασκευή λογ/κού

13 Δεκ 2013 (πριν από 3 χρόνια και 5 μέρες)

44 εμφανίσεις

Today …


The rendering pipeline in detail


What is OpenGL


First OpenGL program


Details about modeling

Basic Rendering Pipeline









Project
ion

Illuminat
ion


Rasteriz
ation

&

Clipping

&

Display




1
V

1
R

N

L





Database of 3D models

Modeling
Transform
ation

(OS
-
>
WS)

Visibility

Culling

Viewing
Transfor
mation

(WS
-
>
CS)

OpenGL and GLUT Overview


What is OpenGL & what can it do for me?


OpenGL in windowing systems


Why GLUT


A GLUT program template

What is a “library”.


Static


Dynamic


OpenGL applications use the window system’s window, input, and
event mechanism


GLU supports quadrics, NURBS, complex polygons, matrix
utilities, and more


Relationship between OpenGL GLU and windowing APIs.

What Is OpenGL?


Graphics rendering API


high
-
quality color images composed of
geometric and image primitives


window system independent


operating system independent

OpenGL Library Functions

GLU

GL

GLUT

GL library contains all primitive and attribute functions

associated with OpenGL

GLU library builds on the GL library to include more

complex primitives (e.g. spheres) and convenience functions

GLUT (GL Utility Toolkit) includes functions to interface

with the native window system, including window creation,

management of input devices

Related APIs


AGL, GLX, WGL


glue between OpenGL and windowing systems


GLU (OpenGL Utility Library)


part of OpenGL


NURBS, tessellators, quadric shapes, etc.


GLUT (OpenGL Utility Toolkit)


portable windowing API


not officially part of OpenGL

OpenGL and Related APIs

GLUT

GLU

GL

GLX, AGL

or WGL

X, Win32, Mac O/S

software and/or hardware

application program

OpenGL Motif

widget or similar

OpenGL Architecture

Display

List

Polynomial

Evaluator

Per Vertex

Operations &

Primitive

Assembly

Rasterization

Per Fragment

Operations

Frame

Buffer

Texture

Memory

CPU

Pixel

Operations

Features of OpenGL


Delivers fast and complete 3D
hardware acceleration


makes real
-
time 3D effects possible


designed to support future
innovations in software and
hardware


Available on many platforms


Stable

OpenGL as a Renderer


Geometric primitives


points, lines and polygons


Image Primitives


images and bitmaps


separate pipeline for images and geometry


linked through texture mapping


Rendering depends on state


colors, materials, light sources, etc.

Other issues


Rendering acceleration (rasterization,
texture map, spacial subdivision, collision
detection, progressive rendering, view
dependent rendering, image
-
based
rendering,…)


Color, Texture, Anti
-
aliasing


Animation or simulation

Student 007: “I am dying to see an OpenGL
program !…”


Prof: Be patient, it is only few more

block(diagram)s away !!

Block Diagram

Preliminaries


Headers Files


#include <GL/gl.h>


#include <GL/glu.h>


#include <GL/glut.h>


Libraries


Enumerated Types


OpenGL defines numerous types for
compatibility


GLfloat, GLint, GLenum, etc.


GLUT Basics


Application Structure


Configure and open window


Initialize OpenGL state


Register input callback functions


render


resize


input: keyboard, mouse, etc.


Enter event processing loop

Sample Program


draw()
function

void drawScene(void)

{


// Clear the rendering window


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);



// Set drawing color to white



glColor3f( 1.0, 1.0, 1.0 );





// Draw triangle


glBegin(GL_TRIANGLES);



glColor3f( 1.0, 0.0, 0.0 );



glVertex2f( 1.0, 1.0 );



glVertex2f( 2.0, 1.0 );



glVertex2f( 2.0, 2.0 );


glEnd();





// Flush the pipeline. (Not usually necessary.)


glFlush();

}


OpenGL Command Formats

glVertex3fv(
v

)

Number of

components

2
-

(x,y)

3
-

(x,y,z)

4
-

(x,y,z,w)

Data Type

b
-

byte

ub
-

unsigned byte

s
-

short

us
-

unsigned short

i
-

int

ui
-

unsigned int

f
-

float

d
-

double

Vector

omit “v” for

scalar form


glVertex2f( x, y )

Specifying Geometric Primitives


Primitives are specified using

glBegin(
primType
);

glEnd();


primType

determines how vertices are combined

GLfloat red, greed, blue;

Glfloat coords[3];

glBegin(
primType
);

for ( i = 0; i < nVerts; ++i ) {


glColor3f( red, green, blue );


glVertex3fv( coords );

}

glEnd();

OpenGL Command Syntax


Function calls: glXxxxx[type] ( [args] )


Example:
glVertex2f (1.0, 2.0);


Defined constants: GL_XXXXXXXXXX


Example:
GL_COLOR_BUFFER_BIT


Type definition: GLxxxxxxx


Example:
GLfloat

Command Suffixes/ Argument Data Types



Suffix

Data Type

Typical Corresponding

C
-
Language Type

OpenGL Type Definition

b

8
-
bit integer

signed char

GLbyte

s

16
-
bit integer

short

GLshort

f

32
-
bit integer

int or long

GLint, GLbyte

i

32
-
bit floating
-
point

float

GLfloat, GLclampf

d

64
-
bit floatin
g
-
point

double

GLdouble,GLclampd

ub

8
-
bit unsigned integer

unsinged char

GLubyte,GLboolean

us

16
-
bit unsinged integer

unsinged short

GLushort

ui

32
-
bit unsinged integer

unsinged int or

unsinged long

GLuint, GLenum,

GLbitfield



Shared Namespace Model


Model

Application

Graphics Lib.

O

I

Function

Calls

OS

}

Run in same process

glFlush();

unnecessary

Client
-
Server Model:


Graphics

Server

O

I

Network Protocol

Model

Application

Graphics Lib.

Function

Calls

OS

OS

glFlush();

May be necessary

OpenGL Architecture

Display

List

Polynomial

Evaluator

Per Vertex

Operations &

Primitive

Assembly

Rasterization

Per Fragment

Operations

Frame

Buffer

Texture

Memory

CPU

Pixel

Operations

State Management and Drawing
in OpenGL


OpenGL Initialization


Set up whatever state you’re going to use

void init( void )

{


glClearColor( 0.0, 0.0, 0.0, 1.0 );


glClearDepth( 1.0 );



glEnable( GL_LIGHT0 );


glEnable( GL_LIGHTING );


glEnable( GL_DEPTH_TEST );

}

GLUT Callback Functions


Routine to call when something happens


window resize or redraw


user input


animation


“Register” callbacks with GLUT

glutDisplayFunc(
display

);

glutIdleFunc(
idle

);

glutKeyboardFunc(
keyboard

);

A Typical Event
-
Driven Program

Initialization

Main Loop

Input

Processing

Procedures

Background

Processing

Procedure

Event

Handler

Route messages to the
application



Event
Button on Mouse
Windows
Route Event to Correct Application
Windows Application
WinMain()
WinProc()
Message
Left Button Down
Message Processed
Call

Function n

GLUT Event Processing

Event

in

Queue

Pop Event

From Queue

Call

Function n

Switch on

Event Type

Call

Function n

Call Idle

Function

No

Yes

Call

Function n

Call

Function n

Call

Function n

Callbacks

Event A

Event B

Event C

Event D

Event E

Event F

No Event

Idle function( )

m
1
, m
2

function_A(m
1
, m
2
)

function_B( )

function_C( )

Event
Type

“Registered” Function

NB: Function parameters must match measures contained


in associated event.

Display Event

Trigger: GLUT determines that the window needs to be

redisplayed. A display event is generated when the window

is first drawn.

Callback function form:


void display();

Registration:



glutDisplayFunc(display);

A display callback function
must

be registered for each window.

GLUT Mouse Event

Trigger: Any mouse button is
depressed
or
released
.

Callback function form:


void mouse_callback_func(int button, int state,


int x, int y);

Registration:



glutMouseFunc(mouse_callback_function);

GLUT Defined Mouse Constants


GLUT_LEFT_BUTTON

GLUT_MIDDLE_BUTTON

GLUT_RIGHT_BUTTON


GLUT_UP

GLUT_DOWN


Systems with only one mouse button can only generate

a

GLUT_LEFT_BUTTON
callback.

GLUT Reshape Event

Trigger: Active window is resized

Callback function form:


void reshape_func(GLsizei w, GLsizei h);

Registration:



glutReshapeFunc(reshape_func);

GLUT Move Event

Trigger: The mouse is moved
while one or more mouse



buttons are pressed.

Callback function form:


void motion_func(int x, int y);

Registration:



glutMotionFunc(motion_func);

GLUT Keyboard Event

Trigger: Any key is depressed.

Callback function form:


void keyboard_function(unsigned char key,


int x, int y);

Registration:



glutKeyboardFunc(keyboard_function);

Idle Callbacks


Use for animation and continuous update

glutIdleFunc( idle );

void idle( void )

{


t += dt;


glutPostRedisplay();

}


Other Defined Events in GLUT


glutPassiveMotionFunc

glutVisibilityFunc

glutEntryFunc

glutSpecialFunc

glutSpaceballMotionFunc

glutSpaceballRotateFunc

glutSpaceballButtonFunc

glutButtonBoxFunc

glutDialsFunc

glutTabletMotionFunc

glutTabletButtonFunc

glutMenuStatusFunc



Simple GLUT Window
Management Functions

glutInit(int *argc, char** argv);

Initializes a window session.


glutCreateWindow(char *name);

Creates a window with title *name.


glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

Sets the display mode to single buffered and RGB color.


glutInitWindowSize (GLsizei h, GLsizei w);

Sets initial window size to h x w.


glutInitWindowPosition(x,y);

Sets initial window position to (x, y).



TA help is there, but use “Help” and try to
help yourselves !!


Show simple demo program

OpenGL Lib

GL Commands

GDI Processor

Graphics

Hardware

OpenGL in MS Windows

GDI


Graphics Device Interface (Windows
-
specific)

Basic Program Skeleton


Header files, Global variables


Rendering initialization


Keyboard / Mouse functions


Drawing / animation


Window resizing


Idle function


main()

Try this …

Draw a red line in a window with black background

Try changing line thickness

Change color combination

What if you want a regular triangle as against a “filled” one

Now try drawing various figures from the library

Try modeling concave polygons