void setup() { size(1024, 768); background(0); setFrameRate(60); // Setup lighting ambientLight(30); light.init(200, 200, 200, 0, 3, 0); // Uncomment this line to see the position of the light light.drawDebug(true); // Init 3d object's properties box.init(50, 25, 75); box.setPosition(width/2.0f, height/2.0f); sphere1.init(30); sphere2.init(60); sphere3.init(100); // Now, we make the spheres children of the box's (scene node) box.addChild( sphere1 ); box.addChild( sphere2 ); box.addChild( sphere3 ); // Translate the sphere (relative to its parent, the box) // This way, when we rotate the box (parent object), the spheres will orbitate around it sphere1.setPosition( 2, 0, 0 ); sphere2.setPosition( 5, 0, 0 ); sphere3.setPosition( 7, 0, 0 ); // Add the second light as child of one of the spheres sphere1.addChild( light ); }
//--------------------------------------------------------------- void LightExporter::exportEnvironmentAmbientLight() { // Add the ambient lighting from the environment settings Point3 ambientColor = mDocumentExporter->getMaxInterface()->GetAmbient(0, FOREVER); COLLADASW::Color ambientLightColor = EffectExporter::maxColor2Color( ambientColor ); COLLADASW::AmbientLight ambientLight(COLLADASW::LibraryLights::mSW, ENVIRONMENT_AMBIENT_LIGHT_ID, ENVIRONMENT_AMBIENT_LIGHT_NAME); ambientLight.setColor( ambientLightColor ); addLight( ambientLight ); }
void setup() { // start AudioInputphone mic.startCapturing(); // setup plane and sphere plane.init( 100000 ); plane.setPosition( width/2, height/2 + 500, 0 ); sphere.init( 100 ); sphere.setPosition( width/2, height/2, 0 ); // setup light ambientLight( 20, 20, 20 ); light.init( 0, 0, 0, width/2, height/2, 200 ); }
void buildAndRenderScene(Scene &scene, Camera &camera, RenderTarget &renderTarget) { // Build scene AmbientLight ambientLight(Color::white); PointLight light1(Vector3D(50.0, 70.0, 0.0)); PointLight light2(Vector3D(50.0, 70.0, 200.0)); Torus sphere1(10, 4, Vector3D(0.0, 20.0, 100.0)); PhongMaterial material1(Color::red); Sphere sphere2(10, Vector3D(0.0, 45.0, 100.0)); PhongMaterial material2(Color::green); Sphere sphere3(10, Vector3D(35.0, 20.0, 100.0)); PhongMaterial material3(Color::blue); Plane plane1(Vector3D(0, 0, 0), Vector3D(0.0, 1.0, 0.0)); PhongMaterial material4(Color(0.0, 1.0, 1.0)); Plane plane2(Vector3D(-100, 0, 0), Vector3D(1.0, 0.0, 0.0)); PhongMaterial material5(Color(1.0, 0.0, 1.0)); Plane plane3(Vector3D(0, 0, 500), Vector3D(0.0, 0.0, -1.0)); PhongMaterial material6(Color(1.0, 1.0, 0.0)); sphere1.setMaterial(&material1); sphere2.setMaterial(&material2); sphere3.setMaterial(&material3); plane1.setMaterial(&material4); plane2.setMaterial(&material5); plane3.setMaterial(&material6); scene.addObject(&sphere1); scene.addObject(&sphere2); scene.addObject(&sphere3); scene.addObject(&plane1); scene.addObject(&plane2); scene.addObject(&plane3); scene.addLight(&light1); scene.addLight(&light2); scene.setAmbientLight(&ambientLight); // Render scene camera.computeFrame(); camera.renderScene(scene, renderTarget); renderTarget.update(); }
void setup() { size( 1024, 768 ); // load sound sound.load( "loop.ogg" ); sound.play( true ); // setup plane and sphere plane.init( 100000 ); plane.setPosition( width/2, height/2 + 200, 0 ); sphere.init( 100 ); sphere.setPosition( width/2, height/2, 0 ); // setup light ambientLight( 20, 20, 20 ); light.init( 0, 0, 0, width/2, height/2, 200 ); }
void setup() { size(1024, 768); background(0); enableShadows( STENCIL_MODULATIVE ); applyCoordinateSystemTransform(OPENGL3D); // Setup lighting ambientLight(10); light.init(255, 255, 255, width/2 + 100, height/2 + 100, 100); // Uncomment this line to see the position of the light light.drawDebug(); // Init 3d object's properties box.init(10, 4, 14); box.setPosition(width/2.0f, 100); sphere1.init(6); sphere2.init(12); sphere3.init(20); // Now, we make the spheres children of the box's (scene node) box.addChild( sphere1 ); box.addChild( sphere2 ); box.addChild( sphere3 ); // Translate the sphere (relative to its parent, the box) // This way, when we rotate the box (parent object), the spheres will orbitate around it sphere1.setPosition( 2, -2, 0 ); sphere2.setPosition( 5, -7, 0 ); sphere3.setPosition( 7, -12, 0 ); // Ground ground.init(5000, 25, 5000 ); ground.setPosition( width/2, -200 ); // Camera control enableDefault3DCameraControl(); }
Vector3d traceRay(Ray* ray, vector<SceneObject*>* objects, vector<SceneObject*>* lights, int remainingDepth) { Vector3d backgroundColour(0, 0, 0); Vector3d ambientLight(25, 25, 25); /*Vector3d RED(255, 0, 0); Vector3d GREEN(0, 255, 0); Vector3d BLUE(255, 0, 255); Vector3d YELLOW(255, 255, 0); Vector3d CYAN(0, 255, 255); Vector3d MAJENTA(255, 0, 255);*/ if (remainingDepth <= 0) return backgroundColour; vector<Intersection*>* intersections = getIntersections(objects, ray, NULL, false, (remainingDepth < 2)); Intersection* closestIntersection = getClosestIntersection(ray->origin, intersections); Vector3d closestOrigin; Vector3d closestDirection; SceneObject* closestObject; if (closestIntersection != NULL) { closestOrigin = *(closestIntersection->origin); closestDirection = *(closestIntersection->direction); closestObject = closestIntersection->object; } freeIntersections(intersections); intersections->clear(); delete intersections; if (closestIntersection != NULL) { Vector3d fullLightColour = ambientLight; Vector3d surfaceColour = *(closestIntersection->object->colour); //for each light, add it to the full light on this point (if not blocked) for (unsigned int lightNum = 0; lightNum < lights->size(); lightNum++) { SceneObject* light = (*lights)[lightNum]; Vector3d toLight = *(light->position) - closestOrigin; Vector3d toLightNormalized = toLight.normalized(); double dot = toLightNormalized.dot(closestDirection); if (dot > 0) { intersections = getIntersections(objects, new Ray(&closestOrigin, &toLightNormalized), closestObject, true, false); bool inLight = false; if (intersections->size() == 0) { inLight = true; } else { Intersection* intersection = (*intersections)[0]; Vector3d intersectionOrigin = Vector3d(*(intersection->origin)); Vector3d intersectionDirection = Vector3d(*(intersection->direction)); SceneObject* obj = intersection->object; if ((intersectionOrigin - closestOrigin).norm() > toLight.norm()) { inLight = true; } } if (inLight) { fullLightColour += *(light->colour) * dot; } freeIntersections(intersections); intersections->clear(); delete intersections; } } double reflectivity = closestObject->reflectivity; if (reflectivity > 0) { Vector3d rayDirection = *(ray->direction); Vector3d normal = closestDirection; Vector3d reflectedDirection = rayDirection - ((2 * (normal.dot(rayDirection))) * normal); Ray* reflectedRay = new Ray(&closestOrigin, &reflectedDirection); Vector3d reflectionColour = traceRay(reflectedRay, objects, lights, remainingDepth - 1); delete reflectedRay; surfaceColour *= 1 - reflectivity; surfaceColour += reflectionColour * reflectivity; } Vector3d endColour = surfaceColour; endColour[0] *= fullLightColour[0] / 255; endColour[1] *= fullLightColour[1] / 255; endColour[2] *= fullLightColour[2] / 255; return endColour; } else { return backgroundColour; } }
bool CEditorWorld::createScene() { String setting; //read ambient light rgb Ogre::ColourValue ambientLight(0,0,0,0); if ( (setting = I_APP->getSetting("/config/world/light/AmbientLight") ) != "") ambientLight += StringConverter::parseColourValue(setting); else ambientLight += Ogre::ColourValue(0.5, 0.5, 0.5, 1); //read main light rgb Ogre::ColourValue mainLight(0,0,0,0); if ( (setting = I_APP->getSetting("/config/world/light/MainLight") ) != "") mainLight += StringConverter::parseColourValue(setting); else mainLight += Ogre::ColourValue(0.2, 0.18, 0.2); bool shadows; if ( (setting = I_APP->getSetting("/config/world/shadows") ) != "") shadows = StringConverter::parseBool(setting); else shadows = true; bool light; if ( (setting = I_APP->getSetting("/config/world/light/enableMainLight") ) != "") light = StringConverter::parseBool(setting); else light = true; //-------------------------------------------------- // Set default mipmap level (NB some APIs ignore this) Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5); Camera* cam = sceneManager()->createCamera("PlayerCam"); cam->setPosition(Ogre::Vector3(0,200,0)); cam->lookAt(Vector3(0,0,0)); cam->setNearClipDistance(1); Ogre::Viewport* vp = CEditorApp::instance()->getRenderWindow()->addViewport(cam); vp->setBackgroundColour(Ogre::ColourValue(0,0,0)); // Alter the camera aspect ratio to match the viewport cam->setAspectRatio( Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight())); CLog::instance()->log(CLog::msgLvlInfo,__FUNCTION__,"Loading world...\n"); // Set ambient light sceneManager()->setAmbientLight(ambientLight); // Create a light Light* l = sceneManager()->createLight("MainLight"); l->setPosition(0,800,-600); l->setSpecularColour(mainLight); CLog::instance()->log(CLog::msgLvlInfo,__FUNCTION__,"Created light.\n"); l->setCastShadows(shadows); // Create a skydome sceneManager()->setSkyDome(true, "Examples/CloudySky", 5, 8); sceneManager()->setShadowTechnique(SHADOWTYPE_STENCIL_MODULATIVE); sceneManager()->setShadowColour(ColourValue(0.5,0.5,0.5)); CLog::instance()->log(CLog::msgLvlInfo,__FUNCTION__,"Scene initialization completed.\n"); sceneManager()->setWorldGeometry("../data/world.ini"); PagingLandscapeListenerManager::getSingleton().addListener(this); vp->setBackgroundColour(ColourValue(0.93, 0.86, 0.76)); // if( !loadZones() ) // return false; CLog::instance()->log(CLog::msgLvlInfo,__FUNCTION__,"Loading world complete.\n"); return true; };
void Window::run() { sf::Window window(sf::VideoMode(1920, 1200), "Graphics", sf::Style::Fullscreen, sf::ContextSettings(32, 0, 0, 3, 2)); window.setVerticalSyncEnabled(true); window.setMouseCursorVisible(false); int sizeX = 1920; int sizeY = 1200; int mouseX, mouseY; // init states, load resources resize(1920, 1200); sf::Mouse::setPosition(sf::Vector2i(sizeX/2, sizeY/2), window); // main loop bool running = true; bool leftKeyDown, rightKeyDown, upKeyDown, downKeyDown, kKeyDown, jKeyDown, wKeyDown, aKeyDown, sKeyDown, dKeyDown; while (running) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) { running = false; } else if (event.type == sf::Event::Resized) { // window size changed resize(event.size.width, event.size.height); } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { running = false; } leftKeyDown = sf::Keyboard::isKeyPressed(sf::Keyboard::Left); rightKeyDown = sf::Keyboard::isKeyPressed(sf::Keyboard::Right); upKeyDown = sf::Keyboard::isKeyPressed(sf::Keyboard::Up); downKeyDown = sf::Keyboard::isKeyPressed(sf::Keyboard::Down); jKeyDown = sf::Keyboard::isKeyPressed(sf::Keyboard::J); kKeyDown = sf::Keyboard::isKeyPressed(sf::Keyboard::K); wKeyDown = sf::Keyboard::isKeyPressed(sf::Keyboard::W); aKeyDown = sf::Keyboard::isKeyPressed(sf::Keyboard::A); sKeyDown = sf::Keyboard::isKeyPressed(sf::Keyboard::S); dKeyDown = sf::Keyboard::isKeyPressed(sf::Keyboard::D); } mouseX = sf::Mouse::getPosition(window).x; mouseY = sf::Mouse::getPosition(window).y; // apply input if (wKeyDown) player.position += player.getLook(); if (sKeyDown) player.position -= player.getLook(); if (aKeyDown) player.position -= player.getRight(); if (dKeyDown) player.position += player.getRight(); glm::mat4 View = player.orientation; View = glm::translate(View, -player.position); glMultMatrixf(glm::value_ptr(View)); // clear buffers glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // render scene glMatrixMode(GL_MODELVIEW); DrawGround(); glShadeModel(GL_SMOOTH); glEnable(GL_LIGHTING); glm::vec4 ambientLight(1.0f,1.0f,1.0f,1.0f); glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, glm::value_ptr(ambientLight)); glBegin(GL_POLYGON); glColor3ub(255,0,0); glVertex3f(-10.0,-10.0,0); glColor3ub(0,255,0); glVertex3f(10.0,-10.0,0.0); glColor3ub(0,0,255); glVertex3f(0.0,10.0,0.0); glEnd(); glLoadIdentity(); // move mouse float delta; delta = mouseX - sizeX/2; player.rotate(delta * 0.2f, glm::vec3(0.0f,1.0f,0.0f)); delta = mouseY - sizeY/2; player.rotateRight(delta * 0.2f); sf::Mouse::setPosition(sf::Vector2i(sizeX/2, sizeY/2), window); // swap buffers window.display(); } }