OpenGL Programming – Day 1

Slides:



Advertisements
Similar presentations
Programming with OpenGL - Getting started - Hanyang University Han Jae-Hyek.
Advertisements

OpenGL CMSC340 3D Character Design & Animation. What is OpenGL? A low-level graphics library specification designed for use with the C and C++ provides…
OPEN GL. Install GLUT Download package di sini Dari devcpp, buka Tools->PackageManager-
Chapter 2: Graphics Programming
Computer Graphics CSCE 441
What is OpenGL? Low level 2D and 3D Graphics Library Competitor to Direct3D (the rendering part of DirectX) Used in: CAD, virtual reality, scientific.
CSC 461: Lecture 51 CSC461 Lecture 5: Simple OpenGL Program Objectives: Discuss a simple program Discuss a simple program Introduce the OpenGL program.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
OpenGL and Projections
30/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 1: Introduction.
Programming with OpenGL Part 1: Background Mohan Sridharan Based on slides created by Edward Angel CS4395: Computer Graphics 1.
Introduction to OpenGL Keng Shih-Ling 2003 Spring.
Write a Simple Program with OpenGL & GLUT. Books and Web Books OpenGL Programming Guide (Red-book) OpenGL Reference Manual (Blue-book) OpenGL Super Bible.
Reference1. [OpenGL course slides by Rasmus Stenholt]
CS380 LAB I OpenGL Donghyuk Kim Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003.
Computer Graphics Bing-Yu Chen National Taiwan University.
Introduction to OpenGL and GLUT GLUT. What is OpenGL? An application programming interface (API) A (low-level) Graphics rendering API Generate high-quality.
Using OpenGL in Visual C++ Opengl32.dll and glu32.dll should be in the system folder Opengl32.dll and glu32.dll should be in the system folder Opengl32.lib.
CSC 461: Lecture 41 CSC461: Lecture 4 Introduction to OpenGL Objectives: Development of the OpenGL API OpenGL Architecture -- OpenGL as a state machine.
OpenGl Graphics Programming. Introduction OpenGL is a low-level graphics library specification. It makes available to the programmer a small set of geomteric.
Chapter 1: Hello, MFC Windows Programming Model Department of Digital Contents Sang Il Park.
Lecture 1: Introduction. Pick Your Version of GLUT OpenGL and the GLUT are available for Windows, Linux and many other Operating Systems and platforms.
Programming with OpenGL Part 1: Background
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
1. OpenGL/GLU/GLUT  OpenGL v4.0 (latest) is the “core” library that is platform independent  GLUT v3.7 is an auxiliary library that handles window creation,
1 Figures are extracted from Angel's book (ISBN x) The Human Visual System vs The Pinhole camera Human Visual System Visible Spectrum Pinhole.
CD2012 Principles of Interactive Graphics Lecture 01 Introduction Abir Hussain (Rome: 6.33,Tel , Web:
Introduction to GL Geb Thomas. Example Code int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals.
Modeling with OpenGL Practice with OpenGL transformations.
Introduction to OpenGL and GLUT. What’s OpenGL? An Application Programming Interface (API) A low-level graphics programming API – Contains over 250 functions.
Chun-Yuan Lin Introduction to OpenGL 2015/12/19 1 CG.
NoufNaief.net TA: Nouf Al-harbi.
CGGM Lab. Tan-Chi Ho Introduction to OpenGL.
Computer Graphics Lab 1 OpenGL.
1 How to Install OpenGL u Software running under Microsoft Windows makes extensive use of "dynamic link libraries." A dynamic link library (DLL) is a set.
Introduction to OpenGL & HW1 Announcement 劉軒銘, 網媒所 碩二 ICG 2012 Fall.
Doç. Dr. Cemil Öz SAÜ Bilgisayar Mühendisliği Dr. Cemil Öz.
Lecture 2: Introduction to OpenGL
OpenGL Basic Drawing 2003 Spring Keng Shih-Ling
Chap 2 Write a Simple OpenGL Program. Preparing 1/2 environment : Microsoft Visual C 、 Microsoft Visual C++.Net Also need : GLUT
Introduction to OpenGL
Introduction to Graphics Programming. Graphics API.
INTRODUCTION TO OPENGL
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Computer Graphics -practical- Lecture 6. (visual c++) open gl library To use open GL with VC++ we add these files:- 1)Glut.h C:\program files\ Microsoft.
Lecture 8: Discussion of papers OpenGL programming Lecturer: Simon Winberg Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
Introduction to OpenGL (IDE: Eclipse)
The Human Visual System vs The Pinhole camera
Programming with OpenGL Part 1: Background
CS380 Lab Spring Myungbae Son.
Programming with OpenGL Part 1: Background
Windows Programming Model
Introduction to OpenGL
Programming with OpenGL Part 2: Complete Programs
OpenGL API 2D Graphic Primitives
Graphics Programming (I)
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
גרפיקה ממוחשבת: מבוא ל-OpenGL
The program in traditional OS
Programming with OpenGL Part 1: Background
Drawing in the plane 455.
Windows Programming Lecture 13
Introduction to OpenGL
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 1: Background
Graphics Laboratory Korea University
GUI Socket Application
Presentation transcript:

OpenGL Programming – Day 1 Young-Min Kang Tongmyong Univ.

Introduction to OpenGL What is OpenGL? Software interface to graphics hardware Open Graphics Library Free Platform independent OpenGL is a library Set of functions OpenGL is platform independent No window management

OpenGL vs. DirectX OpenGL DirectX Advantage of OpenGL Platform independent DirectX Optimized for MS-Windows on PC Advantage of OpenGL Compatibility Advantage of DirectX Optimized Performance

OpenGL

Basic OpenGL Programming OpenGL Source Code #include <WhateverYouNeed.h> main () { InitializeWindowPlease(); glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f, 1.0f, 1.0f); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); glBegin(GL_POLYGON); glVertex3f(0.25, 0.25, 0.0); glVertex3f(0.75, 0.25, 0.0); glVertex3f(0.75, 0.75, 0.0); glVertex3f(0.25, 0.75, 0.0); glEnd(); glFlush(); }

OpenGL Utilities OpenGL provides GLU GLUT A small set of geometric primitives Points, lines, and polygons GLU GL Utilities: modeling and viewing manipulation GLUT A simple windowing API

OpenGL Programming Conventions OpenGL conventions OpenGL Commands Starts with gl- prefix Suffix conventions {2|3|4}{s|i|f|d}[v] 2, 3, 4 determines the dimension s: short / i: integer / f: float / d: double v: array type glVertex3f(0.3, 1.0, 0.0); float vertex[3] = { 0.3, 1.0, 0.0 }; glVertex3fv(vertex);

OpenGL Programming Conventions OpenGL coordinates – Homogeneous coordinate glVertex2f(1, 2);  (1,2,0,1) glVertex3f(1, 2, 0);  (1,2,0,1) glVertex4f(1, 2, 0, 1);  (1,2,0,1) OpenGL as a state machine OpenGL is a state machine States can be turned on or off Rendering conditions are set and maintained until changed

OpenGL Programming Document Resources Programming guide http://www.opengl.org/documentation/red_book_1.0/ Reference manual http://www.rush3d.com/reference/opengl-bluebook-1.0/ What is needed for OpenGL Programming Headers: Gl.h, glu.h Libraries: Opengl32.lib, glut32.lib DLLs: Opengl32.lib, glu32.lib Optional headers and libraries: Glaux.h, glaux.lib / glut.h, glut32.lib, glut32.dll

GLUT download GLUT – The OpenGL Utility Toolkit Why GLUT is needed http://www.opengl.org/resources/libraries/glut.html http://www.xmission.com/~nate/glut.html Why GLUT is needed OpenGL has no methods to manage windows GLUT Window system independent toolkit for writing OpenGL program A simple windowing application programming interface GLUT makes it extremely easy to learn OpenGL programming

GLUT installation GLUT GLUT.h GLUT32.lib GLUT32.dll [$include]/GL/ GLUT32.lib [$lib]/ GLUT32.dll C:\Windows\System

OpenGL Programming OpenGL programming with GLUT MS Visual Studio .Net Environments Where are the OpenGL headers and libraries Headers Microsoft Visual Studio .Net 2003\Vc7\PlatformSDK\Include\gl Gl.h, glu.h, glaux.h Libraries Microsoft Visual Studio .Net 2003\Vc7\PlatformSDK\Lib Opengl32.lib, glu32.lib, glaux.lib Copy glut files to proper directories Glut.h  …\Vc7\PlatformSDK\Include\gl Glut32.lib  …\Vc7\PlatformSDK\Lib Glut32.dll  C:\Windows\System

OpenGL programming Simple Program Create win32 console project

OpenGL programming Menu: Project  Properties (Linker  input) Additional Dependency Opengl32.lib glu32.lib glaux.lib glut32.lib

OpenGL programming Add a source code item

OpenGL Programming with GLUT Headers #include <windows.h> #include <gl/gl.h> #include <gl/glut.h> Function prototypes void display(void); OpenGL display function void init(void); OpenGL initialization function

OpenGL Programming with GLUT Main function int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA); glutInitWindowSize(250, 250); glutInitWindowPosition(100,100); glutCreateWindow("My First OpenGL Program"); init(); glutDisplayFunc(display); glutMainLoop(); return 0; }

OpenGL Programming with GLUT Initialization Function void init(void) { glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); } glClearColor Determines the color to be used for clearing buffer glMatrixMode Determines which matrix would be changed Projection matrix / Model_View matrix LoadIdentity Loads identity matrix glOrtho Sets an orthogonal projection matrix

OpenGL Programming with GLUT OpenGL display function void display(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f, 1.0f, 1.0f); glBegin(GL_POLYGON); glVertex3f(0.25, 0.25, 0.0); glVertex3f(0.75, 0.25, 0.0); glVertex3f(0.75, 0.75, 0.0); glVertex3f(0.25, 0.75, 0.0); glEnd(); glFlush(); }

OpenGL and Win32 OpenGL in MS-Windows OpenGL is platform independent No window management APIs What is needed Option 1 – using GLUT Easy window management Platform independent Disadvantage Console based Option 2 – using Win32 API Perfectly integrated into MS-windows environments Platform dependent Increased complexity in coding

OpenGL and Win32 OpenGL with Win32 Properly include headers and link libraries Get device context Win32 API Set pixel format Create rendering context WGL API is needed Write your own OpenGL code

OpenGL and Win32 Headers and libraries Option 1: GLUT Option 2: Win32 Gl.h / glu.h / glut.h Libraries Opengl32.lib / glu32.lib / glut32.lib Option 2: Win32 Glut.h/.lib/.dll are not necessary

OpenGL and Win32 Device context (DC) How to get a DC? An object which both holds information about a device and provides a way to use the device. Properties DCs refer to an internal data structure in Windows DCs are not accessible to applications programs can only reference device context via a handle How to get a DC? Use GetDC(HWND) function in Win32 API How to release the current DC? Use ReleaseDC(HWND, HDC) function in Win32 API

OpenGL and Win32 Device Context Pixel format Obtaining a DC HDC hDC = GetDC(hWnd); Release the DC ReleaseDC(hDC); Pixel format We need to set the pixel format for the DC Informs the system how we are going to use the DC Whether Double buffering will be used or not Whether Z-buffer will be used or not Which color format will be used …

OpenGL and Win32 Pixel format PIXELFORMATDESCRIPTOR pfd; ZeroMemory( &pfd, sizeof(pfd) ); pfd.nSize = sizeof(pfd); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_GL | PFD_DOUBLEBUFFER; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 24; pfd.cDepthBits = 16; pfd.iLayerType = PFD_MAIN_PLANE; int iFormat = ChoosePixelFormat(hDC, &pfd); SetPixelFormat(hDC, iFormat, &pfd);

OpenGL and Win32 Render Context How to manipulate GL render context A bridge to the OpenGL program HGLRC Handle of GL render context How to manipulate GL render context WGL Creation of GL render context HGLRC hRC; hRC = wglCreateContext(hDC); Deletion of GL render context wglDeleteContext(hRC);

OpenGL and Win32 Current RC Storing old DC and RC Multiple instances of GL RC can run simultaneously Which one is current one? Making a RC current wglMakeCurrent(hDC, hRC); Making a RC not current wglMakeCurrent(NULL, NULL); Storing old DC and RC HDC hOldDC = wglGetCurrentDC(); HDC hOldRC = wglGetCurrentContext(); … wglMakeCurrent(hOldDC, hOldRC);

Win32 Programming Create an empty Win32 project

Win32 Programming Write a simple Win32 program #include <windows.h> HWND Win32Init(HINSTANCE hInst,HINSTANCE hPrev,LPSTR szCmdLine, int sw); void Render(HWND hwnd, HDC hDC); LONG WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam); int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow) { MSG msg; if (!Win32Init(hInst,hPrev,lpCmdLine, nCmdShow)) return FALSE; while(true) { if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam;

Win32 Programming Win32 Initialization Routine HWND Win32Init( HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow); RegisterClass CreateWindow

Win32 Programming BOOL Win32Init(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow) { WNDCLASS WindowClass; BOOL bSuccess; char szWndName[] = "Very Simple W32 Application - Young-Min Kang"; char szClsName[] = "VSW32Class"; if (!hPrev) { WindowClass.hCursor = LoadCursor(0,IDC_ARROW); WindowClass.hIcon = LoadIcon(NULL,IDI_APPLICATION); WindowClass.lpszMenuName = NULL; WindowClass.lpszClassName = szClsName; WindowClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); WindowClass.hInstance = hInst; WindowClass.style = CS_BYTEALIGNCLIENT | CS_VREDRAW | CS_HREDRAW; WindowClass.lpfnWndProc = (WNDPROC)WndProc; WindowClass.cbClsExtra = 0; WindowClass.cbWndExtra = 0; bSuccess = RegisterClass(&WindowClass); if(!bSuccess) return NULL; }

Win32 Programming HWND hWnd = CreateWindow ( szClsName, // Class name szWndName // Window Title /* Window Style */ WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MAXIMIZEBOX | WS_MINIMIZEBOX, 100, 100 // Window Position 512, 512, // Window size 0, // Parent 0, // Class Menu hInst, // Handle to Window Instance 0 //Additional Parameter ); ShowWindow(hWnd, nCmdShow); // display the window return hWnd; }

Win32 Programming WndProc Window procedure that handles message events LONG WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam) { static int x[2], y[2]; HDC hDC = GetDC(hwnd); switch (msg) { case WM_CLOSE: DestroyWindow(hwnd); return FALSE; case WM_DESTROY: PostQuitMessage(0); break; case WM_LBUTTONDOWN: x[0] = LOWORD(lParam); y[0] = HIWORD(lParam); break; case WM_LBUTTONUP: x[1] = LOWORD(lParam); y[1] = HIWORD(lParam); MoveToEx(hDC,x[0],y[0],NULL); LineTo(hDC,x[1],y[1]); ReleaseDC(hwnd,hDC); break; case WM_PAINT: Render(hwnd, hDC); break; } return DefWindowProc(hwnd,msg,wParam,lParam);

OpenGL Programming with Win32 Link libraries

OpenGL Programming with Win32 Include headers #include <windows.h> #include <GL/gl.h> #include <GL/glu.h> OpenGL headers must be proceeded by window.h New functions for OpenGL rendering void OpenGLDisplay(void); void OpenGLInit(void);

OpenGL Programming with Win32 OpenGL Manager class COpenGLManager { public: HWND m_hWnd; HDC m_hDC; HGLRC m_hGLRC; COpenGLManager() { m_hWnd = NULL; m_hDC = NULL; m_hGLRC = NULL; } ~COpenGLManager() { StopGL(); void StartGL(HWND hWnd); void StopGL(void); };

OpenGL Programming with Win32 OpenGL enabling method void COpenGLManager::StartGL(HWND hWnd) { m_hDC = GetDC(m_hWnd = hWnd); PIXELFORMATDESCRIPTOR pfd; ZeroMemory( &pfd, sizeof(pfd)); pfd.nSize = sizeof(pfd); pfd.nVersion = 1; pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 24; pfd.cDepthBits = 16; pfd.iLayerType = PFD_MAIN_PLANE; int pixelFormat = ChoosePixelFormat(m_hDC, &pfd); SetPixelFormat(m_hDC, pixelFormat, &pfd); m_hGLRC = wglCreateContext(m_hDC); wglMakeCurrent(m_hDC, m_hGLRC); }

OpenGL Programming with Win32 OpenGL disabling method void COpenGLManager:: StopGL(void) { wglMakeCurrent(NULL, NULL); if(m_hGLRC) wglDeleteContext(m_hGLRC); if(m_hWnd && m_hDC) ReleaseDC(m_hWnd, m_hDC); } Declaring an OpenGL Manager instance COpenGLManager OGLMan;

OpenGL Programming with Win32 Insert OpenGL-related codes int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nCmdShow) { MSG msg; HWND hWnd; if (! (hWnd = Win32Init(hInst,hPrev,lpCmdLine, nCmdShow))) return FALSE; OGLMan.StartGL(hWnd); OpenGLInit(); while(true) { if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } Render(OGLMan.m_hWnd, OGLMan.m_hDC); OGLMan.StopGL(); return msg.wParam;

OpenGL Programming with Win32 Modify the render function void Render(HWND hwnd, HDC hDC) { OpenGLDisplay(); SwapBuffers(hDC); } Modify the window procedure LONG WndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam) { HDC hDC = GetDC(hwnd); switch (msg) { case WM_CLOSE: OGLMan.StopGL(); DestroyWindow(hwnd); return FALSE; case WM_DESTROY: PostQuitMessage(0); break; return DefWindowProc(hwnd,msg,wParam,lParam);

OpenGL Programming with Win32 OpenGL codes void OpenGLDisplay(void) { glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f, 1.0f, 1.0f); glBegin(GL_POLYGON); glVertex3f(-0.75, -0.75, 0.0); glVertex3f( 0.75, -0.75, 0.0); glVertex3f( 0.75, 0.75, 0.0); glVertex3f(-0.75, 0.75, 0.0); glEnd(); } void OpenGLInit(void) { glClearColor(0.0, 0.0, 0.0, 0.0); glMatrixMode(GL_PROJECTION); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);