-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitOpenGL_4.cpp
More file actions
executable file
·153 lines (129 loc) · 5.02 KB
/
InitOpenGL_4.cpp
File metadata and controls
executable file
·153 lines (129 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//-----------------------------------------------------------------------------
// InitOpenGL_4.cpp by Steve Jones
// Copyright (c) 2015-2016 Game Institute. All Rights Reserved.
//
// - Adds full screen support
//-----------------------------------------------------------------------------
#include <iostream>
#include <sstream>
#define GLEW_STATIC
#include "GL/glew.h" // Important - this header must come before glfw3 header
#include "GLFW/glfw3.h"
// Global Variables
const char* APP_TITLE = "Introduction to Modern OpenGL - Hello Window 3";
const int gWindowWidth = 800;
const int gWindowHeight = 600;
// Function prototypes
void glfw_onKey(GLFWwindow* window, int key, int scancode, int action, int mode);
void glfw_onFramebufferSize(GLFWwindow* window, int width, int height);
void showFPS(GLFWwindow* window);
//-----------------------------------------------------------------------------
// Main Application Entry Point
//-----------------------------------------------------------------------------
int main()
{
// Intialize GLFW
// GLFW is configured. Must be called before calling any GLFW functions
if (!glfwInit())
{
// An error occured
std::cerr << "GLFW initialization failed" << std::endl;
return -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); // forward compatible with newer versions of OpenGL as they become available but not backward compatible (it will not run on devices that do not support OpenGL 3.3
// Create an OpenGL 3.3 core, forward compatible context full screen application
GLFWwindow* pWindow = NULL;
GLFWmonitor* pMonitor = glfwGetPrimaryMonitor();
const GLFWvidmode* pVmode = glfwGetVideoMode(pMonitor);
if (pVmode != NULL)
{
pWindow = glfwCreateWindow(pVmode->width, pVmode->height, APP_TITLE, pMonitor, NULL);
if (pWindow == NULL)
{
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
}
// Make the window's context the current one
glfwMakeContextCurrent(pWindow);
// Initialize GLEW
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
std::cerr << "Failed to initialize GLEW" << std::endl;
return -1;
}
// Set the required callback functions
glfwSetKeyCallback(pWindow, glfw_onKey);
glfwSetFramebufferSizeCallback(pWindow, glfw_onFramebufferSize);
// OpenGL version info
const GLubyte* renderer = glGetString(GL_RENDERER);
const GLubyte* version = glGetString(GL_VERSION);
std::cout << "Renderer: " << renderer << std::endl;
std::cout << "OpenGL version supported: " << version << std::endl;
std::cout << "OpenGL Initialization Complete" << std::endl;
// Main Loop
while (!glfwWindowShouldClose(pWindow))
{
showFPS(pWindow);
// Poll for and process events
glfwPollEvents();
// Render the scene
glClearColor(0.23f, 0.38f, 0.47f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Swap front and back buffers
glfwSwapBuffers(pWindow);
}
// Clean up
glfwTerminate();
return 0;
}
//-----------------------------------------------------------------------------
// Is called whenever a key is pressed/released via GLFW
//-----------------------------------------------------------------------------
void glfw_onKey(GLFWwindow* window, int key, int scancode, int action, int mode)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
//-----------------------------------------------------------------------------
// Is called when the window is resized
//-----------------------------------------------------------------------------
void glfw_onFramebufferSize(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
//-----------------------------------------------------------------------------
// Code computes the average frames per second, and also the average time it takes
// to render one frame. These stats are appended to the window caption bar.
//-----------------------------------------------------------------------------
void showFPS(GLFWwindow* window)
{
static double previousSeconds = 0.0;
static int frameCount = 0;
double elapsedSeconds;
double currentSeconds = glfwGetTime(); // returns number of seconds since GLFW started, as double float
elapsedSeconds = currentSeconds - previousSeconds;
// Limit text updates to 4 times per second
if (elapsedSeconds > 0.25)
{
previousSeconds = currentSeconds;
double fps = (double)frameCount / elapsedSeconds;
double msPerFrame = 1000.0 / fps;
// The C++ way of setting the window title
std::ostringstream outs;
outs.precision(3); // decimal places
outs << std::fixed
<< APP_TITLE << " "
<< "FPS: " << fps << " "
<< "Frame Time: " << msPerFrame << " (ms)";
glfwSetWindowTitle(window, outs.str().c_str());
// Reset for next average.
frameCount = 0;
}
frameCount++;
}