void Mesh::RenderAAQuadAlongXNinePatch(const vec3 &bottom_left, const vec3 &top_right, const vec2i &texture_size, const vec4 &patch_info) { static const Attribute format[] = {kPosition3f, kTexCoord2f, kEND}; static const unsigned short indices[] = { 0, 2, 1, 1, 2, 3, 2, 4, 3, 3, 4, 5, 4, 6, 5, 5, 6, 7, 1, 3, 8, 8, 3, 9, 3, 5, 9, 9, 5, 10, 5, 7, 10, 10, 7, 11, 8, 9, 12, 12, 9, 13, 9, 10, 13, 13, 10, 14, 10, 11, 14, 14, 11, 15, }; auto max = vec2::Max(bottom_left.xy(), top_right.xy()); auto min = vec2::Min(bottom_left.xy(), top_right.xy()); auto p0 = vec2(texture_size) * patch_info.xy() + min; auto p1 = max - vec2(texture_size) * (mathfu::kOnes2f - patch_info.zw()); // Check if the 9 patch edges are not overwrapping. // In that case, adjust 9 patch geometry locations not to overwrap. if (p0.x() > p1.x()) { p0.x() = p1.x() = (min.x() + max.x()) / 2; } if (p0.y() > p1.y()) { p0.y() = p1.y() = (min.y() + max.y()) / 2; } // vertex format is [x, y, z] [u, v]: float z = bottom_left.z(); // clang-format off const float vertices[] = { min.x(), min.y(), z, 0.0f, 0.0f, p0.x(), min.y(), z, patch_info.x(), 0.0f, min.x(), p0.y(), z, 0.0f, patch_info.y(), p0.x(), p0.y(), z, patch_info.x(), patch_info.y(), min.x(), p1.y(), z, 0.0, patch_info.w(), p0.x(), p1.y(), z, patch_info.x(), patch_info.w(), min.x(), max.y(), z, 0.0, 1.0, p0.x(), max.y(), z, patch_info.x(), 1.0, p1.x(), min.y(), z, patch_info.z(), 0.0f, p1.x(), p0.y(), z, patch_info.z(), patch_info.y(), p1.x(), p1.y(), z, patch_info.z(), patch_info.w(), p1.x(), max.y(), z, patch_info.z(), 1.0f, max.x(), min.y(), z, 1.0f, 0.0f, max.x(), p0.y(), z, 1.0f, patch_info.y(), max.x(), p1.y(), z, 1.0f, patch_info.w(), max.x(), max.y(), z, 1.0f, 1.0f, }; // clang-format on Mesh::RenderArray(kTriangles, 6 * 9, format, sizeof(float) * 5, reinterpret_cast<const char *>(vertices), indices); }
bool GroundRenderer::setup(const mat4& projection, unsigned int texture) { projection_ = projection; texture_ = texture; // Program set up static const vec4 materialDiffuse(0.3f, 0.3f, 0.3f, 1.0f); static const string vtx_shader_filename(GLMARK_DATA_PATH"/shaders/shadow.vert"); static const string frg_shader_filename(GLMARK_DATA_PATH"/shaders/shadow.frag"); ShaderSource vtx_source(vtx_shader_filename); ShaderSource frg_source(frg_shader_filename); vtx_source.add_const("MaterialDiffuse", materialDiffuse); if (!Scene::load_shaders_from_strings(program_, vtx_source.str(), frg_source.str())) { return false; } positionLocation_ = program_["position"].location(); // Set up the position data for our "quad". vertices_.push_back(vec2(-1.0, -1.0)); vertices_.push_back(vec2(1.0, -1.0)); vertices_.push_back(vec2(-1.0, 1.0)); vertices_.push_back(vec2(1.0, 1.0)); // Set up the VBO and stash our position data in it. glGenBuffers(1, &bufferObject_); glBindBuffer(GL_ARRAY_BUFFER, bufferObject_); glBufferData(GL_ARRAY_BUFFER, vertices_.size() * sizeof(vec2), &vertices_.front(), GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); // Set up the light matrix with a bias that will convert values // in the range of [-1, 1] to [0, 1)], then add in the projection // and the "look at" matrix from the light position. light_ *= LibMatrix::Mat4::translate(0.5, 0.5, 0.5); light_ *= LibMatrix::Mat4::scale(0.5, 0.5, 0.5); light_ *= projection_; light_ *= LibMatrix::Mat4::lookAt(lightPosition.x(), lightPosition.y(), lightPosition.z(), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); return true; }
void Graphics::setFloat4(ConstantLocation position, vec4 value) { setFloat4(position, value.x(), value.y(), value.z(), value.w()); }
void RefractPrivate::draw() { // To perform the depth pass, set up the model-view transformation so // that we're looking at the horse from the light position. That will // give us the appropriate view for the shadow. modelview_.push(); modelview_.loadIdentity(); modelview_.lookAt(lightPosition.x(), lightPosition.y(), lightPosition.z(), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); modelview_.rotate(rotation_, 0.0f, 1.0f, 0.0f); if (orientModel_) { modelview_.rotate(orientationAngle_, orientationVec_.x(), orientationVec_.y(), orientationVec_.z()); } mat4 mvp(projection_.getCurrent()); mvp *= modelview_.getCurrent(); modelview_.pop(); // Enable the depth render target with our transformation and render. depthTarget_.enable(mvp); vector<GLint> attrib_locations; attrib_locations.push_back(depthTarget_.program()["position"].location()); attrib_locations.push_back(depthTarget_.program()["normal"].location()); mesh_.set_attrib_locations(attrib_locations); if (useVbo_) { mesh_.render_vbo(); } else { mesh_.render_array(); } depthTarget_.disable(); // Draw the "normal" view of the horse modelview_.push(); modelview_.translate(-centerVec_.x(), -centerVec_.y(), -(centerVec_.z() + 2.0 + radius_)); modelview_.rotate(rotation_, 0.0f, 1.0f, 0.0f); if (orientModel_) { modelview_.rotate(orientationAngle_, orientationVec_.x(), orientationVec_.y(), orientationVec_.z()); } mvp = projection_.getCurrent(); mvp *= modelview_.getCurrent(); program_.start(); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, depthTarget_.depthTexture()); program_["DistanceMap"] = 0; glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, depthTarget_.colorTexture()); program_["NormalMap"] = 1; glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, texture_); program_["ImageMap"] = 2; // Load both the modelview*projection as well as the modelview matrix itself program_["ModelViewProjectionMatrix"] = mvp; program_["ModelViewMatrix"] = modelview_.getCurrent(); // Load the NormalMatrix uniform in the shader. The NormalMatrix is the // inverse transpose of the model view matrix. mat4 normal_matrix(modelview_.getCurrent()); normal_matrix.inverse().transpose(); program_["NormalMatrix"] = normal_matrix; program_["LightMatrix"] = light_; attrib_locations.clear(); attrib_locations.push_back(program_["position"].location()); attrib_locations.push_back(program_["normal"].location()); mesh_.set_attrib_locations(attrib_locations); if (useVbo_) { mesh_.render_vbo(); } else { mesh_.render_array(); } // Per-frame cleanup modelview_.pop(); }
bool RefractPrivate::setup(map<string, Scene::Option>& options) { // Program object setup static const string vtx_shader_filename(GLMARK_DATA_PATH"/shaders/light-refract.vert"); static const string frg_shader_filename(GLMARK_DATA_PATH"/shaders/light-refract.frag"); static const vec4 lightColor(0.4, 0.4, 0.4, 1.0); ShaderSource vtx_source(vtx_shader_filename); ShaderSource frg_source(frg_shader_filename); frg_source.add_const("LightColor", lightColor); frg_source.add_const("LightSourcePosition", lightPosition); float refractive_index(Util::fromString<float>(options["index"].value)); frg_source.add_const("RefractiveIndex", refractive_index); if (!Scene::load_shaders_from_strings(program_, vtx_source.str(), frg_source.str())) { return false; } const string& whichTexture(options["texture"].value); if (!Texture::load(whichTexture, &texture_, GL_LINEAR, GL_LINEAR, 0)) return false; // Model setup Model model; const string& whichModel(options["model"].value); bool modelLoaded = model.load(whichModel); if(!modelLoaded) return false; // Now that we're successfully loaded, there are a few quirks about // some of the known models that we need to account for. The draw // logic for the scene wants to rotate the model around the Y axis. // Most of our models are described this way. Some need adjustment // (an additional rotation that gets the model into the correct // orientation). // // Here's a summary: // // Angel rotates around the Y axis // Armadillo rotates around the Y axis // Buddha rotates around the X axis // Bunny rotates around the Y axis // Dragon rotates around the X axis // Horse rotates around the Y axis if (whichModel == "buddha" || whichModel == "dragon") { orientModel_ = true; orientationAngle_ = -90.0; orientationVec_ = vec3(1.0, 0.0, 0.0); } else if (whichModel == "armadillo") { orientModel_ = true; orientationAngle_ = 180.0; orientationVec_ = vec3(0.0, 1.0, 0.0); } if (model.needNormals()) model.calculate_normals(); // Mesh setup vector<std::pair<Model::AttribType, int> > attribs; attribs.push_back(std::pair<Model::AttribType, int>(Model::AttribTypePosition, 3)); attribs.push_back(std::pair<Model::AttribType, int>(Model::AttribTypeNormal, 3)); model.convert_to_mesh(mesh_, attribs); useVbo_ = (options["use-vbo"].value == "true"); bool interleave = (options["interleave"].value == "true"); mesh_.vbo_update_method(Mesh::VBOUpdateMethodMap); mesh_.interleave(interleave); if (useVbo_) { mesh_.build_vbo(); } else { mesh_.build_array(); } // Calculate a projection matrix that is a good fit for the model vec3 maxVec = model.maxVec(); vec3 minVec = model.minVec(); vec3 diffVec = maxVec - minVec; centerVec_ = maxVec + minVec; centerVec_ /= 2.0; float diameter = diffVec.length(); radius_ = diameter / 2; float fovy = 2.0 * atanf(radius_ / (2.0 + radius_)); fovy /= M_PI; fovy *= 180.0; float aspect(static_cast<float>(canvas_.width())/static_cast<float>(canvas_.height())); projection_.perspective(fovy, aspect, 2.0, 2.0 + diameter); // Set up the light matrix with a bias that will convert values // in the range of [-1, 1] to [0, 1)], then add in the projection // and the "look at" matrix from the light position. light_ *= LibMatrix::Mat4::translate(0.5, 0.5, 0.5); light_ *= LibMatrix::Mat4::scale(0.5, 0.5, 0.5); light_ *= projection_.getCurrent(); light_ *= LibMatrix::Mat4::lookAt(lightPosition.x(), lightPosition.y(), lightPosition.z(), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); if (!depthTarget_.setup(canvas_.width(), canvas_.height())) { Log::error("Failed to set up the render target for the depth pass\n"); return false; } return true; }
void ShadowPrivate::draw() { // To perform the depth pass, set up the model-view transformation so // that we're looking at the horse from the light position. That will // give us the appropriate view for the shadow. modelview_.push(); modelview_.loadIdentity(); modelview_.lookAt(lightPosition.x(), lightPosition.y(), lightPosition.z(), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); modelview_.rotate(rotation_, 0.0f, 1.0f, 0.0f); mat4 mvp(projection_.getCurrent()); mvp *= modelview_.getCurrent(); modelview_.pop(); // Enable the depth render target with our transformation and render. depthTarget_.enable(mvp); vector<GLint> attrib_locations; attrib_locations.push_back(depthTarget_.program()["position"].location()); attrib_locations.push_back(depthTarget_.program()["normal"].location()); mesh_.set_attrib_locations(attrib_locations); if (useVbo_) { mesh_.render_vbo(); } else { mesh_.render_array(); } depthTarget_.disable(); // Ground rendering using the above generated texture... ground_.draw(); // Draw the "normal" view of the horse modelview_.push(); modelview_.translate(-centerVec_.x(), -centerVec_.y(), -(centerVec_.z() + 2.0 + radius_)); modelview_.rotate(rotation_, 0.0f, 1.0f, 0.0f); mvp = projection_.getCurrent(); mvp *= modelview_.getCurrent(); program_.start(); program_["ModelViewProjectionMatrix"] = mvp; // Load the NormalMatrix uniform in the shader. The NormalMatrix is the // inverse transpose of the model view matrix. LibMatrix::mat4 normal_matrix(modelview_.getCurrent()); normal_matrix.inverse().transpose(); program_["NormalMatrix"] = normal_matrix; attrib_locations.clear(); attrib_locations.push_back(program_["position"].location()); attrib_locations.push_back(program_["normal"].location()); mesh_.set_attrib_locations(attrib_locations); if (useVbo_) { mesh_.render_vbo(); } else { mesh_.render_array(); } // Per-frame cleanup modelview_.pop(); }