Getting Started. 1 st Week, Sun-Jeong Kim. Computer Graphics Applications

Similar documents
Windows Programming. 1 st Week, 2011

Graphics Programming

Graphics Programming. August 31, Programming of the Sierpinski gasket. Programming with OpenGL and C/C++

We display some text in the middle of a window, and see how the text remains there whenever the window is re-sized or moved.

Game Programming I. Introduction to Windows Programming. Sample Program hello.cpp. 5 th Week,

Input and Interaction

Window programming. Programming

Windows and Messages. Creating the Window

Chapter 15 Programming Paradigm

Meshing and Geometry

Programming using OpenGL: A first Introduction

Programming with OpenGL Part 2: Complete Programs Computer Graphics I, Fall

Teacher Assistant : Tamir Grossinger Reception hours: by - Building 37 / office -102 Assignments: 4 programing using

Computer Graphics. Basic 3D Programming. Contents

Computer graphics MN1

LSN 4 GUI Programming Using The WIN32 API

Precept 2 Aleksey Boyko February 18, 2011

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

Getting Started. Overview (1): Getting Started (1): Getting Started (2): Getting Started (3): COSC 4431/5331 Computer Graphics.

Course 3D_OpenGL: 3D-Graphics with C++ and OpenGL Chapter 1: Moving Triangles

Introduction to OpenGL Week 1

Objectives. Image Formation Revisited. Physical Approaches. The Programmer s Interface. Practical Approach. Introduction to OpenGL Week 1

An Introduction to Windows Programming Using VC++ Computer Graphics. Binghamton University. EngiNet. Thomas J. Watson

Computer Graphics. Chapter 10 Three-Dimensional Viewing

Lectures OpenGL Introduction

OpenGL. Toolkits.

// double buffering and RGB glutinitdisplaymode(glut_double GLUT_RGBA); // your own initializations

To Do. Computer Graphics (Fall 2008) Course Outline. Course Outline. Methodology for Lecture. Demo: Surreal (HW 3)

Drawing Primitives. OpenGL basics

CS 4204 Computer Graphics

CSE4030 Introduction to Computer Graphics

Lecture 4 of 41. Lab 1a: OpenGL Basics

Computer Graphics (Basic OpenGL)

Basic Graphics Programming

ERKELEY DAVIS IRVINE LOS ANGELES RIVERSIDE SAN DIEGO SAN FRANCISCO EECS 104. Fundamentals of Computer Graphics. OpenGL

CMSC 425: Lecture 4 More about OpenGL and GLUT Tuesday, Feb 5, 2013

3D computer graphics: geometric modeling of objects in the computer and rendering them

Early History of APIs. PHIGS and X. SGI and GL. Programming with OpenGL Part 1: Background. Objectives

Introduction to OpenGL Transformations, Viewing and Lighting. Ali Bigdelou

COMPUTER GRAPHICS LAB # 3

CS354 Computer Graphics Viewing and Modeling

Tutorial 3: A Simple Window

Programming with OpenGL Part 1: Background

Introduction to Computer Graphics with OpenGL/GLUT

CS410 Visual Programming Solved Online Quiz No. 01, 02, 03 and 04. For Final Term Exam Preparation by Virtualians Social Network

CSC Graphics Programming. Budditha Hettige Department of Statistics and Computer Science

OpenGL Introduction Computer Graphics and Visualization

Programming with OpenGL Part 1: Background

4 th (Programmatic Branch ) Advance Windows Programming class

Computer Graphics. OpenGL

Advanced Computer Graphics (CS & SE )

Computer Graphics. Bing-Yu Chen National Taiwan University

Cameras (and eye) Ideal Pinhole. Real Pinhole. Real + lens. Depth of field

Graphics Programming. 1. The Sierpinski Gasket. Chapter 2. Introduction:

Computer Programming Lecture 11 이윤진서울대학교

Describe the Orthographic and Perspective projections. How do we combine together transform matrices?

Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

Lecture 2 CISC440/640 Spring Department of Computer and Information Science

Graphics Pipeline & APIs

OpenGL: Open Graphics Library. Introduction to OpenGL Part II. How do I render a geometric primitive? What is OpenGL

OpenGL Tutorial. Ceng 477 Introduction to Computer Graphics

Spring 2013, CS 112 Programming Assignment 2 Submission Due: April 26, 2013

CS450/550. Pipeline Architecture. Adapted From: Angel and Shreiner: Interactive Computer Graphics6E Addison-Wesley 2012

3D Graphics for Game Programming (J. Han) Chapter II Vertex Processing

Models and Architectures. Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico

CSE 167: Lecture #4: Vertex Transformation. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2012

OpenGL Basics I. Seoul National University Graphics & Media Lab

Models and Architectures

Visualizing Molecular Dynamics

Computer graphic -- Programming with OpenGL I

CSC 8470 Computer Graphics. What is Computer Graphics?

Graphics Pipeline & APIs

Introduction to Computer Graphics with WebGL

CSE4030 Introduction to Computer Graphics

Transformation Pipeline

Transformations (Rotations with Quaternions) October 24, 2005

Graphics Hardware and OpenGL

Basic Graphics Programming

CS559: Computer Graphics. Lecture 12: OpenGL Transformation Li Zhang Spring 2008

OpenGL. Jimmy Johansson Norrköping Visualization and Interaction Studio Linköping University

Assignment 1. Simple Graphics program using OpenGL

OpenGL/GLUT Intro. Week 1, Fri Jan 12

Computer Graphics. Chapter 3 Computer Graphics Software

Image Rendering. Rendering can be divided into three sub-problems. Image Formation The hidden surface problem visibility determination steps

11/1/13. Basic Graphics Programming. Teaching Assistant. What is OpenGL. Course Producer. Where is OpenGL used. Graphics library (API)

Luiz Fernando Martha André Pereira

Announcements OpenGL. Computer Graphics. Spring CS4815

Announcements OpenGL. Computer Graphics. Autumn 2009 CS4815

6.837 Introduction to Computer Graphics Assignment 5: OpenGL and Solid Textures Due Wednesday October 22, 2003 at 11:59pm

Announcement. Homework 1 has been posted in dropbox and course website. Due: 1:15 pm, Monday, September 12

Computer Viewing Computer Graphics I, Fall 2008

3D Graphics and OpenGl. First Steps

1 (Practice 1) Introduction to OpenGL

Computer Graphics. Making Pictures. Computer Graphics CSC470 1

CS559: Computer Graphics. Lecture 12: OpenGL Li Zhang Spring 2008

CS 4731 Lecture 3: Introduction to OpenGL and GLUT: Part II. Emmanuel Agu

Computer Graphics 1 Computer Graphics 1

Computer Graphics Programming

OpenGL Transformations

CSE 167: Introduction to Computer Graphics Lecture #4: Vertex Transformation

Transcription:

OpenGL Programming Getting Started 1 st Week, 2008 Sun-Jeong Kim Visual Studio 2005 Windows Programming 2

Visual C++ Win32 Application New Project 3 Empty project Application Settings 4

Solution Prac01 5 Downloading main0.cpp 6

Adding a Source File (1) Project Add Existing Item 7 Adding a Source File (2) 8

Building the Solution 9 Compiling Errors 10

Unicode vs. Multi-Byte SBCS (Single Byte Character Set) ASCII code: 1 byte character set DBCS (Double Byte Character Set) Korean MBCS (Multi Byte Character Set) SBCS + DBCS: ~ Windows 9x WBCS (Wide Byte Character Set) Unicode: Windows 2000 ~ 11 Project Properties 12

Result main0.cpp 13 main0.cpp #include <windows.h> HWND MyMWindowHandle = 0; LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM ); int WINAPI WinMain( HINSTANCE hinstance, HINSTANCE, LPSTR, int nshowcmd ) { // Create the main window } // Main message loop LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam ) { } 14

Includes, Global Variables, and Prototypes Includes #include <windows.h> Obtaining the structures, types, and function declarations needed for using the basic elements of the Win32 API Global Variables HWND MainWindowHandle = 0; Handle to a window to refer our main application window Prototypes LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam); Our main window s window procedure 15 WinMain ( ) The Windows equivalent to the main() in normal C++ programming int WINAPI WinMain( HINSTANCE hinstance, HINSTANCE hprevinstance, PSTR pcmdline, int nshowcmd ); hinstance: Handle to the current application instance hprevinstance: Not used in Win32 programming lpcmdline: The command line argument string used to run the program ncmdshow: Specifying how the application should be displayed 16

WNDCLASS (1) Describing our window with WNDCLSS typedef struct _WNDCLASS { UINT style; WNDPROC lpfnwndproc; int cbclsextra; int cbwndextra; HANDLE hinstance; HICON hicon; HCURSOR hcursor; HBRUSH hbrbackground; LPCTSTR lpszmenuname; LPCTSTR lpszclassname; } WNDCLASS; style: Specifying the class style lpfnwndproc: Pointer to the window procedure function cbclsextra and cbwndextra: Extra memory slots 17 WNDCLASS (2) hinstance: A handle to our application instance hicon: Specifying a handle to an icon hcursor: Specifying a handle to a cursor hbrbackground: Specifying the background of the client area of the window lpszmenuname: Specifying the window s menu lpszclassname: Specifying the name of the window class structure 18

Creating the Window (1) Creating a window based on WNDCLASS description HWND CreateWindow( LPCTSTR lpclassname, LPCTSTR lpwindowname, DWORD dwstyle, int x, int y, int nwidth, int nheight, HWND hwndparent, HMENU hmenu, HANDLE hinstance, PVOID lpparam ); lpclassname: : The name of the registered WNDCLASS structure lpwindowname: The name we want to give our window 19 Creating the Window (2) dwstyle: Defining the style of window x: The position at the top-left corner of the window y: The position at the top-left corner of the window nwidth: The width of the window in pixels nheight: The height of the window in pixels hwndparent: Handle to a window that is to be the parent of this window hmenu: A handle to menu hinstance: Handle to the application lpparam: A pointer to user-defined data 20

The Message Loop (1) The message structure that represents a Windows message typedef struct tagmsg { HWND hwnd; UINT message; WPARAM wparam; LPARAM lparam; DWORD time; POINT pt; } MSG; hwnd: The handle to the window message: A predefined constant value identifying the message (e.g. WM_QUIT) wparam and lparam: : Extra information time: The time the message was posted pt: The (x, y) coordinates of the mouse cursor 21 The Message Loop (2) Entering the message loop while( GetMessage( &msg, 0, 0, 0 ) ) { TranslateMessage( &msg g ); DispatchMessage( &msg ); } GetMessage() Returning true unless a WM_QUIT Retrieving a message from the message queue Filling in members of MSG structure TranslateMessage() Performing some keyboard translation and virtual key messages to character messages specifically DispatchMessage() Dispatching the message off to the appropriate window proc. 22

The Window Procedure (1) Executing in response to a message our window LRESULT CALLBACK WndProc( receives ); HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam Callback function: Windows will be calling this function outside of the code space of the program hwnd: The handle to the window receiving the message umsg: A predefined value that identifies the particular message (e.g. WM_QUIT) wparam and lparam: Extra information about the message 23 The Window Procedure (2) Handling three messages: switch( msg ) { case WM_KEYDOWN: if( wparam == VK_ESCAPE ) if( IDYES == MessageBox( NULL, "Are you sure to exit?", "Hello", MB_YESNO ) ) DestroyWindow( MyMWindowHandle ); break; case WM_DESTROY: PostQuitMessage( 0 ); break; } default: return DefWindowProc( hwnd, msg, wparam, lparam ); WM_KEYDOWN: pressing a key (c.f. virtual key code) WM_DESTROY: destroying the window 24

The Window Procedure (3) Default window procedure return DefWindowProc( hwnd, msg, wparam, lparam ); Win32 API function Using default behavior for all other messages (e.g. minimizing, maximizing, resizing, and closing a window) 25 The Message Box Function Win32 API function int MessageBox( HWND hwnd, LPCTSTR lptext, LPCTSTR lpcaption, UINT utype ); hwnd: Handle of owner window (may specify null) lptext: Text to put in the message box lpcaption: : Text for the title of the message box utype: Style of the message box MessageBox( NULL, Are you sure to exit?", "Hello", MB_YESNO ); 26

Exercises (1) Window 의크기를 800x600 으로고정하시오. Window 의배경을검정색으로칠하시오. Window 의제목을자신의학번과이름으로고치시오. 화살표키를눌렀을때, 다음과같은 Message Box 를생성하시오. 좌측마우스버튼을클릭했을때다음과같은 Message Box를생성하시오. 27 Changing a Source File (1) 28

Changing a Source File (2) 29 Building the Solution 30

Compiling Errors 31 Linking the Library Files Project Properties Configuration Properties Linker Input Additional Dependencies: opengl32.lib lib glu32.lib lib 32

OpenGL API (1) API (application programming interface) Interface between an application program and a graphics system Application Program OpenGL interface Graphics Library (API) GLU Hardware Input Device Output Device OpenGL Application Program GL GLUT Xlib, Xtk Frame Buffer GLX 33 OpenGL interface OpenGL API (2) OpenGL core library (names : gl~) : GL OpenGL utility library (names : glu~) : GLU Using only GL functions Creating common objects Other tasks that users prefer not to write repeatedly OpenGL utility toolkit (names : glut~) : GLUT Interface with the window system 34

OpenGL API (3) OpenGL architecture Immediate Mode Geometry Pipeline Polynomial Evaluator Per Vertex Operations & Primitive Assembly CPU Display List Rasterization Per Fragment Operations Frame Buffer Pixel Operations Texture Memory 35 OpenGL API (4) OpenGL function format Function name Dimensions glvertex3f( x, y, z ); Belongs to GL library x, y, z are floats p is a pointer to an array glvertex3fv( p ); 36

Result main1.cpp 37 main1.cpp #include <windows.h> #include <gl/gl.h> #include <gl/glu.h> HWND MyMWindowHandle dl = 0; HDC MyDC; HGLRC MyRC; LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM ); bool bsetuppixelformat( void ); void Resize( const int cx, const int cy ); void DrawScene( void ); int WINAPI WinMain( HINSTANCE hinstance, HINSTANCE, LPSTR, int nshowcmd ) { // Create the main window // Main message loop } 38

OpenGL Camera OpenGL places a camera at the origin in world space pointing in the negative z direction Default viewing volume a box centered at the origin with a side of length 2 The default camera and an orthographic view volume 39 Orthographic Viewing Default orthographic view Projecting points forward along the z axis onto the plane z=0 z=0 z=0 View Volume Orthographic Projection 40

Viewing (1) In OpenGL, viewing is carried out by projection matrix First, set the matrix mode first Two most important matrices: model-view and projection glmatrixmode(gl_projection GL_PROJECTION); Start with an identity matrix Multiplying or concatenating a number of transformation matrices glloadidentity(); Alter it with a projection matrix Two- and three-dimensional viewing glortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); 41 Viewing (2) Three-dimensional viewing glortho(left, right, bottom, top, near, far); near, far: distances measured from the camera Two-dimensional viewing Application i is in two dimension i Placing all vertices in the plane z=0 gluortho2d(left, right, bottom, top); View or clipping volume == clipping window Objects before clipping 42 Image after clipping

Viewport Viewports A rectangular area of the display window Values in pixels Aspect ratio of a rectangle glviewport(x, y, w, h); The ratio of the rectangle s width to its height 43 Points Lines Line segments Polylines Polygons Polygons Primitives in OpenGL Triangles and quadrilaterals Strips and fans Text Curved objects 44

Points in OpenGL glbegin(gl_points); glvertex2fv(p0); glvertex2fv(p1); p7 p0 p1 glvertex2fv(p2); glvertex2fv(p3); p6 p2 glvertex2fv(p4); glvertex2fv(p5); p5 p3 glvertex2fv(p6); glvertex2fv(p7); p4 glend(); 45 Line Segments Lines in OpenGL (1) glbegin(gl_lines); glvertex2fv(p0); glvertex2fv(p1); p7 p0 p1 glvertex2fv(p2); glvertex2fv(p3); glvertex2fv(p4); p6 p2 glvertex2fv(p5); glvertex2fv(p6); p5 p3 glvertex2fv(p7); glend(); p4 46

Polylines Line Strip Lines in OpenGL (2) glbegin(gl_line_strip); glvertex2fv(p0); glvertex2fv(p1); p7 p0 p1 glvertex2fv(p2); glvertex2fv(p3); glvertex2fv(p4); p6 p2 glvertex2fv(p5); glvertex2fv(p6); glvertex2fv(p7); glend(); p5 p4 p3 47 Polylines Line Loop Lines in OpenGL (3) glbegin(gl_line_loop); glvertex2fv(p0); glvertex2fv(p1); p7 p0 p1 glvertex2fv(p2); glvertex2fv(p3); glvertex2fv(p4); p6 p2 glvertex2fv(p5); glvertex2fv(p6); glvertex2fv(p7); glend(); p5 p4 p3 48

Polygon Polygons in OpenGL (1) glbegin(gl_polygon); glvertex2fv(p0); glvertex2fv(p1); p7 p0 p1 glvertex2fv(p2); glvertex2fv(p3); glvertex2fv(p4); p6 p2 glvertex2fv(p5); glvertex2fv(p6); glvertex2fv(p7); glend(); p5 p4 p3 49 Quadrilaterals Polygons in OpenGL (2) glbegin(gl_quads); glvertex2fv(p0); glvertex2fv(p1); p7 p0 p1 glvertex2fv(p2); glvertex2fv(p3); glvertex2fv(p4); p6 p2 glvertex2fv(p5); glvertex2fv(p6); glvertex2fv(p7); glend(); p5 p4 p3 50

Quadstrip Polygons in OpenGL (3) glbegin(gl_quad_strip); glvertex2fv(p0); glvertex2fv(p1); p0 p1 p3 glvertex2fv(p2); glvertex2fv(p3); glvertex2fv(p4); p2 p5 glvertex2fv(p5); glvertex2fv(p6); glvertex2fv(p7); glend(); p4 p6 p7 51 Triangles Polygons in OpenGL (4) glbegin(gl_triangles); glvertex2fv(p0); glvertex2fv(p1); p7 p0 p1 glvertex2fv(p2); glvertex2fv(p3); glvertex2fv(p4); p6 p2 glvertex2fv(p5); glvertex2fv(p6); glvertex2fv(p7); glend(); p5 p4 p3 52

Triangle Strip Polygons in OpenGL (5) glbegin(gl_triangle_strip); glvertex2fv(p0); glvertex2fv(p1); p1 p0 p2 glvertex2fv(p2); glvertex2fv(p3); glvertex2fv(p4); p3 p4 glvertex2fv(p5); glvertex2fv(p6); glvertex2fv(p7); glend(); p5 p7 p6 53 Triangle Fan Polygons in OpenGL (6) glbegin(gl_triangle_fan); glvertex2fv(p0); glvertex2fv(p1); p7 p0 p1 glvertex2fv(p2); glvertex2fv(p3); glvertex2fv(p4); p6 p2 glvertex2fv(p5); glvertex2fv(p6); glvertex2fv(p7); glend(); p5 p4 p3 54

Setting of Color Attributes (1) Setting of the clear color glclearcolor(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 glcolor3f(1.0, 0.0, 0.0); RGB color red color Setting of the size of points 2 pixels wide: glpointsize(2.0); Setting of the width of lines 2 pixels width: gllinewidth(2.0); 55 Setting of Color Attributes (2) Creating conceptual vertex colors glcolor3f(1.0, 0.0, 0.0); glvertex2f(0.5, 0.5); glcolor3f(0.0, 0.0, 1.0); glvertex2f(-0.5, -0.5); Default smooth shading Alternative flat shading Color of first vertex determines fill color glshademodel(gl_smooth); glshademodel(gl_flat); 56

The Coordinate Systems The origin + the bases (axes) Right-handed handed coordinate system: OpenGL Left-handed coordinate system: DirectX 57 The Coordinate System The Origin 58

Result The Origin 59 The Coordinate System The Axes 60

Result The Axes 61 Exercises (2) glveiwport( ) 함수의입력인자를변경해보시오. 예 ) (cx, cy) (200, 200), (0, 0) (100, 100) glorhto( ) 함수의입력인자를변경해보시오. 예 )10 1.0 20 2.0, ratio 제거 glclearcolor( ) 함수의입력인자를변경해보시오. 예 ) (1.0f, 1.0f, 1.0f, 1.0f) (0.0f, 0.0f, 0.0f, 1.0f) 62

Locating Camera (1) Setting position and orient of the camera within the world coordinate system void glulookat(gldouble eyex, GLdouble eyey, GLdouble eyez, GLdouble atx, GLdouble aty, GLdouble atz, GLdouble upx, GLdouble upy, GLdouble upz) 63 Locating Camera (2) 64

Result Locating Camera 65 Exercises (3) 화살표에따라카메라의위치를변경해보시오. Left 화살표 : 왼쪽으로 0.1 씩이동 Right 화살표 : 오른쪽으로 01 0.1 씩이동 Up 화살표 : 위쪽으로 0.1 씩이동 Down 화살표 : 아래로 01 0.1 씩이동 66