Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //OPENGL 3.2 EXAMPLE
- //NB: DEPRECATED COMMANDS WILL NOT FUNCTION.
- //SEE OPENGL QUICK REFERENCE FOR DETAILS
- #include <windows.h> // Header File For Windows
- //#include <gl\gl.h> // Header File For The OpenGL32 Library
- //#include <gl\glu.h> // Header File For The GLu32 Library
- //-- OPENGL FILES INCLUDED IN GLEW see below
- #include <iostream>
- using namespace std;
- #include <math.h> //sin and cos included in this header file.
- #include "console.h" //Used for displaying the console
- #include "shaders/Shader.h" // include shader header file, this is not part of OpenGL
- //Shader.h has been created using common glsl structure
- //--- OpenGL --- The OpenGL Extension Wrangler Library
- #include "gl/glew.h"
- #include "gl/wglew.h"
- #pragma comment(lib, "glew32.lib")
- //--------------
- unsigned int m_vaoID; // vertex array object
- unsigned int m_vboID[2]; // two VBOs - used for colours and vertex data
- const int numOfVerts = 12; //6 vertices (3 for each tri (x, y, z))
- const int numOfTris = 4; //2 triangles make 1 square
- float verts[36]; //3*6=18 verts (x,y,z for each vert = 1 coord)
- float cols[36]; //same as above for unique colours
- //unsigned int m_vaoID2;
- //unsigned int m_vboID2[2];
- //const int numbOfPoints = 4;
- //float line_vertex[12]; //12 for two lines, 6 for one, XYZ
- //float line_col[12]; //As above for colours RGB
- Shader myShader; //shader object
- ConsoleWindow console;
- int screenWidth=600, screenHeight=600; //changed from 480x480px
- //OPENGL FUNCTION PROTOTYPES
- void display(); //called in winmain to draw everything to the screen
- void init(); //called in winmain when the program starts.
- void createGeometry(); //used to create Vertex Array Objects
- void reshape(int width, int height);
- /************* START OF OPENGL FUNCTIONS ****************/
- void reshape(int width, int height) // Resize the OpenGL window
- {
- int b = 50; //border of 50px
- screenWidth = width;
- screenHeight = height;
- glViewport(0+b,0+b,width-(b*2),height-(b*2)); // set Viewport dimensions
- }
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glUseProgram(myShader.handle()); // use the shader
- //draw objects
- glBindVertexArray(m_vaoID); // select VAO
- //draw some geometry | int points to where to start in array
- glDrawArrays(GL_TRIANGLES, 0, numOfVerts); //change to 3 for half the first triangle
- //glVertexPointer(2, GL_FLOAT,4,&line_vertex);
- //glDrawArrays(GL_LINES,0,numbOfPoints);
- //Enlarge ALL the Points
- glEnable(GL_POINT_SMOOTH);
- glPointSize(50);
- glDrawArrays(GL_POINTS,0, (numOfVerts/2)); //divied the numOfVerts by 2 so only first 12 get enlarged
- glPointSize(25);
- glDrawArrays(GL_POINTS,6, (numOfVerts/2)); //same again but start at 6, only last 6 get enlarged
- // Done
- glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind the buffer
- glBindVertexArray(0); //unbind the vertex array object
- glUseProgram(0); //turn off the current shader
- }
- void createGeometry()
- {
- //First simple object
- float dim = 0.5;
- //Triangle 1
- //points X Y Z
- verts[0] = -dim; verts[ 1] = -dim; verts[ 2] = -dim; //Point 1
- verts[3] = -dim; verts[ 4] = dim; verts[ 5] = -dim; //Point 2
- verts[6] = dim; verts[ 7] = dim; verts[ 8] = -dim; //Point 3
- //Above is same as writing all values in 1 array {..,...,...} etc
- //Triangle 2
- //points X Y Z
- verts[ 9] = dim; verts[10] = -dim; verts[11] = -dim; //Point 4
- verts[12] = -dim; verts[13] = -dim; verts[14] = -dim; //Point 1
- verts[15] = dim; verts[16] = dim; verts[17] = -dim; //Point 3
- //colour R G B
- cols[ 0] = 1.0; cols[ 1] = 0.0; cols[ 2] = 1.0; //Point1 R: 1,0,0
- cols[ 3] = 0.0; cols[ 4] = 1.0; cols[ 5] = 1.0; //Point2 G: 0,1,0
- cols[ 6] = 1.0; cols[ 7] = 1.0; cols[ 8] = 0.0; //Point3 B: 0,0,1
- //colour2 R G B
- cols[ 9] = 0.0; cols[10] = 1.0; cols[11] = 1.0; //Point4
- cols[12] = 1.0; cols[13] = 0.0; cols[14] = 1.0; //Point5
- cols[15] = 1.0; cols[16] = 1.0; cols[17] = 0.0; //Point6
- //Second simple object
- float dim2 = 0.2; //make bigger than 0.5 to overlap frist drawn shape.
- //Triangle 1 (centered)
- //points X Y Z
- //verts[18] = -dim2; verts[19] = -dim2; verts[20] = -dim2; //Point 1
- //verts[21] = -dim2; verts[22] = dim2; verts[23] = -dim2; //Point 2
- //verts[24] = dim2; verts[25] = dim2; verts[26] = -dim2; //Point 3
- //
- ////Triangle 2
- ////points X Y Z
- //verts[27] = dim2; verts[28] = -dim2; verts[29] = -dim2; //Point 4
- //verts[30] = -dim2; verts[31] = -dim2; verts[32] = -dim2; //Point 1
- //verts[33] = dim2; verts[34] = dim2; verts[35] = -dim2; //Point 3
- float a = 0.25, b = 0.75; //0.5 & 1.0 to show viewport.
- //points X Y Z
- verts[18] = a; verts[19] = a; verts[20] = -dim2; //Point 1
- verts[21] = a; verts[22] = b; verts[23] = -dim2; //Point 2
- verts[24] = b; verts[25] = b; verts[26] = -dim2; //Point 3
- //Triangle 2 (offset)
- //points X Y Z
- verts[27] = b; verts[28] = a; verts[29] = -dim2; //Point 4
- verts[30] = a; verts[31] = a; verts[32] = -dim2; //Point 1
- verts[33] = b; verts[34] = b; verts[35] = -dim2; //Point 3
- //colour R G B
- cols[18] = 1.0; cols[19] = 0.0; cols[20] = 0.0; //Point1 R: 1,0,0
- cols[21] = 0.0; cols[22] = 1.0; cols[23] = 0.0; //Point2 G: 0,1,0
- cols[24] = 0.0; cols[25] = 0.0; cols[26] = 1.0; //Point3 B: 0,0,1
- //colour2 R G B
- cols[27] = 1.0; cols[28] = 0.0; cols[29] = 0.0; //Point4
- cols[30] = 0.0; cols[31] = 1.0; cols[32] = 0.0; //Point5
- cols[33] = 0.0; cols[34] = 0.0; cols[35] = 1.0; //Point6
- // VAO allocation
- glGenVertexArrays(1, &m_vaoID);
- // First VAO setup
- glBindVertexArray(m_vaoID);
- glGenBuffers(2, m_vboID);
- glBindBuffer(GL_ARRAY_BUFFER, m_vboID[0]);
- //initialises data storage of vertex buffer object //verts or line_vertex
- glBufferData(GL_ARRAY_BUFFER, numOfVerts*3*sizeof(GLfloat), verts, GL_STATIC_DRAW);
- //draw line | x1,y1,z1 x2,y2,z2
- //float line_vertex[]=
- //{
- // -0.75, -0.75, 0.7, //line 1
- // 0.25, 0.25, 0.7,
- // -0.75, 0.75, 0.7, //line 2
- // 0, 0, 0.7
- //};
- //float line_col[]=
- //{
- // 1,0,0,0,0,1,
- // 0,1,0,1,0,0
- //};
- GLint vertexLocation= glGetAttribLocation(myShader.handle(), "in_Position");
- glVertexAttribPointer(vertexLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
- glEnableVertexAttribArray(vertexLocation);
- glBindBuffer(GL_ARRAY_BUFFER, m_vboID[1]);
- glBufferData(GL_ARRAY_BUFFER, numOfVerts*3*sizeof(GLfloat), cols, GL_STATIC_DRAW);
- GLint colorLocation= glGetAttribLocation(myShader.handle(), "in_Color");
- glVertexAttribPointer(colorLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
- glEnableVertexAttribArray(colorLocation);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glEnableVertexAttribArray(0);
- glBindVertexArray(0);
- }
- void init()
- {
- //(1,1,1,0) to (0,0,0,0) is white to black background.
- glClearColor(0.0,0.0,0.0,0.0); //sets the clear colour to yellow
- //glClear(GL_COLOR_BUFFER_BIT) in the display function//will clear the buffer to this colour.
- // Shaders
- if(!myShader.load("BasicView", "glslfiles/basic.vert", "glslfiles/basic.frag"))
- {
- cout << "failed to load shader" << endl;
- }
- createGeometry();
- glEnable(GL_DEPTH_TEST);
- }
- /**************** END OPENGL FUNCTIONS *************************/
- //WIN32 functions
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Declaration For WndProc
- void KillGLWindow(); // releases and destroys the window
- bool CreateGLWindow(char* title, int width, int height); //creates the window
- int WINAPI WinMain( HINSTANCE, HINSTANCE, LPSTR, int); // Win32 main function
- //win32 global variabless
- HDC hDC=NULL; // Private GDI Device Context
- HGLRC hRC=NULL; // Permanent Rendering Context
- HWND hWnd=NULL; // Holds Our Window Handle
- HINSTANCE hInstance; // Holds The Instance Of The Application
- /******************* WIN32 FUNCTIONS ***************************/
- int WINAPI WinMain( HINSTANCE hInstance, // Instance
- HINSTANCE hPrevInstance, // Previous Instance
- LPSTR lpCmdLine, // Command Line Parameters
- int nCmdShow) // Window Show State
- {
- console.Open();
- MSG msg; // Windows Message Structure
- bool done=false; // Bool Variable To Exit Loop
- // Create Our OpenGL Window
- if (!CreateGLWindow("Something equally exciting!",screenWidth,screenHeight))
- {
- return 0; // Quit If Window Was Not Created
- }
- while(!done) // Loop That Runs While done=FALSE
- {
- if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting?
- {
- if (msg.message==WM_QUIT) // Have We Received A Quit Message?
- {
- done=true; // If So done=TRUE
- }
- else // If Not, Deal With Window Messages
- {
- TranslateMessage(&msg); // Translate The Message
- DispatchMessage(&msg); // Dispatch The Message
- }
- }
- else // If There Are No Messages
- {
- display(); // Draw The Scene
- SwapBuffers(hDC); // Swap Buffers (Double Buffering)
- }
- }
- console.Close();
- // Shutdown
- KillGLWindow(); // Kill The Window
- return (int)(msg.wParam); // Exit The Program
- }
- //WIN32 Processes function - useful for responding to user inputs or other events.
- LRESULT CALLBACK WndProc( HWND hWnd, // Handle For This Window
- UINT uMsg, // Message For This Window
- WPARAM wParam, // Additional Message Information
- LPARAM lParam) // Additional Message Information
- {
- switch (uMsg) // Check For Windows Messages
- {
- case WM_CLOSE: // Did We Receive A Close Message?
- {
- PostQuitMessage(0); // Send A Quit Message
- return 0; // Jump Back
- }
- case WM_SIZE: // Resize The OpenGL Window
- {
- reshape(LOWORD(lParam),HIWORD(lParam)); // LoWord=Width, HiWord=Height
- return 0; // Jump Back
- }
- break;
- }
- // Pass All Unhandled Messages To DefWindowProc
- return DefWindowProc(hWnd,uMsg,wParam,lParam);
- }
- void KillGLWindow() // Properly Kill The Window
- {
- if (hRC) // Do We Have A Rendering Context?
- {
- if (!wglMakeCurrent(NULL,NULL)) // Are We Able To Release The DC And RC Contexts?
- {
- MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
- }
- if (!wglDeleteContext(hRC)) // Are We Able To Delete The RC?
- {
- MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
- }
- hRC=NULL; // Set RC To NULL
- }
- if (hDC && !ReleaseDC(hWnd,hDC)) // Are We Able To Release The DC
- {
- MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
- hDC=NULL; // Set DC To NULL
- }
- if (hWnd && !DestroyWindow(hWnd)) // Are We Able To Destroy The Window?
- {
- MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
- hWnd=NULL; // Set hWnd To NULL
- }
- if (!UnregisterClass("OpenGL",hInstance)) // Are We Able To Unregister Class
- {
- MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
- hInstance=NULL; // Set hInstance To NULL
- }
- }
- /* This Code Creates Our OpenGL Window. Parameters Are: *
- * title - Title To Appear At The Top Of The Window *
- * width - Width Of The GL Window Or Fullscreen Mode *
- * height - Height Of The GL Window Or Fullscreen Mode */
- bool CreateGLWindow(char* title, int width, int height)
- {
- GLuint PixelFormat; // Holds The Results After Searching For A Match
- WNDCLASS wc; // Windows Class Structure
- DWORD dwExStyle; // Window Extended Style
- DWORD dwStyle; // Window Style
- RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values
- WindowRect.left=(long)0; // Set Left Value To 0
- WindowRect.right=(long)width; // Set Right Value To Requested Width
- WindowRect.top=(long)0; // Set Top Value To 0
- WindowRect.bottom=(long)height; // Set Bottom Value To Requested Height
- hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window
- wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.
- wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages
- wc.cbClsExtra = 0; // No Extra Window Data
- wc.cbWndExtra = 0; // No Extra Window Data
- wc.hInstance = hInstance; // Set The Instance
- wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon
- wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer
- wc.hbrBackground = NULL; // No Background Required For GL
- wc.lpszMenuName = NULL; // We Don't Want A Menu
- wc.lpszClassName = "OpenGL"; // Set The Class Name
- if (!RegisterClass(&wc)) // Attempt To Register The Window Class
- {
- MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
- return false; // Return FALSE
- }
- dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
- dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style
- AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size
- // Create The Window
- if (!(hWnd=CreateWindowEx( dwExStyle, // Extended Style For The Window
- "OpenGL", // Class Name
- title, // Window Title
- dwStyle | // Defined Window Style
- WS_CLIPSIBLINGS | // Required Window Style
- WS_CLIPCHILDREN, // Required Window Style
- 0, 0, // Window Position
- WindowRect.right-WindowRect.left, // Calculate Window Width
- WindowRect.bottom-WindowRect.top, // Calculate Window Height
- NULL, // No Parent Window
- NULL, // No Menu
- hInstance, // Instance
- NULL))) // Dont Pass Anything To WM_CREATE
- {
- KillGLWindow(); // Reset The Display
- MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
- return false; // Return FALSE
- }
- static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
- {
- sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
- 1, // Version Number
- PFD_DRAW_TO_WINDOW | // Format Must Support Window
- PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
- PFD_DOUBLEBUFFER, // Must Support Double Buffering
- PFD_TYPE_RGBA, // Request An RGBA Format
- 24, // Select Our Color Depth
- 0, 0, 0, 0, 0, 0, // Color Bits Ignored
- 0, // No Alpha Buffer
- 0, // Shift Bit Ignored
- 0, // No Accumulation Buffer
- 0, 0, 0, 0, // Accumulation Bits Ignored
- 24, // 24Bit Z-Buffer (Depth Buffer)
- 0, // No Stencil Buffer
- 0, // No Auxiliary Buffer
- PFD_MAIN_PLANE, // Main Drawing Layer
- 0, // Reserved
- 0, 0, 0 // Layer Masks Ignored
- };
- if (!(hDC=GetDC(hWnd))) // Did We Get A Device Context?
- {
- KillGLWindow(); // Reset The Display
- MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
- return false; // Return FALSE
- }
- if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?
- {
- KillGLWindow(); // Reset The Display
- MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
- return false; // Return FALSE
- }
- if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?
- {
- KillGLWindow(); // Reset The Display
- MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
- return false; // Return FALSE
- }
- HGLRC tempContext = wglCreateContext(hDC);
- wglMakeCurrent(hDC, tempContext);
- GLenum err = glewInit();
- if (GLEW_OK != err)
- {
- cout << " GLEW ERROR" << endl;
- }
- int attribs[] =
- {
- WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
- WGL_CONTEXT_MINOR_VERSION_ARB, 2,
- WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
- 0
- };
- if(wglewIsSupported("WGL_ARB_create_context") == 1)
- {
- hRC = wglCreateContextAttribsARB(hDC,0, attribs);
- wglMakeCurrent(NULL,NULL);
- wglDeleteContext(tempContext);
- wglMakeCurrent(hDC, hRC);
- }
- else
- { //It's not possible to make a GL 3.x context. Use the old style context (GL 2.1 and before)
- hRC = tempContext;
- cout << " not possible to make context "<< endl;
- }
- //Checking GL version
- const GLubyte *GLVersionString = glGetString(GL_VERSION);
- cout << "OpenGL version is: " << GLVersionString << endl;
- //Which version are we using?
- int OpenGLVersion[2];
- glGetIntegerv(GL_MAJOR_VERSION, &OpenGLVersion[0]);
- glGetIntegerv(GL_MINOR_VERSION, &OpenGLVersion[1]);
- cout << "OpenGL version is : "<< OpenGLVersion[0] << " " << OpenGLVersion[1] << endl;
- ShowWindow(hWnd,SW_SHOW); // Show The Window
- SetForegroundWindow(hWnd); // Slightly Higher Priority
- SetFocus(hWnd); // Sets Keyboard Focus To The Window
- init();
- reshape(width, height); // Set Up Our Perspective GL Screen
- return true; // Success
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement