osx - 2D Geometry Distorts When Translating -
i’ve project i’m using learn opengl on osx, found here. i’ve modified can start using own textures on simple, 2d geometry.
i’m running problem know asked often, can’t find solution works me.
i want translate geometry moves left, right, up, down, when it’s shape becomes distorted.
like this:
it should below moved on x axis:
the code i’m using is:
static glfloat modelview[16]; static glfloat projection[16]; static glfloat objectmvp[16]; static glfloat model[16]; static glfloat view[16]; mtxloadidentity(model); mtxloadidentity(view); mtxloadidentity(modelview); mtxloadidentity(projection); //load desired projection matrix projection var mtxloadperspective(projection, 90, m_viewaspect, 0.1, 100); //m_viewaspect updated every time window changes shape //and window.width/window.height mtxtranslateapply(model, 0.3, 0, -500); mtxscaleapply(model, m_viewaspect, 1, 1); //[*] mtxrotatexapply(model, 0); mtxrotateyapply(model, 0); mtxrotatezapply(model, 0); mtxscaleapply(model, 1/m_viewaspect, 1, 1); //[*] mtxmultiply(modelview, view, model); mtxmultiply(objectmvp, projection, modelview); //and send objectmvp shader rendering… gluniformmatrix4fv(m_uniformidx, 1, gl_false, objectmvp);
all mtx*
functions matrix maths indicated , place result first parameter passed. i’ve got assume work correctly unchanged original apple example project worked fine.
the mtxloadperspective()
function’s signature is:
void mtxloadperspective(float* mtx, float fov, float aspect, float nearz, float farz)
the 2 [*]
lines of code based on answers similar problems on other sites instructions scale in x axis according aspect ratio, rotation , scale back.
am right assume aspect ratio core of issue? , there example codes knows of guide me?
edit: shader uses column-major maths position
gl_position = modelviewprojectionmatrix * inposition;
as indicated answer.
edit: clarify mtx*
functions.
mtxloadperspective()
converts fov this:
float f = 1.0f / tanf( (fov * (m_pi/180)) / 2.0f);
the translate function is:
void mtxtranslateapply(float* mtx, float xtrans, float ytrans, float ztrans) { mtx[12] += mtx[0]*xtrans + mtx[4]*ytrans + mtx[ 8]*ztrans; mtx[13] += mtx[1]*xtrans + mtx[5]*ytrans + mtx[ 9]*ztrans; mtx[14] += mtx[2]*xtrans + mtx[6]*ytrans + mtx[10]*ztrans; }
the other functions are:
void mtxmultiply(float* ret, const float* lhs, const float* rhs) { ret[ 0] = lhs[ 0]*rhs[ 0] + lhs[ 4]*rhs[ 1] + lhs[ 8]*rhs[ 2] + lhs[12]*rhs[ 3]; ret[ 1] = lhs[ 1]*rhs[ 0] + lhs[ 5]*rhs[ 1] + lhs[ 9]*rhs[ 2] + lhs[13]*rhs[ 3]; ret[ 2] = lhs[ 2]*rhs[ 0] + lhs[ 6]*rhs[ 1] + lhs[10]*rhs[ 2] + lhs[14]*rhs[ 3]; ret[ 3] = lhs[ 3]*rhs[ 0] + lhs[ 7]*rhs[ 1] + lhs[11]*rhs[ 2] + lhs[15]*rhs[ 3]; ret[ 4] = lhs[ 0]*rhs[ 4] + lhs[ 4]*rhs[ 5] + lhs[ 8]*rhs[ 6] + lhs[12]*rhs[ 7]; ret[ 5] = lhs[ 1]*rhs[ 4] + lhs[ 5]*rhs[ 5] + lhs[ 9]*rhs[ 6] + lhs[13]*rhs[ 7]; ret[ 6] = lhs[ 2]*rhs[ 4] + lhs[ 6]*rhs[ 5] + lhs[10]*rhs[ 6] + lhs[14]*rhs[ 7]; ret[ 7] = lhs[ 3]*rhs[ 4] + lhs[ 7]*rhs[ 5] + lhs[11]*rhs[ 6] + lhs[15]*rhs[ 7]; ret[ 8] = lhs[ 0]*rhs[ 8] + lhs[ 4]*rhs[ 9] + lhs[ 8]*rhs[10] + lhs[12]*rhs[11]; ret[ 9] = lhs[ 1]*rhs[ 8] + lhs[ 5]*rhs[ 9] + lhs[ 9]*rhs[10] + lhs[13]*rhs[11]; ret[10] = lhs[ 2]*rhs[ 8] + lhs[ 6]*rhs[ 9] + lhs[10]*rhs[10] + lhs[14]*rhs[11]; ret[11] = lhs[ 3]*rhs[ 8] + lhs[ 7]*rhs[ 9] + lhs[11]*rhs[10] + lhs[15]*rhs[11]; ret[12] = lhs[ 0]*rhs[12] + lhs[ 4]*rhs[13] + lhs[ 8]*rhs[14] + lhs[12]*rhs[15]; ret[13] = lhs[ 1]*rhs[12] + lhs[ 5]*rhs[13] + lhs[ 9]*rhs[14] + lhs[13]*rhs[15]; ret[14] = lhs[ 2]*rhs[12] + lhs[ 6]*rhs[13] + lhs[10]*rhs[14] + lhs[14]*rhs[15]; ret[15] = lhs[ 3]*rhs[12] + lhs[ 7]*rhs[13] + lhs[11]*rhs[14] + lhs[15]*rhs[15]; } void mtxscaleapply(float* mtx, float xscale, float yscale, float zscale) { mtx[ 0] *= xscale; mtx[ 4] *= yscale; mtx[ 8] *= zscale; mtx[ 1] *= xscale; mtx[ 5] *= yscale; mtx[ 9] *= zscale; mtx[ 2] *= xscale; mtx[ 6] *= yscale; mtx[10] *= zscale; mtx[ 3] *= xscale; mtx[ 7] *= yscale; mtx[11] *= xscale; } void mtxrotatexmatrix(float* mtx, float rad) { float cosrad = cosf(rad); float sinrad = sinf(rad); float mtx01 = mtx[ 1]; float mtx05 = mtx[ 5]; float mtx09 = mtx[ 9]; float mtx13 = mtx[13]; mtx[ 1] = cosrad*mtx01 - sinrad*mtx[ 2]; mtx[ 2] = sinrad*mtx01 + cosrad*mtx[ 2]; mtx[ 5] = cosrad*mtx05 - sinrad*mtx[ 6]; mtx[ 6] = sinrad*mtx05 + cosrad*mtx[ 6]; mtx[ 9] = cosrad*mtx09 - sinrad*mtx[10]; mtx[10] = sinrad*mtx09 + cosrad*mtx[10]; mtx[13] = cosrad*mtx13 - sinrad*mtx[14]; mtx[14] = sinrad*mtx13 + cosrad*mtx[14]; } void mtxrotateymatrix(float* mtx, float rad) { float cosrad = cosf(rad); float sinrad = sinf(rad); float mtx00 = mtx[ 0]; float mtx04 = mtx[ 4]; float mtx08 = mtx[ 8]; float mtx12 = mtx[12]; mtx[ 0] = cosrad*mtx00 - sinrad*mtx[ 2]; mtx[ 2] = sinrad*mtx00 + cosrad*mtx[ 2]; mtx[ 4] = cosrad*mtx04 - sinrad*mtx[ 6]; mtx[ 6] = sinrad*mtx04 + cosrad*mtx[ 6]; mtx[ 8] = cosrad*mtx08 - sinrad*mtx[10]; mtx[10] = sinrad*mtx08 + cosrad*mtx[10]; mtx[12] = cosrad*mtx12 - sinrad*mtx[14]; mtx[14] = sinrad*mtx12 + cosrad*mtx[14]; } void mtxrotatezmatrix(float* mtx, float rad) { float cosrad = cosf(rad); float sinrad = sinf(rad); float mtx00 = mtx[ 0]; float mtx04 = mtx[ 4]; float mtx08 = mtx[ 8]; float mtx12 = mtx[12]; mtx[ 0] = cosrad*mtx00 - sinrad*mtx[ 1]; mtx[ 1] = sinrad*mtx00 + cosrad*mtx[ 1]; mtx[ 4] = cosrad*mtx04 - sinrad*mtx[ 5]; mtx[ 5] = sinrad*mtx04 + cosrad*mtx[ 5]; mtx[ 8] = cosrad*mtx08 - sinrad*mtx[ 9]; mtx[ 9] = sinrad*mtx08 + cosrad*mtx[ 9]; mtx[12] = cosrad*mtx12 - sinrad*mtx[13]; mtx[13] = sinrad*mtx12 + cosrad*mtx[13]; }
well,
i got code work, i'm not clear why.
in general, overall manner, loaded identity matrices view
, model
, , projection
.
the x, y, z scaling applied model
matrix.
the x, y, z rotation applied model
matrix.
the x , y translation applied model
well.
the z translation, however, applied view
matrix (although understand concept of moving camera see geometry, don't see why applying x , y translations view
matrix nothing).
i multiplication model * view
, goes modelview
matrix.
then modelview * projection
, product sent shader.
if comment , shed light on i'd grateful, in meantime i'll blunder on.
Comments
Post a Comment