c++ - Modern OpenGL Projection View Model transformation not working -
i tried use 1 transformation (and inverse transpose) in shader, shading comes out weird, guess normals transformed incorrectly. lot of online tutorials propose using "projection * view * model" three-stage transformation, , calculate light during "view * model" transformation stage. tried implement these 3 transformations, not teapot not show, simple triangle won't show.
here's vertex shader code:
#version 410 in vec3 position; // position of vertex (and fragment) in world space in vec3 normal; // surface normal vector in world space uniform mat4 model; uniform mat4 view; uniform mat4 proj; uniform mat4 modelview_it; vec3 l_pos = vec3(10, 10, 10); out vertexdata { vec3 normal; vec3 eye; vec3 lightdir; } vertexout; void main() { vec4 pos = view * model * vec4(position, 1.0); vertexout.normal = normalize(vec3(modelview_it * vec4(normal, 0.0))); vertexout.lightdir = l_pos - vec3(pos); vertexout.eye = vec3(0, 0, 0); gl_position = proj * view * model * vec4(position, 1); } and here's fragment shader code
#version 150 out vec4 colorout; vec3 kd = vec3(0.1, 1, 0.1); vec3 ka = vec3(1, 1.0, 1.0); vec3 ks = vec3(0.5, 1, 0.5); float sp = 90; vec3 intensity = vec3(1, 1, 1); in vertexdata { vec3 normal; vec3 eye; vec3 lightdir; } vertexin; void main() { vec3 n = normalize(vertexin.normal); vec3 l = normalize(vertexin.lightdir); vec3 e = normalize(vertexin.eye); vec3 diffuse = kd*intensity*max(0, dot(l, n)); vec3 r = -l + 2*dot(l,n) * n; vec3 specular = ks*intensity*max(pow(dot(r, e), sp),0); // force white intentionally test colorout = vec4(1,1,1,1); } here functions used generate projection , view transformation matrices (code taken other similar stack overflow questions)
//generate projection matrix void setperspective(float fovy, float aspect, float near, float far, matrix4f& mprojectionmatrix) { float theta = fovy*0.5; float range = far - near; float invtan = 1./tan(theta); mprojectionmatrix(0,0) = invtan / aspect; mprojectionmatrix(1,1) = invtan; mprojectionmatrix(2,2) = -(near + far) / range; mprojectionmatrix(3,2) = -1; mprojectionmatrix(2,3) = -2 * near * far / range; mprojectionmatrix(3,3) = 0; } //generate view matrix void lookat(const vector& position, const vector& target, const vector& up, matrix4f& mviewmatrix) { matrix3f r; r.col(2) = (position-target).normalized(); r.col(0) = up.cross(r.col(2)).normalized(); r.col(1) = r.col(2).cross(r.col(0)); mviewmatrix.topleftcorner<3,3>() = r.transpose(); mviewmatrix.toprightcorner<3,1>() = -r.transpose() * position; } here's how create , set uniforms
// create transformation matrices transform3faffine trans = identitytransform(); matrix4f viewmat = matrix4f::zero(); matrix4f projmat = matrix4f::zero(); setperspective(45.0f, //field of view 4.0f / 3.0f, //aspect ratio 0.1f, //near clipping 100.0f, //far clipping projmat); //matrix lookat(vector(3,3,3), //camera position vector(0,0,0), //target @ vector(0,1,0), //up vector viewmat); glint viewid = glgetuniformlocation(shaderprogram, "view"); glint modelid = glgetuniformlocation(shaderprogram, "model"); glint projid = glgetuniformlocation(shaderprogram, "proj"); glint mvit_id = glgetuniformlocation(shaderprogram, "modelview_it"); transformmodel(modelid, mvit_id, trans.matrix(), viewmat); transformview(viewid, mvit_id, viewmat, trans.matrix()); transformproj(projid, projmat); those "transformxxx" functions this
void transformmodel(glint& modelid, glint& mvit_id, matrix4f& modelmat, matrix4f& viewmat) { gluniformmatrix4fv(modelid, 1, gl_false, modelmat.data()); gluniformmatrix4fv(mvit_id, 1, gl_false, (viewmat*modelmat).inverse().transpose().data()); } void transformview(glint& viewid, glint& mvit_id, matrix4f& viewmat, matrix4f& modelmat) { gluniformmatrix4fv(viewid, 1, gl_false, viewmat.data()); gluniformmatrix4fv(mvit_id, 1, gl_false, (viewmat*modelmat).inverse().transpose().data()); } void transformproj(glint& projid, matrix4f& projmat) { gluniformmatrix4fv(projid, 1, gl_false, projmat.data()); } despite these, screen nothing black. not using simple triangle @ origin. if rid of proj * view * in vertex shader (basically having "model" transformation), able see shape , use keyboard events transform fine. means basic pipeline shouldn't problem, i'm able see shape. add "proj * view * " before "model", blacks out.
please help!
update: have located 1 problem: gluniformmatrix4fv giving me gl_invalid_enum error, , it's not setting uniform variable @ all. (i verified hardcoding matrix shader). can hardly find online issue , seems no 1 gets invalid_enum error function. ideas?
the transformxxx functions odd. why there 3 different ones? model, view , projection transformations go together. , both transformmodelandtransformview` set modelview_it matrix.
normally not pass model , view transformation matrices individual uniforms. 1 in not optimized shader code force perform 4×4 matrix-matrix multiplication each vertex, result can precomputed.
you should merge transformmodel , transformview single transformmodelview function.
another thing looks odd is, you're starting 0 matrices. initialize matrices identity , work there.
Comments
Post a Comment