-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
233 lines (191 loc) · 6.47 KB
/
main.c
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include "instanced_renderer.h"
#include "standard_renderer.h"
#include "renderer.h"
#include "argtable2.h"
#define RENDERER_TYPE_STANDARD 1
#define RENDERER_TYPE_INSTANCED 2
void printFps() {
static int nbFrames = 0;
// Measure speed
static double lastTime;
double currentTime = glfwGetTime();
nbFrames++;
if (!lastTime) {
lastTime = currentTime;
return;
}
if ( currentTime - lastTime >= 1.0 ){ // If last prinf() was more than 1 sec ago
// printf and reset timer
printf("%d fps\n", nbFrames);
nbFrames = 0;
lastTime += 1.0;
}
}
GLFWwindow* initGL() {
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return NULL;
// Use OpenGL Core v3.3
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);
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(800, 600, "Instancing test", NULL, NULL);
if (!window)
{
printf("Couldn't create window\n");
glfwTerminate();
return NULL;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
glewExperimental = 1; // Needed for core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return NULL;
}
printf("OpenGL Version supported: %s\n", glGetString(GL_VERSION));
printf("GL Vendor: %s\n", glGetString(GL_VENDOR));
printf("GLSL Version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
printf("GL_EXTENSIONS: %s\n", glGetString(GL_EXTENSIONS));
glClearColor(0.0f, 0.0f, 0.3f, 0.0f);
return window;
}
GLuint createShader(GLenum type, const GLchar * const *source) {
GLuint shader;
GLint result = GL_FALSE;
int len;
shader = glCreateShader(type);
glShaderSource (shader, 1, source, NULL);
glCompileShader (shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &result);
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len);
if (result == 0) {
printf("SHADER FAIL RESULT %d, LEN %d\n", result, len);
if (len > 0) {
char errmsg[len+1];
glGetShaderInfoLog(shader, len, NULL, errmsg);
printf("%s\n", errmsg);
}
return 0;
}
return shader;
}
GLuint createGLProgram(const char* vshader, const char* fshader) {
GLuint programID;
GLuint vs;
GLuint fs;
vs = createShader(GL_VERTEX_SHADER, &vshader);
if (!vs) {
return 0;
}
fs = createShader(GL_FRAGMENT_SHADER, &fshader);
if (!fs) {
return 0;
}
programID = glCreateProgram ();
glAttachShader (programID, fs);
glAttachShader (programID, vs);
glLinkProgram (programID);
return programID;
}
int glMain(renderer* renderer)
{
GLFWwindow* window = initGL();
if (!window) {
return 1;
}
srand(1234);
GLuint programID = createGLProgram(renderer->vshader, renderer->fshader);
if (!programID) {
return 2;
}
renderer->prepare(programID);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
printFps();
// Clear the screen
glClear( GL_COLOR_BUFFER_BIT );
// Use our shader
glUseProgram(programID);
renderer->draw();
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
renderer->finish();
glfwTerminate();
return 0;
}
void printUsage(FILE* stream, const char* progname, void** argtable) {
fprintf(stream, "Usage: %s", progname);
arg_print_syntax(stream, argtable, "\n");
arg_print_glossary(stream, argtable, " %-10s %s\n");
}
int main(int argc, char** argv) {
struct arg_int *numVerticesArg = arg_int0("v","vertices",NULL, "num vertices (default is 20)");
struct arg_int *numObjectsArg = arg_int0("o","objects",NULL, "num objects (default is 20)");
struct arg_lit *help = arg_lit0(NULL,"help", "print this help and exit");
struct arg_str *rendererArg = arg_str1(NULL,NULL,"RENDERER",NULL);
struct arg_end *end = arg_end(20);
void* argtable[] = {numVerticesArg, numObjectsArg, help, rendererArg, end};
const char* progname = "gl-instancing";
int exitcode=0;
int nerrors;
renderer *renderer;
int numVertices = 20;
int numObjects = 20;
/* verify the argtable[] entries were allocated sucessfully */
if (arg_nullcheck(argtable) != 0)
{
/* NULL entries were detected, some allocations must have failed */
printf("%s: insufficient memory\n",progname);
exitcode=1;
goto exit;
}
/* Parse the command line as defined by argtable[] */
nerrors = arg_parse(argc,argv,argtable);
/* special case: '--help' takes precedence over error reporting */
if (help->count > 0)
{
printUsage(stderr, progname, argtable);
goto exit;
}
if (numVerticesArg->count > 0) {
numVertices = numVerticesArg->ival[0];
}
if (numObjectsArg->count > 0) {
numObjects = numObjectsArg->ival[0];
}
if (rendererArg->count == 0) {
fprintf(stderr, "Provide a renderer!\n");
printUsage(stderr, progname, argtable);
goto exit;
}
if (rendererArg->count > 0) {
if (strcmp(rendererArg->sval[0], "standard") == 0) {
renderer = getStandardRenderer(numVertices, numObjects);
} else if (strcmp(rendererArg->sval[0], "instanced") == 0) {
renderer = getInstancedRenderer(numVertices, numObjects);
} else {
fprintf(stderr, "Renderer %s is unknown. Supported renderers: instanced, standard\n", rendererArg->sval[0]);
printUsage(stderr, progname, argtable);
goto exit;
}
}
exitcode = glMain(renderer);
exit:
/* deallocate each non-null entry in argtable[] */
arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));
return exitcode;
}