本文整理汇总了C++中glm::translate方法的典型用法代码示例。如果您正苦于以下问题:C++ glm::translate方法的具体用法?C++ glm::translate怎么用?C++ glm::translate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类glm
的用法示例。
在下文中一共展示了glm::translate方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: rotate
mat4 const& Model::getModelMatrix() const
{
if (!validModelMatrix)
{
mat4 scaleMatrix = glm::scale(mat4(), scale);
mat4 rotateX = rotate(mat4(), rotation.x, vec3(1, 0, 0));
mat4 rotateY = rotate(mat4(), rotation.y, vec3(0, 1, 0));
mat4 rotateZ = rotate(mat4(), rotation.z, vec3(0, 0, 1));
mat4 translateMatrix = translate(mat4(), position);
modelMatrix = translateMatrix * rotateZ * rotateY * rotateX * scaleMatrix;
validModelMatrix = true;
validInverseModelMatrix = false;
}
return modelMatrix;
}
示例2: setViewFP
void setViewFP() {
using namespace glm;
glm::mat4 Vinv(1.0);
// right-left rotate
Vinv = glm::rotate(glm::mat4(1.0), g_CamangleY, glm::vec3(0.0f, 1, 0));
Vinv = glm::rotate(Vinv, g_CamangleX, glm::vec3(1, 0, 0));
// get lookat vec
vec4 t = Vinv * vec4(0, 0, -1, 1);
g_lookAt.x = t.x;
g_lookAt.y = t.y;
g_lookAt.z = t.z;
// S is translation with respect to Cam
glm::mat4 S = glm::translate(glm::mat4(1.0), glm::vec3(g_CamDx, g_CamDy, g_CamDz));
glm::mat4 V = glm::inverse(Vinv);
// S with respect to World
vec4 camPos4f = vec4(g_CamPos.x, g_CamPos.y, g_CamPos.z, 1.0f);
camPos4f = Vinv * S * V * camPos4f;
g_CamPos.x = camPos4f.x;
// == No player y movement
g_CamPos.y = camPos4f.y;
g_CamPos.z = camPos4f.z;
// printf("g_CamPos: %f, %f, %f\n", g_CamPos.x, g_CamPos.y, g_CamPos.z);
// xlate camera in world
Vinv = translate(mat4(1), g_CamPos) * Vinv;
//g_CamDz = g_CamDx = 0;
g_view = inverse(Vinv);
}
示例3: updateMatrices
void FilmCamera::updateMatrices()
{
// view matrix
untranslatedViewMatrix = glm::toMat4(getOrientation());
viewMatrix = translate(untranslatedViewMatrix, -position);
// projection matrix
aspectRatio = (float)getFrameWidth() / (float)getFrameHeight();
float extendedAOV = (atan(tan(angleOfView / 2) * (((float)width + 2 * horizontalMargin) / width))) * 2;
// as found in hearn & baker
float x0 = 1.0f/(tan(extendedAOV/2.0f));
float y1 = (1.0f/(tan(extendedAOV/2.0f))) * aspectRatio;
float z2 = (nearPlane + farPlane)/(nearPlane - farPlane);
float w2 = -1.0f;
float z3 = (-2.0f * nearPlane * farPlane)/(nearPlane - farPlane);
projectionMatrix = mat4(x0, 0, 0, 0,
0, y1, 0, 0,
0, 0, z2, w2,
0, 0, z3, 0);
}
示例4: addMirrorBox
/// <summary>
/// Adds a mirror box of a given size, centered at 0, with no back.
/// </summary>
/// <param name="wallSize">Size of the walls.</param>
void Scene::addMirrorBox(const float wallSize)
{
using glm::translate;
using glm::scale;
using glm::rotate;
int matIdx = materialsVec.size();
materialsVec.push_back(Material(vec3(1.0f, 1.0f, 0.8f), 0.7f)); //white (+0)
materialsVec.push_back(Material(vec3(1.0f, 0.0f, 0.0f), 0.7f)); //red (+1)
materialsVec.push_back(Material(vec3(0.0f, 1.0f, 0.0f), 0.7f)); //green (+2)
materialsVec.push_back(Material(vec3(1.0f, 1.0f, 1.0f))); //white light (+3)
materialsVec.push_back(Material(vec3(0.0f, 0.0f, 0.0f), 0.0f, vec3(1, 1, 1), INFINITY, .9f, 5.8f)); //mirror (+4)
materialsVec[matIdx + 4].flags |= MAT_FLAG_PURE_REFLECTION;
materialsVec.push_back(Material(vec3(1.0f, 0.6f, 1.0f))); //violet light
const float offset = wallSize / 2;
const mat4 scaleToWall = scale(vec3(wallSize, wallSize, wallSize));
//floor
mat4 trans = translate(vec3(0, -offset, -offset)) *
rotate(-(glm::mediump_float)90, vec3(1, 0, 0)) *
scaleToWall;
addRectangularModel(trans, matIdx);
//ceiling
trans = translate(vec3(0, offset, -offset)) *
rotate((glm::mediump_float)90, vec3(1, 0, 0)) *
scaleToWall;
addRectangularModel(trans, matIdx + 4);
//left wall
trans = translate(vec3(-offset + .2 * offset, 0, -offset)) *
rotate((glm::mediump_float)88, vec3(0, 1, 0)) *
scaleToWall;
addRectangularModel(trans, matIdx + 4);
//right wall
trans = translate(vec3(offset, 0, -offset)) *
rotate((glm::mediump_float) - 90, vec3(0, 1, 0)) *
scaleToWall;
addRectangularModel(trans, matIdx + 4);
//back wall
trans = translate(vec3(0, 0, -wallSize)) *
// rotate((glm::mediump_float)90, vec3(1, 0, 0)) *
scaleToWall;
addRectangularModel(trans, matIdx);
//front wall s
trans = translate(vec3(0, 0, 0)) *
rotate((glm::mediump_float)180, vec3(0, 1, 0)) *
scaleToWall;
addRectangularModel(trans, matIdx);
//light
float power = 400;
trans = translate(vec3(0, offset - 0.01f, -offset)) *
rotate((glm::mediump_float) 90, vec3(1, 0, 0)) *
scale(vec3(2.5f, 2.5f, 2.5f));
addAreaLight(trans, matIdx + 3, vec3(power / 4, power, power));
trans = translate(vec3(0, -offset + 0.01f, -offset)) *
rotate((glm::mediump_float) -90, vec3(1, 0, 0)) *
scale(vec3(1.5f, 1.5f, 1.5f));
addAreaLight(trans, matIdx + 5, vec3(power / 3, 0, power / 3));
}
示例5: InitializeGraph
bool SceneGraph::InitializeGraph()
{
// Create objects
// Add the objects into the actor map
// Once an object is in the SceneGraph, object destruction is responsibility of the SceneGraph
using AntiMatter::AppLog;
using std::string;
using std::vector;
using glm::mat4;
using glm::vec3;
using glm::translate;
using glm::rotate;
// just calling to test the WavefrontObj parser
// WavefrontObj j( g_Cfg.AssetsDir() + string("obj\\house\\house.obj") );
// Initialize the SceneLights collection (lights need access to the SceneGraph in order to initialize)
// because of this, light initialization can't be done through plain old RAII
if( ! m_lights.Initialize(this) )
{
AppLog::Ref().LogMsg("SceneGraph lights failed to initialize");
return false;
}
// add lights to the scene graph
typedef vector<Light*>::const_iterator CItr;
const vector<Light*> lights = m_lights.Lights();
for( CItr n = lights.begin(); n != lights.end(); n ++ )
{
Light* pLight = (*n);
m_map.insert( ActorIdPair( pLight->Id(), pLight ) );
}
mat4 tr;
mat4 mW(1.0);
// Terrain
Terrain* pTerrain = new (std::nothrow) Terrain(
this,
&m_RootNode,
string("terrain"),
g_Cfg.AssetsDir() + string("heightfield4.bmp"), // height map
g_Cfg.AssetsDir() + string("grassC.jpg"), // texture map (1 of n ?)
g_Cfg.AssetsDir() + string("256-circle-alphamap.bmp"), // alphamap
128,128,
4.0f, 4.0f, 0.1f
);
if( ! pTerrain || ! pTerrain->Initialized() )
{
AppLog::Ref().LogMsg("SceneGraph failed to initialize Terrain");
return false;
}
m_map.insert( ActorIdPair( pTerrain->Id(), pTerrain) );
tr = translate( mat4(1.0), vec3(0, -275, 0) );
pTerrain->GetNodeData().W() *= tr;
// Globe
Globe* pGlobe = new (std::nothrow) Globe(
this,
&m_RootNode,
std::string("globe"),
350.0f, 30, 30,
string(""),
glm::vec4(1.0, 1.0, 1.0, 0.8)
);
// position the globe
if( ! pGlobe || ! pGlobe->Initialized() )
{
AppLog::Ref().LogMsg("SceneGraph failed to intialize the Globe");
return false;
}
tr = translate( glm::mat4(1.0), vec3(0, 0, 0) );
pGlobe->GetNodeData().W() *= tr;
m_map.insert( ActorIdPair( pGlobe->Id(), pGlobe) );
// snow
Snow* pSnow = new (std::nothrow) Snow(
this,
&m_RootNode,
pGlobe,
string("snowfall"),
g_Cfg.SnowCfg()
);
if( ! pSnow || ! pSnow->Initialized() )
{
AppLog::Ref().LogMsg("SceneGraph failed to allocate heap for snow");
return false;
}
//.........这里部分代码省略.........
示例6: main
int main(int argc, char** argv) {
/* Initialize logging. */
START_EASYLOGGINGPP(argc, argv);
LOG(TRACE) << "Logging initialized.";
/* Initialize SDL. */
LOG(TRACE) << "Initializing SDL...";
int result = SDL_Init(SDL_INIT_VIDEO);
if (result != 0) {
LOG(FATAL) << "Could not initialize SDL: " << SDL_GetError();
}
window = SDL_CreateWindow(argv[0], 0, 0, (int)window_width,
(int)window_height,
SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
if (window == nullptr) {
LOG(FATAL) << "Could not create window: " << SDL_GetError();
}
SDL_ShowCursor(SDL_DISABLE);
SDL_SetRelativeMouseMode(SDL_TRUE);
frequency = SDL_GetPerformanceFrequency();
time_last_frame = SDL_GetPerformanceCounter();
LOG(INFO) << "Performance counter frequency: " << frequency;
/* Initialize OpenGL. */
LOG(TRACE) << "Initializing OpenGL...";
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GLContext gl_context = SDL_GL_CreateContext(window);
if (gl_context == nullptr) {
LOG(FATAL) << SDL_GetError();
}
glewExperimental = GL_TRUE;
GLenum glewError = glewInit();
if (glewError != GLEW_OK) {
LOG(FATAL) << "Could not initialize GLEW: " << glewGetErrorString(glewError);
}
glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glEnable(GL_VERTEX_ARRAY);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
/* Load assets. */
LOG(TRACE) << "Loading assets...";
shaders = new ShaderManager();
{
shader_terrain = shaders->get("terrain");
shader_terrain->apply();
glActiveTexture(GL_TEXTURE0);
texture_terrain = new Texture("terrain");
shader_terrain->updateUniform("tex", 0);
terrain = new Terrain("heightmap.png");
shader_terrain->updateUniform("max_height", terrain->getMaxHeight());
auto world = mat4();
world = translate(world, vec3(-terrain->getWidth() / 2.f,
-terrain->getMaxHeight() / 2.f,
-terrain->getDepth() / 2.f));
shader_terrain->updateUniform("K_a", 0.1f);
shader_terrain->updateUniform("K_d", 0.9f);
shader_terrain->updateWorldMatrix(world);
LOG(INFO) << "Maximum terrain height: " << terrain->getMaxHeight();
LOG(INFO) << "Terrain width: " << terrain->getWidth();
LOG(INFO) << "Terrain depth: " << terrain->getDepth();
} {
shader_skybox = shaders->get("skybox");
shader_skybox->apply();
glActiveTexture(GL_TEXTURE2);
environment_map = new CubeMap("terrain_");
shader_skybox->updateUniform("cubeMap", 2);
auto world = mat4();
world = scale(world, vec3(terrain->getWidth(),
terrain->getWidth(),
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);
//.........这里部分代码省略.........
示例7: translate
glm::mat4 FilmCamera::getTransformation() const
{
return translate(glm::mat4(), position);
}
示例8: initGround
void initGround() {
mat4 xl = translate(mat4(1.0f), vec3(0, -0.5, 0));
mat4 sc = glm::scale(mat4(1.0f), vec3(g_groundSize, 0.1, g_groundSize));
cout << "mat4 xl: \n";
printMat4(xl, stdout);
g_ground_xform = xl * sc;
// A x-z plane at y = g_groundY of dimension [-g_groundSize, g_groundSize]^2
float GrndPos[] = {
-g_groundSize, g_groundY, -g_groundSize,
-g_groundSize, g_groundY, g_groundSize,
g_groundSize, g_groundY, g_groundSize,
g_groundSize, g_groundY, -g_groundSize
};
float GrndNorm[] = {
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
};
unsigned short idx[] = {0, 2, 1, 0, 3, 2};
g_GiboLen = 6;
// position
glGenBuffers(1, &GrndBuffObj);
glBindBuffer(GL_ARRAY_BUFFER, GrndBuffObj);
glBufferData(GL_ARRAY_BUFFER, sizeof(GrndPos), GrndPos, GL_STATIC_DRAW);
// normals
glGenBuffers(1, &GNBuffObj);
glBindBuffer(GL_ARRAY_BUFFER, GNBuffObj);
glBufferData(GL_ARRAY_BUFFER, sizeof(GrndNorm), GrndNorm, GL_STATIC_DRAW);
// index array
glGenBuffers(1, &GIndxBuffObj);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, GIndxBuffObj);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(idx), idx, GL_STATIC_DRAW);
// setup ground vertex array
glGenVertexArrays(1, &groundVAO);
glBindVertexArray(groundVAO);
glBindBuffer(GL_ARRAY_BUFFER, GrndBuffObj);
glVertexAttribPointer(POS_LOCATION, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(POS_LOCATION);
glBindBuffer(GL_ARRAY_BUFFER, GNBuffObj);
glVertexAttribPointer(NORM_LOCATION, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(NORM_LOCATION);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, GIndxBuffObj);
glBindVertexArray(0);
}
示例9: loadMap
bool loadMap(const char* filename, list<ExportObject> &map) {
FILE * file = fopen(filename, "r");
if (NULL == file) {
fprintf(stderr, "Couldn't open %s for reading the map\n", filename);
return false;
}
int num_objs = 0;
fscanf(file, "%d ", &num_objs);
list<ExportObject>::iterator it;
ExportObject reconstructedObject;
char name[100];
int matnum;
mat4 rotmat;
while (num_objs--) {
fscanf(file, "%s %d\n", name, &matnum);
read3f(reconstructedObject.t, file);
printf("read t:\n");
fprint3f(reconstructedObject.t, stdout);
read3f(reconstructedObject.s, file);
printf("read s:\n");
fprint3f(reconstructedObject.s, stdout);
read3f(reconstructedObject.r, file);
printf("read r:\n");
fprint3f(reconstructedObject.r, stdout);
reconstructedObject.materialIndex = matnum;
reconstructedObject.name = name;
reconstructedObject.xt = scale(mat4(1.0), reconstructedObject.s) *
translate(mat4(1.0), reconstructedObject.t);
map.push_back(reconstructedObject);
}
/* with mats
while (num_objs--) {
fscanf(file, "%s %d\n", name, &matnum);
readMat4(reconstructedObject.xt, file);
printf("read mat4\n");
printMat4(reconstructedObject.xt, stdout);
reconstructedObject.materialIndex = matnum;
reconstructedObject.name = name;
map.push_back(reconstructedObject);
}
*/
fclose(file);
return true;
}