本文整理汇总了C++中glm::clamp方法的典型用法代码示例。如果您正苦于以下问题:C++ glm::clamp方法的具体用法?C++ glm::clamp怎么用?C++ glm::clamp使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类glm
的用法示例。
在下文中一共展示了glm::clamp方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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);
//.........这里部分代码省略.........