//---------------------------------------------------------------------------- BspNode* BspNodes::CreateNode (const Vector2f& v0, const Vector2f& v1, VertexColor3Effect* effect, const Float3& color) { // Create the model-space separating plane. Vector2f dir = v1 - v0; AVector normal(dir[1], -dir[0], 0.0f); normal.Normalize(); float constant = normal[0]*v0[0] + normal[1]*v0[1]; HPlane modelPlane(normal, constant); // Create the BSP node. BspNode* bsp = new0 BspNode(modelPlane); VertexFormat* vformat = VertexFormat::Create(2, VertexFormat::AU_POSITION, VertexFormat::AT_FLOAT3, 0, VertexFormat::AU_COLOR, VertexFormat::AT_FLOAT3, 0); // Create the rectangle representation of the model plane and set the // vertex colors to the specified color. float xExtent = 0.5f*dir.Length(); float yExtent = 0.125f; TriMesh* rect = StandardMesh(vformat).Rectangle(2, 2, xExtent, yExtent); VertexBufferAccessor vba(rect); for (int i = 0; i < 4; ++i) { vba.Color<Float3>(0, i) = color; } rect->SetEffectInstance(effect->CreateInstance()); // Set the position and orientation for the world-space plane. APoint trn(0.5f*(v0[0] + v1[0]), 0.5f*(v0[1] + v1[1]), yExtent + 0.001f); HMatrix zRotate(AVector::UNIT_Z, Mathf::ATan2(dir.Y(),dir.X())); HMatrix xRotate(AVector::UNIT_X, Mathf::HALF_PI); HMatrix rotate = zRotate*xRotate; rect->LocalTransform.SetTranslate(trn); rect->LocalTransform.SetRotate(rotate); bsp->AttachCoplanarChild(rect); return bsp; }
int main() { glm::vec3 cross = glm::cross(glm::vec3(2.5, 2.5, 0),glm::vec3(0, 2.5, 0)); try { GLfloat currentFrame; GLFWwindow *window = JPGLHelper::InitEverything(HEIGHT, WIDTH); glfwSetKeyCallback(window, keyCallback); if (glewInit() != GLEW_OK) throw std::runtime_error("glewInit() failed"); ShaderCompiler shaderGround("ground.vert", "ground.frag"); ShaderCompiler shaderTrain("train.vert", "train.frag"); MatrixWrapper modelPlane(shaderGround.GetProgramID(), "model"), modelIdentity(shaderTrain.GetProgramID(), "model"), projection(shaderGround.GetProgramID(), "projection"); modelPlane.mat4 = glm::rotate(modelPlane.mat4, glm::radians(-90.0f), glm::vec3(1.0f, 0.0f, 0.0f)); projection.mat4 = glm::perspective(glm::radians(45.0f), (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 2500.0f); Camera camera(-180.0f, 180.0f, 0.0f, 360.0f, 5.0f, shaderGround.GetProgramID(), 25.0f); camera.UpdateTargetPosition(glm::vec3(0.0f, 0.0f, 0.0f)); modelPlane.mat4 = glm::scale(modelPlane.mat4, glm::vec3(300.0f,300.0f, 1.0f)); modelIdentity.mat4 = glm::mat4(1.0f); Plane ground(shaderGround.GetProgramID(),32); Train train(shaderTrain.GetProgramID()); Light light; TextureWrapper groundTexture(GL_TEXTURE0, shaderGround.GetProgramID(), "ground.png", "Texture0"); keyboardManager.RegisterKey(GLFW_KEY_ESCAPE, std::bind(glfwSetWindowShouldClose, window, GL_TRUE)); keyboardManager.RegisterKey(GLFW_KEY_H, std::bind(&Camera::YawDown, std::ref(camera))); keyboardManager.RegisterKey(GLFW_KEY_J, std::bind(&Camera::PitchDown, std::ref(camera))); keyboardManager.RegisterKey(GLFW_KEY_K, std::bind(&Camera::PitchUp, std::ref(camera))); keyboardManager.RegisterKey(GLFW_KEY_L, std::bind(&Camera::YawUp, std::ref(camera))); keyboardManager.RegisterKey(GLFW_KEY_U, std::bind(&Camera::DistanceUp, std::ref(camera))); keyboardManager.RegisterKey(GLFW_KEY_I, std::bind(&Camera::DistanceDown, std::ref(camera))); keyboardManager.RegisterKey(GLFW_KEY_A, std::bind(&Train::Go, std::ref(train))); keyboardManager.RegisterKey(GLFW_KEY_Z, std::bind(&Train::Back, std::ref(train))); keyboardManager.RegisterKey(GLFW_KEY_S, std::bind(&Light::AmbientUp, std::ref(light))); keyboardManager.RegisterKey(GLFW_KEY_X, std::bind(&Light::AmbientDown, std::ref(light))); keyboardManager.RegisterKey(GLFW_KEY_D, std::bind(&Light::DiffuseUp, std::ref(light))); keyboardManager.RegisterKey(GLFW_KEY_C, std::bind(&Light::DiffuseDown, std::ref(light))); glEnable(GL_MULTISAMPLE); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); while (!glfwWindowShouldClose(window)) { currentFrame = glfwGetTime(); JPGLHelper::deltaTime = currentFrame - JPGLHelper::lastTime; JPGLHelper::lastTime = currentFrame; glfwPollEvents(); keyboardManager.ProcessKeys(); camera.UpdateTargetPosition(train.GetLocation()); train.DoPhysics(); glClearColor(light.InterpolateR(), light.InterpolateG(), light.InterpolateB(), 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); /* rysowanie p³aszczyzny */ groundTexture.SendToGPU(); shaderGround.Execute(); light.UpdateLight(shaderGround.GetProgramID(), camera.cameraPosition); camera.UpdateShader(shaderGround.GetProgramID()); camera.SendUpdatedMatrix(); projection.SetShader(shaderGround.GetProgramID()); projection.SendToGPU(); ground.Draw(modelPlane); /* rysowanie poci¹gu */ shaderTrain.Execute(); light.UpdateLight(shaderTrain.GetProgramID(), camera.cameraPosition); camera.UpdateShader(shaderGround.GetProgramID()); camera.SendUpdatedMatrix(); projection.SetShader(shaderTrain.GetProgramID()); projection.SendToGPU(); train.Draw(); glfwSwapBuffers(window); } } catch (std::exception &e) { std::cout << e.what() << std::endl; system("pause"); } glfwTerminate(); return 0; }