-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmain.cpp
More file actions
198 lines (171 loc) · 5.32 KB
/
main.cpp
File metadata and controls
198 lines (171 loc) · 5.32 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <iostream>
#include <cmath>
#if defined(_MSC_VER)
# include <windows.h>
#endif
#ifdef EMSCRIPTEN
# include <emscripten/emscripten.h>
# define GLFW_INCLUDE_ES3
#else
# include <glad/glad.h> // emscripten and glad didn't work together
#endif
#include "delfem2/opengl/funcs.h"
#include "delfem2/opengl/new/funcs.h"
#include <GLFW/glfw3.h>
namespace dfm2 = delfem2;
static void callback_error(
[[maybe_unused]] int error,
const char* description)
{
fputs(description, stderr);
}
static GLFWwindow* myGLFW_OpenWindow(
const unsigned int SCR_WIDTH,
const unsigned int SCR_HEIGHT)
{
glfwSetErrorCallback(callback_error);
if (!glfwInit()){
exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
#endif
/*
// Decide GL+GLSL versions
#if __APPLE__
// GL 3.2 + GLSL 150
const char *glsl_version = "#version 150";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
#else
// GL 3.0 + GLSL 130
const char *glsl_version = "#version 130";
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
//glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only
//glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only
#endif
*/
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(
SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
// std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return 0;
}
return window;
}
// -------------------------------------------
int shaderProgram;
unsigned int VAO;
unsigned int VBO_Tri;
unsigned int EBO_Tri;
void draw(GLFWwindow* window)
{
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
::glClearColor(0.8, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO_Tri);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
void callback_key(
GLFWwindow* window,
int key,
[[maybe_unused]] int scancode,
int action,
[[maybe_unused]] int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS){
glfwSetWindowShouldClose(window, GL_TRUE);
}
}
void callback_resize(
[[maybe_unused]] GLFWwindow* window,
int width, int height)
{
glViewport(0, 0, width, height);
}
int main()
{
GLFWwindow* window = myGLFW_OpenWindow(800,600);
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, callback_resize);
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, callback_key);
// glad: load all OpenGL function pointers
// ---------------------------------------
#ifndef EMSCRIPTEN
// emscripten and glad didn't work well together
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
#endif
{
float vertices[] = {
0.5f, 0.5f, 0.0f, // top right
0.5f, -0.5f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f // top left
};
unsigned int indices[] = { // note that we start from 0!
0, 1, 3, // first Triangle
1, 2, 3 // second Triangle
};
dfm2::opengl::GL4_VAO_Pos(VAO, VBO_Tri,
vertices,4,3);
{
glBindVertexArray(VAO); // opengl4
glGenBuffers(1, &EBO_Tri);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO_Tri);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int)*6, indices, GL_STATIC_DRAW);
}
}
{
const std::string glslvrt_simplest =
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const std::string glslfrg_simplest =
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
" FragColor = vec4(1.0f, 0.5f, 0.5f, 1.0f);\n"
"}\n\0";
#ifdef EMSCRIPTEN
shaderProgram = dfm2::opengl::GL24_CompileShader((std::string("#version 300 es\n")+
glslvrt_simplest).c_str(),
(std::string("#version 300 es\n")+
std::string("precision highp float;\n")+
glslfrg_simplest).c_str());
#else
shaderProgram = dfm2::opengl::GL24_CompileShader(
("#version 330 core\n"+glslvrt_simplest).c_str(),
("#version 330 core\n"+glslfrg_simplest).c_str());
#endif
}
#ifdef EMSCRIPTEN
emscripten_set_main_loop_arg((em_arg_callback_func) draw, window, 0, 1);
#else
while (!glfwWindowShouldClose(window)) { draw(window); }
#endif
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}