-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
163 lines (124 loc) · 4.41 KB
/
Copy pathmain.c
File metadata and controls
163 lines (124 loc) · 4.41 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
// Include standard headers
#include <stdio.h>
#include <stdlib.h>
// Include GLEW
#include <GL/glew.h>
// Include GLFW
#include <GLFW/glfw3.h>
GLFWwindow* window;
#include <time.h>
// Include GLM
//#include <glm/glm.hpp>
//#include <glm/gtc/matrix_transform.hpp>
//using namespace glm;
#include "common/shader.h"
#include "common/texture.h"
#define WIDTH 1600
#define HEIGHT 1600
static double now_ms() {
struct timespec res;
clock_gettime(CLOCK_REALTIME, &res);
return 1000.0*res.tv_sec + (double)res.tv_nsec/1e6;
}
int main( void ) {
// Initialise GLFW
if( !glfwInit() ) {
fprintf( stderr, "Failed to initialize GLFW\n" );
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a window and create its OpenGL context
window = glfwCreateWindow( WIDTH, HEIGHT, "Langrange", NULL, NULL);
if( window == NULL ){
fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Initialize GLEW
glewExperimental = 1; //true; // Needed for core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
// Turn off v-sync
glfwSwapInterval(0);
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
// Create and compile our GLSL program from the shaders
GLuint programID = LoadShaders();
// Get a handle for our "myTextureSampler" uniform
GLuint TextureID = glGetUniformLocation(programID, "myTextureSampler");
// Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle.
// A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, 1.0f,
1.0f, -1.0f,
};
GLfloat tex_coords[] = {1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f,
0.0f, 0.0f};
GLuint vertexbuffer[2];
glGenBuffers(2, vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer[0]);
glBufferData(GL_ARRAY_BUFFER, 8*sizeof(GLfloat), g_vertex_buffer_data, GL_DYNAMIC_DRAW);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glVertexAttribPointer(
0, // attribute. No particular reason for 0, but must match the layout in the shader.
2, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer[1]);
glBufferData(GL_ARRAY_BUFFER, 8*sizeof(GLfloat), tex_coords, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
// Use our shader
glLinkProgram(programID);
glUseProgram(programID);
if(generate_texture(WIDTH, HEIGHT) == GL_INVALID_VALUE) {
exit(EXIT_FAILURE);
}
double time_now;
double time_diff;
/* main loop */
do{
time_now = now_ms();
update_texture();
// Draw the triangle !
glDrawArrays(GL_TRIANGLE_FAN, 0, 4); // 2*2 indices starting at 0 -> 2 triangles
// Swap buffers
glfwSwapBuffers(window);
time_diff = now_ms() - time_now;
printf("\rfps: %.2f", (float)1000/time_diff);
fflush(stdout);
glfwPollEvents();
} // Check if the ESC key was pressed or the window was closed
while( ((glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS) &&
(glfwGetKey(window, GLFW_KEY_Q) != GLFW_PRESS)) &&
glfwWindowShouldClose(window) == 0 );
printf("\n");
free_texture(all);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
// Cleanup VBO and shader
glDeleteBuffers(2, vertexbuffer);
glDeleteProgram(programID);
glDeleteTextures(1, &TextureID);
glDeleteVertexArrays(1, &VertexArrayID);
// Close OpenGL window and terminate GLFW
glfwTerminate();
return 0;
}