Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12....

26
Lecture 2 : Programming with OpenGL Slides: E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 CITS3003 Graphics & Animation

Transcript of Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12....

Page 1: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

Lecture 2 : Programming with OpenGL

Slides: E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012

CITS3003 Graphics & Animation

Page 2: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

Lectures

1. Introduction & Image Formation2. Programming with OpenGL3. OpenGL: Pipeline Architecture4. OpenGL: An Example Program5. Vertex and Fragment Shaders 16. Vertex and Fragment Shaders 27. Representation and Coordinate

Systems8. Coordinate Frame Transformations9. Transformations and Homogeneous

Coordinates10. Input, Interaction and Callbacks11. More on Callbacks12. Mid-semester Test13. 3D Hidden Surface Removal14. Mid term-test solution and project

discussion

Study Break15. Computer Viewing16. Shading17. Shading Models18. Shading in OpenGL19. Texture Mapping20. Texture Mapping in OpenGL 21. Hierarchical Modelling22. 3D Modelling: Subdivision Surfaces23. Animation Fundamentals and

Quaternions24. Skinning

2

Page 3: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

Objectives

• OpenGL libraries

• OpenGL architecture

• OpenGL types and functions

• A simple program

3

Page 4: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

Modern OpenGL

• Allows the computer program to achieve fast graphics performance by using GPU rather than CPU

• Allows applications to control GPU through programs known as shaders

• It is the application’s job to send data to GPU

• GPU then does all the rendering

4

Page 5: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

Software Organization

• The application programs can use GLEW, GL, GLUT functions but not directly access to GLX, Xlib, etc.

• The program can therefore be recompiled with the GLUT libraries for other operating systems.

Removes OS dependencies

High level Low level

5

X Window System

Provides min. functionalities

expected by windowing sys.

Page 6: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

OpenGL Types

• In OpenGL, we use basic OpenGL types, e.g. GLfloat, GLdouble, GLint, etc (equivalent to float, double, and int in C/C++)

• Additional data types are supplied in header files vec.h and mat.h from Angel and Shreiner, e.g. vec2, vec3, mat2, point2, point3, etc.

6

Page 7: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

State Machine

• We can think of the entire graphics system as a black box (finite-state machine).

• This black box has inputs coming from theapplication program. These inputs can changethe state of the machine or can cause themachine to produce a visible output.

Graphics system as a black box

7

Page 8: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

State Machine (cont.)• From the perspective of the API, there are two

types of graphics functions:1. Functions that define primitives that flow through the

state machine. These functions• define how vertices are processed (the appearance of

primitives are controlled by the state of the machine)• can cause output if the primitive is visible

2. Functions that either change the state inside the machine or return the state information, e.g.• Transformation functions• Attribute functions• In OpenGL, most state variables are defined by the application

and sent to the shaders

8

Page 9: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

State Machine (cont.)

• The latest OpenGL versions have eliminated most ofthe pre-defined variables and functions.

• Application program can define its own state variablesand use them or send their values to the shaders.

• Consequence of state machine view– Most parameters are persistent; their values remain

unchanged until we explicitly change them throughfunctions that alter the state.• E.g., once we set a color, it remains the current color until it is

altered through a color-altering function.

– Attributes can be conceptualized as bound to the objectsas part of the state.

Page 10: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

OpenGL Functions

• OpenGL provides a range of functions for specifying:• Primitives

o Pointso Line Segmentso Triangles

• Attributes• Transformations

o Viewingo Modeling

• Control (GLUT)• Input (GLUT)• Query

10

Page 11: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

What are OpenGL Primitives?

GL_TRIANGLE_STRIP GL_TRIANGLE_FAN

GL_POINTS

GL_LINES

GL_LINE_LOOP

GL_LINE_STRIP

GL_TRIANGLES

11

Page 12: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

What are attributes?

• Attributes are properties associated with the primitives that give them their different appearances, e.g.• Color (for points, lines, polygons)

• Size and width (for points, lines)

• Stipple pattern (for lines, polygons)

• Polygon mode

• Display as filled: solid color or stipple pattern

• Display edges

• Display vertices

• Note: glPointSize: The actual size that is displayed on the screen will be different. More on this later.

12

Page 13: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

OpenGL functions:Lack of Object Orientation

• OpenGL is not object oriented so that there are multiple functions for a given logical function, e.g., the following are the same function but for different parameter types:o glUniform3f

o glUniform2i

o glUniform3dv

• The underlying storage mode is the same

• It is easy to create overloaded functions in C++ but the issue is efficiency

13

Page 14: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

Format of OpenGL functions

glUniform3f(x,y,z)

belongs to GL library

function name

x,y,z are floats

glUniform3fv(p)

p is a pointer to an array

dimensions

14

glUniform — Specify the value of a uniform variable for the current program object

Uniform variables are used to communicate with vertex or fragment shaders from outside. We will come to the details later.

Page 15: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

OpenGL #defines

• Most constants are defined in the include files gl.h, glu.h and glut.h

• Note #include <GL/glut.h> should automatically include the others

• Examples: the functions glEnable and glClear

are both declared in gl.h

• The OpenGL data types GLfloat, GLdouble,….are also declared in gl.h

15

Page 16: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

What is GLSL?

• GLSL is short for OpenGL Shading Language• It is a C-like language with:

o Built-in Matrix and vector types (2, 3, 4 dimensional)o Overloaded operatorso C++ like constructors

• It is similar to Nvidia’s Cg and Microsoft HLSL• Supports loops, if-else constructs, but recursion is not allowed• GLSL codes are not stand-alone applications, they require an

application program that uses OpenGL API• Code sent to shaders as source code• There are OpenGL functions to compile, link and get information

to shaders• More on GLSL in later lectures

16

Page 17: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

A Simple Program

simple.cpp - Generates a white square on a black background

17

Page 18: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

A Simple Program (cont.)

• Most OpenGL programs have a similar structure that consists of the following functions:

o main(): creates the window, calls the init() function, specifies callback functions (more about this in a later topic) relevant to the application, enters event loop (last executable statement)

o init(): defines the vertices, attributes, etc. of the objects to be rendered, specifies the shader programs (more about this later)

o display(): this is a callback function that defines what to draw whenever the window is refreshed.

18

Page 19: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

simple.cpp#include <GL/glut.h>

void init() {

// code to be inserted here

}

void mydisplay(){

glClear(GL_COLOR_BUFFER_BIT);

// need to fill in this part

// and add in shaders

}

int main(int argc, char** argv){

glutCreateWindow("simple");

init();

glutDisplayFunc(mydisplay);

glutMainLoop();

}

includes gl.h

set up OpenGL state

and initialize shaders

enter event loop

create a window

with “simple” as

its title

19

called every time

when the window

is refreshed

define display

callback

Page 20: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

Event Loop

• Note that the program specifies a display callback function named mydisplay

• Every glut program must have a display callback

• The display callback is executed whenever OpenGL decides the display must be refreshed, for example when the window is opened

• The main function ends with the program entering an event loop

20

Page 21: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

simple.cpp – the complete program#include “Angel.h”

using namespace std;

const int NumTriangles = 2; // 2 triangles to be displayedconst int NumVertices = 3 * NumTriangles;

vec3 points[NumVertices] = {vec3( -0.5, -0.5, 0.0 ), vec3( 0.5, -0.5, 0.0 ), vec3( -0.5, 0.5, 0.0 ),vec3( 0.5, 0.5, 0.0 ), vec3( -0.5, 0.5, 0.0 ), vec3( 0.5, -0.5, 0.0 )

};

void init( void ){

// Create a vertex array objectGLuint vao;glGenVertexArrays( 1, &vao );glBindVertexArray( vao );

21

3rd dim. is set to 0,

4 unique locations

1. Define variable name

2. Define the obj. that var. name refers to

3. Bind the obj.

Page 22: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

simple.cpp – the complete program// Create and initialize a buffer objectGLuint buffer;glGenBuffers( 1, &buffer );glBindBuffer( GL_ARRAY_BUFFER, buffer );

// First, we create a buffer of the size we need to store the six pointsglBufferData( GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW );// Load shaders and use the resulting shader programGLuint program = InitShader( "vertex.glsl", "fragment.glsl" );glUseProgram( program );

// Initialize the vertex position attribute from the vertex shaderGLuint vPos = glGetAttribLocation( program, "vPosition" );glEnableVertexAttribArray( vPos );glVertexAttribPointer( vPos, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) );

glClearColor( 0.0, 0.0, 0.0, 0.0 ); /* black background */

}

The two shaderprograms are compiled here to give a GLuintoutput. See InitShader.cpp.

22

Does data need normalization?

stride

Remember state-machine concept:

An object may be created but never

explicitly used

Page 23: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

simple.cpp – the complete programvoid display( void ){

glClear( GL_COLOR_BUFFER_BIT );glDrawArrays( GL_TRIANGLES, 0, NumVertices ); // draw the two trianglesglFlush(); // draw it now!

}

int main( int argc, char **argv ){

glutInit( &argc, argv );glutInitDisplayMode( GLUT_RGBA );glutInitWindowSize( 256, 256 );glutCreateWindow( "simple" );

init();glutDisplayFunc( display );glutMainLoop();

}23

Specifies the startingindex in the enabled array

Page 24: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

Vertex shader for simple.cpp

Contents of the file vertex.glsl

attribute vec4 vPosition;

void main(){

gl_Position = vPosition;}

Must be the same as the name chosen in simple.cpp (see the 2nd parameter passed to the glGetAttribLocation function.

Built-in variable name in GLSL, denoting the vertex coordinates in 4-dimensions.

Application program is simple.cpp. It must work with two shader programs, written in GLSL. They must work together.

24

Page 25: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

Fragment shader for simple.cpp

Contents of the file fragment.glsl

void main(){

gl_FragColor = vec4( 1.0, 1.0, 1.0, 1.0 );}

Built-in variable name in GLSL, denoting the colour (as a 4D vector) to be put at that vertex.

25

Page 26: Lecture 2 : Programming with OpenGL...10. Input, Interaction and Callbacks 11. More on Callbacks 12. Mid-semester Test 13. 3D Hidden Surface Removal 14. Mid term-test solution and

Further Reading

“Interactive Computer Graphics – A Top-Down Approach with Shader-Based OpenGL” by Edward Angel and Dave Shreiner, 6th Ed, 2012

• Sec. 2.4 Primitives and Attributes (up to Sec. 2.4.1)

• Sec. 2.3.1 Graphics Functions

• Sec. 2.3.2 Graphics Pipeline and State Machine

• Sec. 8.10 Graphics and the Internet

• (Advanced) Appendix A.2

26