Repository navigation
Expand file tree
/
Copy pathMapTex.cpp
More file actions
202 lines (163 loc) · 5.76 KB
/
Copy pathMapTex.cpp
File metadata and controls
202 lines (163 loc) · 5.76 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
#include "MapTex.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sstream>
GLenum types[6] = { GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_X };
/** Load a ppm file from disk.
@input filename The location of the PPM file. If the file is not found, an error message
will be printed and this function will return 0
@input width This will be modified to contain the width of the loaded image, or 0 if file not found
@input height This will be modified to contain the height of the loaded image, or 0 if file not found
@return Returns the RGB pixel data as interleaved unsigned chars (R0 G0 B0 R1 G1 B1 R2 G2 B2 .... etc) or 0 if an error ocured
**/
unsigned char* loadPPM(const char* filename, int& width, int& height)
{
const int BUFSIZE = 128;
FILE* fp;
unsigned int read;
unsigned char* rawData;
char buf[3][BUFSIZE];
char* retval_fgets;
size_t retval_sscanf;
if ((fp = fopen(filename, "rb")) == NULL)
{
std::cerr << "error reading ppm file, could not locate " << filename << std::endl;
width = 0;
height = 0;
return NULL;
}
// Read magic number:
retval_fgets = fgets(buf[0], BUFSIZE, fp);
// Read width and height:
do
{
retval_fgets = fgets(buf[0], BUFSIZE, fp);
} while (buf[0][0] == '#');
retval_sscanf = sscanf(buf[0], "%s %s", buf[1], buf[2]);
width = atoi(buf[1]);
height = atoi(buf[2]);
// Read maxval:
do
{
retval_fgets = fgets(buf[0], BUFSIZE, fp);
} while (buf[0][0] == '#');
// Read image data:
rawData = new unsigned char[width * height * 3];
read = fread(rawData, width * height * 3, 1, fp);
fclose(fp);
if (read != 1)
{
std::cerr << "error parsing ppm file, incomplete data" << std::endl;
delete[] rawData;
width = 0;
height = 0;
return NULL;
}
return rawData;
}
bool MapTex::load()
{
glGenTextures(1, &m_textureObj);
glBindTexture(GL_TEXTURE_CUBE_MAP, m_textureObj);
GLuint texture[1]; // storage for one texture
int twidth, theight; // texture width/height [pixels]
unsigned char* tdata; // texture pixel data
for (unsigned int i = 0; i < 6; i++) {
// Load image file
tdata = loadPPM("image.ppm", twidth, theight);
if (tdata == NULL) return false;
glTexImage2D(types[i], 0, GL_RGB, twidth, theight, 0, GL_RGBA,
GL_UNSIGNED_BYTE, tdata);
}
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
return true;
}
float points[] = {
-10.0f, 10.0f, -10.0f,
-10.0f, -10.0f, -10.0f,
10.0f, -10.0f, -10.0f,
10.0f, -10.0f, -10.0f,
10.0f, 10.0f, -10.0f,
-10.0f, 10.0f, -10.0f,
-10.0f, -10.0f, 10.0f,
-10.0f, -10.0f, -10.0f,
-10.0f, 10.0f, -10.0f,
-10.0f, 10.0f, -10.0f,
-10.0f, 10.0f, 10.0f,
-10.0f, -10.0f, 10.0f,
10.0f, -10.0f, -10.0f,
10.0f, -10.0f, 10.0f,
10.0f, 10.0f, 10.0f,
10.0f, 10.0f, 10.0f,
10.0f, 10.0f, -10.0f,
10.0f, -10.0f, -10.0f,
-10.0f, -10.0f, 10.0f,
-10.0f, 10.0f, 10.0f,
10.0f, 10.0f, 10.0f,
10.0f, 10.0f, 10.0f,
10.0f, -10.0f, 10.0f,
-10.0f, -10.0f, 10.0f,
-10.0f, 10.0f, -10.0f,
10.0f, 10.0f, -10.0f,
10.0f, 10.0f, 10.0f,
10.0f, 10.0f, 10.0f,
-10.0f, 10.0f, 10.0f,
-10.0f, 10.0f, -10.0f,
-10.0f, -10.0f, -10.0f,
-10.0f, -10.0f, 10.0f,
10.0f, -10.0f, -10.0f,
10.0f, -10.0f, -10.0f,
-10.0f, -10.0f, 10.0f,
10.0f, -10.0f, 10.0f
};
// load image file into texture object
void MapTex::loadTexture()
{
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 3 * 36 * sizeof(float), &points, GL_STATIC_DRAW);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &m_textureObj);
glBindTexture(GL_TEXTURE_CUBE_MAP, m_textureObj);
GLuint texture[1]; // storage for one texture
int twidth, theight; // texture width/height [pixels]
unsigned char* tdata; // texture pixel data
// Load image file
tdata = loadPPM("image.ppm", twidth, theight);
if (tdata == NULL) return;
glDepthMask(GL_FALSE);
//glUseProgram(shader_programme);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 36);
glDepthMask(GL_TRUE);
// Create ID for texture
glGenTextures(1, &texture[0]);
// Set this texture to be the one we are working with
glBindTexture(GL_TEXTURE_2D, texture[0]);
// Generate the texture
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, 3, twidth, theight, 0, GL_RGB, GL_UNSIGNED_BYTE, tdata);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, 3, twidth, theight, 0, GL_RGB, GL_UNSIGNED_BYTE, tdata);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, 3, twidth, theight, 0, GL_RGB, GL_UNSIGNED_BYTE, tdata);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, 3, twidth, theight, 0, GL_RGB, GL_UNSIGNED_BYTE, tdata);
glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, 3, twidth, theight, 0, GL_RGB, GL_UNSIGNED_BYTE, tdata);
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, 3, twidth, theight, 0, GL_RGB, GL_UNSIGNED_BYTE, tdata);
// Set bi-linear filtering for both minification and magnification
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
}