-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathlab5.cpp
More file actions
296 lines (252 loc) · 10.4 KB
/
Copy pathlab5.cpp
File metadata and controls
296 lines (252 loc) · 10.4 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//created by Lakshitha Premathilake, based on Jeff's example codes
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <stdio.h>
#define USING_INDEX_BUFFER 1
#ifdef USING_INDEX_BUFFER
#define NUM_VERTICES 6
#define NUM_INDICES 9
#else
#define NUM_VERTICES 9
#endif
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
GLuint shaderProgramID;
GLuint vao = 0;
GLuint vbo;
GLuint positionID, colorID;
GLuint indexBufferID;
GLuint texBufferID; // We have to create a buffer to hold the image. However, it WON'T go in the vertex buffer
GLuint texCoordID; // The ID of the "texCoord" variable in the shader
GLuint texID; // The ID of the "texture" variable in the shader
GLubyte* imageData; // This will contain the raw color information from the file
// Function which is used to compute image data and create a texture map
//=============================================================================================================
void loadBitmapFromFile(const char* filename, int* width, int* height, int* size, unsigned char** pixel_data) {
FILE* fp = fopen(filename, "r");
if (fp == NULL) {
printf ("Couldn't open file... aborting\n");
}
short identifier = -1;
fread(&identifier, 1, sizeof(short), fp); printf ("Identifer is: %c\n", identifier);
int filesize = -1;
fread(&filesize, 1, sizeof(int), fp); printf ("filesize is: %d\n", filesize);
int reserved = -1;
fread(&reserved, 1, sizeof(int), fp); printf ("reserved is: %d\n", reserved);
int bitmap_offset = -1;
fread(&bitmap_offset, 1, sizeof(int), fp); printf ("bitmap_offset is: %d\n", bitmap_offset);
int bitmap_header_size = -1;
fread(&bitmap_header_size, 1, sizeof(int), fp); printf ("bitmap_header_size is: %d\n", bitmap_header_size);
int bitmap_width = -1;
fread(&bitmap_width, 1, sizeof(int), fp); printf ("bitmap_width is: %d\n", bitmap_width);
int bitmap_height = -1;
fread(&bitmap_height, 1, sizeof(int), fp); printf ("bitmap_height is: %d\n", bitmap_height);
short bitmap_planes = -1;
fread(&bitmap_planes, 1, sizeof(short), fp); printf ("bitmap_planes is: %d\n", bitmap_planes);
short bits_per_pixel= -1;
fread(&bits_per_pixel, 1, sizeof(short), fp); printf ("bits_per_pixel is: %d\n", bits_per_pixel);
int compression = -1;
fread(&compression, 1, sizeof(int), fp); printf ("compression is: %d\n", compression);
int bitmap_data_size = -1;
fread(&bitmap_data_size, 1, sizeof(int), fp); printf ("bitmap_data_size is: %d\n", bitmap_data_size);
int hresolution = -1;
fread(&hresolution, 1, sizeof(int), fp); printf ("hresolution is: %d\n", hresolution);
int vresolution = -1;
fread(&vresolution, 1, sizeof(int), fp); printf ("vresolution is: %d\n", vresolution);
int num_colors = -1;
fread(&num_colors, 1, sizeof(int), fp); printf ("num_colors is: %d\n", num_colors);
int num_important_colors = -1;
fread(&num_important_colors, 1, sizeof(int), fp); printf ("num_important_colors is: %d\n", num_important_colors);
// Jump to the data already!
fseek (fp, bitmap_offset, SEEK_SET);
unsigned char* data = new unsigned char[bitmap_data_size];
// Read data in BGR format
fread (data, sizeof(unsigned char), bitmap_data_size, fp);
// Make pixel_data point to the pixels
*pixel_data = data;
*size = bitmap_data_size;
*width = bitmap_width;
*height = bitmap_height;
fclose(fp);
}
//===========================================================================================================================
#pragma region SHADER_FUNCTIONS
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;
}
#pragma endregion SHADER_FUNCTIONS
// Any time the window is resized, this function gets called. It's setup to the
// "glutReshapeFunc" in main.
void changeViewport(int w, int h){
glViewport(0, 0, w, h);
}
// Here is the function that gets called each time the window needs to be redrawn.
// It is the "paint" method for our program, and is set up from the glutDisplayFunc in main
void render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
#ifdef USING_INDEX_BUFFER
glDrawElements (GL_TRIANGLES, NUM_INDICES, GL_UNSIGNED_INT, NULL);
#else
glDrawArrays(GL_TRIANGLES, 0, NUM_VERTICES);
#endif
glutSwapBuffers();
}
int main (int argc, char** argv) {
// Standard stuff...
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutCreateWindow("Teture Mapping");
glutReshapeFunc(changeViewport);
glutDisplayFunc(render);
glewInit();
#ifdef USING_INDEX_BUFFER
GLfloat vertices[] = {0.0f, 0.5f, 0.0f, // 0
-0.25f, 0.0f, 0.0f, // 1
0.25f, 0.0f, 0.0f, // 2
-0.5f, -0.5f, 0.0f, // 3
0.0f, -0.5f, 0.0f, // 4
0.5f, -0.5f, 0.0f // 5
};
GLfloat text_coord[] = {0.0f, 0.0f, // 0
0.0f, 1.0f, // 1
1.0f, 1.0f, // 2
0.0f, 0.0f, // 3
1.0f, 0.0f, // 4
0.0f, 0.0f // 5
};
GLuint indices[] = {0, 1, 2, 1, 3, 4, 2, 4, 5};
#else
/*GLfloat vertices[] = {-0.5f, -0.5f, 0.0f,
0.0f, -0.5f, 0.0f,
-0.25f, 0.0f, 0.0f,
-0.25f, 0.0f, 0.0f,
0.25f, 0.0f, 0.0f,
0.0f, 0.5f, 0.0f,
0.0f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.25f, 0.0f, 0.0f
};
GLfloat colors[] = {1.0f, 0.00f, 0.0f, 1.0f,
0.0f, 1.00f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
1.0f, 0.00f, 0.0f, 1.0f,
0.0f, 1.00f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
1.0f, 0.00f, 0.0f, 1.0f,
0.0f, 1.00f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f
};*/
#endif
// Make a shader
char* vertexShaderSourceCode = readFile("vertexShader.vsh");
char* fragmentShaderSourceCode = readFile("fragmentShader.fsh");
GLuint vertShaderID = makeVertexShader(vertexShaderSourceCode);
GLuint fragShaderID = makeFragmentShader(fragmentShaderSourceCode);
shaderProgramID = makeShaderProgram(vertShaderID, fragShaderID);
// Create the "remember all"
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
// Create the buffer, but don't load anything yet
glBufferData(GL_ARRAY_BUFFER, 5*NUM_VERTICES*sizeof(GLfloat), NULL, GL_STATIC_DRAW);
// Load the vertex points
glBufferSubData(GL_ARRAY_BUFFER, 0, 3*NUM_VERTICES*sizeof(GLfloat), vertices);
// Load the colors right after that
glBufferSubData(GL_ARRAY_BUFFER, 3*NUM_VERTICES*sizeof(GLfloat),2*NUM_VERTICES*sizeof(GLfloat), text_coord);
#ifdef USING_INDEX_BUFFER
glGenBuffers(1, &indexBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, NUM_INDICES*sizeof(GLuint), indices, GL_STATIC_DRAW);
#endif
// Find the position of the variables in the shader
positionID = glGetAttribLocation(shaderProgramID, "s_vPosition");
//===========================================================================================================
int bmpWidth = -1;
int bmpHeight = -1;
int bmpSize = -1;
loadBitmapFromFile("txt.bmp", &bmpWidth, &bmpHeight, &bmpSize, (unsigned char**)&imageData);
glEnable (GL_TEXTURE_2D); // Turn on texturing
glGenTextures(1, &texBufferID); // Create an ID for a texture buffer
glBindTexture (GL_TEXTURE_2D, texBufferID); // Bind that buffer so we can then fill it (in next line)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bmpWidth, bmpHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, imageData);
texCoordID = glGetAttribLocation(shaderProgramID, "s_vTexCoord");
glEnableVertexAttribArray(texCoordID);
int textureCoordOffset = 3*NUM_VERTICES*sizeof(GLfloat);
glVertexAttribPointer(texCoordID, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(textureCoordOffset));
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); // Set the preferences
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT );
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
texID = glGetUniformLocation(shaderProgramID, "texture");
glActiveTexture(GL_TEXTURE0); // Turn on texture unit 0
glUniform1i(texID, 0); // Tell "s_vTexCoord" to use the 0th texture unit
//======================================================
glVertexAttribPointer(positionID, 3, GL_FLOAT, GL_FALSE, 0, 0);
//glVertexAttribPointer(colorID, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(vertices)));
glUseProgram(shaderProgramID);
glEnableVertexAttribArray(positionID);
glEnableVertexAttribArray(colorID);
glutMainLoop();
return 0;
}