Incorrect rotations using openGL GLM and shaders -
the goal of program display simple colored triangle rotating on y axis without translation. i'm using glm library. problem transformations of triangle not correct : has strange , not logical behaviour. here's c++ code plus vertex shader , fragment shader code :
c++ code :
float angle = 0.0f; struct vertex { float x, y, z; }; static vertex vertices[9] = { -1.000000f, 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f, 1.000000f, 0.000000f, 0.000000f }; static vertex colors[9] = { 1.000000f, 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f, 0.000000f, 0.000000f, 1.000000f }; [...] int main(int ac, char **av) { bool continuer = true; sdl_event event; gluint vboid[2]; gluint programid = 0; //sdl window initialization sdl_init(sdl_init_video); sdl_wm_setcaption("vbo tests",null); sdl_setvideomode(width, height, 32, sdl_opengl); //viewport initialization glviewport(0, 0, width, height); //projection initialization glm::mat4 projection = glm::mat4(1.0f); projection = glm::perspective<glfloat>(60.0f, (float)(width/height), 1.0f, 1000.0f); //view initialization glm::mat4 view = glm::lookat<glfloat>(glm::vec3(0.0f, 0.0f, 8.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); glewinit(); //shaders initialization programid = initshaders("triangle.vert", "triangle.frag"); //main loop while (continuer) { eventlistener(&event, &continuer); glcleardepth(1.0f); glclearcolor(0.13f, 0.12f, 0.13f, 1.0f); glclear(gl_color_buffer_bit | gl_depth_buffer_bit); gluseprogram(programid); //model transformations glm::mat4 model = glm::mat4(1.0f); model *= glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f)); model *= glm::rotate(model, angle, glm::vec3(0.0f, 1.0f, 0.0f)); glm::mat4 modelviewmatrix = model * view; glm::mat4 modelviewprojectionmatrix = projection * modelviewmatrix; glvertexattribpointer(0, 3, gl_float, gl_false, 0, vertices); glvertexattribpointer(1, 3, gl_float, gl_false, 0, colors); glenablevertexattribarray(0); glenablevertexattribarray(1); gluniformmatrix4fv(glgetuniformlocation(programid, "modelview"), 1, gl_true, glm::value_ptr(modelviewmatrix)); gluniformmatrix4fv(glgetuniformlocation(programid, "mvp"), 1, gl_true, glm::value_ptr(modelviewprojectionmatrix)); gldrawarrays(gl_triangles, 0, 3); gldisablevertexattribarray(1); gldisablevertexattribarray(0); gluseprogram(0); angle += 0.020f; glflush(); sdl_gl_swapbuffers(); } sdl_quit(); return (0); }
vertex shader code :
#version 330 in vec3 vertexposition; in vec3 vertexcolor; uniform mat4 modelviewmatrix; uniform mat4 mvp; out vec3 color; void main() { color = vertexcolor; gl_position = mvp * vec4(vertexposition, 1.0f); }
fragment shader code :
#version 330 in vec3 color; out vec4 fragcolor; void main() { fragcolor = vec4(color, 1.0); }
when declare simple matrix rotation model, send vertex shader , use mvp , works. when add projection matrix + view matrix not work correctly. here's picture :
normally, triangle should @ center of screen.
does can me?
this line:
glm::mat4 modelviewmatrix = model * view;
should read:
glm::mat4 modelviewmatrix = view * model;
matrix calculations done in reverse, , want first multiply vertex model matrix, not view.
you can take @ my other answer, explaining these concepts bit.
Comments
Post a Comment