void TextureProjection::emitTextureCoordinates (std::size_t width, std::size_t height, Winding& w, const Vector3& normal, const Matrix4& localToWorld) { if (w.size() < 3) { return; } Matrix4 local2tex = m_texdef.getTransform((float) width, (float) height); { Matrix4 xyz2st; // we don't care if it's not normalised... basisForNormal(matrix4_transformed_direction(localToWorld, normal), xyz2st); matrix4_multiply_by_matrix4(local2tex, xyz2st); } Vector3 tangent(local2tex.getTransposed().x().getVector3().getNormalised()); Vector3 bitangent(local2tex.getTransposed().y().getVector3().getNormalised()); matrix4_multiply_by_matrix4(local2tex, localToWorld); for (Winding::iterator i = w.begin(); i != w.end(); ++i) { Vector3 texcoord = matrix4_transformed_point(local2tex, (*i).vertex); (*i).texcoord[0] = texcoord[0]; (*i).texcoord[1] = texcoord[1]; (*i).tangent = tangent; (*i).bitangent = bitangent; } }
// Setup lighting void OpenGLShaderPass::setUpLightingCalculation(OpenGLState& current, const RendererLight* light, const Vector3& viewer, const Matrix4& objTransform) { assert(light); // Get the light shader and examine its first (and only valid) layer MaterialPtr lightShader = light->getShader()->getMaterial(); if (lightShader->firstLayer() != 0) { // Calculate viewer location in object space Matrix4 inverseObjTransform = objTransform.getInverse(); Vector3 osViewer = matrix4_transformed_point( inverseObjTransform, viewer ); // Get the XY and Z falloff texture numbers. GLuint attenuation_xy = lightShader->firstLayer()->getTexture()->getGLTexNum(); GLuint attenuation_z = lightShader->lightFalloffImage()->getGLTexNum(); // Bind the falloff textures assert(current.renderFlags & RENDER_TEXTURE_2D); setTextureState( current.texture3, attenuation_xy, GL_TEXTURE3, GL_TEXTURE_2D ); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); setTextureState( current.texture4, attenuation_z, GL_TEXTURE4, GL_TEXTURE_2D ); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER); // Get the world-space to light-space transformation matrix Matrix4 world2light = light->getLightTextureTransformation(); // Set the ambient factor - 1.0 for an ambient light, 0.0 for normal light float ambient = 0.0; if (lightShader->isAmbientLight()) ambient = 1.0; // Bind the GL program parameters current.glProgram->applyRenderParams( osViewer, objTransform, light->getLightOrigin(), light->colour(), world2light, ambient ); } }
void TextureProjection::fitTexture (std::size_t width, std::size_t height, const Vector3& normal, const Winding& w, float s_repeat, float t_repeat) { if (w.size() < 3) { return; } Matrix4 st2tex = m_texdef.getTransform((float) width, (float) height); // the current texture transform Matrix4 local2tex = st2tex; { Matrix4 xyz2st; basisForNormal(normal, xyz2st); matrix4_multiply_by_matrix4(local2tex, xyz2st); } // the bounds of the current texture transform AABB bounds; for (Winding::const_iterator i = w.begin(); i != w.end(); ++i) { Vector3 texcoord = matrix4_transformed_point(local2tex, (*i).vertex); aabb_extend_by_point_safe(bounds, texcoord); } bounds.origin.z() = 0; bounds.extents.z() = 1; // the bounds of a perfectly fitted texture transform AABB perfect(Vector3(s_repeat * 0.5, t_repeat * 0.5, 0), Vector3(s_repeat * 0.5, t_repeat * 0.5, 1)); // the difference between the current texture transform and the perfectly fitted transform Matrix4 matrix(Matrix4::getTranslation(bounds.origin - perfect.origin)); matrix4_pivoted_scale_by_vec3(matrix, bounds.extents / perfect.extents, perfect.origin); matrix4_affine_invert(matrix); // apply the difference to the current texture transform matrix4_premultiply_by_matrix4(st2tex, matrix); setTransform((float) width, (float) height, st2tex); m_texdef.normalise((float) width, (float) height); }
Vector3 testMulti1(const Matrix4& a, const Vector3& b, const Vector3& c) { return vector3_added(matrix4_transformed_point(matrix4_transposed(a), b), c); }
Vector3 testMatrixMultiplied1(const Vector3& a, const Matrix4& b) { return matrix4_transformed_point(b, vector3_added(a, Vector3(1, 0, 0))); }