포스트

[ Opengl ] Lighting

Lighting


라이팅을 위해 suface normal, vertex normal을 계산한다.

P1,P2,P3 삼각형의 suface normal


surface normal의 평균을 통해 vertex normal 계산


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
void Renderer ::makefaceNormals() //외적 
{
	for (int i = 0; i < m_nNumFace; i++) // 각 face의 normal
	{
		Vector4 v1, v2;

		v1.x = m_vertex[m_face[i].m_vertex[0] - 1][0] - m_vertex[m_face[i].m_vertex[1] - 1][0];
		v1.y = m_vertex[m_face[i].m_vertex[0] - 1][1] - m_vertex[m_face[i].m_vertex[1] - 1][1];

		v1.z = m_vertex[m_face[i].m_vertex[0] - 1][2] - m_vertex[m_face[i].m_vertex[1] - 1][2];

		v2.x = m_vertex[m_face[i].m_vertex[1] - 1][0] - m_vertex[m_face[i].m_vertex[2] - 1][0];
		v2.y = m_vertex[m_face[i].m_vertex[1] - 1][1] - m_vertex[m_face[i].m_vertex[2] - 1][1];

		v2.z = m_vertex[m_face[i].m_vertex[1] - 1][2] - m_vertex[m_face[i].m_vertex[2] - 1][2];

		Vector4 ret;

		ret = crossProduct(v1, v2);
		ret = normalize(ret);

		m_faceNormal[i][0] = ret.x;
		m_faceNormal[i][1] = ret.y;
		m_faceNormal[i][2] = ret.z;
	}

}

void Renderer::makeVertexNormals()
{
	for (int i = 0; i < m_nNumFace; i++) // //점의 노말은 점이 속한 삼각형 노말의 평균
	{
		for (int j = 0; j < 3; j++)
		{  
			m_vertexNormal[m_face[i].m_vertex[j] - 1][0] += m_faceNormal[i][0];
			m_vertexNormal[m_face[i].m_vertex[j] - 1][1] += m_faceNormal[i][1];
			m_vertexNormal[m_face[i].m_vertex[j] - 1][2] += m_faceNormal[i][2];
		}
	}

	for (int i = 0; i < m_nNumVertex; i++)
	{
		Vector4 v;

		v.x = m_vertexNormal[i][0];
		v.y = m_vertexNormal[i][1];
		v.z = m_vertexNormal[i][2];
		
		v = normalize(v);

		m_vertexNormal[i][0] = v.x;
		m_vertexNormal[i][1] = v.y;
		m_vertexNormal[i][2] = v.z;
	}

}


광원을 지정하고 빛벡터를 생성한다.

디퓨즈의 빛의 강도 계산


1
2
Vector4 directionLight(5.f, 5.f, 5.f);
Vector4 ndirectionLight = normalize(directionLight);

엣지 테이블에 노말을 추가하고 빛의 강도에 따른 계산을 진행하여 라이팅을 적용해 렌더링한다.


식을 결합, 렌더링


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
void Renderer::buildEdgetable(int nFace)
{
	float vertices[2][3] = { 0 };
	float uvVertices[2][2] = { 0 };
	float nVertices[2][3] = { 0 };
	float lightVertices[2][3] = { 0 };

	int ymin = 0;
	float savedY = 0;
	float yMax = 0;

	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];
			nVertices[0][j] = m_faceNormal[nFace][j];
			nVertices[1][j] = m_faceNormal[nFace][j];

		}

		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;
		
		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]].xperY = (vertices[1][0] - vertices[0][0]) / (vertices[1][1] - vertices[0][1]);
			m_ET[ymin][m_indexCount[ymin]].zPerY = (vertices[1][2] - vertices[0][2]) / (vertices[1][1] - vertices[0][1]);

			m_ET[ymin][m_indexCount[ymin]].uPerY = (uvVertices[1][0] - uvVertices[0][0]) / (vertices[1][1] - vertices[0][1]);
			m_ET[ymin][m_indexCount[ymin]].vPerY = (uvVertices[1][1] - uvVertices[0][1]) / (vertices[1][1] - vertices[0][1]);

			m_ET[ymin][m_indexCount[ymin]].nxPerY = (nVertices[1][0] - nVertices[0][0]) / (vertices[1][1] - vertices[0][1]);
			m_ET[ymin][m_indexCount[ymin]].nyPerY = (nVertices[1][1] - nVertices[0][1]) / (vertices[1][1] - vertices[0][1]);
			m_ET[ymin][m_indexCount[ymin]].nzPerY = (nVertices[1][2] - nVertices[0][2]) / (vertices[1][1] - vertices[0][1]);

			m_ET[ymin][m_indexCount[ymin]].yMax = vertices[1][1];
			m_ET[ymin][m_indexCount[ymin]].x = vertices[0][0];
			m_ET[ymin][m_indexCount[ymin]].z = vertices[0][2];

			m_ET[ymin][m_indexCount[ymin]].u = uvVertices[0][0];
			m_ET[ymin][m_indexCount[ymin]].v = uvVertices[0][1];

			m_ET[ymin][m_indexCount[ymin]].nx = nVertices[0][0];
			m_ET[ymin][m_indexCount[ymin]].ny = nVertices[0][1];
			m_ET[ymin][m_indexCount[ymin]].nz = nVertices[0][2];
	
			if (ymin - savedY != 0)
			{
				m_ET[ymin][m_indexCount[ymin]].x += (ymin - savedY) * m_ET[ymin][m_indexCount[ymin]].xperY;
				m_ET[ymin][m_indexCount[ymin]].z += (ymin - savedY) * m_ET[ymin][m_indexCount[ymin]].zPerY;

				m_ET[ymin][m_indexCount[ymin]].u += (ymin - savedY) * m_ET[ymin][m_indexCount[ymin]].uPerY;
				m_ET[ymin][m_indexCount[ymin]].v += (ymin - savedY) * m_ET[ymin][m_indexCount[ymin]].vPerY;

				m_ET[ymin][m_indexCount[ymin]].nx += (ymin - savedY) * m_ET[ymin][m_indexCount[ymin]].nxPerY;
				m_ET[ymin][m_indexCount[ymin]].ny += (ymin - savedY) * m_ET[ymin][m_indexCount[ymin]].nyPerY;
				m_ET[ymin][m_indexCount[ymin]].nz += (ymin - savedY) * m_ET[ymin][m_indexCount[ymin]].nzPerY;
			}
		}

		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]].xperY = (vertices[1][0] - vertices[0][0]) / (vertices[1][1] - vertices[0][1]);
			m_ET[ymin][m_indexCount[ymin]].zPerY = (vertices[1][2] - vertices[0][2]) / (vertices[1][1] - vertices[0][1]);

			m_ET[ymin][m_indexCount[ymin]].uPerY = (uvVertices[1][0] - uvVertices[0][0]) / (vertices[1][1] - vertices[0][1]);
			m_ET[ymin][m_indexCount[ymin]].vPerY = (uvVertices[1][1] - uvVertices[0][1]) / (vertices[1][1] - vertices[0][1]);

			m_ET[ymin][m_indexCount[ymin]].nxPerY = (nVertices[1][0] - nVertices[0][0]) / (vertices[1][1] - vertices[0][1]);
			m_ET[ymin][m_indexCount[ymin]].nyPerY = (nVertices[1][1] - nVertices[0][1]) / (vertices[1][1] - vertices[0][1]);
			m_ET[ymin][m_indexCount[ymin]].nzPerY = (nVertices[1][2] - nVertices[0][2]) / (vertices[1][1] - vertices[0][1]);


			m_ET[ymin][m_indexCount[ymin]].yMax = vertices[0][1];
			m_ET[ymin][m_indexCount[ymin]].x = vertices[1][0];
			m_ET[ymin][m_indexCount[ymin]].z = vertices[1][2];
			
			m_ET[ymin][m_indexCount[ymin]].u = uvVertices[1][0];
			m_ET[ymin][m_indexCount[ymin]].v = uvVertices[1][1];
			
			m_ET[ymin][m_indexCount[ymin]].nx = nVertices[1][0];
			m_ET[ymin][m_indexCount[ymin]].ny = nVertices[1][1];
			m_ET[ymin][m_indexCount[ymin]].nz = nVertices[1][2];

			if (ymin - savedY != 0)
			{
				m_ET[ymin][m_indexCount[ymin]].x += (ymin - savedY) * m_ET[ymin][m_indexCount[ymin]].xperY;
				m_ET[ymin][m_indexCount[ymin]].z += (ymin - savedY) * m_ET[ymin][m_indexCount[ymin]].zPerY;

				m_ET[ymin][m_indexCount[ymin]].u += (ymin - savedY) * m_ET[ymin][m_indexCount[ymin]].uPerY;
				m_ET[ymin][m_indexCount[ymin]].v += (ymin - savedY) * m_ET[ymin][m_indexCount[ymin]].vPerY;

				m_ET[ymin][m_indexCount[ymin]].nx += (ymin - savedY) * m_ET[ymin][m_indexCount[ymin]].nxPerY;
				m_ET[ymin][m_indexCount[ymin]].ny += (ymin - savedY) * m_ET[ymin][m_indexCount[ymin]].nyPerY;
				m_ET[ymin][m_indexCount[ymin]].nz += (ymin - savedY) * m_ET[ymin][m_indexCount[ymin]].nzPerY;
			}
		}
		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].xperY;
			m_AET[j].z += m_AET[j].zPerY;

			m_AET[j].u += m_AET[j].uPerY;
			m_AET[j].v += m_AET[j].vPerY;

			m_AET[j].nx += m_AET[j].nxPerY;
			m_AET[j].ny += m_AET[j].nyPerY;
			m_AET[j].nz += m_AET[j].nzPerY;
		}

		//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 deltaU = 0;	float deltaV = 0;	float deltaZ = 0;
			float deltaNX = 0;	float deltaNY = 0;	float deltaNZ = 0;
			float uPerX = 0;	float vPerX = 0;	float zPerX = 0;
			float nxPerX = 0;	float nyPerX = 0;	float nzPerX = 0;

			float lxPerX = 0;	float lyPerNY = 0;	float lzPerNZ = 0;
			float deltaLX = 0;	float deltaLY = 0;	float deltaLZ = 0;

			float sx, sy, sz; float scala = 0;

			uPerX = (m_AET[j + 1].u - m_AET[j].u) / (m_AET[j + 1].x - m_AET[j].x);
			vPerX = (m_AET[j + 1].v - m_AET[j].v) / (m_AET[j + 1].x - m_AET[j].x);
			zPerX = (m_AET[j + 1].z - m_AET[j].z) / (m_AET[j + 1].x - m_AET[j].x);

			nxPerX = (m_AET[j + 1].nx - m_AET[j].nx) / (m_AET[j + 1].x - m_AET[j].x);
			nyPerX = (m_AET[j + 1].ny - m_AET[j].ny) / (m_AET[j + 1].x - m_AET[j].x);
			nzPerX = (m_AET[j + 1].nz - m_AET[j].nz) / (m_AET[j + 1].x - m_AET[j].x);

			for (k = xmin; k < xmax; k++)
			{
				sx = (m_AET[j].nx + deltaNX) * (ndirectionLight.x);
				sy = (m_AET[j].ny + deltaNY) * (ndirectionLight.y);
				sz = (m_AET[j].nz + deltaNZ) * (ndirectionLight.z);

				// 빛의 영향을 계산을 위한 스칼라
				scala = sx + sy + sz;

				if (m_AET[j].z + deltaZ < m_zBuffer[i][k])
				{
					checkImage[i][k][0] = (GLubyte)m_texture[(int)(m_AET[j].v + deltaV)][(int)(m_AET[j].u + deltaU)][0] * max(scala, 0.f);
					checkImage[i][k][1] = (GLubyte)m_texture[(int)(m_AET[j].v + deltaV)][(int)(m_AET[j].u + deltaU)][1] * max(scala, 0.f);
					checkImage[i][k][2] = (GLubyte)m_texture[(int)(m_AET[j].v + deltaV)][(int)(m_AET[j].u + deltaU)][2] * max(scala, 0.f);

					m_zBuffer[i][k] = m_AET[j].z + deltaZ;
				}

				deltaU += uPerX;
				deltaV += vPerX;
				deltaZ += zPerX;

				deltaNX += nxPerX;
				deltaNY += nyPerX;
				deltaNZ += nzPerX;
			}
			
		}
	}
}


출력


라이팅을 적용한 이미지 출력



이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.