[ Opengl ] Texture Mapping
Texture mapping
텍스처를 임포트하고 텍스처 좌표를 생성한다.
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
g_renderer.readTextureFile("speckled213.raw");
g_renderer.makeUVVertex();
void Renderer::readTextureFile(char* pFileName)
{
FILE* input_file;
char input_data[checkImageHeight][checkImageWidth][3];
input_file = fopen(pFileName, "rb");
fread(input_data, sizeof(char), checkImageWidth * checkImageHeight * 3, input_file);
fclose(input_file);
for (int i = 0; i < checkImageHeight; i++)
{
for (int j = 0; j < checkImageWidth; j++)
{
m_texture[i][j][0] = (GLubyte)input_data[i][j][0];
m_texture[i][j][1] = (GLubyte)input_data[i][j][1];
m_texture[i][j][2] = (GLubyte)input_data[i][j][2];
}
}
}
void Renderer::makeUVVertex()
{
for (int i = 0; i < m_nNumVertex; i++)
{
Vector4 v(m_vertex[i], 1);
m_uv[i][0] = (v.x + 1) * 320;
m_uv[i][1] = (v.y + 1) * 240;
}
}
엣지 테이블에 텍스처 좌표를 추가하여 각 픽셀에 대응하는 텍셀의 색상을 출력한다.
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
void Renderer::render()
{
if (!texureMappingEnabled)
{
clearCheckImage();
clearZBuffer();
Matrix4 mvp = m_proj * m_view * m_world; //m_world 부터 역순으로 vector에 곱함
applyMatrix(mvp, m_world);
clearEdgetable();
for (int i = 0; i < m_nNumFace; i++)
{
clearEdgetable();
if (isCullEnabled)
{
if (!isBackFace(i))
{
buildEdgetable(i);
fill(m_face[i].m_color);
}
}
else
{
buildEdgetable(i);
fill(m_face[i].m_color);
}
}
}
}
void Renderer::buildEdgetable(int nFace)
{
float vertices[2][3];
int ymin;
float yMax, x, inverseOfSlope, z, zPerY;
for (int i = 0; i < m_face[nFace].m_nNumVertex; i++)
{
for (int j = 0; j < 3; j++)
{
vertices[0][j] = m_tramsformedVertex[m_face[nFace].m_vertex[i] - 1][j];
vertices[1][j] = m_tramsformedVertex[m_face[nFace].m_vertex[(i + 1) % m_face[nFace].m_nNumVertex] - 1][j];
}
float uvVertices[2][2];
float u, v, uPerY, vPerY;
for (int j = 0; j < 2; j++)
{
uvVertices[0][j] = m_uv[m_face[nFace].m_vertex[i] - 1][j];
uvVertices[1][j] = m_uv[m_face[nFace].m_vertex[(i + 1) % m_face[nFace].m_nNumVertex] - 1][j];
}
if (vertices[0][1] == vertices[1][1]) continue;
else
{
inverseOfSlope = (vertices[1][0] - vertices[0][0]) / (vertices[1][1] - vertices[0][1]); //xperY
}
zPerY = (vertices[1][2] - vertices[0][2]) / (vertices[1][1] - vertices[0][1]);
uPerY = (uvVertices[1][0] - uvVertices[0][0]) / (vertices[1][1] - vertices[0][1]);
vPerY = (uvVertices[1][1] - uvVertices[0][1]) / (vertices[1][1] - vertices[0][1]);
float savedY;
float savedV;
if (vertices[0][1] < vertices[1][1]) // 작을때 ceiling 클 때 floor
{
savedY = vertices[0][1];
ymin = ceil(vertices[0][1]);
ymin = max(ymin, 0);
if (ymin > checkImageHeight - 1) continue;
m_ET[ymin][m_indexCount[ymin]].x = vertices[0][0];
if (ymin - savedY != 0)
{
m_ET[ymin][m_indexCount[ymin]].x += (ymin - savedY) * inverseOfSlope;
}
m_ET[ymin][m_indexCount[ymin]].zperY = zPerY;
m_ET[ymin][m_indexCount[ymin]].yMax = vertices[1][1];
m_ET[ymin][m_indexCount[ymin]].inverseOfSlope = inverseOfSlope;
m_ET[ymin][m_indexCount[ymin]].z = vertices[1][2];
if (savedY != 0) v = ymin * uvVertices[0][1] / savedY;
else v = 0;
m_ET[ymin][m_indexCount[ymin]].u = uvVertices[0][0];
if (ymin - savedY != 0)
{
m_ET[ymin][m_indexCount[ymin]].u += (ymin - savedY) * uPerY;
}
m_ET[ymin][m_indexCount[ymin]].v = v;
m_ET[ymin][m_indexCount[ymin]].uPerY = uPerY;
m_ET[ymin][m_indexCount[ymin]].vperY = vPerY;
m_indexCount[ymin]++;
}
else
{
savedY = vertices[1][1];
ymin = ceil(vertices[1][1]);
ymin = max(ymin, 0);
if (ymin > checkImageHeight - 1) continue;
m_ET[ymin][m_indexCount[ymin]].x = vertices[1][0];
if (ymin - savedY != 0)
{
m_ET[ymin][m_indexCount[ymin]].x += (ymin - savedY) * inverseOfSlope;
}
m_ET[ymin][m_indexCount[ymin]].yMax = vertices[0][1];
m_ET[ymin][m_indexCount[ymin]].inverseOfSlope = inverseOfSlope;
m_ET[ymin][m_indexCount[ymin]].z = vertices[0][2];
savedV = uvVertices[1][1];
if (savedY != 0)
{
v = ymin * uvVertices[1][1] / savedY;
}
else v = 0;
m_ET[ymin][m_indexCount[ymin]].u = uvVertices[1][0];
if (ymin - savedY != 0)
{
m_ET[ymin][m_indexCount[ymin]].u += (ymin - savedY) * uPerY;
}
m_ET[ymin][m_indexCount[ymin]].v = v;
m_ET[ymin][m_indexCount[ymin]].uPerY = uPerY;
m_ET[ymin][m_indexCount[ymin]].vperY = vPerY;
m_indexCount[ymin]++;
}
}
}
void Renderer::fill(GLubyte color[3])
{
// AET
for (int i = 0; i < checkImageHeight; i++)
{
//update intersection
for (int j = 0; j < m_numEdgeInAET; j++)
{
m_AET[j].x += m_AET[j].inverseOfSlope;
m_AET[j].z += m_AET[j].zperY;
m_AET[j].u += m_AET[j].uPerY;
m_AET[j].v += m_AET[j].vperY;
}
//Add new edge
for (int j = 0; j < m_indexCount[i]; j++)
{
m_AET[m_numEdgeInAET + j] = m_ET[i][j];
}
m_numEdgeInAET += m_indexCount[i];
//Delete edge
for (int j = 0; j < m_numEdgeInAET; j++)
{
if (m_AET[j].yMax < i)
{
for (int k = j; k < m_numEdgeInAET; k++)
{
m_AET[k] = m_AET[k + 1];
}
j--;
m_numEdgeInAET--;
}
}
//Sort intersections
Edge temp;
for (int j = 0; j < m_numEdgeInAET - 1; j++)
{
for (int k = j + 1; k < m_numEdgeInAET; k++)
{
if (m_AET[j].x > m_AET[k].x)
{
temp = m_AET[j];
m_AET[j] = m_AET[k];
m_AET[k] = temp;
}
}
}
//Render
for (int j = 0; j < m_numEdgeInAET; j += 2)
{
int k;
int xmin = floor(m_AET[j].x);
int xmax = floor(m_AET[j + 1].x);
xmin = max(xmin, 0);
xmax = min(xmax, checkImageWidth - 1);
float uPerX = (m_AET[j + 1].u - m_AET[j].u) / (xmax - xmin);
float deltaX = 0;
float vPerX = (m_AET[j + 1].v - m_AET[j].v) / (xmax - xmin);
float deltaV = 0;
float zPerX = (m_AET[j + 1].z - m_AET[j].z) / (xmax - xmin);
float deltaZ = 0;
for (k = xmin; k < xmax; k++)
{
if (m_AET[j].z + deltaZ < zBuffer[i][k])
{
checkImage[i][k][0] = (GLubyte)texture[(int)(m_AET[j].v + deltaV)][(int)(m_AET[j].u + deltaX)][0];
checkImage[i][k][1] = (GLubyte)texture[(int)(m_AET[j].v + deltaV)][(int)(m_AET[j].u + deltaX)][1];
checkImage[i][k][2] = (GLubyte)texture[(int)(m_AET[j].v + deltaV)][(int)(m_AET[j].u + deltaX)][2];
zBuffer[i][k] = m_AET[j].z + deltaZ;
deltaX += uPerX;
deltaV += vPerX;
deltaZ += zPerX;
}
}
}
}
}
출력
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.