cs380 lab i opengl

41
CS380 LAB I OpenGL Bochang Moon Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [http://nehe.gamedev.net/ ]

Upload: dulcea

Post on 23-Jan-2016

57 views

Category:

Documents


0 download

DESCRIPTION

CS380 LAB I OpenGL. Bochang Moon Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [ http://nehe.gamedev.net/ ]. Goal. Introduce OpenGL programming Help you do CS380 homework by yourself. Outline. Install Dev-C++ and OpenGL environments Run an empty window - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS380 LAB I OpenGL

CS380 LAB IOpenGL

Bochang Moon

Reference1. [OpenGL course slides by Rasmus Stenholt]

Reference2. [http://nehe.gamedev.net/]

Page 2: CS380 LAB I OpenGL

Goal

Introduce OpenGL programming Help you do CS380 homework by yourself

2

Page 3: CS380 LAB I OpenGL

Outline

Install Dev-C++ and OpenGL environments Run an empty window Note: Visual C++ with OpenGL is similar

Reference: course slides “Basic OpenGL structure”

Start with the OpenGL framework Draw a OpenGL primitive Address keyboard and mouse input

3

Page 4: CS380 LAB I OpenGL

Install Dev-C++

Download Dev-C++ beta 9 releasehttp://www.bloodshed.net/dev/devcpp.html

4

Click

Page 5: CS380 LAB I OpenGL

Install Dev-C++

Just click “Next”

5

Page 6: CS380 LAB I OpenGL

Dev-C++ starts up

Click File/New/Project

6

Page 7: CS380 LAB I OpenGL

Dev-C++ starts up

Click “Console Application”

7

Page 8: CS380 LAB I OpenGL

Dev-C++ starts up

Make a new folder, save the project file

8

Page 9: CS380 LAB I OpenGL

Dev-C++ starts up

Click Execute/Compile (Ctrl + F9) Click Execute/Run (Ctrl + F10)

9

Page 10: CS380 LAB I OpenGL

Download and install GLUT

Download GLUT files from http://chortle.ccsu.edu/Bloodshed/glutming.zip

Extract glutming.zip

10

Page 11: CS380 LAB I OpenGL

Download and install GLUT

Copy GLUT files to the install folder of DEV-C++ glut.h: copy this file to C:\Dev-Cpp\include\GL libglut32.a: copy (or replace) this file to C:\DEV-Cpp\

lib glut32.dll: copy this file to C:\Windows\System32

Check whether there is “opengl32.dll” in this folder

11

Page 12: CS380 LAB I OpenGL

Download and install GLUT

Project setting in DEV-C++Click Project/Project Options

12

Page 13: CS380 LAB I OpenGL

Download and install GLUT

Project setting in DEV-C++Click Project/Project Options

13

Page 14: CS380 LAB I OpenGL

Download and install GLUT

Project setting in DEV-C++Click Parameters, and Add Library or Object

14

Page 15: CS380 LAB I OpenGL

Download and install GLUT

Project setting in DEV-C++Add three library files in this order

C:/Dev-Cpp/lib/libopengl32.a C:/Dev-Cpp/lib/libglu32.a C:/Dev-Cpp/lib/libglut32.a

15

Page 16: CS380 LAB I OpenGL

Creating an empty window

#include <GL/glut.h> #include <GL/glu.h> void display() { } int main( int argc, char* argv[] ) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH); glutInitWindowSize(512, 512); glutCreateWindow("CS380 LAB"); glutDisplayFunc( display ); glutMainLoop(); return 0; }

16

Page 17: CS380 LAB I OpenGL

Draw your first polygon

void display()Main display function where we can do all the

drawing

17

Page 18: CS380 LAB I OpenGL

Draw your first polygon

Clear your screen void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glFlush(); }

glClear(parameters) // clear input buffers glFlush() // forces all pending commands to be executed

18

Page 19: CS380 LAB I OpenGL

Draw your first polygon

void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); // Reset our view glBegin(GL_TRIANGLES); // Draw a triangle glVertex3f( 0.0f, 1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f); glEnd(); glFlush(); }

19

(0,0,0)

Page 20: CS380 LAB I OpenGL

Draw your first polygon

20

In OpenGL, geometry is specified by vertices Vertices must be specified between glBegin(primitive type) and glEnd() function calls

The primitive type represents how vertices are to be connected

glBegin(GL_TRIANGLES); glVertex3f( 0.0f, 1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f); glEnd();

Page 21: CS380 LAB I OpenGL

21

OpenGL Primitives

Triangles There are 3 ways of making triangles with OpenGL

Individual triangles Type is GL_TRIANGLES Each triangle requires 3 explicit vertices Sets of unconnected triangles are often called polygon soups

Strips of connected triangles Type is GL_TRIANGLE_STRIP The first triangle requires 3 vertices, the rest use 1 new vertex

and the 2 most recently defined vertices Complex objects are often built from

Fans of connected triangles Type is GL_TRIANGLE_FAN Every triangle use the first, the previous, and a new vertex Useful for creating polygons or approximating circles/ellipses

GL_TRIANGLE_STRIPGL_TRIANGLE_STRIP

GL_TRIANGLESGL_TRIANGLES

GL_TRIANGLE_FANGL_TRIANGLE_FAN

Page 22: CS380 LAB I OpenGL

22

OpenGL Primitives

Quadrilaterals (quads) Individual quads

Type is GL_QUADS A quad is defined by 4 vertices Quads can be decomposed into

two triangles Quads are not necessarily plane

or convex Be careful with the vertex

sequence Strips of connected quads

Type is GL_QUAD_STRIP Uses the most recent 2 vertices

and 2 new vertices

GL_QUADSGL_QUADS

GL_QUAD_STRIPGL_QUAD_STRIP

Page 23: CS380 LAB I OpenGL

23

OpenGL Primitives

PolygonsType is GL_POLYGONPolygons need 3 or more

vertices I.e. can be used for any

polygonPolygons are divided into

triangles by the graphics card

GL_POLYGONGL_POLYGON

Page 24: CS380 LAB I OpenGL

24

OpenGL Primitives

Points Type is GL_POINTS Points are 0-D Points represent the simplest

drawable primitive 1 vertex is used per point Points are rendered as small,

unconnected dots on the screen

Theoretically points have no area

GL_POINTSGL_POINTS

Page 25: CS380 LAB I OpenGL

25

OpenGL Primitives

Lines Type is GL_LINES Lines are 1-D Each line needs 2 vertices Lines have no area

Open series of lines Type is GL_LINE_STRIP

Closed series of lines Type is GL_LINE_LOOP

GL_LINESGL_LINES

GL_LINE_STRIPGL_LINE_STRIP

GL_LINE_LOOPGL_LINE_LOOP

Page 26: CS380 LAB I OpenGL

26

OpenGL functions

OpenGL functions all follow the same naming conventionsFunction names have gl, glu, or glut as prefix

depending on their package of originThe name of the function follows the prefixThe parameter type of the function is placed

as a postfix

Page 27: CS380 LAB I OpenGL

27

OpenGL functions

glVertex3fv( glVertex3fv( vv ) )

Number ofNumber ofcomponentscomponents

2 - (x,y) 2 - (x,y) 3 - (x,y,z)3 - (x,y,z)4 - (x,y,z,w)4 - (x,y,z,w)

Data TypeData Typeb - byteb - byteub - unsigned byteub - unsigned bytes - shorts - shortus - unsigned shortus - unsigned shorti - inti - intui - unsigned intui - unsigned intf - floatf - floatd - doubled - double

VectorVector

omit “v” foromit “v” forscalar formscalar form

glVertex2f( x, y )glVertex2f( x, y )

Page 28: CS380 LAB I OpenGL

Tutorial

Draw this rectangle

28

Page 29: CS380 LAB I OpenGL

Tutorial

Drawing a red rectangle glBegin(GL_QUADS); glVertex3f( -0.5f, -0.5f, 0.0f); glVertex3f(-0.5f, 0.5f, 0.0f); glVertex3f( 0.5f, 0.5f, 0.0f); glVertex3f( 0.5f,-0.5f, 0.0f); glEnd();

29

Page 30: CS380 LAB I OpenGL

Adding colours

glColor3f(red, green, blue)

Drawing a red rectangle glBegin(GL_QUADS); glColor3f(1.0f,0.0f,0.0f); glVertex3f( -0.5f, -0.5f, 0.0f); glVertex3f(-0.5f, 0.5f, 0.0f); glVertex3f( 0.5f, 0.5f, 0.0f); glVertex3f( 0.5f,-0.5f, 0.0f); glEnd();

30

Page 31: CS380 LAB I OpenGL

31

Colours in OpenGL

Colours are modelled using the red-green-blue (RGB) system

Page 32: CS380 LAB I OpenGL

32

Colours in OpenGL

There are several ways of representing colour in OpenGL Directly as RGB-tuples Extended RGBA-tuples Indexed mode

The RGBA mode has an extra component, alpha, which does not affect the colour directly Alpha is used when blending colours

E.g. transparency effects

The indexed mode uses a fixed table of colours to look up colours

Page 33: CS380 LAB I OpenGL

33

Colours in OpenGL

Colours are specified by the glColor*() family of functions

Example: glColor3f() Specifies a colour by three floating point

values in the range [0.0;1.0]The parameters represent R, G, and B,

respectively

Page 34: CS380 LAB I OpenGL

Keyboard input

int main( int argc, char* argv[] ) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH); glutInitWindowSize( width, height ); glutCreateWindow("CS380"); glutDisplayFunc( display ); glutMouseFunc( mouse ); glutKeyboardFunc( keyboard ); glutReshapeFunc( reshape ); glutMainLoop(); return 0; }

34

Page 35: CS380 LAB I OpenGL

Keyboard input

void keyboard(unsigned char key, int x, int y) { if (key == 'r') { // todo } glutPostRedisplay(); }

35

Page 36: CS380 LAB I OpenGL

Tutorial

Change the color of your rectangle Press ‘r’: change it to a red rectanglePress ‘g’: change it to a green rectanglePress ‘b’: change it to a blue rectangle

36

Page 37: CS380 LAB I OpenGL

Tutorial

void display() { … glColor3f(r,g,b); … }

37

void keyboard(unsigned char key, int x, int y){ if (key == 'r') { r = 1.0, g = 0.0, b = 0.0; } else if (key == 'g') { r = 0.0, g = 1.0, b = 0.0; } else if (key == 'b') { r = 0.0, g = 0.0, b = 1.0; } glutPostRedisplay();}

Page 38: CS380 LAB I OpenGL

Mouse input

int main( int argc, char* argv[] ) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH); glutInitWindowSize( width, height ); glutCreateWindow("CS380"); glutDisplayFunc( display ); glutMouseFunc( mouse ); glutKeyboardFunc( keyboard ); glutReshapeFunc( reshape ); glutMainLoop(); return 0; }

38

Page 39: CS380 LAB I OpenGL

Mouse input

void mouse( int button, int state, int mx, int my ) button

GLUT_LEFT_BUTTON GLUT_MIDDLE_BUTTON GLUT_RIGHT_BUTTON

state GLUT_DOWN GLUT_UP

mx, my positions

39

Page 40: CS380 LAB I OpenGL

Mouse input

void mouse( int button, int state, int mx, int my ) { if((button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN) ) { r = 1.0, g = 1.0, b = 1.0; display(); } }

40

Page 41: CS380 LAB I OpenGL

41

Next time

Transformation in OpenGL