This repository was archived by the owner on Sep 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathmain.cpp
155 lines (119 loc) · 3.87 KB
/
main.cpp
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
154
155
/*
* OpenGL instanced mesh rendering
*
* Copyright (C) 2015 by Sascha Willems - www.saschawillems.de
*
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <string>
#include "glRenderer.h"
#include "meshLoader.hpp"
glRenderer renderer;
const std::string appTitle = "OpenGL example - Instanced mesh rendering";
//Define an error callback
static void error_callback(int error, const char* description)
{
fputs(description, stderr);
_fgetchar();
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
renderer.keyCallback(key, scancode, action, mods);
}
static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
int main(void)
{
//Set the error callback
glfwSetErrorCallback(error_callback);
//Initialize GLFW
if (!glfwInit())
{
exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//Declare a window object
GLFWwindow* window;
//Create a window and create its OpenGL context
window = glfwCreateWindow(1280, 720, appTitle.c_str(), NULL, NULL);
//If the window couldn't be created
if (!window)
{
fprintf(stderr, "Failed to open GLFW window.\n");
glfwTerminate();
exit(EXIT_FAILURE);
}
//This function makes the context of the specified window current on the calling thread.
glfwMakeContextCurrent(window);
//Set callbacks
glfwSetKeyCallback(window, key_callback);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
//Initialize GLEW
glewExperimental = GL_TRUE;
GLenum err = glewInit();
//If GLEW hasn't initialized
if (err != GLEW_OK)
{
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
return -1;
}
// Output some info on the OpenGL implementation
const GLubyte* glvendor = glGetString(GL_VENDOR);
const GLubyte* glrenderer = glGetString(GL_RENDERER);
const GLubyte* glversion = glGetString(GL_VERSION);
printf("Vendor: %s\n", glvendor);
printf("Renderer: %s\n", glrenderer);
printf("Version: %s\n", glversion);
//Set a background color
renderer = glRenderer();
renderer.window = window;
renderer.generateBuffers();
renderer.generateShaders();
glDisable(GL_CULL_FACE);
printf("\nKeys:\n");
printf("""g"" : Toggle geometry shader\n");
printf("""w"" : Toggle wireframe\n");
printf("""+"" : increase number of subdivisions\n");
printf("""-"" : decrease number of subdivisions\n");
printf("""shift +"" : increase radius\n");
printf("""shift -"" : decrease radius\n");
double lastFPStime = glfwGetTime();
int frameCounter = 0;
//Main Loop
do
{
double thisFPStime = glfwGetTime();
frameCounter++;
if (thisFPStime - lastFPStime >= 1.0)
{
lastFPStime = thisFPStime;
std::string windowTitle = appTitle +" (";
windowTitle += std::to_string(frameCounter);
windowTitle += " fps) - 2015 by Sascha Willems (www.saschawillems.de)";
const char* windowCaption = windowTitle.c_str();
glfwSetWindowTitle(window, windowCaption);
frameCounter = 0;
}
renderer.renderScene();
//Get and organize events, like keyboard and mouse input, window resizing, etc...
glfwPollEvents();
} //Check if the ESC key had been pressed or if the window had been closed
while (!glfwWindowShouldClose(window));
//Close OpenGL window and terminate GLFW
glfwDestroyWindow(window);
//Finalize and clean up GLFW
glfwTerminate();
exit(EXIT_SUCCESS);
}