/
grid.cpp
196 lines (167 loc) · 5.23 KB
/
grid.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
/*
Project: OpenGL Grid Creation
Author : Nishant Dania
Email: nishantdania@gmail.com
Website: nishantdania.github.io
Last Modified: 15 June,2014
*/
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <stdio.h>
#define DEPTH 32
#define NUM_VERTICES (DEPTH+1)*(DEPTH+1)
#define NUM_INDICES 2*3*DEPTH*DEPTH
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
GLuint shaderProgramID;
GLuint vao = 0;
GLuint vbo;
GLuint positionID, colorID;
GLuint indexBufferID;
GLfloat var;
float timer= 0.01;
float diff = 0.005;
static char* readFile(const char* filename) {
// Open the file
FILE* fp = fopen (filename, "r");
// Move the file pointer to the end of the file and determing the length
fseek(fp, 0, SEEK_END);
long file_length = ftell(fp);
fseek(fp, 0, SEEK_SET);
char* contents = new char[file_length+1];
// zero out memory
for (int i = 0; i < file_length+1; i++) {
contents[i] = 0;
}
// Here's the actual read
fread (contents, 1, file_length, fp);
// This is how you denote the end of a string in C
contents[file_length+1] = '\0';
fclose(fp);
return contents;
}
bool compiledStatus(GLint shaderID){
GLint compiled = 0;
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compiled);
if (compiled) {
return true;
}
else {
GLint logLength;
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &logLength);
char* msgBuffer = new char[logLength];
glGetShaderInfoLog(shaderID, logLength, NULL, msgBuffer);
printf ("%s\n", msgBuffer);
delete (msgBuffer);
return false;
}
}
GLuint makeVertexShader(const char* shaderSource) {
GLuint vertexShaderID = glCreateShader(GL_VERTEX_SHADER);
glShaderSource (vertexShaderID, 1, (const GLchar**)&shaderSource, NULL);
glCompileShader(vertexShaderID);
bool compiledCorrectly = compiledStatus(vertexShaderID);
if (compiledCorrectly) {
return vertexShaderID;
}
return -1;
}
GLuint makeFragmentShader(const char* shaderSource) {
GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShaderID, 1, (const GLchar**)&shaderSource, NULL);
glCompileShader(fragmentShaderID);
bool compiledCorrectly = compiledStatus(fragmentShaderID);
if (compiledCorrectly) {
return fragmentShaderID;
}
return -1;
}
GLuint makeShaderProgram (GLuint vertexShaderID, GLuint fragmentShaderID) {
GLuint shaderID = glCreateProgram();
glAttachShader(shaderID, vertexShaderID);
glAttachShader(shaderID, fragmentShaderID);
glLinkProgram(shaderID);
return shaderID;
}
void changeViewport(int w, int h){
glViewport(0, 0, w, h);
}
void render() {
glUniform1f(var, timer);
timer+=diff;
if (timer>=0.8 || timer<0.01)
{
diff = diff * -1;
}
// glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
//glPointSize(6);
// glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
// glLoadIdentity();
gluLookAt(0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,1.0f,0.0f);
glDrawElements (GL_TRIANGLES, NUM_INDICES, GL_UNSIGNED_INT, NULL);
glutSwapBuffers();
}
void Timer(int iUnused){
glutPostRedisplay();
glutTimerFunc(30,Timer,0);
}
int main (int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(600, 600);
glutCreateWindow("Grid Plane");
glutReshapeFunc(changeViewport);
glutDisplayFunc(render);
glewInit();
//loop for vertex creation
GLfloat vertices[3*NUM_VERTICES];
int count = 0;
for (int y = 0; y < DEPTH+1; ++y)
{
for (int x = 0; x < DEPTH+1; ++x)
{
vertices[count++] = (x - (float)(DEPTH/2))/DEPTH;
vertices[count++] = (y - (float)(DEPTH/2))/DEPTH;
vertices[count++] = 0.0f;
}
}
GLuint indices[NUM_INDICES];
count = 0;
//loop for index creation
for (int i = 0; i < DEPTH; ++i)
{
for (int start = ((DEPTH*i)+i)+1; start < ((DEPTH*i)+i)+DEPTH+1; ++start)
{
indices[count++]=start;
indices[count++]=start+DEPTH;
indices[count++]=start-1;
indices[count++]=start;
indices[count++]=start+DEPTH+1;
indices[count++]=start+DEPTH;
}
}
char* vertexShaderSourceCode = readFile("vertexShader.glsl");
char* fragmentShaderSourceCode = readFile("fragmentShader.glsl");
GLuint vertShaderID = makeVertexShader(vertexShaderSourceCode);
GLuint fragShaderID = makeFragmentShader(fragmentShaderSourceCode);
shaderProgramID = makeShaderProgram(vertShaderID, fragShaderID);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 3*NUM_VERTICES*sizeof(GLfloat), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, 3*NUM_VERTICES*sizeof(GLfloat), vertices);
glGenBuffers(1, &indexBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, NUM_INDICES*sizeof(GLuint), indices, GL_STATIC_DRAW);
positionID = glGetAttribLocation(shaderProgramID, "s_vPosition");
var = glGetUniformLocation(shaderProgramID, "var");
glVertexAttribPointer(positionID, 3, GL_FLOAT, GL_FALSE, 0, 0);
glUseProgram(shaderProgramID);
glEnableVertexAttribArray(positionID);
Timer(0);
glutMainLoop();
return 0;
}