-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdi.cpp
208 lines (165 loc) · 5.49 KB
/
mdi.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
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
198
199
200
201
202
203
204
205
206
207
208
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
using namespace std;
float vertices[] = {
-0.3f, 0.5f, -1.0f,
-0.8f, -0.5f, -1.0f,
0.2f, -0.5f, -1.0f,
-0.2f, 0.5f, -1.0f,
0.3f, -0.5f, -1.0f,
0.8f, 0.5f, -1.0f
};
unsigned int indices[] = {
0, 1, 2, 0, 1, 2
};
typedef struct {
GLuint count;
GLuint instanceCount;
GLuint firstIndex;
GLuint baseVertex;
GLuint baseInstance;
} DrawElementsIndirectCommand;
DrawElementsIndirectCommand commands[2];
GLchar* loadFile(const string &fileName)
{
string* result = new string();
ifstream file(fileName.c_str());
if (!file) {
std::cerr << "Cannot open file " << fileName << endl;
throw exception();
}
string line;
while (getline(file, line)) {
*result += line;
*result += '\n';
}
file.close();
return (GLchar*) result->c_str();
}
void printShaderLog(int shaderId)
{
int logLength;
glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &logLength);
if (logLength > 0) {
char *log = new char[logLength];
glGetShaderInfoLog(shaderId, logLength, &logLength, log);
cout << string(log);
delete[] log;
}
}
void specifySceneVertexAttributes(GLuint shaderProgram, GLuint vao, GLuint vbo, GLuint ibo)
{
GLint posAttrib = glGetAttribLocation(shaderProgram, "in_Position");
glEnableVertexArrayAttrib(vao, posAttrib);
glVertexArrayAttribFormat(vao, posAttrib, 3, GL_FLOAT, GL_FALSE, 0*sizeof(float));
glVertexArrayAttribBinding(vao, posAttrib, 0);
glVertexArrayVertexBuffer(vao, 0, vbo, 0, 3*sizeof(float));
glVertexArrayElementBuffer(vao, ibo);
commands[0].count = 3;
commands[0].instanceCount = 1;
commands[0].firstIndex = 0;
commands[0].baseVertex = 0;
commands[0].baseInstance = 2;
commands[1].count = 3;
commands[1].instanceCount = 1;
commands[1].firstIndex = 3;
commands[1].baseVertex = 3;
commands[1].baseInstance = 2;
}
static void error_callback(int error, const char* description)
{
cout << "GLFW: " << description << endl;
}
static void window_close_callback(GLFWwindow* window)
{
cout << "Closing window" << endl;
}
int main(void)
{
GLFWwindow* window;
glfwSetErrorCallback(error_callback);
// Initialize the library
if (!glfwInit())
exit(EXIT_FAILURE);
// Require OpenGL 3.3
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);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
// Create a windowed mode window and its OpenGL context
window = glfwCreateWindow(500, 500, "MDI", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetWindowCloseCallback(window, window_close_callback);
// Make the window's context current
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
glewInit();
cout << "ignore this error: " << glGetError() << endl;
cout << "errors: " << glGetError() << endl;
const GLchar* shaderVSsrc = loadFile("mdi.vert");
const GLchar* shaderFSsrc = loadFile("mdi.frag");
GLuint shaderVS = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(shaderVS, 1, &shaderVSsrc, NULL);
glCompileShader(shaderVS);
printShaderLog(shaderVS);
cout << "Created VS: " << glGetError() << endl;
GLuint shaderFS = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(shaderFS, 1, &shaderFSsrc, NULL);
glCompileShader(shaderFS);
printShaderLog(shaderFS);
cout << "Created FS: " << glGetError() << endl;
// Create program
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, shaderVS);
glAttachShader(shaderProgram, shaderFS);
glBindFragDataLocation(shaderProgram, 0, "out_Color");
glLinkProgram(shaderProgram);
cout << "Created program: " << glGetError() << endl;
// Vertex Array Objects
GLuint vao;
glCreateVertexArrays(1, &vao);
// Vertex Buffer Object
GLuint vbo, ibo, cbo;
glCreateBuffers(1, &vbo);
glCreateBuffers(1, &ibo);
glCreateBuffers(1, &cbo);
cout << "errors: " << glGetError() << endl;
glNamedBufferData(vbo, sizeof(vertices), vertices, GL_STATIC_DRAW);
glNamedBufferData(ibo, sizeof(indices), indices, GL_STATIC_DRAW);
specifySceneVertexAttributes(shaderProgram, vao, vbo, ibo);
glNamedBufferData(cbo, sizeof(commands), commands, GL_STATIC_DRAW);
cout << "Created VAO: " << glGetError() << endl;
glBindVertexArray(vao);
glUseProgram(shaderProgram);
cout << "Running: " << glGetError() << endl;
glBindBuffer(GL_DRAW_INDIRECT_BUFFER, cbo);
// Loop until the user closes the window
while (!glfwWindowShouldClose(window))
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glMultiDrawElementsIndirect(GL_TRIANGLES, GL_UNSIGNED_INT, NULL, 2, 0);
// Swap front and back buffers and poll for and process events
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteProgram(shaderProgram);
glDeleteShader(shaderVS);
glDeleteShader(shaderFS);
glDeleteBuffers(1, &vbo);
glDeleteBuffers(1, &ibo);
glDeleteBuffers(1, &cbo);
glDeleteVertexArrays(1, &vao);
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}