void Renderer::update(const float deltaTime){ shader->bind(); shader->enableVertexAttribArrays(); shader->sendUniform("texture0", 0); spriteMesh->indexBuffer->bind(); spriteMesh->vertex2Buffer->bind(); spriteMesh->vertex2Buffer->vertexAttribPointer(shader->getAttribLocation("vertex0"), 2, GL_FLOAT, false, 0, 0); spriteMesh->texCoordBuffer->bind(); spriteMesh->texCoordBuffer->vertexAttribPointer(shader->getAttribLocation("texCoord0"), 2, GL_FLOAT, false, 0, 0); camera->calculateOrthoMatrix3x3(); for(auto entity : entities){ auto texture = std::static_pointer_cast<bb::Texture>(entity->getComponent("Texture")); auto position = std::static_pointer_cast<bb::Position2D>(entity->getComponent("Position")); auto object = std::static_pointer_cast<bb::Object2D>(entity->getComponent("Object2D")); auto duration = std::static_pointer_cast<Duration>(entity->getComponent("Duration")); auto animation = std::static_pointer_cast<Animation>(entity->getComponent("Animation")); auto color = std::static_pointer_cast<Color>(entity->getComponent("Color")); if(position && object && object->visible){ object->calculateModelMatrix(position); if(texture){ texture->bind(); } shader->sendUniform3x3("pm", (camera->orthoMatrix3*object->modelMatrix).getArray()); shader->sendUniform("alpha", 1.0f); shader->sendUniform("texScale", 1.0f, 1.0f); shader->sendUniform("texOffset", 0.0f, 0.0f); shader->sendUniform("color", 0.0f, 0.0f, 0.0f); if(duration){ shader->sendUniform("alpha", duration->alpha); } if(animation && animation->current()){ shader->sendUniform("texScale", animation->current()->current()->scale.x, animation->current()->current()->scale.y); shader->sendUniform("texOffset", animation->current()->current()->offset.x, animation->current()->current()->offset.y); } if(color){ shader->sendUniform("color", color->color.x, color->color.y, color->color.z); } glDrawElements(GL_TRIANGLES, spriteMesh->indexBuffer->size(), GL_UNSIGNED_INT, 0); if(texture){ texture->unbind(); } } } shader->disableVertexAttribArrays(); shader->unbind(); }
StaticObject::StaticObject(char* url,vec3 position,vec3 rotation,vec3 scale){ mat4 t_normalizeMatrix = loadObj(url); this->speed = vec3(0); this->position = position; this->rotation = rotation; this->scale = scale; calculateModelMatrix(); }
void Entity::update(unsigned millisElapsed) { // Apply all logic necessary /* * Then we update the model matrix, only if this entity is not a child. * This is because this function also update all children's matrices. */ if (parent == nullptr) { calculateModelMatrix(); } for (std::vector<Entity*>::iterator it = childEntities->begin(); it != childEntities->end(); ++it) { (*it)->update(millisElapsed); } }
void TextRenderer::update(const float deltaTime){ shader->bind(); shader->enableVertexAttribArrays(); shader->sendUniform("texture0", 0); camera->calculateOrthoMatrix3x3(); font->bind(); for(auto entity : entities){ auto mesh = std::static_pointer_cast<bb::Mesh>(entity->getComponent("Mesh")); auto position = std::static_pointer_cast<bb::Position2D>(entity->getComponent("Position")); auto object = std::static_pointer_cast<bb::Object2D>(entity->getComponent("Object2D")); if(position && object && mesh && object->visible){ object->calculateModelMatrix(position); shader->sendUniform3x3("pm", (camera->orthoMatrix3*object->modelMatrix).getArray()); shader->sendUniform("alpha", 1.0f); shader->sendUniform("texScale", 1.0f, 1.0f); shader->sendUniform("texOffset", 0.0f, 0.0f); shader->sendUniform("color", 0.0f, 0.0f, 0.0f); mesh->indexBuffer->bind(); mesh->vertex2Buffer->bind(); mesh->vertex2Buffer->vertexAttribPointer(shader->getAttribLocation("vertex0"), 2, GL_FLOAT, false, 0, 0); mesh->texCoordBuffer->bind(); mesh->texCoordBuffer->vertexAttribPointer(shader->getAttribLocation("texCoord0"), 2, GL_FLOAT, false, 0, 0); glDrawElements(GL_TRIANGLES, mesh->indexBuffer->size(), GL_UNSIGNED_INT, 0); } } font->unbind(); shader->disableVertexAttribArrays(); shader->unbind(); }
void Scene::beforeRender(){ // 先对 children 排序 calculateModelMatrix(Mat4::IDENTITY); }