Top Banner
OpenGL Programming 1 t W k 2009 1 st Week , 2009 Visual Studio 2008 Visual Studio 2008
23

OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

Feb 23, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

OpenGL Programming

1 t W k 20091st Week, 2009

Visual Studio 2008Visual Studio 2008

Page 2: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

새 프로젝트새 프로젝트

■ Visual C++ Win32 프로젝트■ Visual C++ Win32 프로젝트

응용 프로그램 설정응용 프로그램 설정

■ 빈 프로젝트■ 빈 프로젝트

Page 3: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

솔루션 ‘Prac01’솔루션 Prac01

새 항목 추가새 항목 추가

Page 4: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

‘main cpp’ 추가main.cpp 추가

‘main cpp’main.cpp

Page 5: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

Downloading Sample ProgramDownloading Sample Program

‘main cpp’ 소스 코드main.cpp 소스 코드

Page 6: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

프로젝트 속성프로젝트 속성

멀티바이트 문자 집합 사용멀티바이트 문자 집합 사용

Page 7: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

Unicode vs Multi ByteUnicode vs. Multi-Byte

■ SBCS (Single Byte Character Set)■ SBCS (Single Byte Character Set)■■ ASCIIASCII code: 1 byte character set

■ DBCS (Double Byte Character Set)■ Korean■ Korean

■ MBCS (Multi Byte Character Set)■ MBCS (Multi Byte Character Set)■ SBCS + DBCS: ~ Windows 9x

■ WBCS (Wide Byte Character Set)■■ UnicodeUnicode: Windows 2000 ~■■ UnicodeUnicode: Windows 2000

OpenGL 라이브러리 링크OpenGL 라이브러리 링크

Page 8: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

OpenGL API (1)OpenGL API (1)

■ API (application programming interface)■ API (application programming interface)■ Interface between an application program and a

graphics system Inputg p yApplication Program

Application Program

Graphics Library (API)

Graphics Library (API)

HardwareHardware

InputDevice

Output

■ OpenGL API■ Easy to learn, compared with other APIs

Device

■ Nevertheless powerful

■ Graphics functions: black boxblack boxp■ Described by only its inputs and outputs■ Nothing known about its internal working

F ti C ll O t tUserProgram

UserProgram

GraphicsSystemGraphicsSystem

Input/OutputDevices

Input/OutputDevices

Function Calls Output

Data Input

OpenGL API (2)OpenGL API (2)

■ OpenGL interface■ OpenGL interface

OpenGLOpenGL

GLUGLU

GLGLOpenGLApplicationProgram

OpenGLApplicationProgram

GLGL

GLUTGLUT Xlib, XtkXlib, XtkFrameBufferFrameBuffer

■ OpenGL core library (names : gl~) : GLGLGLXGLX

■ OpenGL utility library : GLUGLU■ Using only GL functions

C ti bj t■ Creating common objects■ Other tasks that users prefer not to write repeatedly

■ OpenGL utility toolkit : GLUTGLUT■ OpenGL utility toolkit : GLUTGLUT■ Interface with the window system

Page 9: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

OpenGL API (3)OpenGL API (3)

■ OpenGL function format■ OpenGL function format

Function name Dimensions

lV t 3f( )glVertex3f( x, y, z );

Belongs to GL library x, y, z are floats

p is a pointer to an arrayp p y

glVertex3fv( p );

실행 결과실행 결과

Page 10: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

GLUT LibraryGLUT Library

■■ glut hglut h ( /Microsoft SDKs/Windows/v6 0A/Include/gl)■■ glut.hglut.h ( /Microsoft SDKs/Windows/v6.0A/Include/gl)■■ glut32.libglut32.lib ( /Microsoft SDKs/Windows/v6.0A/Lib)■■ glut32.dllglut32.dll ( Windows/system32)void glutWireSphere(GLdouble radius, GLint slices, Glint stacks);void glutSoildSphere(GLdouble radius, GLint slices, Glint stacks);void glutWireSphere(GLdouble radius, GLint slices, Glint stacks);void glutSoildSphere(GLdouble radius, GLint slices, Glint stacks);g p ( , , );g p ( , , );

void glutWireCone(GLdouble base, GLdouble height, GLint slices, Glint stacks);void glutSoildCone(GLdouble base, GLdouble height, GLint slices, Glint stacks);void glutWireCone(GLdouble base, GLdouble height, GLint slices, Glint stacks);void glutSoildCone(GLdouble base, GLdouble height, GLint slices, Glint stacks);

void glutWireCube(GLdouble size);void glutWireCube(GLdouble size);

void glutWireTorus(GLdouble inner, GLdouble outer, GLint sides, Glint slices);void glutSoildTorus(GLdouble inner, GLdouble outer, GLint sides, Glint slices);void glutWireTorus(GLdouble inner, GLdouble outer, GLint sides, Glint slices);void glutSoildTorus(GLdouble inner, GLdouble outer, GLint sides, Glint slices);

void glutWireTetrahedron();void glutWireTetrahedron();void glutWireCube(GLdouble size);void glutSoildCube(GLdouble size);void glutWireCube(GLdouble size);void glutSoildCube(GLdouble size);

void glutWireTeapot(GLdouble size);void glutSoildTeapot(GLdouble size);void glutWireTeapot(GLdouble size);void glutSoildTeapot(GLdouble size);

void glutWireTetrahedron();void glutSoildTetrahedron();void glutWireOctahedron();void glutWireOctahedron();void glutWireDodecahedron();

void glutWireTetrahedron();void glutSoildTetrahedron();void glutWireOctahedron();void glutWireOctahedron();void glutWireDodecahedron();void glutSoildTeapot(GLdouble size);void glutSoildTeapot(GLdouble size); void glutWireDodecahedron();void glutSoildDodecahedron();void glutWireIcosahedron();void glutWireIcosahedron();

void glutWireDodecahedron();void glutSoildDodecahedron();void glutWireIcosahedron();void glutWireIcosahedron();

연습문제 (1)연습문제 (1)

■ GLUT 함수를 이용하여 다른 오브젝트로 변경하■ GLUT 함수를 이용하여 다른 오브젝트로 변경하시오.

Page 11: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

Rendering PipelineRendering Pipeline

3D Primitives Local coordinates3D Primitives

World Transformation

Local coordinates

World coordinates

Viewing Transformation

LightingWorld coordinates

Viewing Transformation

Projection Transformation

Viewing coordinates

P j ti di tClipping

Vi t T f ti

Projection coordinates

Viewport Transformation

Rasterization

Device coordinates

2D Image

World TransformationWorld Transformation

■ Changing coordinates relative local coordinate■ Changing coordinates relative local coordinate system into world coordinate system

World

Localdi

WorldTransformation

CoordinateSystem W3

WorldC di t

W2

W1Coordinate

System

Page 12: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

Viewing TransformationsViewing Transformations

■ Transforming all of the objects in world space■ Transforming all of the objects in world space to viewing space

Letting camera be centered about

Up Vector

■ Letting camera be centered about camera coordinate system

Right Vector

Front VectorViewing

CoordinateSystemSystem

Viewing

WorldCoordinate V

gTransformation

CoordinateSystem

Projection TransformationProjection Transformation

■ Mapping a view volume into a box■ Mapping a view volume into a box■ x and y coordinates: [-1, 1] 2D projection coord.

coordinates: [0 1] depth testing■ z coordinates: [0, 1] depth testing(1, 1, 1)

ViewingCoordinate

System

ViewVolume Normalized

ProjectionTransformation

VolumeView

Volume

(-1, -1, 0)

Page 13: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

Viewport TransformationViewport Transformation

■ Transforming projection coordinates to a■ Transforming projection coordinates to a rectangle (viewport)

Aspect ratio = width / height■ Aspect ratio = width / height

LightingLighting

■ Realistic scene■ Realistic scene■ Specifying lights in world space

transforming lights into viewing spacetransforming lights into viewing space carrying out the light calculation

A U it S h A Lit S h A Lit SphereAn Unit Sphere A Lit Sphere A Lit Sphereand its Shadow

Page 14: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

ClippingClipping

■ Culling the geometry that is outside the■ Culling the geometry that is outside the viewing volume

Three possible locations

+z

■ Three possible locations(1) completely inside

keeping it Insidekeeping it(2) completely outside

lli itculling it(3) partially inside

( ti ll t id )(partially outside)splitting it

OutsidePartially Inside

+x+x

RasterizationRasterization

■ Computing the color of the individual pixels■ Computing the color of the individual pixels that make up the interiors and boundaries of trianglestriangles

RasterizationRasterization

Page 15: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

Primitives in OpenGLPrimitives in OpenGL

■ Points■ Points■ Lines

Li■ Line segments■ Polylines

■ Polygons■ Polygons■ Triangles and quadrilaterals■ Strips and fans

■ Text■ Curved objects■ Curved objects

Points in OpenGLPoints in OpenGL

glBegin(GL_POINTS);glVertex2fv(p0);

glBegin(GL_POINTS);glVertex2fv(p0); p0

7glVertex2fv(p0);glVertex2fv(p1);glVertex2fv(p2);glVertex2fv(p3);

glVertex2fv(p0);glVertex2fv(p1);glVertex2fv(p2);glVertex2fv(p3);

p0p1

26

p7

glVertex2fv(p3);glVertex2fv(p4);glVertex2fv(p5);glVertex2fv(p6);

glVertex2fv(p3);glVertex2fv(p4);glVertex2fv(p5);glVertex2fv(p6);

p2

p3p5

p6

g (p );glVertex2fv(p7);

glEnd();

g (p );glVertex2fv(p7);

glEnd();

p3

p4

p

Page 16: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

Lines in OpenGL (1)Lines in OpenGL (1)

■ Line segments■ Line segments

glBegin(GL_LINES);glVertex2fv(p0);

glBegin(GL_LINES);glVertex2fv(p0); p0glVertex2fv(p0);glVertex2fv(p1);glVertex2fv(p2);glVertex2fv(p3);

glVertex2fv(p0);glVertex2fv(p1);glVertex2fv(p2);glVertex2fv(p3);

p0p1

p7

glVertex2fv(p3);glVertex2fv(p4);glVertex2fv(p5);glVertex2fv(p6);

glVertex2fv(p3);glVertex2fv(p4);glVertex2fv(p5);glVertex2fv(p6);

p2p6

g (p );glVertex2fv(p7);

glEnd();

g (p );glVertex2fv(p7);

glEnd();p3

p4

p5

Lines in OpenGL (2)Lines in OpenGL (2)

■ Polylines – line strip■ Polylines – line strip

glBegin(GL_LINE_STRIP);glVertex2fv(p0);

glBegin(GL_LINE_STRIP);glVertex2fv(p0); p0glVertex2fv(p0);glVertex2fv(p1);glVertex2fv(p2);glVertex2fv(p3);

glVertex2fv(p0);glVertex2fv(p1);glVertex2fv(p2);glVertex2fv(p3);

p0p1

p7

glVertex2fv(p3);glVertex2fv(p4);glVertex2fv(p5);glVertex2fv(p6);

glVertex2fv(p3);glVertex2fv(p4);glVertex2fv(p5);glVertex2fv(p6);

p2p6

g (p );glVertex2fv(p7);

glEnd();

g (p );glVertex2fv(p7);

glEnd(); p3p4

p5

Page 17: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

Lines in OpenGL (3)Lines in OpenGL (3)

■ Polylines – line loop■ Polylines – line loop

glBegin(GL_LINE_LOOP);glVertex2fv(p0);

glBegin(GL_LINE_LOOP);glVertex2fv(p0); p0glVertex2fv(p0);glVertex2fv(p1);glVertex2fv(p2);glVertex2fv(p3);

glVertex2fv(p0);glVertex2fv(p1);glVertex2fv(p2);glVertex2fv(p3);

p0p1

p7

glVertex2fv(p3);glVertex2fv(p4);glVertex2fv(p5);glVertex2fv(p6);

glVertex2fv(p3);glVertex2fv(p4);glVertex2fv(p5);glVertex2fv(p6);

p2p6

g (p );glVertex2fv(p7);

glEnd();

g (p );glVertex2fv(p7);

glEnd(); p3p4

p5

연습문제 (2)연습문제 (2)

■ 오브젝트 앞에 X모양의 선을 추가하시오■ 오브젝트 앞에 X모양의 선을 추가하시오.

Page 18: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

Polygons in OpenGL (1)Polygons in OpenGL (1)

■ Polygon■ Polygon

glBegin(GL_POLYGON);glVertex2fv(p0);

glBegin(GL_POLYGON);glVertex2fv(p0); p0glVertex2fv(p0);glVertex2fv(p1);glVertex2fv(p2);glVertex2fv(p3);

glVertex2fv(p0);glVertex2fv(p1);glVertex2fv(p2);glVertex2fv(p3);

p0p1

p7

glVertex2fv(p3);glVertex2fv(p4);glVertex2fv(p5);glVertex2fv(p6);

glVertex2fv(p3);glVertex2fv(p4);glVertex2fv(p5);glVertex2fv(p6);

p2p6

g (p );glVertex2fv(p7);

glEnd();

g (p );glVertex2fv(p7);

glEnd(); p3p4

p5

Polygons in OpenGL (2)Polygons in OpenGL (2)

■ Quadrilaterals■ Quadrilaterals

glBegin(GL_QUADS);glVertex2fv(p0);

glBegin(GL_QUADS);glVertex2fv(p0); p0glVertex2fv(p0);glVertex2fv(p1);glVertex2fv(p2);glVertex2fv(p3);

glVertex2fv(p0);glVertex2fv(p1);glVertex2fv(p2);glVertex2fv(p3);

p0p1

p7

glVertex2fv(p3);glVertex2fv(p4);glVertex2fv(p5);glVertex2fv(p6);

glVertex2fv(p3);glVertex2fv(p4);glVertex2fv(p5);glVertex2fv(p6);

p2p6

g (p );glVertex2fv(p7);

glEnd();

g (p );glVertex2fv(p7);

glEnd(); p3p4

p5

Page 19: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

Polygons in OpenGL (3)Polygons in OpenGL (3)

■ Quadstrip■ Quadstrip

glBegin(GL_QUAD_STRIP);glVertex2fv(p0);

glBegin(GL_QUAD_STRIP);glVertex2fv(p0); p1glVertex2fv(p0);glVertex2fv(p1);glVertex2fv(p2);glVertex2fv(p3);

glVertex2fv(p0);glVertex2fv(p1);glVertex2fv(p2);glVertex2fv(p3);

pp3

p0

glVertex2fv(p3);glVertex2fv(p4);glVertex2fv(p5);glVertex2fv(p6);

glVertex2fv(p3);glVertex2fv(p4);glVertex2fv(p5);glVertex2fv(p6);

p5p2

g (p );glVertex2fv(p7);

glEnd();

g (p );glVertex2fv(p7);

glEnd(); p7p6

p4

Polygons in OpenGL (4)Polygons in OpenGL (4)

■ Triangles■ Triangles

glBegin(GL_TRIANGLES);glVertex2fv(p0);

glBegin(GL_TRIANGLES);glVertex2fv(p0); p0glVertex2fv(p0);glVertex2fv(p1);glVertex2fv(p2);glVertex2fv(p3);

glVertex2fv(p0);glVertex2fv(p1);glVertex2fv(p2);glVertex2fv(p3);

p0p1

p7

glVertex2fv(p3);glVertex2fv(p4);glVertex2fv(p5);glVertex2fv(p6);

glVertex2fv(p3);glVertex2fv(p4);glVertex2fv(p5);glVertex2fv(p6);

p2p6

g (p );glVertex2fv(p7);

glEnd();

g (p );glVertex2fv(p7);

glEnd(); p3p4

p5

Page 20: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

Polygons in OpenGL (5)Polygons in OpenGL (5)

■ Triangle strip■ Triangle strip

glBegin(GL_TRIANGLE_STRIP);glVertex2fv(p0);

glBegin(GL_TRIANGLE_STRIP);glVertex2fv(p0); p0glVertex2fv(p0);glVertex2fv(p1);glVertex2fv(p2);glVertex2fv(p3);

glVertex2fv(p0);glVertex2fv(p1);glVertex2fv(p2);glVertex2fv(p3);

p0p2

p1

glVertex2fv(p3);glVertex2fv(p4);glVertex2fv(p5);glVertex2fv(p6);

glVertex2fv(p3);glVertex2fv(p4);glVertex2fv(p5);glVertex2fv(p6);

p4p3

g (p );glVertex2fv(p7);

glEnd();

g (p );glVertex2fv(p7);

glEnd(); p6p7

p5

Polygons in OpenGL (6)Polygons in OpenGL (6)

■ Triangle fan■ Triangle fan

glBegin(GL_TRIANGLE_FAN);glVertex2fv(p0);

glBegin(GL_TRIANGLE_FAN);glVertex2fv(p0); p0glVertex2fv(p0);glVertex2fv(p1);glVertex2fv(p2);glVertex2fv(p3);

glVertex2fv(p0);glVertex2fv(p1);glVertex2fv(p2);glVertex2fv(p3);

p0p1

p7

glVertex2fv(p3);glVertex2fv(p4);glVertex2fv(p5);glVertex2fv(p6);

glVertex2fv(p3);glVertex2fv(p4);glVertex2fv(p5);glVertex2fv(p6);

p2p6

g (p );glVertex2fv(p7);

glEnd();

g (p );glVertex2fv(p7);

glEnd(); p3p4

p5

Page 21: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

연습문제 (3)연습문제 (3)

■ 오브젝트 뒤에 사각형을 추가하시오■ 오브젝트 뒤에 사각형을 추가하시오.

AttributesAttributes

■ Properties that determines how to render a■ Properties that determines how to render a geometric primitive (appearance of objects)

Color (points lines polygons)■ Color (points, lines, polygons)■ Size and width (points, lines)

Stipple pattern (lines polygons)■ Stipple pattern (lines, polygons)■ Polygon mode

Display as filled: solid color or stipple pattern■ Display as filled: solid color or stipple pattern■ Display edges■ Display verticesp y

Page 22: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

Setting of Color Attributes (1)Setting of Color Attributes (1)

■ Setting of the clear color■ Setting of the clear color

■ Opaque: opacity is 1.0

glClearColorglClearColor(1.0, 1.0, 1.0, 1.0);

■ Opaque: opacity is 1.0■ Window is cleared by white color

■ Setting of the color state variable■ Setting of the color state variable

■ RGB color – red colorglColor3fglColor3f(1.0, 0.0, 0.0);

■ Setting of the size of points■ 2 pixels wide: glPointSizeglPointSize(2.0);

■ Setting of the width of lines■ 2 pixels width: glLineWidthglLineWidth(2.0);

Setting of Color Attributes (2)Setting of Color Attributes (2)

■ Creating conceptual vertex colors■ Creating conceptual vertex colorsglColor3fglColor3f(1.0, 0.0, 0.0);glVertex2fglVertex2f(0.5, 0.5);glVertex2fglVertex2f(0.5, 0.5);glColor3fglColor3f(0.0, 0.0, 1.0);glVertex2fglVertex2f(-0.5, -0.5);

■ Default – smooth shading■ Alternative – flat shading

■ Color of first vertex determines fill color

glShadeModelglShadeModel(GL_SMOOTH);glShadeModelglShadeModel(GL_FLAT);

Page 23: OpenGL Programminggraphics.hallym.ac.kr/teach/2009/cga/src/01prac.pdf · 2010-03-02 · OpenGL API Easy to learn, compared with other APIs Device Nevertheless powerful Graphics functions:

연습문제 (4)연습문제 (4)

■ 사각형의 꼭지점마다 색을 다르게 지정하고 사■ 사각형의 꼭지점마다 색을 다르게 지정하고, 사각형의 테두리를 두께 10.0f로 그리시오.