glm::vec3 Transform::position(){ if (!mParent){ return mLocalPosition; } if (mDirty.globalPos){ auto pos = globalMatrix() * vec4(0,0,0,1); mGlobalPosition = vec3(pos); } return mGlobalPosition; }
void BillboardNode::draw(const glm::mat4 & view_matrix, const glm::mat4 & projection_matrix) { // inherited draw - draws all children SceneNode::draw(view_matrix, projection_matrix); glm::mat4 matrix; if( m_type == WORLD_ALIGNED ) matrix = projection_matrix * view_matrix * globalMatrix(); else { glm::mat4 screen_aligned_matrix = glm::mat4(1.0f); // identity screen_aligned_matrix[3] = view_matrix[3]; // with translation matrix = projection_matrix * screen_aligned_matrix * globalMatrix(); } glUseProgram(m_program); glUniformMatrix4fv(m_PVMmatrixLoc, 1, GL_FALSE, glm::value_ptr(matrix) ); glUniform2fv(m_texCoordOffsetLoc, 1, glm::value_ptr(m_texture_offset) ); glEnable(GL_BLEND); glBlendFunc( m_sfactor, m_dfactor ); glBindVertexArray( m_vertexArrayObject ); glUniform1i(m_texSamplerID, 0); // texturing unit 0 -> samplerID [for the GPU linker] glActiveTexture(GL_TEXTURE0 + 0); // texturing unit 0 -> to be bound [for OpenGL BindTexture] // =============================== BEGIN OF SOLUTION - TASK 2b ====================================== // based on m_time and m_textureIDs.size();,display all the frames in a loop unsigned frameNumber = (((int)(m_time*10))%m_textureIDs.size()); // compute the frame here glBindTexture(GL_TEXTURE_2D, m_textureIDs[frameNumber] ); // =============================== END OF SOLUTION - TASK 2b ====================================== glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, m_wrap_s); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, m_wrap_t); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); // just one quad glBindVertexArray( 0 ); glDisable(GL_BLEND); }
void SplineSegmentNode::draw(const glm::mat4 & view_matrix, const glm::mat4 & projection_matrix) { // inherited draw - draws all children SceneNode::draw(view_matrix, projection_matrix); glm::mat4 matrix = projection_matrix * view_matrix * globalMatrix(); glUseProgram(m_program); glUniformMatrix4fv(m_PVMmatrixLoc, 1, GL_FALSE, glm::value_ptr(matrix) ); glBindVertexArray( m_vertexArrayObject ); // Draw envelope glUniform4fv( m_colLoc, 1, glm::value_ptr(vec4(1.0, 1.0, 1.0, 1.0) )); glDrawArrays(GL_LINE_STRIP, 0, curveTestSize ); // draw the spline curve glLineWidth( 3.0f); glUniform4fv( m_colLoc, 1, glm::value_ptr(vec4(1.0, 1.0, 0.0, 1.0) )); glDrawArrays(GL_LINE_STRIP, curveTestSize, m_numberOfCurveSamples ); glLineWidth( 1.0f); // draw control points // set OpenGL glDisable(GL_DEPTH_TEST); glEnable(GL_POINT_SMOOTH); glPointSize(5.0f); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glUniform4fv( m_colLoc, 1, glm::value_ptr(vec4(1.0, 0.0, 0.0, 1.0) )); glDrawArrays(GL_POINTS, 0, 1); glDrawArrays(GL_POINTS, 1, 1); glDrawArrays(GL_POINTS, 2, 1); glDrawArrays(GL_POINTS, 3, 1); glDisable(GL_POINT_SMOOTH); // tangents glUniform4fv( m_colLoc, 1, glm::value_ptr(vec4(0.0, 0.0, 1.0, 1.0) )); // blue glDrawArrays( GL_LINES, curveTestSize + m_numberOfCurveSamples, 4); // Draw envelope plananars glDisable(GL_CULL_FACE ); glUniform4fv( m_colLoc, 1, glm::value_ptr(vec4(0.5, 1.0, 0.5, 0.1) )); glDrawArrays( GL_TRIANGLES, 0, 3); glDrawArrays( GL_TRIANGLES, 1, 3); glEnable(GL_CULL_FACE ); glEnable(GL_DEPTH_TEST); glBindVertexArray( 0 ); }
void dgCollisionInstance::CalculateBuoyancyAcceleration (const dgMatrix& matrix, const dgVector& origin, const dgVector& gravity, const dgVector& fluidPlane, dgFloat32 fluidDensity, dgFloat32 fluidViscosity, dgVector& unitForce, dgVector& unitTorque) { dgMatrix globalMatrix (m_localMatrix * matrix); unitForce = dgVector (dgFloat32 (0.0f)); unitTorque = dgVector (dgFloat32 (0.0f)); dgVector volumeIntegral (m_childShape->CalculateVolumeIntegral (globalMatrix, fluidPlane, *this)); if (volumeIntegral.m_w > dgFloat32 (0.0f)) { dgVector buoyanceCenter (volumeIntegral - origin); dgVector force (gravity.Scale (-fluidDensity * volumeIntegral.m_w)); dgVector torque (buoyanceCenter.CrossProduct(force)); unitForce += force; unitTorque += torque; } }
void Canvas::updateVertexBuffer(std::vector<Sprite*> &sprites) { vector<vec3> position; vector<vec2> textureCoords; vector<vec4> colors; vector<GLushort> indices; sort(sprites.begin(), sprites.end(), [](Sprite* s1, Sprite* s2){ return s1->order() < s2->order(); }); unsigned short index = 0; for (unsigned short i=0;i<sprites.size();i++){ auto sprite = sprites[i]; auto transform = sprite->transform(); vec2 size = (vec2) sprite->textureAtlas()->textureSize(); mat4 toWorld = transform->globalMatrix(); vec2 scale = sprite->scale(); TextureAtlasEntry entry = sprite->entry(); Bounds2 bounds = sprite->trimmedBounds(); vec4 color = sprite->color(); if (sprite->type() == SpriteType::Simple) { position.push_back((vec3) (toWorld * vec4{bounds.lowLeft(), 0, 1})); position.push_back((vec3) (toWorld * vec4{bounds.lowRight(), 0, 1})); position.push_back((vec3) (toWorld * vec4{bounds.upperRight(), 0, 1})); position.push_back((vec3) (toWorld * vec4{bounds.upperLeft(), 0, 1})); vec2 min{entry.frame.x / size.x, 1.0 - (entry.frame.y + entry.frame.w) / size.y}; vec2 max{(entry.frame.x + entry.frame.z) / size.x, 1.0 - entry.frame.y / size.y}; textureCoords.push_back(vec2{min.x, min.y}); textureCoords.push_back(vec2{max.x, min.y}); textureCoords.push_back(vec2{max.x, max.y}); textureCoords.push_back(vec2{min.x, max.y}); colors.push_back(color); colors.push_back(color); colors.push_back(color); colors.push_back(color); // push two triangles indices.push_back(index); indices.push_back(index + 1); indices.push_back(index + 2); indices.push_back(index + 2); indices.push_back(index + 3); indices.push_back(index); index+=4; } else if (sprite->type() == SpriteType::Sliced) { vec2 dim {entry.frame.z,entry.frame.w}; vec4 sliceX = vec4{bounds.min.x, bounds.min.x + dim.x * sprite->sliceX()[0],bounds.max.x - dim.x * (1.0- sprite->sliceX()[1]), bounds.max.x}; vec4 sliceY = vec4{bounds.min.y, bounds.min.y + dim.y * sprite->sliceY()[0],bounds.max.y - dim.y * (1.0- sprite->sliceY()[1]), bounds.max.y}; vec2 min{entry.frame.x / size.x, 1.0 - (entry.frame.y + entry.frame.w) / size.y}; vec2 max{(entry.frame.x + entry.frame.z) / size.x, 1.0 - entry.frame.y / size.y}; vec4 uvX{min.x, lerp(min.x, max.x, sprite->sliceX()[0]),lerp(min.x, max.x, sprite->sliceX()[1]),max.x}; vec4 uvY{min.y, lerp(min.y, max.y, sprite->sliceY()[0]),lerp(min.y, max.y, sprite->sliceY()[1]),max.y}; for (int x=0;x<3;x++){ for (int y=0;y<3;y++){ position.push_back((vec3) (toWorld * vec4{sliceX[x], sliceY[y], 0, 1})); position.push_back((vec3) (toWorld * vec4{sliceX[x+1], sliceY[y], 0, 1})); position.push_back((vec3) (toWorld * vec4{sliceX[x+1], sliceY[y+1], 0, 1})); position.push_back((vec3) (toWorld * vec4{sliceX[x], sliceY[y+1], 0, 1})); textureCoords.push_back(vec2{uvX[x], uvY[y]}); textureCoords.push_back(vec2{uvX[x+1], uvY[y]}); textureCoords.push_back(vec2{uvX[x+1], uvY[y+1]}); textureCoords.push_back(vec2{uvX[x], uvY[y+1]}); colors.push_back(color); colors.push_back(color); colors.push_back(color); colors.push_back(color); // push two triangles indices.push_back(index); indices.push_back(index + 1); indices.push_back(index + 2); indices.push_back(index + 2); indices.push_back(index + 3); indices.push_back(index); index+=4; } } } } mMeshData->setPosition(position); mMeshData->setColor(colors); mMeshData->setTexCoord0(textureCoords); mMeshData->setSubmesh(0,indices, MeshType::Triangles); mMesh->setMeshData(mMeshData); }