How to Setup OpenGL Project
Date: 2019.03.26
[OpenGL] Installation of OpenGL with Visual Studio 2017 (GLFW, GLEW, GLUT)
Reference: [Link]
1. Download and Install Visual Studio
Download
Link: [Link]
* Students, download Visual Studio Community 2017 version (with your portal id)
* If you do not want to use with Visual Studio, you can use Eclipse, Intellij, Netbeans ... etc.
* Please note that when you download, select language service package (English, Korean...)
2. Create Empty Project
2-1 Click File->New->Project to create an empty project named CGOpenGLExample.
* Click Empty Project.
* Naming the name of your project. (ex. CGOpenGLExample)
* Note that the location of your project. (ex. C:\Users\heave\source\repos)
2-2 On the Solution Explorer, select Source Files, right mouse click, Add->New item.
* Click C++ File (.cpp)
* Naming the cpp file. (ex. [Link])
2-3 Change the Solution Platform to x64.
3. Download GLFW
Download Link: [Link]
* Please use the pre-compiled binaries. (If you are the Windows OS users)
* Please download source package and compile them.
Source Package Download Link: [Link]
Guide for compiling GLFW: [Link]
3-1 Unzip the downloaded compiled file, and copy include folder to your project location.
* ex. Copy include file to the C:\Users\heave\source\repos\CGOpenGLExample\CGOpenGLExample
Project Location: C:\Users\heave\source\repos\CGOpenGLExample\CGOpenGLExample
3-2 Copy the lib-vc2015 to the Project Location, rename to lib.
4. Download GLEW
Download Link: [Link]
4-1 Unzip the downloaded compiled file, and copy include folder to your project location. (it will be
overwritten)
* Please make sure, in include file (in the Project Location), you have GL and GLFW folder.
4-2 Copy files named [Link], [Link] in the glew-2.1.0-win32\glew-
2.1.0\lib\Release\x64 folder to your lib project location.
4-3 Copy to the ... CGOpenGLExample\CGOpenGLExample\lib directory.
4-4 As the same, copy files named [Link] in the glew-2.1.0-win32\glew-
2.1.0\bin\Release\x64 folder to your lib project location to your
CGOpenGLExample\CGOpenGLExample\lib.
5. Download FreeGLUT
Download Link: [Link]
5-1 Copy downloaded include folder to the project include folder.
5-2 Copy [Link] file to the project lib folder.
5-3 Copy [Link] file to the project location.
6. Project Configuration
Right mouse click on the project named CGOpenGLExample (Solution Explorer).
6-1 Properties -> C/C++ -> General
Set up additional include directories as ./include
6-2 Properties -> Linker -> General
Set up additional library directories as ./lib
6-3 Properties -> Linker -> Input
Add [Link];[Link];[Link];[Link]; to Additional Dependencies.
Apply and OK.
Before you run the code, please copy the [Link], [Link], [Link] to the
C:\Windows\SysWOW64 (if your system is 64bit computer, if not copy to
C:\Windows\System32).
6. Run Example Code
6-1 GLUT Example code Ctrl+F5
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
void MyDisplay() { //디스플레이 콜백함수
glClear(GL_COLOR_BUFFER_BIT); //GL 상태변수 설정
glViewport(0, 0, 300, 300);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POLYGON); //입력 기본요소 정의
glVertex3f(-0.5, -0.5, 0.0);
glVertex3f(0.5, -0.5, 0.0);
glVertex3f(0.5, 0.5, 0.0);
glVertex3f(-0.5, 0.5, 0.0);
glEnd();
glFlush();
}
int main(int argc, char** argv) {
glutInit(&argc, argv); //GLUT 윈도우 함수
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(300, 300);
glutInitWindowPosition(0, 0);
glutCreateWindow("OpenGL Sample Drawing");
glClearColor(0.0, 0.0, 0.0, 1.0); //GL 상태변수 설정
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glutDisplayFunc(MyDisplay); //GLUT 콜백함수 등록
glutMainLoop(); //이벤트 루프 진입
return 0;
}
6-2 GLEW Example code Ctrl + F5
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
void window_resized(GLFWwindow* window, int width, int height);
void key_pressed(GLFWwindow* window, int key, int scancode, int action, int mods);
void show_glfw_error(int error, const char* description);
int main()
{
glfwSetErrorCallback(show_glfw_error);
if (!glfwInit()) {
std::cerr << "GLFW 초기화 실패" << '\n';
exit(-1);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
GLFWwindow* window = glfwCreateWindow(
800, // width
600, // height
"OpenGL Example",
NULL, NULL);
if (!window)
{
std::cerr << "윈도우 생성 실패" << '\n';
glfwTerminate();
exit(-1);
}
glfwMakeContextCurrent(window);
glfwSetWindowSizeCallback(window, window_resized);
glfwSetKeyCallback(window, key_pressed);
glfwSwapInterval(1);
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (err != GLEW_OK) {
std::cerr << "GLEW 초기화 실패 " << glewGetErrorString(err) << '\n';
glfwTerminate();
exit(-1);
}
std::cout << glGetString(GL_VERSION) << '\n';
int nr_extensions = 0;
glGetIntegerv(GL_NUM_EXTENSIONS, &nr_extensions);
for (int i = 0; i < nr_extensions; ++i) {
std::cout << glGetStringi(GL_EXTENSIONS, i) << '\n';
}
glClearColor(0, 0, 1, 1);
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
void show_glfw_error(int error, const char* description) {
std::cerr << "Error: " << description << '\n';
}
void window_resized(GLFWwindow* window, int width, int height) {
std::cout << "Window resized, new window size: " << width << " x " << height << '\n';
glClearColor(0, 0, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
}
void key_pressed(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == 'Q' && action == GLFW_PRESS) {
glfwTerminate();
exit(0);
}
}
Posted 19th March 2018 by Guohua Li
Labels: GLEW GLFW GLUT OpenGL Visual Studio 2017