void graphics::scale(point p) { //glScalef(p.x, p.y, p.z); v3 vec = {p.x, p.y, p.z}; switch (matrixMode) { case SINNCA_MODELVIEW_MATRIX: { snScaleM4(getModelMatrix(), getModelMatrix(), &vec); break; } case SINNCA_PROJECTION_MATRIX: { snScaleM4(getProjectionMatrix(), getProjectionMatrix(), &vec); break; } case SINNCA_TEXTURE_MATRIX: { snScaleM4(getTextureMatrix(), getTextureMatrix(), &vec); break; } } }
void graphics::move(float x, float y, float z) { //glTranslatef(x, y, z); v3 vec = {x, y, z}; switch (matrixMode) { case SINNCA_MODELVIEW_MATRIX: { snTranslateM4(getModelMatrix(), getModelMatrix(), &vec); break; } case SINNCA_PROJECTION_MATRIX: { snTranslateM4(getProjectionMatrix(), getProjectionMatrix(), &vec); break; } case SINNCA_TEXTURE_MATRIX: { snTranslateM4(getTextureMatrix(), getTextureMatrix(), &vec); break; } } }
void Node::draw(bool child, int numInstances, std::vector<mat4> rotMats, std::vector<ci::mat4> positions, std::vector<ci::mat4> scales){ { if (numInstances == 0){ gl::pushModelView(); matrix = rotmat * glm::scale(scale); matrix[3] = vec4(trans,1); gl::multModelMatrix(matrix); } else { matrix = rotmat; rotMats.push_back(matrix); positions.push_back(glm::translate(trans)); scales.push_back(glm::scale(scale)); } //matrix[3] = vec4(trans,1); //gl::multModelMatrix(matrix); //double t1 = ci::app::getElapsedSeconds(); //ci::app::console() << (t1 - t0) * 1000 << " : " << name << " : matrix node time " << std::endl; for (MeshRef pMesh : pMeshes) { //double t0 = ci::app::getElapsedSeconds(); if (visible) pMesh->draw(numInstances, rotMats, positions, scales); //double t1 = ci::app::getElapsedSeconds(); //ci::app::console() << (t1 - t0) * 1000 << " : "<< name << " : node draw time " << std::endl; } vec4 xform = getModelMatrix() * vec4(0, 0, 0, 1); worldPos = xform; std::vector<float> distances; for (NodeRef pChild : pChildren) { vec4 start = vec4(1); vec4 xform = getProjectionMatrix() * getViewMatrix() * getModelMatrix() * pChild->matrix * vec4(pChild->bounds.getCenter(), 1); distances.push_back(xform.z); } vector<pair<size_t, myiter> > order(distances.size()); size_t n = 0; for (myiter it = distances.begin(); it != distances.end(); ++it, ++n) order[n] = make_pair(n, it); sort(order.begin(), order.end(), ordering()); //ci::app::console() << ":::::::::::" << endl; for (auto pDrawOrder : order) { pChildren[pDrawOrder.first]->draw(true, numInstances, rotMats, positions, scales); } if (numInstances == 0){ gl::popModelView(); } } };
glm::mat4 Transform::getCumulativeModelMatrix(){ // if the transform has no parent, return the identity matrix if(parents.size() == 0){ return getModelMatrix(); } if(cumulativeModelMatrixDirty){ cumulativeModelMatrix = firstParent()->getCumulativeModelMatrix() * getModelMatrix(); cumulativeModelMatrixDirty = false; } return cumulativeModelMatrix; }
void Transform::render(sweet::MatrixStack * _matrixStack, RenderOptions * _renderOptions){ // don't bother doing any work if we aren't rendering anyway if(!isVisible()){ return; } // save previous matrix state _matrixStack->pushMatrix(); // apply the transform's matrix if(!isIdentity){ _matrixStack->applyMatrix(getModelMatrix()); } // render all of the transform's children for(unsigned long int i = 0; i < children.size(); i++){ if(children.at(i)->asNodeRenderable() != nullptr){ children.at(i)->asNodeRenderable()->render(_matrixStack, _renderOptions); } } if(drawTransforms){ Shader * prev = _renderOptions->shader; _renderOptions->shader = transformShader; float prevLineWidth; glGetFloatv(GL_LINE_WIDTH, &prevLineWidth); glLineWidth(5); transformIndicator->render(_matrixStack, _renderOptions); glLineWidth(prevLineWidth); _renderOptions->shader = prev; } // restore previous matrix state _matrixStack->popMatrix(); }
/*! \brief SkeletonRenderObject::renderV Renders the object, if visible is set to \b true. \param projection \param view */ void SkeletonRenderObject::renderV( const QMatrix4x4& projection, const QMatrix4x4& view ) { Q_ASSERT( mp_shaderProgram ); if ( !visible() ) { return; } glPolygonMode( GL_FRONT, GL_FILL ); glPolygonMode( GL_BACK, GL_FILL ); m_vao.bind(); mp_shaderProgram->bind(); mp_shaderProgram->setUniformValue( "viewMatrix", view ); mp_shaderProgram->setUniformValue( "projectionMatrix", projection ); mp_shaderProgram->setUniformValue( "modelMatrix", getModelMatrix() ); mp_shaderProgram->setUniformValue( "useTexture", false ); mp_shaderProgram->setUniformValue( "useSecondTexture", false ); m_lineIndexBuffer.bind(); glDrawElements( GL_LINES, 38, GL_UNSIGNED_INT, 0 ); m_pointIndexBuffer.bind(); glDrawElements( GL_POINTS, 20, GL_UNSIGNED_INT, 0 ); m_vao.release(); }
void ModelStatic::calculateBoundingBox(AABB& box) { zeq_model_proto_t* proto = getModelPrototype(); if (!proto) return; Mat4 matrix = getModelMatrix(); Vec3 pos; for (VertexBuffer* vb : proto->getVertexBuffers()) { for (Vertex& vert : *vb) { matrix.transformVector(pos, vert.pos); box.addInternalPoint(pos); } } for (VertexBuffer* vb : proto->getVertexBuffersNoCollide()) { for (Vertex& vert : *vb) { matrix.transformVector(pos, vert.pos); box.addInternalPoint(pos); } } }
void RigidBody::getVertices(vec3 *vertices) { mat4 m = getModelMatrix(); for (int i = 0; i < 8; i++) { vec3 v = v3((i&4)?1.0f:-1.0f, (i&2)?1.0f:-1.0f, (i&1)?1.0f:-1.0f); vertices[i] = v3(m*v4(v, 1.0f)); } }
// multiplication of model * projection MutableMatrix44D getModelViewMatrix() const { if (_dirtyFlags._modelViewMatrixDirty) { _dirtyFlags._modelViewMatrixDirty = false; _modelViewMatrix = getProjectionMatrix().multiply(getModelMatrix()); } return _modelViewMatrix; }
void EntityDirectionalLight::update(float dt, glm::mat4 parentMatrix) { m_transformMatrix = getModelMatrix(parentMatrix); m_rotation += m_angularVelocity * dt; //Renderer::draw(ModelSystem::getById(0), m_transformMatrix); //m_rotation = glm::vec3(parentMatrix * glm::vec4(m_rotation,1.0f)); #if DEBUG LOG_DEBUG("Update()\n\nentityID %d, entiryTag %s, \nm_position %f, %f, %f\nm_rotation %f, %f, %f\nm_angVel %f, %f, %f\ndeltatime %f\n", m_entityID, m_entityTag.data(), m_position.x, m_position.y, m_position.z, glm::degrees(m_rotation.x), glm::degrees(m_rotation.y), glm::degrees(m_rotation.z), glm::degrees(m_angularVelocity.x), glm::degrees(m_angularVelocity.y), glm::degrees(m_angularVelocity.z), dt); Util::printMatrix(parentMatrix, "ParentMatrix:"); Util::printMatrix(m_transformMatrix, "TransormMatrix:"); printf("\n\n"); #endif if (m_childIDs.size() > 0) // If we actually have kids. { for (const auto child : m_childIDs) // For every childID in childIDs-vector. { //LOG_DEBUG("\n\nI am entityID %d Updating child with entityID %d.\n", m_entityID, child); Scene::getEntity(child)->update(dt, m_transformMatrix); } } }
void graphics::ortho(float near, float far) { float left = (float)resW * 0.5f; float right = -(float)resW * 0.5f; float up = (float)resH * 0.5f; float down = -(float)resH * 0.5f; switch (matrixMode) { case SINNCA_MODELVIEW_MATRIX: { snOrthoMatrix(getModelMatrix(), left, right, up, down, near, far); break; } case SINNCA_PROJECTION_MATRIX: { snOrthoMatrix(getProjectionMatrix(), left, right, up, down, near, far); break; } case SINNCA_TEXTURE_MATRIX: { snOrthoMatrix(getTextureMatrix(), left, right, up, down, near, far); break; } } }
EntityNode::EntityNode(C::Tag entityTag, int entityID, glm::vec3 pos, glm::vec3 rot, glm::vec3 scale, glm::vec3 vel, glm::vec3 angVel) : Entity(entityTag, entityID, pos, rot, scale, vel, angVel) { m_transformMatrix = getModelMatrix(); update(0); }
Frustum* getHalfFrustumMC() const { if (_dirtyFlags._halfFrustumMCDirty) { _dirtyFlags._halfFrustumMCDirty = false; delete _halfFrustumInModelCoordinates; _halfFrustumInModelCoordinates = getHalfFrustum()->transformedBy_P(getModelMatrix()); } return _halfFrustumInModelCoordinates; }
const glm::mat4& CTransformer::getModelViewProjectionMatrix() const { if (m_modelViewProjection.m_matrixDirty) { m_modelViewProjection.m_matrix = getViewProjectionMatrix() * getModelMatrix(); m_modelViewProjection.m_matrixDirty = false; } return m_modelViewProjection.m_matrix; }
const Frustum* const getFrustumInModelCoordinates() const { // return getFrustumMC(); if (_dirtyFlags._frustumMCDirty) { _dirtyFlags._frustumMCDirty = false; delete _frustumInModelCoordinates; _frustumInModelCoordinates = getFrustum()->transformedBy_P(getModelMatrix()); } return _frustumInModelCoordinates; }
const Matrix4f* pipeline::getMVP() { Matrix4f Cam, CamPos, Persp; Cam.LoadCameraMatrix(this->m_camera.xAxis, this->m_camera.yAxis, this->m_camera.zAxis); CamPos.LoadCameraPosMatrix(this->m_camera.pos); Persp.LoadPerspMatrix(this->m_persp); this->MVP = Persp * Cam * CamPos * (*getModelMatrix()); return &this->MVP; }
void Table::draw(mat4 trans) { mat4 m(1.0f); for (int i=0; i<getModelCount(); ++i) { m = trans * getModelMatrix(i); cube->draw(m); } }
bool VisualActor::intersectRay(const Math::Ray &ray, const Math::Vector3d position, float direction) { Math::Matrix4 inverseModelMatrix = getModelMatrix(position, direction); inverseModelMatrix.inverse(); // Build an object local ray from the world ray Math::Ray localRay = ray; localRay.transform(inverseModelMatrix); return _model->intersectRay(localRay); }
void Entity::setQueued(QueuedEntity* queued) { m_queued = queued; #if 0 m_queued->modelMatrix = getModelMatrix(); #endif m_queued->m_matrix = m_affineMat; m_queued->instanceParam = getInstanceParam(); m_queued->flags = m_flags; m_queued->distance = m_distance; }
void PointLightRenderable::do_draw() { //Location int positionLocation = m_shaderProgram->getAttributeLocation("vPosition"); int colorLocation = m_shaderProgram->getAttributeLocation("vColor"); int normalLocation = m_shaderProgram->getAttributeLocation("vNormal"); int modelLocation = m_shaderProgram->getUniformLocation("modelMat"); //Send data to GPU if(modelLocation != ShaderProgram::null_location) { glcheck(glUniformMatrix4fv(modelLocation, 1, GL_FALSE, glm::value_ptr(getModelMatrix()))); } if(positionLocation != ShaderProgram::null_location) { //Activate location glcheck(glEnableVertexAttribArray(positionLocation)); //Bind buffer glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_pBuffer)); //Specify internal format glcheck(glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)0)); } if(colorLocation != ShaderProgram::null_location) { glcheck(glEnableVertexAttribArray(colorLocation)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_cBuffer)); glcheck(glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, 0, (void*)0)); } if(normalLocation != ShaderProgram::null_location) { glcheck(glEnableVertexAttribArray(normalLocation)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_nBuffer)); glcheck(glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)0)); } //Draw triangles elements glcheck(glDrawArrays(GL_TRIANGLES,0, m_positions.size())); if(positionLocation != ShaderProgram::null_location) { glcheck(glDisableVertexAttribArray(positionLocation)); } if(colorLocation != ShaderProgram::null_location) { glcheck(glDisableVertexAttribArray(colorLocation)); } if(normalLocation != ShaderProgram::null_location) { glcheck(glDisableVertexAttribArray(normalLocation)); } }
void ModelStatic::draw() { // Assumes matrix mode is GL_MODELVIEW and that the camera view has been applied zeq_model_proto_t* proto = getModelPrototype(); if (!proto) return; glPushMatrix(); glMultMatrixf(getModelMatrix().ptr()); zeq_blend_t lastBlend = ZEQ_BLEND_INVISIBLE; uint32_t lastDiffuseId = 0; for (VertexBuffer* vb : proto->getVertexBuffers()) { if (vb->isInvisible()) continue; vb->setActiveTexture(lastDiffuseId); if (vb->setActiveBlend(lastBlend)) { if (lastBlend == ZEQ_BLEND_PARTICLE) Fog::disable(); else Fog::enable(); } vb->draw(); } for (VertexBuffer* vb : proto->getVertexBuffersNoCollide()) { if (vb->isInvisible()) continue; vb->setActiveTexture(lastDiffuseId); if (vb->setActiveBlend(lastBlend)) { if (lastBlend == ZEQ_BLEND_PARTICLE) Fog::disable(); else Fog::enable(); } vb->draw(); } zeq_material_t::deactivateBlend(lastBlend); glPopMatrix(); }
m3* graphics::getNormalMatrix() { m4 matrix; copyMatrix(&matrix, getModelMatrix()); invert(&matrix); transpose(&matrix); copyMatrix(&normalMatrix, &matrix); return &normalMatrix; }
void graphics::perspective(float end, float start = 0.1f, float fov = 35.0f) { m4 base; float aspectRatio = (float)resW / (float)resH; float d = end - start, r = (fov * 0.5) * (M_PI / 180), s = sin(r), c = sin(r) / s; snLoadIdentity(&base); base.m[0].x = c / aspectRatio; base.m[1].y = c; base.m[2].z = -(end + start) / d; base.m[2].w = -1.0f; base.m[3].z = -2.0f * start * end / d; base.m[3].w = 0.0f; switch (matrixMode) { case SINNCA_MODELVIEW_MATRIX: { multiply(getModelMatrix(), getModelMatrix(), &base); break; } case SINNCA_PROJECTION_MATRIX: { multiply(getProjectionMatrix(), getProjectionMatrix(), &base); break; } case SINNCA_TEXTURE_MATRIX: { multiply(getTextureMatrix(), getTextureMatrix(), &base); break; } } }
void ParticleRenderable::do_draw() { //Update the parent and local transform matrix to position the geometric data according to the particle's data. const float& pRadius = m_particle->getRadius(); const glm::vec3& pPosition = m_particle->getPosition(); float toRotate = m_particle->getAngle(); glm::mat4 scale = glm::scale(glm::mat4(1.0), glm::vec3(pRadius)); glm::mat4 translate = glm::translate(glm::mat4(1.0), glm::vec3(pPosition)); glm::mat4 rotate = glm::rotate(glm::mat4(1.0), toRotate, glm::vec3(0,0,1)); setLocalTransform(translate*scale*rotate); //Draw geometric data int positionLocation = m_shaderProgram->getAttributeLocation("vPosition"); int colorLocation = m_shaderProgram->getAttributeLocation("vColor"); int normalLocation = m_shaderProgram->getAttributeLocation("vNormal"); int modelLocation = m_shaderProgram->getUniformLocation("modelMat"); if (modelLocation != ShaderProgram::null_location) { glcheck(glUniformMatrix4fv(modelLocation, 1, GL_FALSE, glm::value_ptr(getModelMatrix()))); } if (positionLocation != ShaderProgram::null_location) { glcheck(glEnableVertexAttribArray(positionLocation)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_pBuffer)); glcheck(glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)0)); } if (colorLocation != ShaderProgram::null_location) { glcheck(glEnableVertexAttribArray(colorLocation)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_cBuffer)); glcheck(glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, 0, (void*)0)); } if (normalLocation != ShaderProgram::null_location) { glcheck(glEnableVertexAttribArray(normalLocation)); glcheck(glBindBuffer(GL_ARRAY_BUFFER, m_nBuffer)); glcheck(glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, 0, (void*)0)); } //Draw triangles elements glcheck(glDrawArrays(GL_TRIANGLES,0, m_positions.size())); if (positionLocation != ShaderProgram::null_location) { glcheck(glDisableVertexAttribArray(positionLocation)); } if (colorLocation != ShaderProgram::null_location) { glcheck(glDisableVertexAttribArray(colorLocation)); } if (normalLocation != ShaderProgram::null_location) { glcheck(glDisableVertexAttribArray(normalLocation)); } }
void RenderObject::renderV( const QMatrix4x4& projection, const QMatrix4x4& view ) { if ( !visible() ) { return; } if ( !mp_shaderProgram ) { return; } m_vao.bind(); mp_shaderProgram->bind(); if ( m_useTexture ) { if ( m_activeTextures.at( 0 ) ) { QOpenGLContext::currentContext()->functions()->glActiveTexture( GL_TEXTURE0 ); m_textures.at( 0 )->bind(); } if ( m_activeTextures.at( 1 ) ) { QOpenGLContext::currentContext()->functions()->glActiveTexture( GL_TEXTURE1 ); m_textures.at( 1 )->bind(); } } if ( m_wireFrameMode ) { // Wireframe mode glPolygonMode( GL_FRONT, GL_LINE ); glPolygonMode( GL_BACK, GL_LINE ); } else { glPolygonMode( GL_FRONT, GL_FILL ); glPolygonMode( GL_BACK, GL_FILL ); } mp_shaderProgram->setUniformValue( "projectionMatrix", projection ); mp_shaderProgram->setUniformValue( "viewMatrix", view ); mp_shaderProgram->setUniformValue( "modelMatrix", getModelMatrix() ); mp_shaderProgram->setUniformValue( "useTexture", m_activeTextures.at( 0 ) ); mp_shaderProgram->setUniformValue( "useSecondTexture", m_activeTextures.at( 1 ) ); glDrawElements( m_renderMode, m_indices.size(), GL_UNSIGNED_INT, 0 ); m_vao.release(); }
// Constructor(s) ################################################ //################################################################ Object3D(bool active = true, glm::mat4 scaleMatrix = glm::mat4(), glm::mat4 translationMatrix = glm::mat4(), glm::mat4 rotationalMatrix_0 = glm::mat4(), glm::mat4 rotationalMatrix_1 = glm::mat4()) { setActive(active); setRotationMatrix_0(rotationalMatrix_0); setRotationMatrix_1(rotationalMatrix_1); setScaleMatrix(scaleMatrix); setTranslationMatrix(translationMatrix); setLastModelMatrix(getModelMatrix()); }
void TMIP::process() { auto volumes = inport_.getData(); if (volumes->empty()) { return; } auto firstVol = volumes->at(0); if (inport_.isChanged()) { const DataFormatBase* format = firstVol->getDataFormat(); volume0_ = std::make_shared<Volume>(firstVol->getDimensions(), format); volume0_->setModelMatrix(firstVol->getModelMatrix()); volume0_->setWorldMatrix(firstVol->getWorldMatrix()); // pass on metadata volume0_->copyMetaDataFrom(*firstVol); volume0_->dataMap_ = firstVol->dataMap_; volume1_ = std::shared_ptr<Volume>(volume0_->clone()); } int iterations = static_cast<int>(std::ceil(volumes->size() / static_cast<float>(maxSamplers_))); LogInfo(iterations); std::shared_ptr<Volume> readVol = volume0_; std::shared_ptr<Volume> writeVol = volume1_; int offset = 0; for (int i = 0; i < iterations; i++) { bool firstIT = i == 0; bool lastIT = i != 0 && iterations; auto startVolIT = volumes->begin() + offset + 1; if (firstIT) { auto endVolIT = volumes->begin() + offset + maxSamplers_; iteration(shader_, volumes->at(0), writeVol, startVolIT, endVolIT); } else if (!lastIT) { auto endVolIT = volumes->begin() + offset + maxSamplers_; iteration(shader_, readVol, writeVol, startVolIT, endVolIT); } else { iteration(shaderLast_, readVol, writeVol, startVolIT, volumes->end()); } std::swap(readVol, writeVol); offset += maxSamplers_; } outport_.setData(readVol); }
void graphics::scale(float x, float y, float z) { v3 vec = {x, y, z}; switch (matrixMode) { case SINNCA_MODELVIEW_MATRIX: { snScaleM4(getModelMatrix(), getModelMatrix(), &vec); break; } case SINNCA_PROJECTION_MATRIX: { snScaleM4(getProjectionMatrix(), getProjectionMatrix(), &vec); break; } case SINNCA_TEXTURE_MATRIX: { snScaleM4(getTextureMatrix(), getTextureMatrix(), &vec); break; } } }
void OpenGLSPropRenderer::render(const Math::Vector3d position, float direction) { if (_faceVBO == -1) { // Update the OpenGL Buffer Objects if required clearVertices(); uploadVertices(); } _gfx->set3DMode(); Math::Matrix4 model = getModelMatrix(position, direction); Math::Matrix4 view = StarkScene->getViewMatrix(); Math::Matrix4 projection = StarkScene->getProjectionMatrix(); Math::Matrix4 mvp = projection * view * model; mvp.transpose(); _shader->use(true); _shader->setUniform("mvp", mvp); const Common::Array<Formats::BiffMesh::Face> &faces = _model->getFaces(); const Common::Array<Formats::BiffMesh::Material> &materials = _model->getMaterials(); for (Common::Array<Formats::BiffMesh::Face>::const_iterator face = faces.begin(); face != faces.end(); ++face) { const Formats::BiffMesh::Material &material = materials[face->materialId]; // For each face draw its vertices from the VBO, indexed by the EBO const Gfx::Texture *tex = _texture->getTexture(material.texture); if (tex) { tex->bind(); } else { glBindTexture(GL_TEXTURE_2D, 0); } GLuint ebo = _faceEBO[face]; _shader->enableVertexAttribute("position", _faceVBO, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), 0); _shader->enableVertexAttribute("normal", _faceVBO, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), 12); _shader->enableVertexAttribute("texcoord", _faceVBO, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), 24); _shader->use(true); _shader->setUniform("textured", tex != nullptr); _shader->setUniform("color", Math::Vector3d(material.r, material.g, material.b)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); glDrawElements(GL_TRIANGLES, face->vertexIndices.size(), GL_UNSIGNED_INT, 0); glUseProgram(0); } }
void EntityNode::update(float dt, glm::mat4 parentMatrix) { m_position += m_velocity * dt; m_rotation += m_angularVelocity * dt; m_transformMatrix = getModelMatrix(parentMatrix); if (m_childIDs.size() > 0) // If we actually have kids. { for (const auto child : m_childIDs) // For every childID in childIDs-vector. { //LOG_DEBUG("\n\nI am entityID () %d Updating child with entityID %d.\n", m_entityID, child); Scene::getEntity(child)-> update(dt, m_transformMatrix); } } }