//生成6个面 clip 是 wvp matrix ///////////////////////// void Frustum::Construct(Matrix4x4 & clip) { Vector4 & column1(clip.GetColumn(0)); Vector4 & column2(clip.GetColumn(1)); Vector4 & column3(clip.GetColumn(2)); Vector4 & column4(clip.GetColumn(3)); //根据平面方程 //Near: (P0,P4,P5) n=(0,0,-1),d=0 0x+0y-1z+0=0 m_planes[0] = column4 - column3;//这个函数规格化平面方程,使|a,b,c| == 1。 //Far: (P2,P6,P7) n=(0,0,1),d=-1 0x+0y+1z-1=0 m_planes[1] = column4 + column3; //Left: (P0,P3,P7) n=(-1,0,0),d=-1 -1x+0y+0z-1=0 m_planes[2] = column4 - column1; //Right: (P1,P5,P6) n=(1,0,0),d=-1 1x+0y+0z-1=0 m_planes[3] = column4 + column1; //Top: (P4,P7,P6) n=(0,1,0),d=-1 0x+1y+0z-1=0 m_planes[4] = column4 + column2; //Bottom: (P0,P1,P2) n=(0,-1,0),d=-1 0x-1y+0z-1=0 m_planes[5] = column4 - column2; //归一 for(int i=0; i<6; i++) { m_planes[i].Normalize(); } //做个标记??? // build a bit-field that will tell us the indices for the nearest and farthest vertices from each plane... for(int i=0; i<6; i++) { m_flag[i] = ((m_planes[i].x<0) ? 1 : 0) | ((m_planes[i].y<0) ? 2 : 0) | ((m_planes[i].z) ? 4 : 0); } }
void CToyUtil::AddVertex(size_t iMaterial, const Vector& vecPosition, const Vector2D& vecUV, const Matrix4x4& mNormals) { m_aaflData[iMaterial].push_back(vecPosition.x); m_aaflData[iMaterial].push_back(vecPosition.y); m_aaflData[iMaterial].push_back(vecPosition.z); if (m_bUseUV) { m_aaflData[iMaterial].push_back(vecUV.x); m_aaflData[iMaterial].push_back(vecUV.y); } if (m_bUseNormals) { m_aaflData[iMaterial].push_back(mNormals.GetColumn(0).x); m_aaflData[iMaterial].push_back(mNormals.GetColumn(0).y); m_aaflData[iMaterial].push_back(mNormals.GetColumn(0).z); m_aaflData[iMaterial].push_back(mNormals.GetColumn(1).x); m_aaflData[iMaterial].push_back(mNormals.GetColumn(1).y); m_aaflData[iMaterial].push_back(mNormals.GetColumn(1).z); m_aaflData[iMaterial].push_back(mNormals.GetColumn(2).x); m_aaflData[iMaterial].push_back(mNormals.GetColumn(2).y); m_aaflData[iMaterial].push_back(mNormals.GetColumn(2).z); } for (int i = 0; i < 3; i++) { if (vecPosition[i] < m_aabbVisBounds.m_vecMins[i]) m_aabbVisBounds.m_vecMins[i] = vecPosition[i]; if (vecPosition[i] > m_aabbVisBounds.m_vecMaxs[i]) m_aabbVisBounds.m_vecMaxs[i] = vecPosition[i]; } }
int main() { using namespace icarus::graphics; using namespace icarus::maths; std::cout << "Hello World!" << std::endl; #if 0 std::cout << (1 - 0.3f) << std::endl; Vector4 vec(10, 10, 12, 5); Vector4 vec2(5, 5, 8, 9.3f); std::cout << vec << std::endl; std::cout << vec2 << std::endl; std::cout << vec + vec2 << std::endl; std::cout << vec * vec2 << std::endl; std::cout << vec * 2.5f << std::endl; std::cout << (vec == vec2) << std::endl; std::cout << "----------------------" << std::endl; std::cout << vec << std::endl; vec *= 5; std::cout << vec << std::endl; std::cout << (vec *= 2) << std::endl; std::cout << vec << std::endl; std::cout << "----------------------" << std::endl; Matrix4x4 mat = Matrix4x4::Identity(); Matrix4x4 mat2 = Matrix4x4::Identity(); std::cout << (mat * mat2) << std::endl; std::cout << "----------------------" << std::endl; Matrix4x4 position = Matrix4x4::Translate(2, 3, 4); std::cout << position << std::endl; Vector4& column = position.GetColumn(3); std::cout << column << std::endl; std::cout << position.elements[12] << std::endl; column.x++; std::cout << position.elements[12] << std::endl; #endif if (!glfwInit()) { return -1; } Window window("Game Engine", WIDTH, HEIGHT, true); std::cout << glGetString(GL_VERSION) << std::endl; { glClearColor(0.0f, 0.5f, 0.8f, 1.0f); GLfloat vertices[] = { //-0.5f, -0.5f, 0, //-0.5f, 0.5f, 0, // 0.5f, 0.5f, 0, // 0.5f, 0.5f, 0, // 0.5f, -0.5f, 0, //-0.5f, -0.5f, 0, 0, 0, 0, // 0 0, 3, 0, // 1 8, 3, 0, // 2 8, 0, 0, // 3 }; GLuint indicies[] = { 0, 1, 2, 2, 3, 0, }; VertexArray va; VertexBuffer vb(vertices, 4 * 3 * sizeof(GLfloat)); VertexBufferLayout layout; layout.Push<float>(3); va.AddBuffer(vb, layout); IndexBuffer ib(indicies, 6); Shader shader("src/shaders/Basic.shader"); Matrix4x4 model(1.0f); //model *= Matrix4x4::Rotate(45, 0, 0, 1); //model *= Matrix4x4::Scale(1, 0.5f, 1); //model *= Matrix4x4::Translate(4, 3, 0); //model = Matrix4x4::Identity(); Matrix4x4 view = Matrix4x4::Identity(); Matrix4x4 proj = Matrix4x4::Orthographic(0, 16, 0, 9, -1, 1); shader.Bind(); shader.SetUniformMat4f("u_MVP", proj * view * model); shader.SetUniform4f("u_Color", Vector4(0.2f, 0.3f, 0.8f, 1.0f)); shader.SetUniform2f("u_LightPos", Vector2(4, 1.5f)); Renderer renderer; ImGuiContext* imguiContext = ImGui::CreateContext(); ImGui_ImplGlfwGL3_Init(window.GetGLFWWindow(), false); ImGui::StyleColorsDark(); float r = 0.0f; float incrementR = 0.05f; float lightXPos = 4.0f; float incrementL = 0.05f; while (!window.ShouldClose()) { //std::cout << window.GetWidth() << std::endl; renderer.Clear(); ImGui_ImplGlfwGL3_NewFrame(); // 1. Show a simple window. // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets automatically appears in a window called "Debug". { static float f = 0.0f; static int counter = 0; ImGui::Text("Hello, world!"); // Display some text (you can use a format string too) //ImGui::SliderFloat3("Translation", &translation.x, 0.0f, WIDTH); //ImGui::SliderFloat3("Camera", &camera.x, 0.0f, WIDTH); //ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color } shader.Bind(); model = Matrix4x4::Translate(4, 3, 0); shader.SetUniformMat4f("u_MVP", proj * view * model); shader.SetUniformMat4f("u_modelMat", model); shader.SetUniform4f("u_Color", Vector4(r, 0.3f, 0.8f, 1.0f)); double x, y; window.GetMousePosition(x, y); //shader.SetUniform2f("u_LightPos", Vector2(lightXPos, 1.5f)); shader.SetUniform2f("u_LightPos", Vector2((float)x * 16.0f / 960.0f, 9.0f - (float)y * 9.0f / 540.0f)); renderer.Draw(va, ib, shader); model = Matrix4x4::Translate(0, 0, 0); shader.SetUniformMat4f("u_MVP", proj * view * model); shader.SetUniformMat4f("u_modelMat", model); renderer.Draw(va, ib, shader); ImGui::Render(); ImGui_ImplGlfwGL3_RenderDrawData(ImGui::GetDrawData()); if (r > 1.0f) { incrementR = -0.05f; } else if (r < 0.0f) { incrementR = 0.05f; } r += incrementR; //if (lightXPos > 8.0f) { // incrementL = -0.05f; //} else if (lightXPos < 0.0f) { // incrementL = 0.05f; //} //lightXPos += incrementL; window.Update(); } } ImGui_ImplGlfwGL3_Shutdown(); ImGui::DestroyContext(); glfwTerminate(); return 0; }