예제 #1
0
파일: BSPMap.cpp 프로젝트: koosk/siege
	void BSPMap::spawn(scene::Camera& cam, int i) const{
		if(i<-1 || i>=(int)spawnPoints.size())
			throw BadIndexException("Bad index while getting spawn point in BSPMap!");
		if(i == -1)
			i = rand() % spawnPoints.size();
		cam.setPosition(spawnPoints[i]);	
		math::Vector3 v(1, 0, 0);
		Matrix4 m;
		math::Vector3 v2;
		v2[1] = spawnPoints[i].getA() * (3.14/180);
		m.rotate(v2);
		v = v*m;
		cam.setLookAt(spawnPoints[i]+v);
	}
예제 #2
0
void Particle_renderer::prepare_lighting(const scene::Scene& scene, const scene::Camera& camera)
{
	if (!scene.irradiance_volumes().empty())
	{
		auto& volume = *scene.irradiance_volumes()[0];

		const float4x4& world = volume.world_transformation();

		Rendering_device& device = rendering_tool_.device();

		auto& change_per_light_data = change_per_light_.data();
		change_per_light_data.light_transformation = invert(world * camera.view());
		change_per_light_.update(device);

		device.set_shader_resources(scene::Irradiance_volume::num_textures(), volume.textures(), irradiance_volume_texture_offset_);
	}
}
예제 #3
0
    void Run(){
        wnd->SetTitle("nctoolkit");
        
        Scene::Compound_Basic * scene = NULL;
        Graph::Program * prog = NULL;
        
        try{
            dev = Graph::CreateDevice(wnd,Graph::DEVICE_AUTO,wnd->GetWidth(),wnd->GetHeight());
            dev->ClearColor(0.5, 0.5, 0.5, 1.0);
            dev->ClearFlags(Graph::BUFFER_COLOR_BIT|Graph::BUFFER_DEPTH_BIT);
            
            Core::FileReader * fr = Core::FileReader::Open("model://test.bxon");
            BXON::ReaderContext * frCtx = new BXON::ReaderContext(fr);
            BXON::Object * obj = BXON::Object::Parse(dynamic_cast<BXON::Context*>(frCtx));
            
            BXON::Map * map = dynamic_cast<BXON::Map*>(obj);
            Core::DebugLog(BXON::Object::ToJSON(map, 0, 1));
            
            scene = new Scene::Compound_Basic(dev);
            scene->Load(map);
            
            prog = dev->LoadProgram("shader://caravela.cpp");
            
            dev->Enable(Graph::STATE_DEPTH_TEST);
            
            delete map;
            delete frCtx;
            delete fr;
        }
        catch(const Core::Exception & ex){
            ex.PrintStackTrace();
            SafeDelete(dev);
            return;
        }
        
        Scene::Camera * camera = (Scene::Camera*)scene->GetDatablock(Scene::DATABLOCK_CAMERA,"Camera");
        Scene::Armature * armature = (Scene::Armature*)scene->GetDatablock(Scene::DATABLOCK_ARMATURE, "Armature.001");
        
        float zRotation = 0.0;
        float alpha = 0.0;
        
        while(!IsTearingDown())
        {
            dev->Clear();
            
            // Define the rendering area in window space.
            dev->Viewport(0,0,wnd->GetWidth(),wnd->GetHeight());

            // Set projection matrix.
            dev->MatrixMode(Graph::MATRIX_PROJECTION);
            dev->Identity();
            
            // Use camera properties as projection matrix (fov & aspect).
            camera->Enable(Graph::MATRIX_PROJECTION);

            // Set the model matrix.
            dev->MatrixMode(Graph::MATRIX_MODEL);
            dev->Identity();
            
            // Set camera properties as view matrix (rotation & position).
            camera->Enable(Graph::MATRIX_VIEW);
            
            dev->Color(255, 255, 255);
            
            // Rotate the caravela in the Z axis.
            //dev->Rotate(zRotation,0,0,1);
            
            prog->Enable();
            
            // Render the caravela.
            //scene->Render();
            armature->Render();
            
            prog->Disable();
            
            dev->PresentAll();
            
            if(alpha > 60.0)
                alpha = 0.0;
            
            armature->Play("", alpha);
            armature->Update();
            
            //camera->GetObject()->Play(alpha);
            
            alpha+= 2.0/60.0;
            
            zRotation += 0.1;
            
            wnd->SetTitle(Math::IntToString(alpha));
        }
        
        SafeDelete(scene);
        SafeDelete(dev);
    }
예제 #4
0
	virtual void update() override {
		auto kbd = mEngine->get<Input>()->getKeyboardList().front(); // should mainKeyboard be a thing?
		auto mouse = mEngine->get<Input>()->getMouseList().front(); // should mainMouse be a thing?

		// [WASD keyboard controls]
		{
			auto cameraAxes = mCamera.getDirections();

			glm::vec3 deltaPosition(0.0f, 0.0f, 0.0f);

			if ((*kbd)['W'])
				deltaPosition -= cameraAxes.mForward;
			if ((*kbd)['S'])
				deltaPosition += cameraAxes.mForward;
			if ((*kbd)['A'])
				deltaPosition -= cameraAxes.mRight;
			if ((*kbd)['D'])
				deltaPosition += cameraAxes.mRight;
			if ((*kbd)['Q'])
				deltaPosition += cameraAxes.mUp;
			if ((*kbd)['Z'])
				deltaPosition -= cameraAxes.mUp;

			if ((*kbd)[GLFW_KEY_LEFT_SHIFT])
				deltaPosition *= 5;

			float elapsedSeconds = mEngine->getClock().deltaFrame().count() * 0.001f; // deltaframe is in milliseconds
			float movementSpeed = 1.0f * elapsedSeconds;
			deltaPosition *= movementSpeed;

			mCamera.setPosition(mCamera.getPosition() + deltaPosition);
		}

		// [Mouselook] ~ yaw/pitch based
		{
			static std::pair<double, double> previousMousePosition = std::make_pair(0.0, 0.0);
			auto currentPos = mouse->getPosition();

			if ((previousMousePosition.first == 0.0) && (previousMousePosition.second == 0.0))
				previousMousePosition = currentPos;
			
			float dx = 0.01f * static_cast<float>(currentPos.first - previousMousePosition.first);
			float dy = 0.01f * static_cast<float>(currentPos.second - previousMousePosition.second);
			previousMousePosition = currentPos;

			mCameraYaw += dx;
			mCameraPitch += dy;

			auto yawQ = glm::rotate(mCameraYaw, glm::vec3(0.0f, 1.0f, 0.0f));
			auto pitchQ = glm::rotate(mCameraPitch, glm::vec3(1.0f, 0.0f, 0.0f));

			glm::quat q = glm::quat_cast(pitchQ * yawQ);

			mCamera.setOrientation(q);
		}

		mCamera.update();

		mRenderState.clear();

		// render that skybox
		mSkyBoxProgram.bind();
		mSkyBoxProgram.setUniform("uView", mCamera.getView());
		mSkyBoxProgram.setUniform("uProjection", mCamera.getProjection());
		mSkyBoxProgram.setUniform("uModel", glm::translate(mCamera.getPosition()));
		
		mSkyBoxTexture.bind(0);
		mSkyBoxProgram.setUniform("uCubeMap", 0);
		
		mSkyBox->draw();

		// render the cubes
		mProgram.bind();
		mProgram.setUniform("uView", mCamera.getView());
		mProgram.setUniform("uProjection", mCamera.getProjection());

		mProgram.setUniform("uLightDirection", glm::vec4(0.2, -1, 0.5, 1));
		mProgram.setUniform("uLightAmbient", glm::vec4(0.1, 0.1, 0.1, 1.0));
		mProgram.setUniform("uLightDiffuse", glm::vec4(0.85, 0.85, 0.85, 1.0));
		
		mTexture.bind(0);
		mProgram.setUniform("uTexture", 0);
		
		// draw the cube at 100 different locations
		for (int i = 0; i < 10; ++i)
			for (int j = 0; j < 10; ++j) {
				mProgram.setUniform("uModel", glm::translate(glm::vec3(2 * (i - 5), 0, 2 * (j - 5))));

				mSphere->draw();
				//mCube->draw();
			}
	}
예제 #5
0
int main(int argc, char* argv[])
{
    MANAGER.init(argc, argv);
    int windowWidth = 256;
    int windowHeight = 256;
    GlutUI::Window & mainWindow = MANAGER.createWindow(windowWidth, windowHeight, "Render Window");
    GlutUI::Panel & mainPanel = MANAGER.createPanel(mainWindow, windowWidth, windowHeight, "Render Panel");
    Scene::World world = Scene::createWorld();
    std::cout << std::string((char *)glGetString(GL_VENDOR)) << std::endl;
    std::cout << std::string((char *)glGetString(GL_RENDERER)) << std::endl;
    std::cout << "OpenGL " << std::string((char *)glGetString(GL_VERSION)) << std::endl;
    std::cout << "====================================================" << std::endl;

    Scene::Shader* rainbowShader = new Scene::Shader("shaders/rainbow_vert.glsl", "shaders/rainbow_frag.glsl");

    //char* fileName = argv[1];


    char* fileName = "models/sphere.off";

    Scene::MeshObject* meshObject = new Scene::MeshObject(fileName);
    world.assignShader(meshObject, rainbowShader);
    meshObject->readGeom();
    world.addObject(meshObject);

    float xMin = meshObject->xMin();
    float xMax = meshObject->xMax();
    float yMin = meshObject->yMin();
    float yMax = meshObject->yMax();
    float zMin = meshObject->zMin();
    float zMax = meshObject->zMax();
    float xSpan = xMax - xMin;
    float ySpan = yMax - yMin;
    float zSpan = zMax - zMin;
    float xMid = (xMin + xMax) / 2;
    float yMid = (yMin + yMax) / 2;
    float zMid = (zMin + zMax) / 2;
    meshObject->setTx(-xMid);
    meshObject->setTy(-yMid);
    meshObject->setTz(-zMid);
    float s = 0.5;
    float xAxisMin = -s*xSpan;
    float yAxisMin = -s*ySpan;
    float zAxisMin = -s*zSpan;
    float xAxisMax = s*xSpan;
    float yAxisMax = s*ySpan;
    float zAxisMax = s*zSpan;
    Scene::Arrow * xAxis = new Scene::Arrow(glm::vec3(xAxisMin, yAxisMin, zAxisMin), glm::vec3(xAxisMax, yAxisMin, zAxisMin), glm::vec4(1, 0, 0, 1));
    Scene::Arrow * yAxis = new Scene::Arrow(glm::vec3(xAxisMin, yAxisMin, zAxisMin), glm::vec3(xAxisMin, yAxisMax, zAxisMin), glm::vec4(0, 1, 0, 1));
    Scene::Arrow * zAxis = new Scene::Arrow(glm::vec3(xAxisMin, yAxisMin, zAxisMin), glm::vec3(xAxisMin, yAxisMin, zAxisMax), glm::vec4(0, 0, 1, 1));
    world.addObject(xAxis);
    world.addObject(yAxis);
    world.addObject(zAxis);
    float maxSpan = std::max({ xSpan, ySpan, zSpan });
    float minSpan = std::min({ xSpan, ySpan, zSpan });
    s = 1.5;
    Scene::Camera * cam = new Scene::Camera();
    if (xSpan == minSpan) {
        cam->setThe(-90);
        cam->setTz(xMid + s * maxSpan);
    }
    else if (ySpan == minSpan) {
        cam->setPhi(90);
        cam->setThe(90);
        cam->setTz(yMid + s * maxSpan);
    }
    else {
        cam->setTz(zMid + s * maxSpan);
    }
    world.addObject(cam);
    mainPanel.setWorld(&world);
    mainPanel.setCamera(cam);
    mainPanel.setMeshObject(meshObject);
    GlutUI::Controls::Keyboard keyboard(&mainPanel);
    GlutUI::Controls::Mouse mouse(&mainPanel, mainPanel.getCamera(), mainPanel.getMeshObject());
    /*std::set<int> f11 = meshObject->adjacency(11);
    std::set<int> f23 = meshObject->adjacency(23);
    std::set<int> vSet;
    for (auto it = f11.begin(); it != f11.end(); it++) {
    for (int i = 0; i < 3; i++) {
    vSet.insert(meshObject->faces(*it)[i]);
    }
    }
    for (auto it = f23.begin(); it != f23.end(); it++) {
    for (int i = 0; i < 3; i++) {
    vSet.insert(meshObject->faces(*it)[i]);
    }
    }
    for (auto it = vSet.begin(); it != vSet.end(); it++) {
    printf("\n%i: ", *it);
    std::set<int> asdf = meshObject->adjacency(*it);
    for (auto i = asdf.begin(); i != asdf.end(); i++) {
    printf("(%i,%i,%i) ", meshObject->faces(*i)[0], meshObject->faces(*i)[1], meshObject->faces(*i)[2]);
    }
    }
    meshObject->collapse(11, 23, Scene::BINARY_APPROXIMATION_METHOD);
    std::set<int> f11New = meshObject->adjacency(11);
    std::set<int> vSetNew;
    for (auto it = f11New.begin(); it != f11New.end(); it++) {
    for (int i = 0; i < 3; i++) {
    vSetNew.insert(meshObject->faces(*it)[i]);
    }
    }
    printf("\n\n");
    for (auto it = vSetNew.begin(); it != vSetNew.end(); it++) {
    printf("\n%i: ", *it);
    std::set<int> asdf = meshObject->adjacency(*it);
    for (auto i = asdf.begin(); i != asdf.end(); i++) {
    printf("(%i,%i,%i) ", meshObject->faces(*i)[0], meshObject->faces(*i)[1], meshObject->faces(*i)[2]);
    }
    }*/
    ///////////////////////////////////////
    ///// Keyboard Hotkey Assignments /////
    ///////////////////////////////////////
    int bmpCounter = 0;
    float complexityMultiplier = 1.0 / 32.0;
    float complexityIncrement = 1.0 / 16.0;
    auto alambda = [&]() {
        if (meshObject->format() == "off") {
            meshObject->collapseRandomEdge(Scene::MIDPOINT_APPROXIMATION_METHOD);
        }
    };
    auto slambda = [&]() {
        
        if (meshObject->format() == "off") {
            int nVV = meshObject->nVisibleVertices();
            int n = fmax(1, nVV / 100);
            printf("Random edge midpoint collapsing %i times...\n", n);
            for (int i = 0; i < n; i++) meshObject->collapseRandomEdge(Scene::MIDPOINT_APPROXIMATION_METHOD);
        }
    };
    auto zlambda = [&]() {
        if (meshObject->format() == "off"){
            meshObject->quadricSimplify();
        }
    };
    auto xlambda = [&]() {
        if (meshObject->format() == "off") {
            int nCP = meshObject->nCollapsablePairs();
            int n = sqrt(nCP) / 2;
            if (n == 0) n = fmin(nCP, 1);
            printf("%i  ", n);
            for (int i = 0; i < n; i++) {
                meshObject->quadricSimplify();
            }
        }
    };
    auto nlambda = [&]() {
        printf("Writing progressive mesh data to %s\n", meshObject->outFileName());
        meshObject->makeProgressiveMeshFile();
    };
    auto mlambda = [&]() {
        if (meshObject->format() == "off") {
            int collapseCount = 0;
            for (int i = 0; i < meshObject->nVertices(); i++) {
                if (collapseCount % 10 == 0) printf("Pairs Collapsed: %i\r", collapseCount);
                meshObject->quadricSimplify();
                collapseCount++;
            }
            //printf("Pairs Collapsed: %i\n", collapseCount);
            printf("\nMaking Progressive Mesh File: %s\n", meshObject->outFileName());
            meshObject->makeProgressiveMeshFile();
        }
    };
    auto pluslambda = [&]() {
        if (meshObject->format() == "offpm") {
            if (meshObject->complexity() < meshObject->nVertices()) {
                //std::string bmpName = fileName + std::to_string(bmpCounter) + ".bmp";
                //SaveAsBMP(bmpName.c_str());
                //bmpCounter++;
                meshObject->collapseTo((1.0f + complexityMultiplier)*meshObject->complexity());
            }
        }
    };
    auto minuslambda = [&]() {
        if (meshObject->format() == "offpm") meshObject->collapseTo((1.0 - complexityMultiplier)*meshObject->complexity());
    };
    auto rbracketlambda = [&]() {
        if (meshObject->format() == "offpm") {
            if (meshObject->complexity() < meshObject->nVertices()) {
                //std::string bmpName = fileName + std::to_string(bmpCounter) + ".bmp";
                //SaveAsBMP(bmpName.c_str());
                //bmpCounter++;
                meshObject->collapseTo(meshObject->complexity() + complexityIncrement);
            }
        }
    };
    auto lbracketlambda = [&]() {
        if (meshObject->format() == "offpm") meshObject->collapseTo(meshObject->complexity() - complexityIncrement);
    };
    auto ilambda = [&]() {
        std::string bmpName = fileName + std::to_string(bmpCounter) + ".bmp";
        SaveAsBMP(bmpName.c_str());
        bmpCounter++;
    };
    auto quotelambda = [&]() {
        complexityMultiplier += 1.0 / 256.0;
        printf("Complexity Multiplier:_%f_____\r", complexityMultiplier);
    };
    auto colonlambda = [&]() {
        complexityMultiplier = fmax(0.0, complexityMultiplier - 1.0 / 256);
        printf("Complexity Multiplier:_%f_____\r", complexityMultiplier);
    };
    auto slashlambda = [&]() {
        complexityIncrement += 1.0 / 256.0;
        printf("Complexity Increment:__%f_____\r", complexityIncrement);
    };
    auto periodlambda = [&]() {
        complexityIncrement = fmin(0.0, complexityIncrement - 1.0 / 256.0);
        printf("Complexity Increment:__%f_____\r", complexityIncrement);
    };
    auto tlambda = [&]() {
        meshObject->toggleDrawMode();
    };
    auto ylambda = [&]() {
        meshObject->toggleCustomColors();
    };
    keyboard.register_hotkey('\'', quotelambda);
    keyboard.register_hotkey(';', colonlambda);
    keyboard.register_hotkey('/', slashlambda);
    keyboard.register_hotkey('.', periodlambda);
    keyboard.register_hotkey(']', rbracketlambda);
    keyboard.register_hotkey('[', lbracketlambda);
    keyboard.register_hotkey('i', ilambda);
    keyboard.register_hotkey('=', pluslambda);
    keyboard.register_hotkey('-', minuslambda);
    keyboard.register_hotkey('a', alambda);
    keyboard.register_hotkey('s', slambda);
    keyboard.register_hotkey('x', xlambda);
    keyboard.register_hotkey('z', zlambda);
    keyboard.register_hotkey('t', tlambda);
    keyboard.register_hotkey('y', ylambda);
    keyboard.register_hotkey('n', nlambda);
    keyboard.register_hotkey('m', mlambda);

    MANAGER.drawElements();

    return 0;
}