osg::Geode* SolarSystem::createPlanet( double radius, const std::string& name, const osg::Vec4& color , const std::string& textureName1, const std::string& textureName2) { osg::Geode* geodePlanet = createPlanet( radius, name, color , textureName1); if( !textureName2.empty() ) { osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile( textureName2 ); if ( image ) { osg::StateSet* stateset = geodePlanet->getOrCreateStateSet(); osg::TexEnvCombine* texenv = new osg::TexEnvCombine; texenv->setCombine_RGB(osg::TexEnvCombine::INTERPOLATE); texenv->setSource0_RGB(osg::TexEnvCombine::PREVIOUS); texenv->setOperand0_RGB(osg::TexEnvCombine::SRC_COLOR); texenv->setSource1_RGB(osg::TexEnvCombine::TEXTURE); texenv->setOperand1_RGB(osg::TexEnvCombine::SRC_COLOR); texenv->setSource2_RGB(osg::TexEnvCombine::PRIMARY_COLOR); texenv->setOperand2_RGB(osg::TexEnvCombine::SRC_COLOR); stateset->setTextureAttribute( 1, texenv ); osg::Texture2D* tex2d = new osg::Texture2D( image ); tex2d->setWrap( osg::Texture::WRAP_S, osg::Texture::REPEAT ); tex2d->setWrap( osg::Texture::WRAP_T, osg::Texture::REPEAT ); stateset->setTextureAttributeAndModes( 1, tex2d, osg::StateAttribute::ON ); } } return( geodePlanet ); }// end SolarSystem::createPlanet
int main() { GLFWwindow* window; if (!glfwInit()) { printf("Error initializing GLFW\n"); return -1; } glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, GL_VERSION_MAJOR); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, GL_VERSION_MINOR); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); //glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); glfwWindowHint(GLFW_SAMPLES, 8); window = glfwCreateWindow(1920, 1080, "Test Window", glfwGetPrimaryMonitor(), NULL); if (!window) { printf("Error creating GLFW window\n"); glfwTerminate(); return -1; } glfwMakeContextCurrent(window); glfwSetKeyCallback(window, key_callback); glfwSetMouseButtonCallback(window, mouse_button_callback); glfwSetCursorPosCallback(window, cursor_position_callback); glfwSetScrollCallback(window, scroll_callback); if (gl3wInit()) { printf("Error initializing OpenGL\n"); glfwTerminate(); return -1; } printf("OpenGL %s GLSL %s\n", glGetString(GL_VERSION), glGetString(GL_SHADING_LANGUAGE_VERSION)); if (!gl3wIsSupported(GL_VERSION_MAJOR, GL_VERSION_MINOR)) { printf("OpenGL %i.%i is not supported\n", GL_VERSION_MAJOR, GL_VERSION_MINOR); glfwTerminate(); return -1; } glViewport(0, 0, 1920, 1080); glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); GLuint programID = loadShadersvf("shaders/simple.vsh", "shaders/simple.fsh"); GLuint vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); GLuint vbo; glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); //glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_buffer_data), vertex_buffer_data, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(sphere_model), sphere_model, GL_STATIC_DRAW); GLuint ibo; glGenBuffers(1, &ibo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(sphere_indices), sphere_indices, GL_STATIC_DRAW); //glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(index_buffer_data), index_buffer_data, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glDisableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); double timesteps[] = {1, 60, 60 * 60, 60 * 60 * 24, 60 * 60 * 24 * 7, 60 * 60 * 24 * 30, 60 * 60 * 24 * 365}; int timestepCounter = 0; World world; world.num_functions = 0; //Add the functions for gravitational attraction to the world's object list, and have it execute when updateWorld() is called addFunction(&world, attract, COMPONENT_POSITION | COMPONENT_VELOCITY); addFunction(&world, move, COMPONENT_POSITION | COMPONENT_VELOCITY); unsigned int bodies[11]; //Create all the planets with the correct masses, sizes, and velocities bodies[0] = createStar(&world, "Sun", 0, 0, 0, 0, 0, 0, 1.989 * pow(10, 30), 6.963 * pow(10,8), 1); bodies[1] = createPlanet(&world, "Mercury", -0.35790 * AU, -0.04240 * AU, -0.11618 * AU, -0.00000004 * AU, 0.00000003 * AU, 0.00000031 * AU, 3.285 * pow(10,23), 2.44 * pow(10,6), 0.4549, 0.4509, 0.4705); bodies[2] = createPlanet(&world, "Venus", -0.46075 * AU, -0.03422 * AU, -0.56289 * AU, -0.00000017 * AU, -0.00000001 * AU, 0.00000014 * AU, 4.867 * pow(10, 24), 6.052 * pow(10,6), 0.4627, 0.568, 0.380); bodies[3] = createPlanet(&world, "Earth", 0.99542 * AU, 0, 0.01938 * AU, 0.00000001 * AU, 0, -0.00000019 * AU, 5.972 * pow(10, 24), 6.371 * pow(10,6), 0, 0.4, 0.6); bodies[4] = createPlanet(&world, "Moon", 0.99708 * AU, -0.00016 * AU, 0.02141 * AU, 0.00000001 * AU + 722.66, 0, -0.00000019 * AU - 722.66, 7.348 * pow(10, 22), 1.737 * pow(10,6), 0.4, 0.4, 0.4); bodies[5] = createPlanet(&world, "Mars", 1.38763 * AU, 0.01755 * AU, -0.79510 * AU, -0.00000009 * AU, -0.00000001 * AU, -0.00000013 * AU, 6.39 * pow(10, 23), 3.39 * pow(10,6), 0.8157, 0.5294, 0.3922); bodies[6] = createPlanet(&world, "Jupiter", 5.32462 * AU, 0.11488 * AU, 1.04223 * AU, 0.00000002 * AU, 0, -0.00000008 * AU, 1.898 * pow(10,27), 69.911 * pow(10,6), 0.8824, 0.7961, 0.7412); bodies[7] = createPlanet(&world, "Saturn", 3.30063 * AU, 0.29595 * AU, -9.47771 * AU, -0.00000006 * AU, 0, -0.00000002 * AU, 5.683 * pow(10,26), 58.232 * pow(10,6), 0.7843, 0.6392, 0.4314); bodies[8] = createPlanet(&world, "Uranus", -18.76415 * AU, -0.21783 * AU, 6.83528 * AU, 0.00000002 * AU, 0, 0.00000004 * AU, 8.681 * pow(10,25), 25.362 * pow(10,6), 0.8314, 0.9804, 0.9922); bodies[9] = createPlanet(&world, "Neptune", -28.07048 * AU, -0.42924 * AU, -10.42730 * AU, -0.00000002 * AU, 0, 0.0000003 * AU, 1.024 * pow(10,26), 24.622 * pow(10,6), 0.2, 0.2902, 0.7451); bodies[10] = createPlanet(&world, "Pluto", -8.88081 * AU, 0.86973 * AU, -31.76914 * AU, -0.00000003 * AU, -0.00000001 * AU, 0.0000001 / 41.0 * AU, 1.309 * pow(10,22), 1.186 * pow(10, 6), 0.7137, 0.6824, 0.6314); double mat4d_projection[4][4] = MATRIX_IDENTITY_4; mat4dProjection(M_PI / 2.0, 16.0/9.0, pow(10,6), pow(10,13), mat4d_projection); unsigned int parent = 3; Camera camera; makeCamera(&camera, 0, 0, pow(10, 9), &world, bodies[parent]); double mat4d_camera[4][4] = MATRIX_IDENTITY_4; float mat4_camera[4][4]; double sensitivity = 0.005; printf("Starting main loop\n"); while (!glfwWindowShouldClose(window)) { clearInput(); glfwPollEvents(); //rotate or zoom out the camera depending on camera movement and scrolling double dx, dy, dz; getMouseDelta(&dx, &dy, &dz); updateCamera(&camera, dx * sensitivity, dy * sensitivity, dz); if (getClicked()) { if(++parent > 10) parent = 0; changeCameraParent(&camera, pow(10, 9), &world, bodies[parent]); } timestepCounter += getTimeStepChange(); if (timestepCounter > 6) timestepCounter = 0; else if (timestepCounter < 0) timestepCounter = 6; setTimeStep(timesteps[timestepCounter]); //get the time since the last frame to do proper delta-timing for the gravitational attraction and movement double delta = timeTick(); updateWorld(&world, delta); getCameraMatrix(&camera, mat4d_camera); double modelView[4][4]; mat4dMlt(mat4d_projection, mat4d_camera, modelView); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(programID); glBindVertexArray(vao); glEnableVertexAttribArray(0); //For each of the objects in the world, loop through them and render them if they have a position and radius, //rendering works by rendering the same sphere over and over, with a different translation and scale //because all objects in this program are spheres for (int i = 0; i < WORLD_MAX_ENTITIES; ++i) { if (isEntity(&world, i, COMPONENT_POSITION | COMPONENT_RADIUS)) { /*double pos[4] = {world.position[i].position[0], world.position[i].position[1], world.position[i].position[2], 1}; vec4dMltMat(mat4d_camera, pos, pos); double distance = vec3dLength(pos); printf("%i: %f\n", i, distance); double mat4d_model[4][4] = MATRIX_IDENTITY_4; double radius = world.radius[i].radius; double scaleFactor = distance * (1 / pow(radius,2)); printf("Scale factor: %f\n", scaleFactor); if (scaleFactor > pow(10,-3)) { printf("Has been scaled\n"); radius = radius / scaleFactor; } printf("Radius: %f\n", radius);*/ double mat4d_model[4][4] = MATRIX_IDENTITY_4; double radius = world.radius[i].radius; double scale[3] = {radius, radius, radius}; mat4dGenScale(scale, mat4d_model); mat4dTranslate(world.position[i].position, mat4d_model); double modelViewProjection[4][4]; mat4dMlt(modelView, mat4d_model, modelViewProjection); float modelViewProjectionf[4][4]; doubleToSingle(modelViewProjection[0], modelViewProjectionf[0], 16); double color[3] = {1,1,1}; if (isEntity(&world, i, COMPONENT_COLOR)) { vec3dSetEqual(world.color[i].color, color); } float colorf[3]; doubleToSingle(color, colorf, 3); GLint colorLoc = glGetUniformLocation(programID, "in_color"); glUniform3fv(colorLoc, 1, colorf); GLint mvpLoc = glGetUniformLocation(programID, "mvp"); glUniformMatrix4fv(mvpLoc, 1, GL_FALSE, modelViewProjectionf[0]); glDrawElements(GL_TRIANGLES, sphere_num_indices, GL_UNSIGNED_INT, 0); } } glDisableVertexAttribArray(0); glBindVertexArray(0); glfwSwapBuffers(window); } glfwDestroyWindow(window); glfwTerminate(); return 0; }
bool DemoScene::userStart() { // System Tests // // addSystem<RotationForceSystem>(0); addSystem<MeshRendererSystem>(0); addSystem<GraphNodeSystem>(100); addSystem<TrackBallSystem>(150); addSystem<CameraSystem>(200); // // // end System Test std::string perModelVars[] = { "model" }; std::string perFrameVars[] = { "projection", "view", "light", "time" }; std::string materialBasic[] = { "ambient", "diffuse", "specular", "transmittance", "emission", "shininess" }; OpenGLTools::Shader &s = _engine.getInstance<Renderer>().addShader("MaterialBasic", "./Shaders/MaterialBasic.vp", "./Shaders/MaterialBasic.fp"); _engine.getInstance<Renderer>().addUniform("MaterialBasic") .init(&s, "MaterialBasic", materialBasic); _engine.getInstance<Renderer>().addUniform("PerFrame") .init(&s, "PerFrame", perFrameVars); _engine.getInstance<Renderer>().addUniform("PerModel") .init(&s, "PerModel", perModelVars); _engine.getInstance<Renderer>().addShader("earth", "./Shaders/earth.vp", "./Shaders/earth.fp"); _engine.getInstance<Renderer>().addShader("basic", "Shaders/basic.vp", "Shaders/basic.fp", "Shaders/tesselation.gp"); _engine.getInstance<Renderer>().addShader("basicLight", "Shaders/light.vp", "Shaders/light.fp"); _engine.getInstance<Renderer>().addShader("bump", "Shaders/bump.vp", "Shaders/bump.fp"); _engine.getInstance<Renderer>().addShader("fboToScreen", "Shaders/fboToScreen.vp", "Shaders/fboToScreen.fp"); _engine.getInstance<Renderer>().addShader("brightnessFilter", "Shaders/brightnessFilter.vp", "Shaders/brightnessFilter.fp"); _engine.getInstance<Renderer>().addShader("blurY", "Shaders/brightnessFilter.vp", "Shaders/blur1.fp"); _engine.getInstance<Renderer>().getShader("MaterialBasic")->addTarget(GL_COLOR_ATTACHMENT0).setTextureNumber(4).build(); _engine.getInstance<Renderer>().getShader("basic")->addTarget(GL_COLOR_ATTACHMENT0).setTextureNumber(1).build(); _engine.getInstance<Renderer>().getShader("basicLight")->addTarget(GL_COLOR_ATTACHMENT0).setTextureNumber(1).build(); _engine.getInstance<Renderer>().getShader("bump")->addTarget(GL_COLOR_ATTACHMENT0).setTextureNumber(2).build(); _engine.getInstance<Renderer>().getShader("fboToScreen")->addTarget(GL_COLOR_ATTACHMENT0) .addLayer(GL_COLOR_ATTACHMENT0).build(); _engine.getInstance<Renderer>().getShader("earth")->addTarget(GL_COLOR_ATTACHMENT0).setTextureNumber(4).build(); _engine.getInstance<Renderer>().getShader("brightnessFilter")->addTarget(GL_COLOR_ATTACHMENT1) .addLayer(GL_COLOR_ATTACHMENT0).build(); _engine.getInstance<Renderer>().getShader("blurY")->addTarget(GL_COLOR_ATTACHMENT2) .addLayer(GL_COLOR_ATTACHMENT0).addLayer(GL_COLOR_ATTACHMENT1).build(); _engine.getInstance<Renderer>().getUniform("PerFrame")->setUniform("light", glm::vec4(0, 0, 0, 1)); _engine.getInstance<Renderer>().bindShaderToUniform("basicLight", "PerFrame", "PerFrame"); _engine.getInstance<Renderer>().bindShaderToUniform("basicLight", "PerModel", "PerModel"); _engine.getInstance<Renderer>().bindShaderToUniform("basicLight", "MaterialBasic", "MaterialBasic"); _engine.getInstance<Renderer>().bindShaderToUniform("basic", "PerFrame", "PerFrame"); _engine.getInstance<Renderer>().bindShaderToUniform("basic", "PerModel", "PerModel"); _engine.getInstance<Renderer>().bindShaderToUniform("basic", "MaterialBasic", "MaterialBasic"); _engine.getInstance<Renderer>().bindShaderToUniform("earth", "PerFrame", "PerFrame"); _engine.getInstance<Renderer>().bindShaderToUniform("earth", "PerModel", "PerModel"); _engine.getInstance<Renderer>().bindShaderToUniform("earth", "MaterialBasic", "MaterialBasic"); _engine.getInstance<Renderer>().bindShaderToUniform("bump", "PerFrame", "PerFrame"); _engine.getInstance<Renderer>().bindShaderToUniform("bump", "PerModel", "PerModel"); _engine.getInstance<Renderer>().bindShaderToUniform("bump", "MaterialBasic", "MaterialBasic"); _engine.getInstance<Renderer>().bindShaderToUniform("MaterialBasic", "PerFrame", "PerFrame"); _engine.getInstance<Renderer>().bindShaderToUniform("MaterialBasic", "PerModel", "PerModel"); _engine.getInstance<Renderer>().bindShaderToUniform("MaterialBasic", "MaterialBasic", "MaterialBasic"); _engine.getInstance<Resources::ResourceManager>().addResource("model:ball", new Resources::SharedMesh(), "./Assets/ball/ball.obj"); SmartPointer<Resources::Texture> toRepeat = new Resources::Texture(); toRepeat->setWrapMode(GL_REPEAT); _engine.getInstance<Resources::ResourceManager>().addResource("texture:sun", new Resources::Texture(), "./Assets/SunTexture.tga"); _engine.getInstance<Resources::ResourceManager>().addResource("texture:earth", new Resources::Texture(), "./Assets/EarthTexture.tga"); _engine.getInstance<Resources::ResourceManager>().addResource("texture:earthBump", new Resources::Texture(), "./Assets/EarthTextureBump.tga"); _engine.getInstance<Resources::ResourceManager>().addResource("texture:earthNight", new Resources::Texture(), "./Assets/EarthNightTexture.tga"); _engine.getInstance<Resources::ResourceManager>().addResource("texture:earthClouds", toRepeat, "./Assets/EarthClouds.tga"); _engine.getInstance<Resources::ResourceManager>().addResource("texture:sun", new Resources::Texture(), "./Assets/SunTexture.tga"); _engine.getInstance<Resources::ResourceManager>().addResource("texture:moon", new Resources::Texture(), "./Assets/MoonTexture.tga"); _engine.getInstance<Resources::ResourceManager>().addResource("texture:moonBump", new Resources::Texture(), "./Assets/MoonNormalMap.tga"); _engine.getInstance<Resources::ResourceManager>().addResource("cubemap:space", new Resources::CubeMap(), "./Assets/skyboxSpace"); auto sun = createPlanet(0, 0, glm::vec3(0), glm::vec3(100), "basic", "texture:sun"); auto earth = createPlanet(7, 20, glm::vec3(300, 0, 0), glm::vec3(20), "earth", "texture:earth", "texture:earthNight", "texture:earthClouds", "texture:earthBump"); auto moon = createPlanet(0, 10, glm::vec3(5, 0, 0), glm::vec3(0.5), "bump", "texture:moon", "texture:moonBump"); earth->getComponent<Component::GraphNode>()->getSonsBegin()->get()->getComponent<Component::GraphNode>()->addSon(moon); // Generating a lot of planet for performance test // // { unsigned int nbPlanet = 70; Entity planets[70]; for (unsigned int i = 0; i < nbPlanet; ++i) { planets[i] = createPlanet((std::rand() % 200) / 100.0f , (std::rand() % 200) / 100.0f, glm::vec3(std::rand() % 300 - 150, std::rand() % 300 - 150, std::rand() % 300 - 150), glm::vec3(std::rand() % 10 + 10), "basic", "texture:sun"); if (i == 0) sun->getComponent<Component::GraphNode>()->addSon(planets[i]); else planets[i - 1]->getComponent<Component::GraphNode>()->addSon(planets[i]); } } // // // END : Generating a lot of planet for performance test // -- // Setting camera with skybox // -- auto camera = createEntity(); camera->addComponent<Component::GraphNode>(); auto cameraComponent = camera->addComponent<Component::CameraComponent>(); auto trackBall = camera->addComponent<Component::TrackBall>( *(earth->getComponent<Component::GraphNode>()->getSonsBegin()), 50.0f, 3.0f, 1.0f); std::string vars[] = { "projection", "view" }; OpenGLTools::Shader &sky = _engine.getInstance<Renderer>().addShader("cubemapShader", "Shaders/cubemap.vp", "Shaders/cubemap.fp"); _engine.getInstance<Renderer>().getShader("cubemapShader")->addTarget(GL_COLOR_ATTACHMENT0).setTextureNumber(1).build(); _engine.getInstance<Renderer>().addUniform("cameraUniform"). init(&sky, "cameraUniform", vars); _engine.getInstance<Renderer>().bindShaderToUniform("cubemapShader", "cameraUniform", "cameraUniform"); cameraComponent->attachSkybox("cubemap:space", "cubemapShader"); // Compute shader Tests // // //OpenGLTools::ComputeShader *cs = new OpenGLTools::ComputeShader(); //cs->init(File("../GameEngine/Shaders/test.cp")); //cs->use(); //glDispatchCompute(512/16, 512/16, 1); // // // End compute shader test return (true); }