void RoundConfig::readSpecJson(const JsonLoader &loader, const Json::Value &obj) { SpecType type; loader.readVal(obj["_type"], &type); switch (type) { case SPEC_BRICK: loader.readVal(obj, &addBrick()); break; case SPEC_BRICK_RING: loader.readVal(obj, &addBrickRing()); break; case SPEC_BRICK_QUADS: loader.readVal(obj, &addBrickQuads()); break; case SPEC_WALL: loader.readVal(obj, &addWall()); break; case SPEC_WALL_RING: loader.readVal(obj, &addWallRing()); break; case SPEC_CURVED_BRICK_COLUMN: loader.readVal(obj, &addCurvedBrickColumn()); break; case SPEC_CURVED_WALL: loader.readVal(obj, &addCurvedWallSet()); break; case SPEC_BALL: { BallSpec spec; loader.readVal(obj, &spec); addBall(spec); break; } case SPEC_MODIFIER: { ModifierSpec spec; loader.readVal(obj, &spec); addModifierDef(spec.name, spec); break; } case SPEC_MESSAGE: { MessageSpec spec; loader.readVal(obj, &spec); addStartMessage(spec); break; } case SPEC_RING_SET: loader.readVal(obj, &addRingSet()); break; case SPEC_UNKNOWN: default: //... break; } }
void ObjectCreater::threadedFunction() { addRectangleBar(0); int id = 1; while(isThreadRunning()) { addBall(id); ++id; Sleep(500); //delete process deleteObjectByLocation(); } }
void Physics::registBodies(){ addFloor(); addBall(2.5,btVector3(SIZE_WIDTH/1.5,0,SIZE_DEPTH/1.5),0.08); btVector3 posTeam1[] = {btVector3(15,4,SIZE_DEPTH- 55),btVector3(35,4,30),btVector3(55,4,45)}; btVector3 posTeam2[] = {btVector3(SIZE_WIDTH-15,4,55),btVector3(SIZE_WIDTH-25,4,SIZE_DEPTH - SIZE_DEPTH/2.5 + 20),btVector3(SIZE_WIDTH-55,4,85)}; //Create robots here //Team 1 for(int i = 0;i < numRobotsTeam;i++){ if(numTeams >= 1){ addRobot(Color(0.3,0.3,0.3),posTeam1[i],btVector3(0,90,0),8,0.25,clrPlayers[i],clrTeams[0]); } } for(int i = 0;i < numRobotsTeam;i++){ if(numTeams == 2){ addRobot(Color(0.3,0.3,0.3),posTeam2[i],btVector3(0,-100,0),8,0.25,clrPlayers[i],clrTeams[1]); } } addWall(Color(0,0,0),btVector3(SIZE_WIDTH/2+1,7.5,-2.5),SIZE_WIDTH,15,5,0); addWall(Color(0,0,0),btVector3(SIZE_WIDTH/2+1,7.5,SIZE_DEPTH+2.5),SIZE_WIDTH,15,5,0); addWall(Color(0,0,0),btVector3(-1.6,7.5,SIZE_DEPTH/6),5,15,SIZE_DEPTH/3,0); addWall(Color(0,0,0),btVector3(-1.6,7.5,SIZE_DEPTH-SIZE_DEPTH/6),5,15,SIZE_DEPTH/3,0); addWall(Color(0,0,0),btVector3(-12.5,7.5, SIZE_DEPTH/2),5,15,SIZE_DEPTH/3,0); addWall(Color(0,0,0),btVector3(-7,7.5,SIZE_DEPTH/3),15,15,5,0); addWall(Color(0,0,0),btVector3(-7,7.5,2*SIZE_DEPTH/3+2.5),15,15,5,0); addWall(Color(0,0,0),btVector3(SIZE_WIDTH+3.5,7.5,SIZE_DEPTH/6),5,15,SIZE_DEPTH/3,0); addWall(Color(0,0,0),btVector3(SIZE_WIDTH+3.5,7.5,SIZE_DEPTH-SIZE_DEPTH/6),5,15,SIZE_DEPTH/3,0); addWall(Color(0,0,0),btVector3(SIZE_WIDTH+13.5,7.5, SIZE_DEPTH/2),5,15,SIZE_DEPTH/3,0); addWall(Color(0,0,0),btVector3(SIZE_WIDTH+8.5,7.5,SIZE_DEPTH/3),15,15,5,0); addWall(Color(0,0,0),btVector3(SIZE_WIDTH+8.5,7.5,2*SIZE_DEPTH/3+2.5),15,15,5,0); addCorner(Color(0,0,0),btVector3(10,7.5,10),30,15,btVector3(0,45,0)); addCorner(Color(0,0,0),btVector3(SIZE_WIDTH-9,7.5,10),30,15,btVector3(0,-45,0)); addCorner(Color(0,0,0),btVector3(SIZE_WIDTH-9,7.5,SIZE_DEPTH-10),30,15,btVector3(0,45,0)); addCorner(Color(0,0,0),btVector3(10,7.5,SIZE_DEPTH-10),30,15,btVector3(0,-45,0)); }
void ItemFactory::produce(float dt) { //produce food when necessary addFood(); //add the transfer doors //load from json //addDoor(); //add the score apple addApple(dt); //add the speed star addStar(dt); //add the slow down ball addBall(dt); //check and remove expired item removeExpiredItem(dt); }
bool TwoPlayer::init() { setBackground( load_image("images/background.png" )); apply_surface( 0, 0, getBackground(), getScreen() ); setScore( 0 , 0) ; setCollisionSound( "sounds/pong.wav"); setScoreMessage(new Text((SCREEN_WIDTH / 2) - 100, 0, "Score : 0 - 0","fonts/Allcaps.ttf", 28, 255, 255, 255)); // Left - right addBall( new Ball(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 200.0, 10, "images/ball.png")); addPaddle( new HumanPaddle(5, SCREEN_HEIGHT / 2, 0, 0, 10, 10,"images/paddle.png", SDLK_w, SDLK_s)); addPaddle( new HumanPaddle(615, SCREEN_HEIGHT / 2, 0, 0, 10, 10,"images/paddle.png", SDLK_UP, SDLK_DOWN)); setState(PLAYING) ; return true ; }
void Logstalgia::logic(float t, float dt) { float sdt = dt * settings.simulation_speed; //increment clock elapsed_time += sdt; currtime = starttime + (long)(elapsed_time); if(settings.stop_time && currtime > settings.stop_time) { currtime = settings.stop_time; } if(mousehide_timeout>0.0f) { mousehide_timeout -= dt; if(mousehide_timeout<0.0f) { SDL_ShowCursor(false); } } infowindow.hide(); if(end_reached && balls.empty()) { appFinished = true; return; } //if paused, dont move anything, only check what is under mouse if(paused) { for(auto& it: paddles) { Paddle* paddle = it.second; if(paddle->mouseOver(infowindow, mousepos)) { break; } } for(RequestBall* ball : balls) { if(ball->mouseOver(infowindow, mousepos)) { break; } } if(!ipSummarizer->mouseOver(infowindow,mousepos)) { for(Summarizer* s: summarizers) { if(s->mouseOver(infowindow, mousepos)) break; } } return; } //next will fast forward clock to the time of the next entry, //if the next entry is in the future if(next || (!settings.disable_auto_skip && balls.empty())) { if(!queued_entries.empty()) { LogEntry* le = queued_entries.front(); long entrytime = le->timestamp; if(entrytime > currtime) { elapsed_time = entrytime - starttime; currtime = starttime + (long)(elapsed_time); } } next = false; } //recalc spawn speed each second by if(currtime != lasttime) { //dont bother reading the log if we dont need to if(queued_entries.empty() || queued_entries.back()->timestamp <= currtime) { readLog(); } profile_start("determine new entries"); int items_to_spawn=0; for(LogEntry* le : queued_entries) { if(le->timestamp > currtime) break; items_to_spawn++; addStrings(le); } profile_stop(); //debugLog("items to spawn %d\n", items_to_spawn); if(items_to_spawn > 0) { profile_start("add new strings"); //re-summarize ipSummarizer->summarize(); for(Summarizer* s : summarizers) { s->summarize(); } profile_stop(); profile_start("add new entries"); float item_offset = 1.0 / (float) (items_to_spawn); int item_no = 0; while(!queued_entries.empty()) { LogEntry* le = queued_entries.front(); if(le->timestamp > currtime) break; float pos_offset = item_offset * (float) item_no++; float start_offset = std::min(1.0f, pos_offset); addBall(le, start_offset); queued_entries.pop_front(); } } //update date if(total_entries>0) { char datestr[256]; char timestr[256]; struct tm* timeinfo = localtime ( &currtime ); strftime(datestr, 256, "%A, %B %d, %Y", timeinfo); strftime(timestr, 256, "%X", timeinfo); displaydate = datestr; displaytime = timestr; } else { displaydate = ""; displaytime = ""; } lasttime=currtime; profile_stop(); } else { //do small reads per frame if we havent buffered the next second if(queued_entries.empty() || queued_entries.back()->timestamp <= currtime+1) { readLog(50); } } std::list<Paddle*> inactivePaddles; //update paddles for(auto& it: paddles) { std::string paddle_token = it.first; Paddle* paddle = it.second; if(settings.paddle_mode > PADDLE_SINGLE && !paddle->moving() && !paddle->visible()) { bool token_match = false; //are there any requests that will match this paddle? for(RequestBall* ball : balls) { if( (settings.paddle_mode == PADDLE_VHOST && ball->le->vhost == paddle_token) || (settings.paddle_mode == PADDLE_PID && ball->le->pid == paddle_token)) { token_match = true; break; } } //mark this paddle for deletion, continue if(!token_match) { inactivePaddles.push_back(paddle); continue; } } // find nearest ball to this paddle if( (retarget || !paddle->getTarget())) { RequestBall* ball = findNearest(paddle, paddle_token); if(ball != 0) { paddle->setTarget(ball); } else if(!paddle->moving()) { paddle->setTarget(0); } } paddle->logic(sdt); } retarget = false; profile_start("check ball status"); // NOTE: special handling for this iterator as items are being removed for(auto it = balls.begin(); it != balls.end();) { RequestBall* ball = *it; highscore += ball->logic(sdt); if(ball->isFinished()) { it = balls.erase(it); removeBall(ball); } else { it++; } } profile_stop(); profile_start("ipSummarizer logic"); ipSummarizer->logic(dt); profile_stop(); profile_start("updateGroups logic"); updateGroups(dt); profile_stop(); screen_blank_elapsed += dt; if(screen_blank_elapsed-screen_blank_interval > screen_blank_period) screen_blank_elapsed = 0.0f; //update font alpha font_alpha = 1.0f; if(screen_blank_elapsed>screen_blank_interval) { font_alpha = std::min(1.0f, (float) fabs(1.0f - (screen_blank_elapsed-screen_blank_interval)/(screen_blank_period*0.5))); font_alpha *= font_alpha; } }
void GameScene_02::update(float delta) { int num = 0; int addBalls = 0; auto deleteBalls = new Vector<Ball*>(); auto deleteBricks = new Vector<Brick*>(); Timers->setString(StringUtils::format("%d",(slider->getoffset()))); if (bricks->empty()) { Director::getInstance()->replaceScene(Win::createScene(2)); unscheduleUpdate(); } else if (balls->empty() || slider->getPositionY() > visibleSize.height) { Director::getInstance()->replaceScene(Fail::createScene(2)); unscheduleUpdate(); } //±éÀúÿһ¸öÇò for( auto iter1 = balls->begin(); iter1 != balls->end(); iter1++) { for (int i=0; i<(*iter1)->get_speed(); i++) { if( !(*iter1)->collideWithEdges() ) { deleteBalls->pushBack(*iter1); break; } (*iter1)->collideWithSlider(slider); for (auto iter = bricks->begin(); iter != bricks->end(); iter++) { if((*iter1)->isCollideWithBrick(*iter)) { (*iter1)->collideWithBricks(*iter); if ( ADD_BALL == (*iter)->getBf() ) { addBalls++; } if ( ADD_LENGTH == (*iter)->getBf()) { slider->setTexture("Game/level_1/paddle_long.png"); } if ( SUB_LENGTH == (*iter)->getBf()) { slider->setTexture("Game/level_1/paddle_short.png"); } deleteBricks->pushBack(*iter); break; } } (*iter1)->BallMove(); //move every ball } } for(int i = 0; i<addBalls; i++) { addBall(); } //ɾ³ý»º´æµÄСÇò if( !deleteBalls->empty()) { for (auto it = deleteBalls->begin(); it != deleteBalls->end(); it++) { this->removeChild(*it); balls->eraseObject(*it); } } //ɾ³ý»º´æµÄש¿é if( !deleteBricks->empty()) { for (auto it = deleteBricks->begin(); it != deleteBricks->end(); it++) { this->removeChild(*it); bricks->eraseObject(*it); } } }
bool StageSelect::init() { if (Layer::init()) { selected_cell = nullptr; s_playerConfig.overstage = 50; if(s_playerConfig.overstage >=50 ) { s_playerConfig.overstage = 50; fake_overstage = 49; } else fake_overstage = s_playerConfig.overstage; _selectItem = fake_overstage; _noTouch = true ; isCloseClick = true; auto listener = EventListenerTouchOneByOne::create(); listener->setSwallowTouches(true); listener->onTouchBegan = [](Touch* touch, Event* event){ return true; }; _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto panelSize = Size(520,s_visibleRect.visibleHeight * 0.88f);//s_visibleRect.visibleWidth - 97 _cellSize.width = panelSize.width; _cellSize.height = 100; _panel = Scale9Sprite::create("mission.png"); _panel->setAnchorPoint(Point::ANCHOR_MIDDLE_TOP); _panel->setPosition(Point(s_visibleRect.center.x,s_visibleRect.top.y + panelSize.height)); _panel->setContentSize(panelSize); this->addChild(_panel); auto stageselect_text = TextSprite::create(s_gameStrings.mainMenu->stageselect.c_str()); stageselect_text->setAnchorPoint(Point::ANCHOR_MIDDLE); stageselect_text->setPosition(Point(panelSize.width/2, panelSize.height-170)); _panel->addChild(stageselect_text); //door effect. left_door = Sprite::create("door_l.png"); right_door = Sprite::create("door_r.png"); left_door->setScale(1.0f, 0.9f); right_door->setScale(1.0f, 0.9f); left_door->setAnchorPoint(Point::ANCHOR_MIDDLE_RIGHT); right_door->setAnchorPoint(Point::ANCHOR_MIDDLE_LEFT); left_door->setPosition(Point(panelSize.width/2+25,panelSize.height/2-39)); right_door->setPosition(Point(panelSize.width/2-25,panelSize.height/2-39)); _panel->addChild(left_door,2); _panel->addChild(right_door,2); auto actionmovedone = CallFunc::create( [=](){ PLAY_OPENDOOR_EFFECT; auto size = left_door->getContentSize(); auto move = MoveBy::create(0.5, Point(-(size.width), 0)); left_door->runAction(move); left_door->runAction(ScaleTo::create(0.5, 0, 0.9f)); auto move1 = move->reverse(); right_door->runAction(move1); right_door->runAction(Sequence::create(ScaleTo::create(0.5, 0, 0.9f), CallFunc::create([=]() { PLAY_STAGELIST_EFFECT; isCloseClick = false; if(fake_overstage>2 && fake_overstage<47) tableView->setContentOffsetInDuration(tableView->getContentOffset() + Point(0,_cellSize.height*(fake_overstage-2)),fake_overstage*0.02f); else if(fake_overstage>=47) tableView->setContentOffsetInDuration(tableView->getContentOffset() + Point(0,_cellSize.height*45),46*0.02f); }), nullptr)); }); _panel->runAction(Sequence::create(MoveTo::create(0.15f,s_visibleRect.top), DelayTime::create(0.3f), actionmovedone,nullptr)); tableView = TableView::create(this, Size(panelSize.width, panelSize.height - 345)); tableView->setDirection(ScrollView::Direction::VERTICAL); tableView->setVerticalFillOrder(TableView::VerticalFillOrder::TOP_DOWN); tableView->setPosition(Point(0,135)); tableView->setDelegate(this); tableView->reloadData(); _panel->addChild(tableView,0); tableView->setTouchEnabled(false); runAction(Sequence::create(DelayTime::create(1.0f),CallFunc::create([=](){tableView->setTouchEnabled(true);}), nullptr)); auto itemClosed = MenuItemImageLabel::createWithFrameName("back_0.png","back_1.png", CC_CALLBACK_1(StageSelect::menuCallbackClosed,this)); itemClosed->setAnchorPoint(Point::ANCHOR_TOP_RIGHT); itemClosed->setPosition((s_visibleRect.visibleWidth + panelSize.width)/2,panelSize.height); auto itemFight = MenuItemImageLabel::createWithFrameName("btA_0.png","btA_1.png", CC_CALLBACK_1(StageSelect::menuCallbackFight,this), s_gameStrings.mainMenu->stage_fight.c_str()); itemFight->setPosition(Point(panelSize.width / 2,77)); itemFight->setTextColor(Color3B(255, 255, 0)); auto menu = Menu::create( itemClosed, itemFight, nullptr); menu->setPosition(Point::ZERO); _panel->addChild(menu); addBall(); return true; } return false; }
void start() { addBall(); }
int main( void ) { //init bullet initBullet(); // Initialise GLFW if( !glfwInit() ) { fprintf( stderr, "Failed to initialize GLFW\n" ); return -1; } glfwWindowHint(GLFW_SAMPLES, 4); // glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); // glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //screen resolution GLFW const GLFWvidmode * mode = glfwGetVideoMode(glfwGetPrimaryMonitor()); int w = mode->width; int h = mode->height; if(MessageBox(NULL, L"Would you like to run in fullscreen?", L"Fullscreen", MB_ICONQUESTION | MB_YESNO) == IDYES) { window = glfwCreateWindow( w, h, "Ray Tracing - Alfonso Oricchio", glfwGetPrimaryMonitor(), NULL); printf("fullscreen\n"); } else { window = glfwCreateWindow( WINDOW_WIDTH, WINDOW_HEIGHT, "Ray Tracing - Alfonso Oricchio", NULL, NULL); printf("window\n"); } // Open a window and create its OpenGL context if( window == NULL ){ fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" ); glfwTerminate(); return -1; } glfwMakeContextCurrent(window); // Initialize GLEW glewExperimental = true; // Needed for core profile if (glewInit() != GLEW_OK) { fprintf(stderr, "Failed to initialize GLEW\n"); return -1; } // Ensure we can capture the escape key being pressed below glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE); glfwSetCursorPos(window, WINDOW_WIDTH/2, WINDOW_HEIGHT/2); // Dark blue background glClearColor(0.0f, 0.0f, 0.4f, 0.0f); // Enable depth test glEnable(GL_DEPTH_TEST); // Accept fragment if it closer to the camera than the former one glDepthFunc(GL_LESS); // Cull triangles which normal is not towards the camera //glEnable(GL_CULL_FACE); GLuint VertexArrayID; glGenVertexArrays(1, &VertexArrayID); glBindVertexArray(VertexArrayID); // Create and compile our GLSL program from the shaders GLuint depthProgramID = LoadShaders( "DepthRTT.vertexshader", "DepthRTT.fragmentshader" ); // Get a handle for our "MVP" uniform GLuint depthMatrixID = glGetUniformLocation(depthProgramID, "depthMVP"); // The framebuffer, which regroups 0, 1, or more textures, and 0 or 1 depth buffer. GLuint FramebufferName = 0; glGenFramebuffers(1, &FramebufferName); glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName); // Depth texture. Slower than a depth buffer, but you can sample it later in your shader GLuint depthTexture; glGenTextures(1, &depthTexture); glBindTexture(GL_TEXTURE_2D, depthTexture); glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT16, 1024, 1024, 0,GL_DEPTH_COMPONENT, GL_FLOAT, 0); 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_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthTexture, 0); // No color output in the bound framebuffer, only depth. glDrawBuffer(GL_NONE); // Always check that our framebuffer is ok if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) return false; /***********************************************************/ // The quad's FBO. Used only for visualizing the shadowmap. static const GLfloat g_quad_vertex_buffer_data[] = { -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, -1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, }; GLuint quad_vertexbuffer; glGenBuffers(1, &quad_vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(g_quad_vertex_buffer_data), g_quad_vertex_buffer_data, GL_STATIC_DRAW); // Create and compile our GLSL program from the shaders GLuint quad_programID = LoadShaders( "Passthrough.vertexshader", "SimpleTexture.fragmentshader" ); GLuint texID = glGetUniformLocation(quad_programID, "texture"); /**********************************************************/ // Create and compile our GLSL program from the shaders GLuint programID = LoadShaders( "TransformVertexShader.vertexshader", "ColorFragmentShader.fragmentshader" ); // Get a handle for our "MVP" uniform GLuint MatrixID = glGetUniformLocation(programID, "MVP"); GLuint ViewMatrixID = glGetUniformLocation(programID, "V"); GLuint ModelMatrixID = glGetUniformLocation(programID, "M"); GLuint DepthBiasID = glGetUniformLocation(programID, "DepthBiasMVP"); GLuint ShadowMapID = glGetUniformLocation(programID, "shadowMap"); // Get a handle for our "LightPosition" uniform GLuint lightInvDirID = glGetUniformLocation(programID, "LightInvDirection_worldspace"); //CUBO1 addBox(2,2,2,0,0,0,1.0); //CUBO2 addBox2(2,2,2,0,6,0,1.0); //FLOOR initFloor(); //WALL initWall(); //BALL addScene(); do{ world->stepSimulation(1/60.f); if (glfwGetKey( window, GLFW_KEY_SPACE ) == GLFW_PRESS){ btRigidBody* ball = addBall(1.0,CamGetPosition().x,CamGetPosition().y,CamGetPosition().z,2.0); ball->setLinearVelocity(btVector3((CamGetDirection().x*50),(CamGetDirection().y*50),(CamGetDirection().z*50))); } // Render to our framebuffer glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName); glViewport(0,0,1024,1024); // Render on the whole framebuffer, complete from the lower left corner to the upper right // Clear the screen glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); ////////////////////////////// // Use our shader //SHADOW RENDER glUseProgram(depthProgramID); glm::vec3 lightInvDir = glm::vec3(0.5f,2,2); // Compute the MVP matrix from the light's point of view glm::mat4 depthProjectionMatrix = glm::ortho<float>(-10,10,-10,10,-10,20); glm::mat4 depthViewMatrix = glm::lookAt(lightInvDir, glm::vec3(0,0,0), glm::vec3(0,1,0)); // or, for spot light : //glm::vec3 lightPos(5, 20, 20); //glm::mat4 depthProjectionMatrix = glm::perspective<float>(45.0f, 1.0f, 2.0f, 50.0f); //glm::mat4 depthViewMatrix = glm::lookAt(lightPos, lightPos-lightInvDir, glm::vec3(0,1,0)); glm::mat4 depthModelMatrix = glm::mat4(1.0); glm::mat4 depthMVP = depthProjectionMatrix * depthViewMatrix * depthModelMatrix; // Send our transformation to the currently bound shader, // in the "MVP" uniform glUniformMatrix4fv(depthMatrixID, 1, GL_FALSE, &depthMVP[0][0]); // CUBE1 ------------------------------------------------------------------------- renderBox(bodies[0],true); //just a cube not really needed //CUBE2 -------------------------------------------------------------------------- renderBox2(bodies[1],true); //just a cube not really needed //FLOOR -------------------------------------------------------------------------- renderPlane(bodies[2],true); //floor //WALL --------------------------------------------------------------------------- renderWall(bodies[3],true); //back wall //CASTLE ------------------------------------------------------------------------- renderScene(true); //castle, main scene //BALL --------------------------------------------------------------------------- int iv; for(iv = 4; iv < bodies.size();iv++) { renderBall(bodies[iv],(iv - 4),true); //"cannon balls" shooted from the camera } ////////////////////////////// //STANDARD RENDER // Compute the MVP matrix from keyboard and mouse input // Clear the screen // Render to the screen glBindFramebuffer(GL_FRAMEBUFFER, 0); glViewport(0,0,1024,768); // Render on the whole framebuffer, complete from the lower left corner to the upper right glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Use our shader glUseProgram(programID); computeMatricesFromInputs(); glm::mat4 ProjectionMatrix = getProjectionMatrix(); glm::mat4 ViewMatrix = getViewMatrix(); glm::mat4 ModelMatrix = glm::mat4(1.0); glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix; glm::mat4 biasMatrix( 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, 0.0, 0.5, 0.5, 0.5, 1.0 ); glm::mat4 depthBiasMVP = biasMatrix*depthMVP; // Send our transformation to the currently bound shader, // in the "MVP" uniform glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]); glUniformMatrix4fv(ModelMatrixID, 1, GL_FALSE, &ModelMatrix[0][0]); glUniformMatrix4fv(ViewMatrixID, 1, GL_FALSE, &ViewMatrix[0][0]); glUniformMatrix4fv(DepthBiasID, 1, GL_FALSE, &depthBiasMVP[0][0]); glUniform3f(lightInvDirID, lightInvDir.x, lightInvDir.y, lightInvDir.z); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, depthTexture); glUniform1i(ShadowMapID, 0); // CUBE1 ------------------------------------------------------------------------- renderBox(bodies[0],false); //just a cube not really needed //CUBE2 -------------------------------------------------------------------------- renderBox2(bodies[1],false); //just a cube not really needed //FLOOR -------------------------------------------------------------------------- renderPlane(bodies[2],false); //floor //WALL --------------------------------------------------------------------------- renderWall(bodies[3],false); //back wall //CASTLE ------------------------------------------------------------------------- renderScene(false); //castle, main scene //BALL --------------------------------------------------------------------------- // int iv; for(iv = 4; iv < bodies.size();iv++) { renderBall(bodies[iv],(iv - 4),false); //"cannon balls" shooted from the camera } /*--------------------------------------------------*/ // Optionally render the shadowmap (for debug only) // Render only on a corner of the window (or we we won't see the real rendering...) glViewport(0,0,512,512); // Use our shader glUseProgram(quad_programID); // Bind our texture in Texture Unit 0 glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, depthTexture); // Set our "renderedTexture" sampler to user Texture Unit 0 glUniform1i(texID, 0); // 1rst attribute buffer : vertices glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer); glVertexAttribPointer( 0, // attribute 0. No particular reason for 0, but must match the layout in the shader. 3, // size GL_FLOAT, // type GL_FALSE, // normalized? 0, // stride (void*)0 // array buffer offset ); // Draw the triangle ! // You have to disable GL_COMPARE_R_TO_TEXTURE above in order to see anything ! //glDrawArrays(GL_TRIANGLES, 0, 6); // 2*3 indices starting at 0 -> 2 triangles glDisableVertexAttribArray(0); /*--------------------------------------------------*/ // Swap buffers glfwSwapBuffers(window); glfwPollEvents(); } // Check if the ESC key was pressed or the window was closed while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && glfwWindowShouldClose(window) == 0 ); deleteALL(); glDeleteProgram(programID); glDeleteVertexArrays(1, &VertexArrayID); // Close OpenGL window and terminate GLFW glfwTerminate(); return 0; }
b2Body* World::addBall(float x, float y, float radius) { return addBall(b2Vec2(x,y),radius); }
void Model::makeBall(float radius, size_t step, size_t rstep, const glm::vec3 ¢er) { clearVertices(); clearIndices(); setPrimitive(GLTriangles); addBall(radius, step, rstep, center); }
void Model::addBall(float radius, size_t step) { addBall(radius, step, glm::vec3()); }
void Model::addBall(float radius, size_t step, const glm::vec3 ¢er) { addBall(radius, step, 2 * step, center); }
static cpSpace * init(void) { space = cpSpaceNew(); cpSpaceSetIterations(space, 10); cpSpaceSetGravity(space, cpv(0, -100)); cpSpaceSetSleepTimeThreshold(space, 0.5f); cpBody *staticBody = cpSpaceGetStaticBody(space); cpShape *shape; shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,120), cpv(320,120), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,0), cpv(320,0), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-120), cpv(320,-120), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-160,-240), cpv(-160,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(0,-240), cpv(0,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(160,-240), cpv(160,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); cpShapeSetElasticity(shape, 1.0f); cpShapeSetFriction(shape, 1.0f); cpShapeSetLayers(shape, NOT_GRABABLE_MASK); cpVect boxOffset; cpBody *body1, *body2; cpVect posA = cpv( 50, 60); cpVect posB = cpv(110, 60); #define POS_A cpvadd(boxOffset, posA) #define POS_B cpvadd(boxOffset, posB) // Pin Joints - Link shapes with a solid bar or pin. // Keeps the anchor points the same distance apart from when the joint was created. boxOffset = cpv(-320, -240); body1 = addBall(posA, boxOffset); body2 = addBall(posB, boxOffset); cpSpaceAddConstraint(space, cpPinJointNew(body1, body2, cpv(15,0), cpv(-15,0))); // Slide Joints - Like pin joints but with a min/max distance. // Can be used for a cheap approximation of a rope. boxOffset = cpv(-160, -240); body1 = addBall(posA, boxOffset); body2 = addBall(posB, boxOffset); cpSpaceAddConstraint(space, cpSlideJointNew(body1, body2, cpv(15,0), cpv(-15,0), 20.0f, 40.0f)); // Pivot Joints - Holds the two anchor points together. Like a swivel. boxOffset = cpv(0, -240); body1 = addBall(posA, boxOffset); body2 = addBall(posB, boxOffset); cpSpaceAddConstraint(space, cpPivotJointNew(body1, body2, cpvadd(boxOffset, cpv(80,60)))); // cpPivotJointNew() takes it's anchor parameter in world coordinates. The anchors are calculated from that // cpPivotJointNew2() lets you specify the two anchor points explicitly // Groove Joints - Like a pivot joint, but one of the anchors is a line segment that the pivot can slide in boxOffset = cpv(160, -240); body1 = addBall(posA, boxOffset); body2 = addBall(posB, boxOffset); cpSpaceAddConstraint(space, cpGrooveJointNew(body1, body2, cpv(30,30), cpv(30,-30), cpv(-30,0))); // Damped Springs boxOffset = cpv(-320, -120); body1 = addBall(posA, boxOffset); body2 = addBall(posB, boxOffset); cpSpaceAddConstraint(space, cpDampedSpringNew(body1, body2, cpv(15,0), cpv(-15,0), 20.0f, 5.0f, 0.3f)); // Damped Rotary Springs boxOffset = cpv(-160, -120); body1 = addBar(posA, boxOffset); body2 = addBar(posB, boxOffset); // Add some pin joints to hold the circles in place. cpSpaceAddConstraint(space, cpPivotJointNew(body1, staticBody, POS_A)); cpSpaceAddConstraint(space, cpPivotJointNew(body2, staticBody, POS_B)); cpSpaceAddConstraint(space, cpDampedRotarySpringNew(body1, body2, 0.0f, 3000.0f, 60.0f)); // Rotary Limit Joint boxOffset = cpv(0, -120); body1 = addLever(posA, boxOffset); body2 = addLever(posB, boxOffset); // Add some pin joints to hold the circles in place. cpSpaceAddConstraint(space, cpPivotJointNew(body1, staticBody, POS_A)); cpSpaceAddConstraint(space, cpPivotJointNew(body2, staticBody, POS_B)); // Hold their rotation within 90 degrees of each other. cpSpaceAddConstraint(space, cpRotaryLimitJointNew(body1, body2, -M_PI_2, M_PI_2)); // Ratchet Joint - A rotary ratchet, like a socket wrench boxOffset = cpv(160, -120); body1 = addLever(posA, boxOffset); body2 = addLever(posB, boxOffset); // Add some pin joints to hold the circles in place. cpSpaceAddConstraint(space, cpPivotJointNew(body1, staticBody, POS_A)); cpSpaceAddConstraint(space, cpPivotJointNew(body2, staticBody, POS_B)); // Ratchet every 90 degrees cpSpaceAddConstraint(space, cpRatchetJointNew(body1, body2, 0.0f, M_PI_2)); // Gear Joint - Maintain a specific angular velocity ratio boxOffset = cpv(-320, 0); body1 = addBar(posA, boxOffset); body2 = addBar(posB, boxOffset); // Add some pin joints to hold the circles in place. cpSpaceAddConstraint(space, cpPivotJointNew(body1, staticBody, POS_A)); cpSpaceAddConstraint(space, cpPivotJointNew(body2, staticBody, POS_B)); // Force one to sping 2x as fast as the other cpSpaceAddConstraint(space, cpGearJointNew(body1, body2, 0.0f, 2.0f)); // Simple Motor - Maintain a specific angular relative velocity boxOffset = cpv(-160, 0); body1 = addBar(posA, boxOffset); body2 = addBar(posB, boxOffset); // Add some pin joints to hold the circles in place. cpSpaceAddConstraint(space, cpPivotJointNew(body1, staticBody, POS_A)); cpSpaceAddConstraint(space, cpPivotJointNew(body2, staticBody, POS_B)); // Make them spin at 1/2 revolution per second in relation to each other. cpSpaceAddConstraint(space, cpSimpleMotorNew(body1, body2, M_PI)); // Make a car with some nice soft suspension boxOffset = cpv(0, 0); cpBody *wheel1 = addWheel(posA, boxOffset); cpBody *wheel2 = addWheel(posB, boxOffset); cpBody *chassis = addChassis(cpv(80, 100), boxOffset); cpSpaceAddConstraint(space, cpGrooveJointNew(chassis, wheel1, cpv(-30, -10), cpv(-30, -40), cpvzero)); cpSpaceAddConstraint(space, cpGrooveJointNew(chassis, wheel2, cpv( 30, -10), cpv( 30, -40), cpvzero)); cpSpaceAddConstraint(space, cpDampedSpringNew(chassis, wheel1, cpv(-30, 0), cpvzero, 50.0f, 20.0f, 10.0f)); cpSpaceAddConstraint(space, cpDampedSpringNew(chassis, wheel2, cpv( 30, 0), cpvzero, 50.0f, 20.0f, 10.0f)); return space; }