View Full Version : simple translational code giving what appear to be wrong answers

technologist

11-24-2017, 04:21 PM

using namespace glm;

int main()

{

glm::vec4 vec(1.0f, 0.0f, 0.0f, 1.0f);

glm::mat4 trans;

trans = glm::translate(trans, glm::vec3(1.0f, 1.0f, 0.0f));

vec = trans * vec;

std::cout << vec.x << vec.y << vec.z << std::endl;

std::cout<<"snippet is supposed to be 2 1 0";

return 0;

}

The result is supposed to be 2 1 0. Instead I get:

-1.18134e-0111.26952e+0349.56182e+033

What gives?

GClements

11-24-2017, 07:37 PM

I get 210. Does the code compile without warnings?

DragonautX

11-24-2017, 09:42 PM

I'm also getting 210. Here's my console output:

210

snippet is supposed to be 2 1 0

Your code looks right in printing out 210 for your vec printing line. Maybe a recompile will do? My only other thought is using a new set of glm include files, maybe you might've accidentally modified one of them.

technologist

11-25-2017, 11:09 AM

I'm also getting 210. Here's my console output:

Your code looks right in printing out 210 for your vec printing line. Maybe a recompile will do? My only other thought is using a new set of glm include files, maybe you might've accidentally modified one of them.

I've cleaned the worksite, restarted the IDE, and, restarted the machine. Same result. Open to any ideas.

UPDATE: ran the program on Qt - same result.

technologist

11-25-2017, 11:37 AM

#define GLM_ENABLE_EXPERIMENTAL

#include <iomanip>

#include <glm/glm.hpp>

#include <glm/gtc/matrix_transform.hpp>

#include <glm/gtx/transform.hpp>

#include "glm/gtx/string_cast.hpp"

#include <glm/gtc/type_ptr.hpp>

#include <iostream>

using namespace glm;

int main()

{

glm::vec4 vec(1.0f, 0.0f, 0.0f, 1.0f);

glm::mat4 trans;

trans = glm::translate(trans, glm::vec3(1.0f, 1.0f, 0.0f));

std::cout << vec.x << vec.y << vec.z << std::endl;

vec = trans * vec;

std::cout << vec.x << vec.y << vec.z << std::endl;

std::cout<<"snippet is supposed to be 2 1 0";

return 0;

}

doing a little debugging it looks like the numbers get trashed in this step:

vec = trans * vec;

I've put the extended includes for brevity; maybe something is happening there?

technologist

11-25-2017, 12:12 PM

Yay:

From the tutorial site:

Since GLM version 0.9.9, GLM default initializates matrix types to a 0-initalized matrix, instead of the identity matrix. From that version it is required to initialize matrix types as: glm::mat4 mat = glm::mat4(1.0f). For consistency with the tutorials' code it's advised to use a version of GLM lower than 0.9.9 or initialize all matrices as mentioned above.

using namespace glm;

int main()

{

glm::vec4 vec(1.0f, 0.0f, 0.0f, 1.0f);

std::cout << vec.x << vec.y << vec.z << std::endl;

glm::mat4 trans = glm::mat4(1.0);

trans = glm::translate(trans, glm::vec3(1.0f, 1.0f, 0.0f));

std::cout << vec.x << vec.y << vec.z << std::endl;

vec = trans * vec;

std::cout << vec.x << vec.y << vec.z << std::endl;

std::cout<<"snippet is supposed to be 2 1 0";

return 0;

}

output:

100

100

210

snippet is supposed to be 2 1 0

Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.