draw, camera, test
Remove an unecessary function that draws a triangle using per-vertex coloring Cut camera implementation and paste into draw.cpp Use per-triangle tension mapping
This commit is contained in:
		| @@ -31,6 +31,110 @@ Camera* g_camera = nullptr; | ||||
| Draw* g_draw = nullptr; | ||||
| u32 g_drawFlags = 0; | ||||
|  | ||||
| Camera::Camera() | ||||
| { | ||||
| 	m_center.SetZero(); | ||||
| 	m_q.SetIdentity(); | ||||
| 	m_width = 1024.0f; | ||||
| 	m_height = 768.0f; | ||||
| 	m_zNear = 1.0f; | ||||
| 	m_zFar = 1000.0f; | ||||
| 	m_fovy = 0.25f * B3_PI; | ||||
| 	m_zoom = 10.0f; | ||||
| } | ||||
|  | ||||
| b3Mat44 Camera::BuildProjectionMatrix() const | ||||
| { | ||||
| 	float32 t = tan(0.5f * m_fovy); | ||||
| 	float32 sy = 1.0f / t; | ||||
|  | ||||
| 	float32 aspect = m_width / m_height; | ||||
| 	float32 sx = 1.0f / (aspect * t); | ||||
|  | ||||
| 	float32 invRange = 1.0f / (m_zNear - m_zFar); | ||||
| 	float32 sz = invRange * (m_zNear + m_zFar); | ||||
| 	float32 tz = invRange * m_zNear * m_zFar; | ||||
|  | ||||
| 	b3Mat44 m; | ||||
| 	m.x = b3Vec4(sx, 0.0f, 0.0f, 0.0f); | ||||
| 	m.y = b3Vec4(0.0f, sy, 0.0f, 0.0f); | ||||
| 	m.z = b3Vec4(0.0f, 0.0f, sz, -1.0f); | ||||
| 	m.w = b3Vec4(0.0f, 0.0f, tz, 0.0f); | ||||
| 	return m; | ||||
| } | ||||
|  | ||||
| b3Transform Camera::BuildWorldTransform() const | ||||
| { | ||||
| 	b3Transform xf; | ||||
| 	xf.rotation = b3QuatMat33(m_q); | ||||
| 	xf.position = (m_zoom * xf.rotation.z) - m_center; | ||||
| 	return xf; | ||||
| } | ||||
|  | ||||
| b3Mat44 Camera::BuildWorldMatrix() const | ||||
| { | ||||
| 	b3Transform xf = BuildWorldTransform(); | ||||
| 	return MakeMat44(xf); | ||||
| } | ||||
|  | ||||
| b3Transform Camera::BuildViewTransform() const | ||||
| { | ||||
| 	b3Transform xf; | ||||
| 	xf.rotation = b3QuatMat33(m_q); | ||||
| 	xf.position = (m_zoom * xf.rotation.z) - m_center; | ||||
| 	return b3Inverse(xf); | ||||
| } | ||||
|  | ||||
| b3Mat44 Camera::BuildViewMatrix() const | ||||
| { | ||||
| 	b3Transform xf = BuildViewTransform(); | ||||
| 	return MakeMat44(xf); | ||||
| } | ||||
|  | ||||
| b3Vec2 Camera::ConvertWorldToScreen(const b3Vec3& pw3) const | ||||
| { | ||||
| 	float32 w = m_width, h = m_height; | ||||
| 	b3Mat44 P = BuildProjectionMatrix(); | ||||
| 	b3Mat44 V = BuildViewMatrix(); | ||||
|  | ||||
| 	b3Vec4 pw(pw3.x, pw3.y, pw3.z, 1.0f); | ||||
|  | ||||
| 	b3Vec4 pp = P * V * pw; | ||||
|  | ||||
| 	b3Vec3 pn(pp.x, pp.y, pp.z); | ||||
| 	float32 inv_w = pp.w != 0.0f ? 1.0f / pp.w : 1.0f; | ||||
| 	pn *= inv_w; | ||||
|  | ||||
| 	float32 u = 0.5f * (pn.x + 1.0f); | ||||
| 	float32 v = 0.5f * (pn.y + 1.0f); | ||||
|  | ||||
| 	b3Vec2 ps; | ||||
| 	ps.x = u * w; | ||||
| 	ps.y = (1.0f - v) * h; | ||||
| 	return ps; | ||||
| } | ||||
|  | ||||
| Ray3 Camera::ConvertScreenToWorld(const b3Vec2& ps) const | ||||
| { | ||||
| 	// Essential Math, page 250. | ||||
| 	float32 t = tan(0.5f * m_fovy); | ||||
| 	float32 aspect = m_width / m_height; | ||||
|  | ||||
| 	b3Vec3 pv; | ||||
| 	pv.x = 2.0f * aspect * ps.x / m_width - aspect; | ||||
| 	pv.y = -2.0f * ps.y / m_height + 1.0f; | ||||
| 	pv.z = -1.0f / t; | ||||
|  | ||||
| 	b3Transform xf = BuildWorldTransform(); | ||||
| 	b3Vec3 pw = xf * pv; | ||||
|  | ||||
| 	Ray3 rw; | ||||
| 	rw.direction = b3Normalize(pw - xf.position); | ||||
| 	rw.origin = xf.position; | ||||
| 	rw.fraction = m_zFar; | ||||
| 	return rw; | ||||
| } | ||||
|  | ||||
| Draw::Draw() | ||||
| { | ||||
| 	m_points = new DrawPoints(); | ||||
| @@ -77,16 +181,6 @@ void Draw::DrawSolidTriangle(const b3Vec3& normal, const b3Vec3& p1, const b3Vec | ||||
| 	DrawTriangle(p1, p2, p3, edgeColor); | ||||
| } | ||||
|  | ||||
| void Draw::DrawSolidTriangle(const b3Vec3& normal, const b3Vec3& p1, const b3Color& color1, const b3Vec3& p2, const b3Color& color2, const b3Vec3& p3, const b3Color& color3) | ||||
| { | ||||
| 	m_triangles->Vertex(p1, color1, normal); | ||||
| 	m_triangles->Vertex(p2, color2, normal); | ||||
| 	m_triangles->Vertex(p3, color3, normal); | ||||
|  | ||||
| 	b3Color edgeColor(0.0f, 0.0f, 0.0f, 1.0f); | ||||
| 	DrawTriangle(p1, p2, p3, edgeColor); | ||||
| } | ||||
|  | ||||
| void Draw::DrawPolygon(const b3Vec3* vertices, u32 count, const b3Color& color) | ||||
| { | ||||
| 	b3Vec3 p1 = vertices[count - 1]; | ||||
| @@ -334,15 +428,45 @@ void Draw::DrawAABB(const b3AABB3& aabb, const b3Color& color) | ||||
| 	DrawSegment(vs[1], vs[7], color); | ||||
| } | ||||
|  | ||||
| void Draw::DrawString(const b3Color& color, const b3Vec2& ps, const char* text, ...) | ||||
| { | ||||
| 	va_list args; | ||||
| 	va_start(args, text); | ||||
| 	ImGui::SetNextWindowBgAlpha(0.0f); | ||||
| 	ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f)); | ||||
| 	ImGui::SetNextWindowSize(ImVec2(g_camera->m_width, g_camera->m_height)); | ||||
| 	ImGui::Begin("Superlay", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoScrollbar); | ||||
| 	ImGui::SetCursorPos(ImVec2(ps.x, ps.y)); | ||||
| 	ImGui::TextColoredV(ImVec4(color.r, color.g, color.b, color.a), text, args); | ||||
| 	ImGui::End(); | ||||
| 	va_end(args); | ||||
| } | ||||
|  | ||||
| void Draw::DrawString(const b3Color& color, const b3Vec3& pw, const char* text, ...) | ||||
| { | ||||
| 	b3Vec2 ps = g_camera->ConvertWorldToScreen(pw); | ||||
|  | ||||
| 	va_list args; | ||||
| 	va_start(args, text); | ||||
| 	ImGui::SetNextWindowBgAlpha(0.0f); | ||||
| 	ImGui::SetNextWindowPos(ImVec2(0.0f, 0.0f)); | ||||
| 	ImGui::SetNextWindowSize(ImVec2(g_camera->m_width, g_camera->m_height)); | ||||
| 	ImGui::Begin("Superlay", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoScrollbar); | ||||
| 	ImGui::SetCursorPos(ImVec2(ps.x, ps.y)); | ||||
| 	ImGui::TextColoredV(ImVec4(color.r, color.g, color.b, color.a), text, args); | ||||
| 	ImGui::End(); | ||||
| 	va_end(args); | ||||
| } | ||||
|  | ||||
| void Draw::DrawString(const b3Color& color, const char* text, ...) | ||||
| { | ||||
| 	va_list args; | ||||
| 	va_start(args, text); | ||||
|  | ||||
| 	ImGui::SetNextWindowBgAlpha(0.0f); | ||||
| 	ImGui::SetNextWindowPos(ImVec2(0.0f, 40.0f)); | ||||
| 	ImGui::SetNextWindowSize(ImVec2(g_camera->m_width, g_camera->m_height)); | ||||
|  | ||||
| 	ImGui::Begin("Overlay", NULL, ImVec2(0.0f, 0.0f), 0.0f, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoScrollbar); | ||||
| 	ImGui::Begin("Overlay", NULL, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoScrollbar); | ||||
| 	ImGui::TextColoredV(ImVec4(color.r, color.g, color.b, color.a), text, args); | ||||
| 	ImGui::End(); | ||||
|  | ||||
|   | ||||
| @@ -52,17 +52,7 @@ inline b3Vec3 Ray3::B() const | ||||
| class Camera | ||||
| { | ||||
| public: | ||||
| 	Camera() | ||||
| 	{ | ||||
| 		m_center.SetZero(); | ||||
| 		m_q.SetIdentity(); | ||||
| 		m_width = 1024.0f; | ||||
| 		m_height = 768.0f; | ||||
| 		m_zNear = 1.0f; | ||||
| 		m_zFar = 1000.0f; | ||||
| 		m_fovy = 0.25f * B3_PI; | ||||
| 		m_zoom = 10.0f; | ||||
| 	} | ||||
| 	Camera(); | ||||
|  | ||||
| 	b3Mat44 BuildProjectionMatrix() const; | ||||
| 	b3Mat44 BuildViewMatrix() const; | ||||
| @@ -91,82 +81,6 @@ inline b3Mat44 MakeMat44(const b3Transform& T) | ||||
| 		b3Vec4(T.position.x, T.position.y, T.position.z, 1.0f)); | ||||
| } | ||||
|  | ||||
| inline b3Mat44 Camera::BuildProjectionMatrix() const | ||||
| { | ||||
| 	float32 t = tan(0.5f * m_fovy); | ||||
| 	float32 sy = 1.0f / t; | ||||
|  | ||||
| 	float32 aspect = m_width / m_height; | ||||
| 	float32 sx = 1.0f / (aspect * t); | ||||
|  | ||||
| 	float32 invRange = 1.0f / (m_zNear - m_zFar); | ||||
| 	float32 sz = invRange * (m_zNear + m_zFar); | ||||
| 	float32 tz = invRange * m_zNear * m_zFar; | ||||
|  | ||||
| 	b3Mat44 m; | ||||
| 	m.x = b3Vec4(sx, 0.0f, 0.0f, 0.0f); | ||||
| 	m.y = b3Vec4(0.0f, sy, 0.0f, 0.0f); | ||||
| 	m.z = b3Vec4(0.0f, 0.0f, sz, -1.0f); | ||||
| 	m.w = b3Vec4(0.0f, 0.0f, tz, 0.0f); | ||||
| 	return m; | ||||
| } | ||||
|  | ||||
| inline b3Transform Camera::BuildWorldTransform() const | ||||
| { | ||||
| 	b3Transform xf; | ||||
| 	xf.rotation = b3QuatMat33(m_q); | ||||
| 	xf.position = (m_zoom * xf.rotation.z) - m_center; | ||||
| 	return xf; | ||||
| } | ||||
|  | ||||
| inline b3Mat44 Camera::BuildWorldMatrix() const | ||||
| { | ||||
| 	b3Transform xf = BuildWorldTransform(); | ||||
| 	return MakeMat44(xf); | ||||
| } | ||||
|  | ||||
| inline b3Transform Camera::BuildViewTransform() const | ||||
| { | ||||
| 	b3Transform xf; | ||||
| 	xf.rotation = b3QuatMat33(m_q); | ||||
| 	xf.position = (m_zoom * xf.rotation.z) - m_center; | ||||
| 	return b3Inverse(xf); | ||||
| } | ||||
|  | ||||
| inline b3Mat44 Camera::BuildViewMatrix() const | ||||
| { | ||||
| 	b3Transform xf = BuildViewTransform(); | ||||
| 	return MakeMat44(xf); | ||||
| } | ||||
|  | ||||
| inline b3Vec2 Camera::ConvertWorldToScreen(const b3Vec3& pw) const | ||||
| { | ||||
| 	b3Vec2 ps; | ||||
| 	ps.SetZero(); | ||||
| 	return ps; | ||||
| } | ||||
|  | ||||
| inline Ray3 Camera::ConvertScreenToWorld(const b3Vec2& ps) const | ||||
| { | ||||
| 	// Essential Math, page 250. | ||||
| 	float32 t = tan(0.5f * m_fovy); | ||||
| 	float32 aspect = m_width / m_height; | ||||
|  | ||||
| 	b3Vec3 pv; | ||||
| 	pv.x = 2.0f * aspect * ps.x / m_width - aspect; | ||||
| 	pv.y = -2.0f * ps.y / m_height + 1.0f; | ||||
| 	pv.z = -1.0f / t; | ||||
|  | ||||
| 	b3Transform xf = BuildWorldTransform(); | ||||
| 	b3Vec3 pw = xf * pv; | ||||
|  | ||||
| 	Ray3 rw; | ||||
| 	rw.direction = b3Normalize(pw - xf.position); | ||||
| 	rw.origin = xf.position; | ||||
| 	rw.fraction = m_zFar; | ||||
| 	return rw; | ||||
| } | ||||
|  | ||||
| // | ||||
| enum DrawFlags | ||||
| { | ||||
| @@ -189,8 +103,6 @@ public: | ||||
|  | ||||
| 	void DrawSolidTriangle(const b3Vec3& normal, const b3Vec3& p1, const b3Vec3& p2, const b3Vec3& p3, const b3Color& color); | ||||
|  | ||||
| 	void DrawSolidTriangle(const b3Vec3& normal, const b3Vec3& p1, const b3Color& color1, const b3Vec3& p2, const b3Color& color2, const b3Vec3& p3, const b3Color& color3); | ||||
| 	 | ||||
| 	void DrawPolygon(const b3Vec3* vertices, u32 count, const b3Color& color); | ||||
| 	 | ||||
| 	void DrawSolidPolygon(const b3Vec3& normal, const b3Vec3* vertices, u32 count, const b3Color& color); | ||||
| @@ -211,6 +123,10 @@ public: | ||||
|  | ||||
| 	void DrawTransform(const b3Transform& xf); | ||||
|  | ||||
| 	void DrawString(const b3Color& color, const b3Vec2& ps, const char* string, ...); | ||||
| 	 | ||||
| 	void DrawString(const b3Color& color, const b3Vec3& pw, const char* string, ...); | ||||
|  | ||||
| 	void DrawString(const b3Color& color, const char* string, ...); | ||||
|  | ||||
| 	void DrawSolidSphere(const b3SphereShape* s, const b3Color& c, const b3Transform& xf); | ||||
|   | ||||
| @@ -96,19 +96,19 @@ public: | ||||
| 		{ | ||||
| 			b3Triangle* t = m_clothMesh.triangles + i; | ||||
|  | ||||
| 			const float32 kMaxT = 100000.0f; | ||||
|  | ||||
| 			b3Vec3 f1 = T[t->v1]; | ||||
| 			float32 L1 = b3Length(f1); | ||||
| 			b3Color c1 = Color(L1, 0.0f, kMaxT); | ||||
|  | ||||
| 			b3Vec3 f2 = T[t->v2]; | ||||
| 			float32 L2 = b3Length(f2); | ||||
| 			b3Color c2 = Color(L2, 0.0f, kMaxT); | ||||
|  | ||||
| 			b3Vec3 f3 = T[t->v3]; | ||||
| 			float32 L3 = b3Length(f3); | ||||
| 			b3Color c3 = Color(L3, 0.0f, kMaxT); | ||||
|  | ||||
| 			float32 L = (L1 + L2 + L3) / 3.0f; | ||||
|  | ||||
| 			const float32 kMaxT = 100000.0f; | ||||
| 			b3Color color = Color(L, 0.0f, kMaxT); | ||||
| 			 | ||||
| 			b3Vec3 v1 = m_clothMesh.vertices[t->v1]; | ||||
| 			b3Vec3 v2 = m_clothMesh.vertices[t->v2]; | ||||
| @@ -119,8 +119,8 @@ public: | ||||
|  | ||||
| 			b3Vec3 n2 = -n1; | ||||
|  | ||||
| 			g_draw->DrawSolidTriangle(n1, v1, c1, v2, c2, v3, c3); | ||||
| 			g_draw->DrawSolidTriangle(n2, v1, c1, v3, c3, v2, c2); | ||||
| 			g_draw->DrawSolidTriangle(n1, v1, v2, v3, color); | ||||
| 			g_draw->DrawSolidTriangle(n2, v1, v3, v2, color); | ||||
| 		} | ||||
|  | ||||
| 		b3SpringClothStep step = m_cloth.GetStep(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user