1 / 41

OpenGL Programming – Day 1

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.

florrie
Download Presentation

OpenGL Programming – Day 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

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

  4. OpenGL

  5. 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(); }

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

  7. 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);

  8. 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

  9. 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

  10. GLUT download • GLUT – The OpenGL Utility Toolkit • 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

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

  12. 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

  13. OpenGL programming • Simple Program • Create win32 console project

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

  15. OpenGL programming • Add a source code item

  16. 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

  17. 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; }

  18. 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

  19. 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(); }

  20. 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 • Disadvantage • Platform dependent • Increased complexity in coding

  21. 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

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

  23. OpenGL and Win32 • Device context (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

  24. OpenGL and Win32 • Device Context • Obtaining a DC • HDC hDC = GetDC(hWnd); • Release the DC • ReleaseDC(hDC); • Pixel format • We need to set the pixel format for the DC • Pixel format • 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 • …

  25. 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);

  26. OpenGL and Win32 • Render Context • 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);

  27. OpenGL and Win32 • Current 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(hDC, hRC); … wglMakeCurrent(hOldDC, hOldRC);

  28. Win32 Programming • Create an empty Win32 project

  29. 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; }

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

  31. 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; }

  32. 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; }

  33. 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); }

  34. OpenGL Programming with Win32 • Link libraries

  35. 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);

  36. 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); };

  37. 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); }

  38. 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;

  39. 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; }

  40. 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: OGLMan.StopGL(); PostQuitMessage(0); break; } return DefWindowProc(hwnd,msg,wParam,lParam); }

  41. 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); glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); }

More Related