int main() { Display window("Window", 1500, 800); Camera camera(glm::vec3(0.0f, 0.0f, 0.0f), 70.0f, (float)window.GetReference().getSize().x / (float)window.GetReference().getSize().y, 0.1f, 1800.0f); BasicShader terrain_shader; terrain_shader.loadFromFile("./res/shader/BasicShader"); BasicShader skaybox_shader; skaybox_shader.loadFromFile("./res/shader/BasicShader"); BasicShader grass_shader; grass_shader.loadFromFile("./res/shader/BasicShader"); grass_shader.SetMinLightStr(0.7); grass_shader.SetMaxLightStr(1.0); BasicShader model_shader; model_shader.loadFromFile("./res/shader/BasicShader"); Skaybox skaybox; skaybox.SetShader(skaybox_shader); skaybox.AtachCamera(camera); skaybox.SetBack("./res/skybox2/skybox_back.bmp"); skaybox.SetFront("./res/skybox2/skybox_front.bmp"); skaybox.SetLeft("./res/skybox2/skybox_left.bmp"); skaybox.SetRight("./res/skybox2/skybox_right.bmp"); skaybox.SetBotom("./res/skybox2/skybox_bottom.bmp"); skaybox.SetTop("./res/skybox2/skybox_top.bmp"); skaybox.SetSize(1000); skaybox.Init(); Terrain terrain; terrain.LoadFromFile("./res/height_maps/TestMap.png"); terrain.SetTexture("./res/texture/grass_m.jpg"); terrain.SetShader(terrain_shader); terrain.AtachCamera(camera); terrain.SetHeight(25); terrain.Init(); Mesh Grass[100]; Mesh Tree[25]; for (int i = 0; i < 100; i++) { Grass[i].LoadMesh("./res/models/Grass pack/Grass_02.obj"); Grass[i].SetShader(grass_shader); Grass[i].AtachCamera(camera); int x = 0 + rand() % (int)terrain.GetSize().x ; int y = 0 + rand() % (int)terrain.GetSize().y ; Grass[i].SetPosition(x,terrain.GetHeightOnPoint(x,y)-0.4,y); } for (int i = 0; i < 25; i++) { Tree[i].LoadMesh("./res/models/Tree/Tree.obj"); Tree[i].SetShader(model_shader); Tree[i].AtachCamera(camera); int x = 0 + rand() % (int)terrain.GetSize().x; int y = 0 + rand() % (int)terrain.GetSize().y; Tree[i].SetPosition(x, terrain.GetHeightOnPoint(x, y) - 0.4, y); } sf::Clock clock; float speed; camera.setPosition(glm::vec3(terrain.GetSize().x / 2, 8, terrain.GetSize().y / 2-3)); while (window.GetHandle()->isOpen()) { speed = clock.getElapsedTime().asSeconds()*70; clock.restart(); sf::Event event; while (window.GetHandle()->pollEvent(event)) { //debug.Event(event, window.GetReference()); if (event.type == sf::Event::Closed) { window.GetHandle()->close(); return EXIT_SUCCESS; } if (event.type == sf::Event::Resized) { camera.forResize((float)window.GetReference().getSize().x / (float)window.GetReference().getSize().y); window.Resize(); } if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { window.GetHandle()->close(); return EXIT_SUCCESS; } } if (window.GetReference().hasFocus() && !sf::Keyboard::isKeyPressed(sf::Keyboard::LControl)) { camera.Update(window.GetReference(),speed); } else { ShowCursor(true); } // camera.setPosition(glm::vec3(camera.getPosition().x, terrain.GetHeightOnPoint(camera.getPosition().x, camera.getPosition().z)+3,camera.getPosition().z)); window.Clear(); skaybox.Draw(); terrain.Draw(); for (int i = 0; i < 100 ; i++) { Grass[i].Draw(); } for (int i = 0; i < 25; i++) { Tree[i].Draw(); } window.Update(); } return EXIT_SUCCESS; }
int main() { // Create the main window int width = 600; int height = 600; bool wireframe = true; sf::Texture sea; //texture object called car sf::Texture sand; //texture object called car sf::Texture grass; //texture object called car sf::Texture rock; //texture object called car sf::Texture snowrock; //texture object called car glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); sea.loadFromFile("../water.png"); sand.loadFromFile("../sand.png"); grass.loadFromFile("../grass.png"); rock.loadFromFile("../rock.png"); snowrock.loadFromFile("../snowyrock.png"); // create the window sf::RenderWindow App(sf::VideoMode(width, height), "OpenGL", sf::Style::Default, sf::ContextSettings(24)); // Create a clock for measuring time elapsed sf::Clock Clock; aiVector3D position(0,10,-30); Camera camera; camera.Init(position); //create a camera //prepare OpenGL surface for HSR glClearDepth(1.f); glClearColor(0.3f, 0.3f, 0.6f, 0.f); //background colour glEnable(GL_DEPTH_TEST); glDepthMask(GL_TRUE); //// Setup a perspective projection & Camera position glMatrixMode(GL_PROJECTION); glLoadIdentity(); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); GLfloat light_color[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat light_position[] = { 5.0, 2.6, 0.0, 0.0 }; glLightfv(GL_LIGHT0, GL_DIFFUSE, light_color); // set color of diffuse component glLightfv(GL_LIGHT0, GL_SPECULAR, light_color); // set color of specular component glLightfv(GL_LIGHT0, GL_POSITION, light_position); // set position ///////////////////////////setting material GLfloat materialAmbDiff[] = { 1.0f, 1.0f, 1.0f, 1.0f }; // create an array of RGBA values glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, materialAmbDiff); // set the diffuse & ambient reflection colour for the front of faces GLfloat materialSpecular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; // create an array of RGBA values (White) GLfloat materialShininess[] = { 128.0f }; // select value between 0-128, 128=shiniest glMaterialfv(GL_FRONT, GL_SPECULAR, materialSpecular); // set the colour of specular reflection glMaterialfv(GL_FRONT, GL_SHININESS, materialShininess); // set shininess of the material //set up a 3D Perspective View volume gluPerspective(90.f, (float)width/height, 1.f, 300.0f);//fov, aspect, zNear, zFar //load & bind the shader sf::Shader shader; //all the lighting & texture blending code should be put in 'fragment.glsl' if(!shader.loadFromFile("vertex.glsl","fragment.glsl")){ exit(1); } shader.setParameter("sea", sea); shader.setParameter("sand", sand); shader.setParameter("grass", grass); shader.setParameter("rock", rock); shader.setParameter("snowrock", snowrock); //Create our Terrain Terrain terrain; terrain.Init(); // Start game loop while (App.isOpen()) { // Process events sf::Event Event; while (App.pollEvent(Event)) { // Close window : exit if (Event.type == sf::Event::Closed) App.close(); // Escape key : exit if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::Escape)) App.close(); if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::O)) { // select the projection matrix and clear it out glMatrixMode(GL_PROJECTION); glLoadIdentity(); // set up an orthographic projection with the same near clip plane glOrtho(-16.0, 16.0, -10.0, 16.0, 1.f, 300.0f); // select modelview matrix and clear it out glMatrixMode(GL_MODELVIEW); } else if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::P)) { // select the projection matrix and clear it out glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-5.0, 5.0, -5.0, 5.0, 5, 100); // select modelview matrix and clear it out glMatrixMode(GL_MODELVIEW); } if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::I)) { wireframe = !wireframe; if (wireframe == true) { glPolygonMode(GL_FRONT, GL_FILL); glPolygonMode(GL_BACK, GL_FILL); } else { glPolygonMode(GL_FRONT, GL_LINE); glPolygonMode(GL_BACK, GL_LINE); } } //update the camera camera.Update(Event); } //Prepare for drawing // Clear color and depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); sf::Shader::bind(&shader); GLfloat light_position[] = { 5.0, 2, 0.5, 0.1 }; glLightfv(GL_LIGHT0, GL_POSITION, light_position); // set position // Apply some transformations //initialise the worldview matrix glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //get the viewing transform from the camera camera.ViewingTransform(); //make the world spin //TODO:probably should remove this in final //static float ang=0.0; //ang+=0.01f; //glRotatef(ang*2,0,1,0);//spin about y-axis //draw the world terrain.Draw(); // Finally, display rendered frame on screen App.display(); } return EXIT_SUCCESS; }
bool DuckHuntMain::Init() { if (!D3DApp::Init()) return false; // Must init Effects first since InputLayouts depend on shader signatures. Effects::InitAll(md3dDevice); InputLayouts::InitAll(md3dDevice); RenderStates::InitAll(md3dDevice); mCrosshair = new Crosshair(md3dDevice); mTexMgr.Init(md3dDevice); DuckHuntMain::ShowCursors(false); mSky = new Sky(md3dDevice, L"Textures/desertcube1024.dds", 5000.0f); mSmap = new ShadowMap(md3dDevice, SMapSize, SMapSize); Terrain::InitInfo tii; tii.HeightMapFilename = L"Textures/myT5.raw"; tii.LayerMapFilename0 = L"Textures/grass.dds"; tii.LayerMapFilename1 = L"Textures/darkdirt.dds"; tii.LayerMapFilename2 = L"Textures/stone.dds"; tii.LayerMapFilename3 = L"Textures/lightdirt.dds"; tii.LayerMapFilename4 = L"Textures/snow.dds"; tii.BlendMapFilename = L"Textures/blend.dds"; tii.HeightScale = 50.0f; tii.HeightmapWidth = 2049; tii.HeightmapHeight = 2049; tii.CellSpacing = 0.5f; mTerrain.Init(md3dDevice, md3dImmediateContext, tii); //Sound result = FMOD::System_Create(&mSystem); result = mSystem->init(32, FMOD_INIT_NORMAL, 0); result = mSystem->createSound("Sounds/GunFire.wav", FMOD_DEFAULT, 0, &mGunFire); result = mGunFire->setMode(FMOD_LOOP_OFF); mCam.SetLens(0.25f*MathHelper::Pi, AspectRatio(), 1.0f, 1000.0f); mSsao = new Ssao(md3dDevice, md3dImmediateContext, mClientWidth, mClientHeight, mCam.GetFovY(), mCam.GetFarZ()); BuildScreenQuadGeometryBuffers(); testModelDuck = new BasicModel(md3dDevice, mTexMgr, "Models\\duck.obj", L"Textures\\DUCK.jpg"); BasicModelInstance testInstanceDuck; BasicModelInstance testInstanceDuck2; BasicModelInstance testInstanceDuck3; BasicModelInstance testInstanceDuck4; testInstanceDuck.Model = testModelDuck; testInstanceDuck2.Model = testModelDuck; testInstanceDuck3.Model = testModelDuck; testInstanceDuck4.Model = testModelDuck; //Duck 1 XMMATRIX modelScale = XMMatrixScaling(1.0f, 1.0f, -1.0f); XMMATRIX modelRot = XMMatrixRotationY(0.0f); XMMATRIX modelOffset = XMMatrixTranslation(0.0f, 0.0f, 0.0f); //Duck 2 XMMATRIX modelScale2 = XMMatrixScaling(2.0f, 2.0f, -1.0f); XMMATRIX modelRot2 = XMMatrixRotationY(-1.0f); XMMATRIX modelOffset2 = XMMatrixTranslation(1.0f, 1.0f, -1.0f); //Duck3 XMMATRIX modelScale3 = XMMatrixScaling(1.5f, 1.5f, 1.5f); XMMATRIX modelRot3 = XMMatrixRotationY(0.5f); XMMATRIX modelOffset3 = XMMatrixTranslation(2.0f, 1.0f, -1.0f); //Duck4 XMMATRIX modelScale4 = XMMatrixScaling(0.5f, 0.5f, -1.0f); XMMATRIX modelRot4 = XMMatrixRotationY(1.0f); XMMATRIX modelOffset4 = XMMatrixTranslation(0.5f, 1.0f, -1.0f); //Duck 1 XMStoreFloat4x4(&testInstanceDuck.World, modelScale*modelRot*modelOffset); mModelInstances.push_back(testInstanceDuck); //Duck 2 XMStoreFloat4x4(&testInstanceDuck2.World, modelScale2*modelRot2*modelOffset2); mModelInstances.push_back(testInstanceDuck2); //Duck 3 XMStoreFloat4x4(&testInstanceDuck3.World, modelScale3*modelRot3*modelOffset3); mModelInstances.push_back(testInstanceDuck3); //Duck 4 XMStoreFloat4x4(&testInstanceDuck4.World, modelScale4*modelRot4*modelOffset4); mModelInstances.push_back(testInstanceDuck4); //create collision box for (unsigned i = 0; i < mModelInstances.size(); i++) { mModelInstances[i].Model->CreateCollisionBox(mModelInstances[i].Model->BasicVertices); } // // Compute scene bounding box. // XMFLOAT3 minPt(+MathHelper::Infinity, +MathHelper::Infinity, +MathHelper::Infinity); XMFLOAT3 maxPt(-MathHelper::Infinity, -MathHelper::Infinity, -MathHelper::Infinity); for (UINT i = 0; i < mModelInstances.size(); ++i) { for (UINT j = 0; j < mModelInstances[i].Model->BasicVertices.size(); ++j) { XMFLOAT3 P = mModelInstances[i].Model->BasicVertices[j].Pos; minPt.x = MathHelper::Min(minPt.x, P.x); minPt.y = MathHelper::Min(minPt.x, P.x); minPt.z = MathHelper::Min(minPt.x, P.x); maxPt.x = MathHelper::Max(maxPt.x, P.x); maxPt.y = MathHelper::Max(maxPt.x, P.x); maxPt.z = MathHelper::Max(maxPt.x, P.x); } } // // Derive scene bounding sphere from bounding box. // mSceneBounds.Center = XMFLOAT3( 0.5f*(minPt.x + maxPt.x), 0.5f*(minPt.y + maxPt.y), 0.5f*(minPt.z + maxPt.z)); XMFLOAT3 extent( 0.5f*(maxPt.x - minPt.x), 0.5f*(maxPt.y - minPt.y), 0.5f*(maxPt.z - minPt.z)); mSceneBounds.Radius = sqrtf(extent.x*extent.x + extent.y*extent.y + extent.z*extent.z); return true; }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { XEngine *engine = XEngine::GetInstance(); if( engine->Init(hInstance) == FALSE ) { return -1; } Rect rec(Position(-10000.0, 10000.0), Position(10000.0, -10000.0)); TestScene *scene = new TestScene; scene->SetTime(0); //Scene *scene = new Scene; scene->Init(rec); scene->camera->MoveForwardBy(-40); scene->camera->MoveUpBy(18); scene->camera->Pitch(-XM_PI / 10); Material material; material.ambient = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); material.diffuse = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); material.specular = XMFLOAT4(1.0f, 1.0f, 1.0f, 1.0f); material.power = 9.0f; //material.texture = SRVBatch::GetInstance()->LoadSRV("media/", "cup.jpg"); Particle *fire = new Particle; fire->Init("media/","fire.png"); fire->SetPosition(0, 0, 10); //scene->AddRenderableThing(*fire); SkyBox *sky = new SkyBox; //sky->Init("textures/", "skymap.dds"); sky->Init("textures/", "Above_The_Sea.dds"); //sky->Init("textures/", "desert_skymap.dds"); scene->AddSky(sky); Terrain *terrain = new Terrain; //terrain->Init("terrain/testHight.bmp", "terrain/", "dirt01.dds"); terrain->Init("terrain/heightmap01.bmp", "terrain/", "grass.jpg"); scene->AddTerrain(terrain); Wall *wall = new Wall; wall->Init(30, 30, 0.3); wall->Pitch(XM_PI / 2); wall->SetPosition(0, -3, 0); //scene->AddRenderableThing(*wall); //¿É¼ûÐÔ²âÊÔ Wall *wall0 = new Wall; wall0->Init(30, 30, 0.3); wall0->Pitch(XM_PI / 2); wall0->SetPosition(1000, -3, 0); //scene->AddRenderableThing(*wall0); Obj *obj = new Obj; obj->CreateBox(1, 3, 2, material); obj->SetPosition(5, 1, 12); obj->Yaw(XM_PI / 3); obj->Roll(XM_PI / 3); obj->SetScale(1.3); //scene->AddRenderableThing(*obj); Cube *cube = new Cube; cube->Init(2); cube->Pitch(XM_PI / 3); cube->Roll(XM_PI / 3); cube->SetMaterial(material); //scene->AddRenderableThing(*cube); Ball *ball = new Ball; ball->Init(1); ball->SetPosition(0, 0, 6); Obj *objInWall = new Obj; objInWall->CreateBox(1, 1, 1, material); objInWall->SetPosition(0, 1, 0); ball->AddChild(objInWall); //scene->AddRenderableThing(*ball); Ship *ship = new Ship; //ship->Init(); //ship->SetPosition(-10, 0, 0); //scene->AddRenderableThing(*ship); Model *test = new Model; test->LoadModel("media/", "chair.obj"); test->SetScale(1.7); test->SetPosition(-15, 0, 0); test->Pitch(-1.2); test->Yaw(-1.5); //scene->AddRenderableThing(*test); //SmileBoy *smileBoy= new SmileBoy; //smileBoy->Init(); //smileBoy->SetHandleInput(FALSE); //scene->AddRenderableThing(*smileBoy); Female *female = new Female; female->Init(); //female->SetPosition(-7, 0,0); female->SetHandleInput(TRUE); scene->AddRenderableThing(*female); RenderSystem::GetInstance()->SetShadowOpen(true); engine->LoadScene(scene); { Light *light = new Light; light->type = 0; light->position = XMFLOAT4(0.0f, 5.0f, -5.0f, 1.0f); XMStoreFloat4(&light->direction, XMVector4Normalize(XMVectorSet(1.0f, -1.0f, 0.0f, 0.0f))); light->ambient = XMFLOAT4(0.4f, 0.2f, 0.3f, 1.0f); light->diffuse = XMFLOAT4(0.3f, 0.4f, 0.5f, 1.0f); light->specular = XMFLOAT4(0.5f, 0.3f, 0.2f, 1.0f); light->attenuation0 = 0.0f; light->attenuation1 = 0.0f; light->attenuation2 = 0.0f; light->alpha = 0.0f; light->beta = 0.0f; light->falloff = 0.0f; scene->AddLight(light); } //******************************************* engine->Run(); return 0; }
void Map::LoadMap(char* fileName, ResourceManager& resMan) { ////////////////////////////////////////////////////////////////FILE I/O STUFF std::stringstream ss; std::ifstream file; char lineType; Terrain tempTerrain; D3DVECTOR pos, corner1, corner2, size; float rot; int zone; PrimStruct *tempPrim; PrimObj tempObj; sPoint tempSpawn; tempObj.mat = &mat; tempObj.Tex = resMan.loadTexture("uvtest.png",0,0,0,0,D3DFMT_UNKNOWN,D3DPOOL_MANAGED,D3DX_DEFAULT,D3DX_DEFAULT,D3DCOLOR_XRGB(255,0,255),0); file.open(fileName); if(file.is_open()) { while(!file.eof()) { lineType = ' '; file>>lineType; if(lineType == '#') { //comment line file.ignore(256,'\n'); } else if(lineType == 'f'||lineType == 'F') { //floor //get top left file>>size.x; file>>size.z; file>>size.y; //get bottom right file>>pos.x; file>>pos.y; file>>pos.z; //zone file>>zone; ss<<"Floor"<<size.x<<"x"<<size.y<<"x"<<size.z; tempPrim = resMan.loadPrim(ss.str().c_str(),size.y,size.x,size.z); D3DXMatrixIdentity(&tempObj.matrix); D3DXMatrixTranslation(&tempObj.matrix,pos.x,pos.y,pos.z); tempObj.primInfo = tempPrim; renderInfo.push_back(tempObj); tempTerrain.Init(pos,tempPrim,FLOOR); AddFloor(tempTerrain); floorZone.push_back(zone); file.ignore(); } else if(lineType == 'w' || lineType == 'W') { //walls //get top left file>>size.x; file>>size.z; file>>size.y; //get bottom right file>>pos.x; file>>pos.y; file>>pos.z; //zone file>>zone; ss<<"Wall"<<size.x<<"x"<<size.y<<"x"<<size.z; tempPrim = resMan.loadPrim(ss.str().c_str(),size.y,size.x,size.z); D3DXMatrixIdentity(&tempObj.matrix); D3DXMatrixTranslation(&tempObj.matrix,pos.x,pos.y,pos.z); tempObj.primInfo = tempPrim; renderInfo.push_back(tempObj); tempTerrain.Init(pos,tempPrim,WALL); AddWall(tempTerrain); wallZone.push_back(zone); file.ignore(); } else if(lineType == 's' || lineType == 'S'){
int main() { // Create the main window bool wireFrame = false; int width = 600, height = 600; sf::ContextSettings Settings; Settings.depthBits = 24; // Request a 24 bits depth buffer Settings.stencilBits = 8; // Request a 8 bits stencil buffer Settings.antialiasingLevel = 16; // Request 2 levels of antialiasing sf::RenderWindow App(sf::VideoMode(width, height, 32), "SFML OpenGL" , sf::Style::Close, Settings); // Create a clock for measuring time elapsed sf::Clock Clock; aiVector3D position(0, 10, -30); Camera camera; camera.Init(position); //create a camera //prepare OpenGL surface for HSR glClearDepth(1.f); glClearColor(0.3f, 0.3f, 0.6f, 0.f); //background colour glEnable(GL_DEPTH_TEST); glDepthMask(GL_TRUE); //to enable texture tiling glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //// Setup a perspective projection & Camera position glMatrixMode(GL_PROJECTION); glEnable(GL_LIGHTING); // switch on lighting glEnable(GL_LIGHT0); // switch on light0 GLfloat light_color[] = { 1.0f, 1.0f, 1.0f, 1.0f }; GLfloat light_position[] = { 15.0f, -20.0f, -10.0f, 0 }; glLightfv(GL_LIGHT0, GL_DIFFUSE, light_color); // set color of diffuse component glLightfv(GL_LIGHT0, GL_SPECULAR, light_color); // set color of specular component glLightfv(GL_LIGHT0, GL_POSITION, light_position); // set position GLfloat materialAmbDiff[] = { 1, 1, 1, 1.0f }; // create an array of RGBA values glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, materialAmbDiff); // set the diffuse & ambient reflection colour for the front of faces GLfloat materialSpecular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; // create an array of RGBA values (White) GLfloat materialShininess[] = { 120 }; // select value between 0-128, 128=shiniest glMaterialfv(GL_FRONT, GL_SPECULAR, materialSpecular); // set the colour of specular reflection glMaterialfv(GL_FRONT, GL_SHININESS, materialShininess); // set shininess of the material glLoadIdentity(); //set up a 3D Perspective View volume gluPerspective(90.f, (float)width / height, 1.f, 300.0f);//fov, aspect, zNear, zFar //load in different textures sf::Texture seaTexture; seaTexture.loadFromFile("sea.png"); sf::Texture grassTexture; grassTexture.loadFromFile("grass.png"); sf::Texture rockTexture; rockTexture.loadFromFile("rock.png"); //load the shader sf::Shader shader; //all the lighting & texture blending code should be put in 'fragment.glsl' if (!shader.loadFromFile("vertex.glsl", "fragment.glsl")){ exit(1); } //set textures in the shader shader.setParameter("seaTexture", seaTexture); shader.setParameter("grassTexture", grassTexture); shader.setParameter("rockTexture", rockTexture); //load the height map image sf::Image heightMap; heightMap.loadFromFile("heightmap.png"); //Create our Terrain Terrain terrain; terrain.Init(heightMap); //create from height map shader.setParameter("heightScale", terrain.getHeightScale()); //set the height scaler in the shader // Start game loop while (App.isOpen()) { // Process events sf::Event Event; while (App.pollEvent(Event)) { // Close window : exit if (Event.type == sf::Event::Closed) App.close(); // Escape key : exit if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::Escape)) App.close(); //update the camera if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::I)) wireFrame = !wireFrame; //update the camera camera.Update(Event); } //Prepare for drawing // Clear color and depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); sf::Shader::bind(&shader); //shader has to be binded here to allow changing textures //glEnable(GL_TEXTURE_2D); if (wireFrame) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glLightfv(GL_LIGHT0, GL_POSITION, light_position); // set position // Apply some transformations //initialise the worldview matrix glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE); //get the viewing transform from the camera camera.ViewingTransform(); //make the world spin //TODO:probably should remove this in final //static float ang=0.0; //ang+=0.01f; //glRotatef(ang*2,0,1,0);//spin about y-axis //draw the world terrain.Draw(); // Finally, display rendered frame on screen App.display(); } return EXIT_SUCCESS; }
bool Projekt::Init() { if (!D3D11App::Init()) return false; // Initialize effects, input layouts and texture manager Effects::InitAll(mDirect3D->GetDevice()); InputLayouts::InitAll(mDirect3D->GetDevice()); mTextureMgr.init(mDirect3D->GetDevice()); // Initialize wireframe render state D3D11_RASTERIZER_DESC wireFrameDesc; ZeroMemory(&wireFrameDesc, sizeof(D3D11_RASTERIZER_DESC)); wireFrameDesc.FillMode = D3D11_FILL_WIREFRAME; wireFrameDesc.CullMode = D3D11_CULL_BACK; wireFrameDesc.FrontCounterClockwise = false; wireFrameDesc.DepthClipEnable = true; HR(mDirect3D->GetDevice()->CreateRasterizerState(&wireFrameDesc, &WireFrameRS)); //-------------------------------------------------------- // Create sky //-------------------------------------------------------- mSky = new Sky(mDirect3D->GetDevice(), L"Data/Textures/snowcube1024.dds", 5000.0f); //-------------------------------------------------------- // Create terrain //-------------------------------------------------------- // Describe terrain Terrain::InitInfo tInitInfo; tInitInfo.HeightMapFilename = L"Data/Textures/Heightmaps/terrain.raw"; tInitInfo.LayerMapFilename0 = L"Data/Textures/grass.dds"; tInitInfo.LayerMapFilename1 = L"Data/Textures/darkdirt.dds"; tInitInfo.LayerMapFilename2 = L"Data/Textures/stone.dds"; tInitInfo.LayerMapFilename3 = L"Data/Textures/lightdirt.dds"; tInitInfo.LayerMapFilename4 = L"Data/Textures/snow.dds"; tInitInfo.BlendMapFilename = L"Data/Textures/blend.dds"; tInitInfo.HeightScale = 50.0f; tInitInfo.HeightmapWidth = 2049; tInitInfo.HeightmapHeight = 2049; tInitInfo.CellSpacing = 0.5f; // Initialize terrain mTerrain.Init(mDirect3D->GetDevice(), mDirect3D->GetImmediateContext(), tInitInfo); //-------------------------------------------------------- // Particle systems //-------------------------------------------------------- mRandomTexSRV = d3dHelper::CreateRandomTexture1DSRV(mDirect3D->GetDevice()); std::vector<std::wstring> flares; flares.push_back(L"Data\\Textures\\flare0.dds"); mFlareTexSRV = d3dHelper::CreateTexture2DArraySRV(mDirect3D->GetDevice(), mDirect3D->GetImmediateContext(), flares); mFire.init(mDirect3D->GetDevice(), Effects::FireFX, mFlareTexSRV, mRandomTexSRV, 500); mFire.setEmitPos(XMFLOAT3(65.0f, 5.0f, 0.0f)); //-------------------------------------------------------- // Create shadow map //-------------------------------------------------------- mShadowMapSize = 2048; mShadowMap = new ShadowMap(mDirect3D->GetDevice(), mShadowMapSize, mShadowMapSize); //-------------------------------------------------------- // Load models //-------------------------------------------------------- mGenericModel = new GenericModel(mDirect3D->GetDevice(), mTextureMgr, "Data\\Models\\Collada\\duck.dae", L"Data\\Models\\Collada\\"); //mSkinnedModel = new GenericSkinnedModel(mDirect3D->GetDevice(), mTextureMgr, "Data\\Models\\Collada\\AnimTest\\test_Collada_DAE.dae", L"Data\\Models\\Collada\\AnimTest\\"); mPlayerModel = new GenericModel(mDirect3D->GetDevice(), mTextureMgr, "Data\\Models\\OBJ\\Cop\\cop.obj", L"Data\\Models\\OBJ\\Cop\\"); Player::InitProperties playerProp; playerProp.PlayerID = 0; playerProp.Nickname = "Hyzor"; playerProp.Speed = 1.0f; playerProp.Health = 1.0f; playerProp.Position = XMFLOAT3(0.0f, 0.0f, 0.0f); playerProp.Scale = XMFLOAT3(1.0f, 1.0f, 1.0f); playerProp.Angle = 0.0f; playerProp.Model = mPlayerModel; mPlayer.Init(playerProp); //-------------------------------------------------------- // Create model instances //-------------------------------------------------------- GenericModelInstance genericInstance; genericInstance.model = mGenericModel; // GenericSkinnedModelInstance genSkinnedInstance; // genSkinnedInstance.model = mSkinnedModel; // genSkinnedInstance.FinalTransforms.resize(mSkinnedModel->skinnedData.getBoneCount()); // genSkinnedInstance.ClipName = "animation"; // genSkinnedInstance.TimePos = 0.0f; //-------------------------------------------------------- // Scale, rotate and move model instances //-------------------------------------------------------- XMMATRIX modelScale = XMMatrixScaling(1.0f, 1.0f, 1.0f); XMMATRIX modelRot = XMMatrixRotationY(0.0f); XMMATRIX modelOffset = XMMatrixTranslation(0.0f, 0.0f, 0.0f); //modelScale = XMMatrixScaling(0.1f, 0.1f, 0.1f); modelOffset = XMMatrixTranslation(-30.0f, 15.0f, -110.0f); XMStoreFloat4x4(&genericInstance.world, modelScale*modelRot*modelOffset); // modelOffset = XMMatrixTranslation(50.0f, 15.0f, -110.0f); // XMStoreFloat4x4(&genSkinnedInstance.world, modelScale*modelRot*modelOffset); //-------------------------------------------------------- // Insert model instances to the vector //-------------------------------------------------------- mGenericInstances.push_back(genericInstance); /* mGenSkinnedInstances.push_back(genSkinnedInstance);*/ //-------------------------------------------------------- // Compute scene bounding box //-------------------------------------------------------- XMFLOAT3 minPt(+MathHelper::infinity, +MathHelper::infinity, +MathHelper::infinity); XMFLOAT3 maxPt(-MathHelper::infinity, -MathHelper::infinity, -MathHelper::infinity); // Get vertex positions from all models for (UINT i = 0; i < mGenericInstances.size(); ++i) { for (UINT j = 0; j < mGenericInstances[i].model->vertices.size(); ++j) { XMFLOAT3 vPos = mGenericInstances[i].model->vertices[j]->position; minPt.x = MathHelper::getMin(minPt.x, vPos.x); minPt.y = MathHelper::getMin(minPt.x, vPos.x); minPt.z = MathHelper::getMin(minPt.x, vPos.x); maxPt.x = MathHelper::getMax(maxPt.x, vPos.x); maxPt.y = MathHelper::getMax(maxPt.x, vPos.x); maxPt.z = MathHelper::getMax(maxPt.x, vPos.x); } } // Get vertex positions from all skinned models // for (UINT i = 0; i < mGenSkinnedInstances.size(); ++i) // { // for (UINT j = 0; j < mGenSkinnedInstances[i].model->vertices.size(); ++j) // { // XMFLOAT3 vPos = mGenSkinnedInstances[i].model->vertices[j]->position; // // minPt.x = MathHelper::getMin(minPt.x, vPos.x); // minPt.y = MathHelper::getMin(minPt.x, vPos.x); // minPt.z = MathHelper::getMin(minPt.x, vPos.x); // // maxPt.x = MathHelper::getMax(maxPt.x, vPos.x); // maxPt.y = MathHelper::getMax(maxPt.x, vPos.x); // maxPt.z = MathHelper::getMax(maxPt.x, vPos.x); // } // } // Now continue with terrain vertex positions for (UINT i = 0; i < mTerrain.getPatchVertices().size(); ++i) { XMFLOAT3 vPos = mTerrain.getPatchVertices()[i].position; minPt.x = MathHelper::getMin(minPt.x, vPos.x); minPt.y = MathHelper::getMin(minPt.x, vPos.x); minPt.z = MathHelper::getMin(minPt.x, vPos.x); maxPt.x = MathHelper::getMax(maxPt.x, vPos.x); maxPt.y = MathHelper::getMax(maxPt.x, vPos.x); maxPt.z = MathHelper::getMax(maxPt.x, vPos.x); } // Sphere center is at half of these new dimensions mSceneBounds.Center = XMFLOAT3( 0.5f*(minPt.x + maxPt.x), 0.5f*(minPt.y + maxPt.y), 0.5f*(minPt.z + maxPt.z)); // Calculate the sphere radius XMFLOAT3 extent(0.5f*(maxPt.x - minPt.x), 0.5f*(maxPt.y - minPt.y), 0.5f*(maxPt.z - minPt.z)); mSceneBounds.Radius = sqrtf(extent.x*extent.x + extent.y*extent.y + extent.z*extent.z); OnResize(); return true; }