This repository has been archived by the owner on Apr 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexturedraw.hpp
185 lines (163 loc) · 5.65 KB
/
texturedraw.hpp
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
#ifndef TEXTUREDRAW_HPP
#define TEXTUREDRAW_HPP
#include "modelerdraw.h"
#include <FL/gl.h>
#include "bitmap.h"
#include "gl_header.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <math.h>
const int NUM_TEXTURES = 3;
int texImageWidth[NUM_TEXTURES];
int texImageHeight[NUM_TEXTURES];
GLubyte *texImage[NUM_TEXTURES]{nullptr, nullptr, nullptr};
GLuint texImageId[NUM_TEXTURES];
char *texImagePaths[NUM_TEXTURES]{
#ifdef __APPLE__
"/Users/dannylau/Desktop/COMP4411-Modeler/textures/fur.bmp",
#else
"textures/fur.bmp",
#endif
#ifdef __APPLE__
"/Users/dannylau/Desktop/COMP4411-Modeler/textures/grey.bmp",
#else
"textures/grey.bmp",
#endif
#ifdef __APPLE__
"/Users/dannylau/Desktop/COMP4411-Modeler/textures/spots.bmp",
#else
"textures/spots.bmp",
#endif
};
extern void _setupOpenGl();
void initTextureMap() {
printf("Initializing texture maps\n");
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
for (int i = 0; i < NUM_TEXTURES; ++i) {
printf("the tex image id is %d\n", texImageId[i]);
char *path = texImagePaths[i];
texImage[i] = readBMP(path, texImageWidth[i], texImageHeight[i]);
if (!texImage[i]) {
printf("Could not load the texture map\n");
continue;
}
printf("Loaded texture image %d %d\n", texImageWidth[i], texImageHeight[i]);
}
glGenTextures(3, texImageId);
}
void drawTextureQuad(double x, double y, double z, int individual) {
individual = individual - 1; // change from UI value to index
ModelerDrawState *mds = ModelerDrawState::Instance();
_setupOpenGl();
if (mds->m_rayFile) {
printf("Oops I don't care about the previous version of Modeler\n");
} else {
/* remember which matrix mode OpenGL was in. */
int savemode;
glGetIntegerv(GL_MATRIX_MODE, &savemode);
glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint *)&texImageId[individual]);
/* switch to the model matrix and scale by x,y,z. */
glMatrixMode(GL_MODELVIEW);
glBindTexture(GL_TEXTURE_2D, texImageId[individual]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texImageWidth[individual],
texImageHeight[individual], 0, GL_RGB, GL_UNSIGNED_BYTE,
texImage[individual]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
GL_MODULATE); // default is GL_MODULATE for interacting with light
glBindTexture(GL_TEXTURE_2D, texImageId[individual]); // repeat?
glPushMatrix();
glScaled(x, y, z);
glBegin(GL_QUADS);
glNormal3d(0.0, 0.0, -1.0);
glTexCoord2f(0.0, 0.0);
glVertex3d(0.0, 0.0, 0.0);
glTexCoord2f(0.0, 1.0);
glVertex3d(0.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0);
glVertex3d(1.0, 1.0, 0.0);
glTexCoord2f(1.0, 0.0);
glVertex3d(1.0, 0.0, 0.0);
glNormal3d(0.0, -1.0, 0.0);
glTexCoord2f(0.0, 0.0);
glVertex3d(0.0, 0.0, 0.0);
glTexCoord2f(0.0, 1.0);
glVertex3d(1.0, 0.0, 0.0);
glTexCoord2f(1.0, 1.0);
glVertex3d(1.0, 0.0, 1.0);
glTexCoord2f(1.0, 0.0);
glVertex3d(0.0, 0.0, 1.0);
glNormal3d(-1.0, 0.0, 0.0);
glTexCoord2f(0.0, 0.0);
glVertex3d(0.0, 0.0, 0.0);
glTexCoord2f(0.0, 1.0);
glVertex3d(0.0, 0.0, 1.0);
glTexCoord2f(1.0, 1.0);
glVertex3d(0.0, 1.0, 1.0);
glTexCoord2f(1.0, 0.0);
glVertex3d(0.0, 1.0, 0.0);
glNormal3d(0.0, 0.0, 1.0);
glTexCoord2f(0.0, 0.0);
glVertex3d(0.0, 0.0, 1.0);
glTexCoord2f(0.0, 1.0);
glVertex3d(1.0, 0.0, 1.0);
glTexCoord2f(1.0, 1.0);
glVertex3d(1.0, 1.0, 1.0);
glTexCoord2f(1.0, 0.0);
glVertex3d(0.0, 1.0, 1.0);
glNormal3d(0.0, 1.0, 0.0);
glTexCoord2f(0.0, 0.0);
glVertex3d(0.0, 1.0, 0.0);
glTexCoord2f(0.0, 1.0);
glVertex3d(0.0, 1.0, 1.0);
glTexCoord2f(1.0, 1.0);
glVertex3d(1.0, 1.0, 1.0);
glTexCoord2f(1.0, 0.0);
glVertex3d(1.0, 1.0, 0.0);
glNormal3d(1.0, 0.0, 0.0);
glTexCoord2f(0.0, 0.0);
glVertex3d(1.0, 0.0, 0.0);
glTexCoord2f(0.0, 1.0);
glVertex3d(1.0, 1.0, 0.0);
glTexCoord2f(1.0, 1.0);
glVertex3d(1.0, 1.0, 1.0);
glTexCoord2f(1.0, 0.0);
glVertex3d(1.0, 0.0, 1.0);
glEnd();
glDisable(GL_TEXTURE_2D);
/* restore the model matrix stack, and switch back to the matrix
mode we were in. */
glPopMatrix();
glMatrixMode(savemode);
}
}
void drawTextureCylinder(double h, double r1, double r2, int individual) {
individual = individual - 1; // change from UI value to index
ModelerDrawState *mds = ModelerDrawState::Instance();
_setupOpenGl();
if (mds->m_rayFile) {
printf("Oops I don't care about the previous version of Modeler\n");
} else {
glGetIntegerv(GL_TEXTURE_BINDING_2D, (GLint *)&texImageId[individual]);
glBindTexture(GL_TEXTURE_2D, texImageId[individual]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texImageWidth[individual],
texImageHeight[individual], 0, GL_RGB, GL_UNSIGNED_BYTE,
texImage[individual]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
GL_MODULATE); // default is GL_MODULATE for interacting with light
glBindTexture(GL_TEXTURE_2D, texImageId[individual]); // repeat?
drawCylinder(h, r1, r2);
glDisable(GL_TEXTURE_2D);
}
}
#endif