Code samplesCompute a triangle normal:- #include <glm/vec3.hpp>
- #include <glm/geometric.hpp>
- void computeNormal(triangle & Triangle)
-
{
-
glm::vec3 const & a = Triangle.Position[0];
-
glm::vec3 const & b = Triangle.Position[1];
-
glm::vec3 const & c = Triangle.Position[2];
-
Triangle.Normal = glm::normalize(glm::cross(c - a, b - a));
-
}
Matrix transform:- #include <glm/vec3.hpp>
- #include <glm/vec4.hpp>
- #include <glm/mat4x4.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- #include <glm/gtc/type_ptr.hpp>
- void func(GLuint LocationMVP, float Translate, glm::vec2 const & Rotate)
-
{
-
glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 4.0f / 3.0f, 0.1f, 100.f);
-
glm::mat4 ViewTranslate = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -Translate));
-
glm::mat4 ViewRotateX = glm::rotate(ViewTranslate, Rotate.y, glm::vec3(-1.0f, 0.0f, 0.0f));
-
glm::mat4 View = glm::rotate(ViewRotateX, Rotate.x, glm::vec3(0.0f, 1.0f, 0.0f));
-
glm::mat4 Model = glm::scale(glm::mat4(1.0f), glm::vec3(0.5f));
-
glm::mat4 MVP = Projection * View * Model;
-
glUniformMatrix4fv(LocationMVP, 1, GL_FALSE, glm::value_ptr(MVP));
-
}
Vector types:- #include <glm/vec2.hpp>
- #include <glm/packing.hpp>
- #include <glm/integer.hpp>
- #include <glm/gtc/type_precision.hpp>
-
std::size_t const VertexCount = 4;
-
std::size_t const PositionSizeF32 = VertexCount * sizeof(glm::vec2);
-
glm::vec2 const PositionDataF32[VertexCount] =
-
{
-
glm::vec2(-1.0f,-1.0f),
-
glm::vec2( 1.0f,-1.0f),
-
glm::vec2( 1.0f, 1.0f),
-
glm::vec2(-1.0f, 1.0f)
-
};
-
std::size_t const PositionSizeF16 = VertexCount * sizeof(glm::uint);
-
glm::uint const PositionDataF16[VertexCount] =
-
{
-
glm::uint(glm::packUnorm2x16(glm::vec2(-1.0f, -1.0f))),
-
glm::uint(glm::packUnorm2x16(glm::vec2( 1.0f, -1.0f)),
-
glm::uint(glm::packUnorm2x16(glm::vec2( 1.0f, 1.0f))),
-
glm::uint(glm::packUnorm2x16(glm::vec2(-1.0f, 1.0f)))
-
};
-
std::size_t const PositionSizeI8 = VertexCount * sizeof(glm::i8vec2);
-
glm::i8vec2 const PositionDataI8[VertexCount] =
-
{
-
glm::i8vec2(-1,-1),
-
glm::i8vec2( 1,-1),
-
glm::i8vec2( 1, 1),
-
glm::i8vec2(-1, 1)
-
};
-
std::size_t const PositionSizeI32 = VertexCount * sizeof(glm::i32vec2);
-
glm::i32vec2 const PositionDataI32[VertexCount] =
-
{
-
glm::i32vec2 (-1,-1),
-
glm::i32vec2 ( 1,-1),
-
glm::i32vec2 ( 1, 1),
-
glm::i32vec2 (-1, 1)
-
};
Lighting:- #include <glm/vec3.hpp>
- #include <glm/geometric.hpp>
- #include <glm/exponential.hpp>
- #include <glm/gtc/random.hpp>
-
glm::vec3 lighting
-
(
-
intersection const & Intersection,
-
material const & Material,
-
light const & Light,
-
glm::vec3 const & View
-
)
-
{
-
glm::vec3 Color(0.0f);
-
glm::vec3 LightVector(glm::normalize(
-
Light.position() - Intersection.globalPosition() +
-
glm::vecRand3(0.0f, Light.inaccuracy()));
- if(!shadow(Intersection.globalPosition(), Light.position(), LightVector))
-
{
- float Diffuse = glm::dot(Intersection.normal(), LightVector);
- if(Diffuse <= 0.0f)
- return Color;
- if(Material.isDiffuse())
-
Color += Light.color() * Material.diffuse() * Diffuse;
- if(Material.isSpecular())
-
{
-
glm::vec3 Reflect(glm::reflect(
-
glm::normalize(-LightVector),
-
glm::normalize(Intersection.normal())));
- float Dot = glm::dot(Reflect, View);
- float Base = Dot > 0.0f ? Dot : 0.0f;
- float Specular = glm::pow(Base, Material.exponent());
-
Color += Material.specular() * Specular;
-
}
-
}
- return Color;
-
}
|