frag_color phong_tangent_space_shader::fragment( const vec3& bary, const mat3& verts, const mat3x2& tex_coords, const mat3& vert_norms) const { auto uv = bary_lerp(tex_coords[0], tex_coords[1], tex_coords[2], bary); auto objspace_norm = bary_lerp(vert_norms[0], vert_norms[1], vert_norms[2], bary); auto ts_normval = normalmap.get_from_ratio(uv[0], uv[1]); auto tanspace_norm = normalize(vec3(ts_normval.r - 127.5, ts_normval.g - 127.5, ts_normval.b - 127.5)); // objspace and texture space verts auto o1 = verts[1] - verts[0]; auto o2 = verts[2] - verts[0]; auto t1 = tex_coords[1] - tex_coords[0]; auto t2 = tex_coords[2] - tex_coords[0]; auto tanspace_mat = mat2(t1.x, t2.x, t1.y, t2.y); auto objspace_mat = mat3x2(o1.x, o2.x, o1.y, o2.y, o1.z, o2.z); // S * t1.x + T * t1.y = o1 // S * t2.x + T * t2.y = o2 // // [t1.x t1.y] * [tan = [o1.x o1.y o1.z] // t2.x t2.y] bitan] o2.x o2.y o2.z] // // tanspace_mat * tan_bitan_mat = objspace_mat // inverse(tanspace_mat) * tanspace_mat * tan_bitan_mat = inverse(tanspace_mat) * objspace_mat // I * tan_bitan_mat = inverse(tansapce_mat) * objspace_mat // tan_bitan_mat = inverse(tanspace_mat) * objspace_mat auto tan_bitan_mat = transpose(inverse(tanspace_mat) * objspace_mat); auto tan_to_objspace = mat3(normalize(tan_bitan_mat[0]), normalize(tan_bitan_mat[1]), normalize(objspace_norm)); auto norm = normalize(tan_to_objspace * tanspace_norm); // ambient color TGAColor ambient(5, 5, 5, 255); // specular color auto spec_val = specular.get_from_ratio(uv[0], uv[1]).raw[0]; auto r = normalize(reflect(light_dir(), norm)); auto spec_intensity = pow(max(0.0f, dot(r, to_cam)), spec_val); // diffuse color float diff_intensity = max(0.0f, dot(to_light(), norm)); auto diff_color = diffuse.get_from_ratio(uv[0], uv[1]); diff_color.scale(diff_intensity + spec_intensity * .6); return frag_color {ambient + diff_color, true}; }
frag_color phong_shader::fragment( const vec3& bary, const mat3& verts, const mat3x2& tex_coords, const mat3& vert_norms) const { auto uv = bary_lerp(tex_coords[0], tex_coords[1], tex_coords[2], bary); auto normval = normalmap.get_from_ratio(uv[0], uv[1]); auto norm = normalize(vec3(normval.r - 127.5, normval.g - 127.5, normval.b - 127.5)); // ambient color TGAColor ambient(5, 5, 5, 255); // specular color auto spec_val = specular.get_from_ratio(uv[0], uv[1]).raw[0]; auto r = normalize(reflect(light_dir(), norm)); auto spec_intensity = pow(max(0.0f, dot(r, to_cam)), spec_val); // diffuse color float diff_intensity = max(0.0f, dot(to_light(), norm)); auto diff_color = diffuse.get_from_ratio(uv[0], uv[1]); diff_color.scale(diff_intensity + spec_intensity * .6); return frag_color {ambient + diff_color, true}; }
float Line::DistancePointPlane(vec3 Point, Line Plane) { float sb, sn, sd; sn = -dot( Plane.m_directionVector, (Point - Plane.m_position)); sd = dot(Plane.m_directionVector, Plane.m_directionVector); sb = sn / sd; vec3 intersection = Point + sb * Plane.m_directionVector; return DistanceTwoPoints(Point, intersection); }
void GLWidget::mouseMoveEvent(QMouseEvent *event) { last.x = event->x(); last.y = event->y(); vec3 begin = pointOnVirtualTrackball(first); vec3 end = pointOnVirtualTrackball(last); float dotProduct = dot(normalize(begin), normalize(end)); float angle = acos(dotProduct); vec3 crossP = cross(begin, end); if(length(crossP) > .00001f) { rotationMatrix = rotate(mat4(1.0), angle, normalize(crossP)) * rotationMatrix; glUseProgram(cubeProg); glUniformMatrix4fv(cubeRotationMatrixLoc, 1, false, value_ptr(rotationMatrix)); glUseProgram(gridProg); glUniformMatrix4fv(gridRotationMatrixLoc, 1, false, value_ptr(rotationMatrix)); update(); } first.x = last.x; first.y = last.y; }
void operator()(Particle* p, const vec2& position, float& t1, float& t2){ const vec2 r = position - p->position; const float norm2 = dot(r,r); if (norm2 < kernelRadius2) { const float tmp = cst * pow(kernelRadius2-norm2,3); // POLY 6 // const float tmp = cst * pow(kernelRadius-sqrt(norm2),3); t1 += p->massDensity * tmp; t2 += tmp; } }
frag_color simple_texture_shader::fragment( const vec3& bary, const mat3& verts, const mat3x2& tex_coords, const mat3& vert_norms) const { auto norm = bary_lerp(vert_norms[0], vert_norms[1], vert_norms[2], bary); float intensity = max(0.0f, dot(to_light(), normalize(norm))); auto uv = bary_lerp(tex_coords[0], tex_coords[1], tex_coords[2], bary); auto color = tex.get_from_ratio(uv[0], uv[1]); color.scale(intensity); return frag_color {color, true}; }
void Line::ClosestPointsOnTwoLines(Line l1, Line l2, vec3 &closestPointLine1, vec3 &closestPointLine2) { float a = dot(l1.m_directionVector, l1.m_directionVector); float b = dot(l1.m_directionVector, l2.m_directionVector); float e = dot(l2.m_directionVector, l2.m_directionVector); float d = a*e - b*b; // if lines are not parallel if(d != 0) { vec3 r = l1.m_position - l2.m_position; float c = dot(l1.m_directionVector, r); float f = dot(l2.m_directionVector, r); float s = (b*f - c*e) / d; float t = (a*f - c*b) / d; closestPointLine1 = l1.m_position + l1.m_directionVector * s; closestPointLine2 = l2.m_position + l2.m_directionVector * t; //std::cout << DistanceTwoPoints(closestPointLine1, closestPointLine2) << std::endl; } }
frag_color normal_shader::fragment( const vec3& bary, const mat3& verts, const mat3x2& tex_coords, const mat3& vert_norms) const { auto uv = bary_lerp(tex_coords[0], tex_coords[1], tex_coords[2], bary); auto normval = normalmap.get_from_ratio(uv[0], uv[1]); auto normal = normalize(vec3(normval.r - 127.5, normval.g - 127.5, normval.b - 127.5)); float intensity = max(0.0f, dot(to_light(), normal)); vec3 norm_color = normalize(bary_lerp(vert_norms[0], vert_norms[1], vert_norms[2], bary)) * 255.0f; TGAColor c(abs(norm_color.x), abs(norm_color.y), abs(norm_color.z), 255); c.scale(intensity); return frag_color {c, true}; }
/* * Phillips wave spectrum, equation 40 with modification specified in equation 41 */ float Ocean::phillips(int n_prime, int m_prime) const { // Wavevector float kx = M_PI * (2 * n_prime - _N) / _length; float kz = M_PI * (2 * m_prime - _N) / _length; vec2 k(kx, kz); // Magnitude of wavevector float k_length = glm::length(k); // Wind speed float w_length = glm::length(_w); // If wavevector is very small, no need to calculate, just return zero if (k_length < 0.000001) return 0.0; // Precaculate k^2 and k^4 float k_length2 = k_length * k_length; float k_length4 = k_length2 * k_length2; // Cosine factor - eliminates waves that travel perpendicular to the wind float k_dot_w = dot(normalize(k), normalize(_w)); float k_dot_w2 = k_dot_w * k_dot_w; float L = w_length * w_length / _g; float L2 = L * L; // Something a bit extra to keep waves from exploding. Not in the paper. float damping = 0.001; float l2 = L2 * damping * damping; // Phillips spectrum as described in eqn 40 with modification described in 41 to // suppress very small waves that cause convergence problems return _A * (exp(-1.0f / (k_length2 * L2)) / k_length4) * k_dot_w2 * exp(-k_length2 * l2); }
float Line::LineAngle(Line l1, Line l2) { return acos( dot(l1.m_directionVector, l2.m_directionVector)/sqrt(glm::length2(l1.m_directionVector) * glm::length2(l2.m_directionVector)) ); }