In Previous Part we Introduce Computer Graphics, OpenGL, GLEW and GLFW
rajkumarpatil.hashnode.dev/learning-modern-..
In this part we are going to Setup and do Basic code(OpenGL) in C++
Setting Up GLEW and GLFW for Windows System
- First download Visual studio Community visualstudio.microsoft.com/downloads
- Install Visual Studio (Desktop Development with c++) docs.microsoft.com/en-us/visualstudio/insta..
- Download GLEW glew.sourceforge.net
- Download GLFW glfw.org/download
Two Ways to Setup
First Way
- Follow the YouTube Link youtu.be/gCkcP0GcCe0
Second Way
- Extract both GLEW and GLFW zip file to new folder Name GLEW and GLFW respectively.
- Find Install folder of Microsoft Visual Studio (By default it is C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\VS )
- Go to Community\VC\Auxiliary\VS\include
- Copy folder GL (GLEW-extracted) from GLEW\include to Community\VC\Auxiliary\VS\include
- Copy all libraries from Extracted GLEW(GLEW\lib\Release\Win32) and GLFW(GLFW\lib-vc2019(select according to version of VS)) to Community\VC\Auxiliary\VS\lib
- Open Visual Studio Create a blank c++ project.(I had name Morden_World_OpenGL )
- Paste GLEW\bin\Release\Win32*glew32.dll* to Project Folder.
Basic Code in C++
#include<iostream>
#include<conio.h>
using namespace std;
#include<GL/glew.h>
#include<GLFW/glfw3.h>
const GLint WIDTH = 800, HEIGHT= 600 ;
int main() {
//Initialization of GLFW
if (!glfwInit()) {
cout << "Error in Initialization of glfwInit()";
glfwTerminate(); // Terminate if error one created
return 1;
}
// Setup GLFW Windows Properties -- openGL version
glfwInitHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We are using version 3.3 3(Major).3(Minor)
glfwInitHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// Core Profile No backward compatiblity
glfwInitHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Allow forward compatiblity
glfwInitHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
GLFWwindow* mainWindow = glfwCreateWindow(WIDTH, HEIGHT, "Mordern World openGL Window", NULL, NULL);
if (!mainWindow) {
cout << "GLFW Window is not Created There is some Error";
glfwTerminate();
return 1;
}
//Get Buffer Size Information
int bWidth, bHeight;
glfwGetFramebufferSize(mainWindow, &bWidth, &bHeight);
//Seting Contex for GLEW to use
glfwMakeContextCurrent(mainWindow);
// Add or Allow Mordern Extension
glewExperimental = GL_TRUE;
//Initialization of GLEW
if (glewInit() != GLEW_OK) { //glewInit() Function retrun GLEW_OK in success
cout << "Error in GLEW Initialization ";
glfwDestroyWindow(mainWindow);
glfwTerminate();
return 1;
}
//Setup Viewport
glViewport(0, 0, bWidth, bHeight);
//loop for window until we close
while (!glfwWindowShouldClose(mainWindow)) {
// Handle user input event
glfwPollEvents();
//clear Window
glClearColor(0.0f, 1.0f, 0.0f, 1.0f); // Green Color Window will display (R=0, G=1, B=0)
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(mainWindow);
}
return 0;
}