Thursday, 15 August 2013

rotation - Opengl Vertices Camera orientation -



rotation - Opengl Vertices Camera orientation -

i working on 3d project using vertices, started simple glulookat in order have first person photographic camera moving in environment, utilize way :

glulookat(_position.x,_position.y,_position.z, _target.x,_target.y,_target.z, 0,0,1);

everything working fine, calculating target according position of mouse , angles (theta , phi), project moved on using vertices performance issues, had utilize same photographic camera these new objects, in order used glm library way :

glm::mat4 projection = glm::perspective(90.0f, 800.0f / 600.0f, 0.1f, 100.f); glm::mat4 view = glm::lookat( glm::vec3(position.x,position.y,position.z), glm::vec3(target.x,target.y,target.z), glm::vec3(0,0,1) ); glm::mat4 model = glm::mat4(1.0f); ! // our modelviewprojection : multiplication of our 3 matrices glm::mat4 mvp = projection * view * model; gluint matrixid = glgetuniformlocation(this->shaderprogram, "mvp");

here shader utilize :

const glchar* default_vertexsource = "#version 150 core\n" "in vec2 position;" "in vec3 color;" "out vec3 color;" "uniform vec3 translation;" "uniform mat4 rotation;" "uniform mat4 mvp;" "void main() {" " color = color;" " gl_position = mvp*rotation*vec4(position.x + translation.x, position.y + translation.y, 0.0 + translation.z, 1.0);" "}";

what happens my object's coordinate reference not same camera, drawn above on current x/z plan whereas should facing photographic camera on x/y plan.

from point of view seems missunderstanding how translation in opengl works.

glm::lookat returns 1 modelview matrix. -> how objects in scene translated according camera. in opengl not "move" camera, moving objects around camera. if have 2 objects in 0 0 0 (origin of world photographic camera system) , ur photographic camera @ eye(0,0,-3), center(0,0,0), up(0,1,0) , want move 1 object left , on object right need have matrix stack glm

std::stack<glm::mat4> glm_projectionmatrix;

here can force modelview photographic camera top object.

for object movemtn left side can utilize glm::translate, upload mat4 modelview matrix shader. (here not render scene)

reset top matrix modelview (eiter pop() if made re-create of top element before) or reset using glm::lookat. glm::translate in other direction.

in vertex shader need no vec3 translate or rotate.

you

gl_position = perspective * modelview *vec4(position,1);

this wil update 2 object accordingly given translation.

if want move objects around, update modelview matrix 1 object.

http://www.songho.ca/opengl/gl_transform.html

i hope can understand answer.

the basics of motion (rot , trans) in opengl matrix multipication. not need add together translation vec modelview in shader. glm can stuff in c++ code

math:

modelview 4*4 matrix if object should not rotated or translated modelview equals identity.

if want rotate object miltiply modelview matrix right 4*4 rotation matrix (see homogenous coordinates)

if want translate vertex multiply right translation matrix

lets x = (x,y,z,w) vertex

t translation , r rotation

a valid operation may looks this:

modelview * rotation * translation *v = v'

v' new position of point in 3d coordinate system

some examplecode can find here: http://incentivelabs.de/sourcecode/ see "teil 13" or later. if able understand german, can find tutorials on opengl c++ on youtube using channel-name "incentivelabs"

edit:

if want move object/rotate object using c++ glm

glm_projectionmatrix.top() = camera.getprojectionmat(); gluniformmatrix4fv(uniformlocations["projection"], 1, false,glm::value_ptr(glm_projectionmatrix.top())); glm_modelviewmatrix.top() = camera.getmodelviewmat(); glm_modelviewmatrix.top() = glm::translate( glm_modelviewmatrix.top(), glm::vec3(0.0f, 0.0f, -translate)); gluniformmatrix4fv(uniformlocations["modelview"], 1, false,glm::value_ptr(glm_modelviewmatrix.top()));

in glsl vertex shader:

gl_position = projection * modelview * vec4(vertexpos,1);

vertexpos attribute (vec3 position)

this codes moves vertices drawn after upload of modelview , projection shader same translation.

opengl rotation vertices

No comments:

Post a Comment