当前位置: 首页>>代码示例>>C++>>正文


C++ glm::normalize方法代码示例

本文整理汇总了C++中glm::normalize方法的典型用法代码示例。如果您正苦于以下问题:C++ glm::normalize方法的具体用法?C++ glm::normalize怎么用?C++ glm::normalize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在glm的用法示例。


在下文中一共展示了glm::normalize方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: fragment

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};
}
开发者ID:scottnm,项目名称:tinyrenderer,代码行数:26,代码来源:shaders.cpp

示例2: mouseMoveEvent

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;
}
开发者ID:joshatron,项目名称:CSCI-441-Labs,代码行数:25,代码来源:glwidget.cpp

示例3: phillips

/*
 * 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);
}
开发者ID:riskybacon,项目名称:simulation,代码行数:39,代码来源:ocean.cpp

示例4: sceneRender

void sceneRender()
{
    using glm::cross;
    using glm::clamp;
    using glm::vec3;
    using glm::normalize;

    // TODO: Save scene pixels as png
    int width = g_theScene->output_size.x;
    int height = g_theScene->output_size.y;
    const char* output = isEmpty(g_theScene->output) ? "out.png" : g_theScene->output;
    unsigned char* pixels = (unsigned char*) malloc(c_bpp * width * height);

    camera_t& cam = g_theScene->camera;
    float halfFov = cam.fov / 2;
    float tanFov = tan(halfFov);
    float halfWidth = width / 2.0f;
    float halfHeight = height / 2.0f;

    vec3 w = normalize(cam.look_from - cam.look_at);
    vec3 u = normalize(cross(cam.up, w));
    vec3 v = normalize(cross(u, w));

    ray_t ray;
    ray.origin = cam.look_from;

    ray_query_t best;

    float multi = tanFov / halfHeight;
    unsigned char* currentPixel = pixels;
    for (int y = 0; y < height; ++y)
    {
        float cy = halfHeight - (y + 0.5f);
        for (int x = 0; x < width; currentPixel += c_bpp, ++x)
        {
            // reset best query.
            best.obj = NULL;
            best.t = FLT_MAX;

            // Get Ray through pixel.
            float cx = (x + 0.5f) - halfWidth;
            float a = cx * multi;
            float b = cy * multi;

            vec3 rayDirection = normalize((a * u) + (b * v) - w);

            // Find intersection with scene.
            ray_query_t query;
            vec3* vertices = g_theScene->vertices;
            triangle_t* tri = g_theScene->triangles;
            for (int t = 0; t < g_theScene->triangle_count; ++t, ++tri)
            {
                glm::vec4 rayOriginT = tri->xform_inv * glm::vec4(cam.look_from, 1.0f);
                glm::vec4 rayDirectionT = tri->xform_inv * glm::vec4(rayDirection, 0.0f);

                ray.origin = vec3(rayOriginT.x, rayOriginT.y, rayOriginT.z);
                ray.direction = vec3(rayDirectionT.x, rayDirectionT.y, rayDirectionT.z);

                int* indices = tri->indicies;
                if (intersectRayTriangle(ray, vertices[indices[0]], vertices[indices[1]], vertices[indices[2]], query))
                {
                    query.obj = tri;
                    if (query.t < best.t) best = query;
                }
            }

            sphere_t* sph = g_theScene->spheres;
            for (int s = 0; s < g_theScene->sphere_count; ++s, ++sph)
            {
                glm::vec4 rayOriginT = sph->xform_inv * glm::vec4(cam.look_from, 1.0f);
                glm::vec4 rayDirectionT = sph->xform_inv * glm::vec4(rayDirection, 0.0f);

                ray.origin = vec3(rayOriginT.x, rayOriginT.y, rayOriginT.z);
                ray.direction = vec3(rayDirectionT.x, rayDirectionT.y, rayDirectionT.z);
                
                if (intersectRaySphere(ray, *sph, query))
                {
                    query.obj = sph;
                    if (query.t < best.t) best = query;
                }
            }

            // TODO: Light object
            if (best.obj != NULL)
            {
                material_t& mat = best.obj->material;
                vec3 color = mat.ambient;

                // final color conversion.
                currentPixel[0] = (unsigned char) (clamp(color.b, 0.0f, 1.0f) * 255.0f);
                currentPixel[1] = (unsigned char) (clamp(color.g, 0.0f, 1.0f) * 255.0f);
                currentPixel[2] = (unsigned char) (clamp(color.r, 0.0f, 1.0f) * 255.0f);
            }
        }
    }

    printf("Rendering scene to %s...\n", output);
    FIBITMAP *img = FreeImage_ConvertFromRawBits(pixels, width, height, width * c_bpp, c_bpp * 8, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, false);
    FreeImage_Save(FIF_PNG, img, isEmpty(g_theScene->output) ? "out.png" : g_theScene->output, 0);

//.........这里部分代码省略.........
开发者ID:mattrudder,项目名称:raytracer-cs184,代码行数:101,代码来源:scene.cpp

示例5: main


//.........这里部分代码省略.........
                                  terrain->getDepth()));
        skybox = new Cube();
        shader_terrain->apply();
        shader_skybox->updateWorldMatrix(world);
    } {
        auto world = mat4();
        world = scale(world, vec3(terrain->getWidth() / 2.f,
                                  terrain->getMaxHeight() * 2.f,
                                  terrain->getDepth() / 2.f));
        shader_colour = shaders->get("colour");
        shader_colour->apply();
        shader_colour->updateWorldMatrix(world);
        origin = new Origin();
    } {
        auto world = mat4();
        world = translate(world, vec3(-terrain->getWidth() / 2.f,
                                      -terrain->getMaxHeight() / 2.f + 25.f,
                                      -terrain->getDepth() / 2.f));
        shader_water = shaders->get("water");
        shader_water->apply();
        shader_water->updateUniform("K_a", 0.1f);
        shader_water->updateUniform("K_d", 0.0f);
        shader_water->updateUniform("K_s", 0.9f);
        shader_water->updateWorldMatrix(world);
        water = new Grid(terrain->getDepth(), 1000.f);
    }


    /* Set up light. */
    auto light = Camera();
    light.eye = vec3(1024.0f, 1024.f, 1024.f);
    light.at = vec3(0.0f, 0.0f, 0.0f);
    light.up = vec3(0.0f, 0.0f, -1.0f);
    auto light_dir = normalize(vec3(0.f, 0.25f, -1.f));
    shader_terrain->apply();
    shader_terrain->updateUniform("light_dir", light_dir);
    shader_water->apply();
    shader_water->updateUniform("light_dir", light_dir);


    /* Set up view. */
    auto camera = Camera();
    auto camera_height = terrain->getMaxHeight() * 3.f;
    camera.eye = glm::vec3(0.f, camera_height, -terrain->getDepth() / 2.f);
    camera.at = glm::vec3(0, 0, 0);
    camera.up = glm::vec3(0, 1, 0);
    auto view = glm::lookAt(camera.eye, camera.at, camera.up);
    shaders->updateViewMatrices(view);


    /* Set up projection. */
    auto proj = glm::perspective(45.f, window_width / window_height, 100.f, 25000.f);
    shaders->updateProjectionMatrices(proj);


    /* Set up frame buffers. */
    frame_buffer_color = new ColorFrameBuffer(window_width, window_height);


    /* Main loop. */
    float angle = 0.0f;
    bool done = false;
    SDL_Event event;
    LOG(TRACE) << "Entering main loop...";
    while (!done) {
        while (SDL_PollEvent(&event) != 0) {
开发者ID:fluffels,项目名称:probable-octo-engine,代码行数:67,代码来源:main.C


注:本文中的glm::normalize方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。