Translation Transformation in openGL -
i learning opengl. got problem when try translation transformation. result isn't translation wanted. static image
and code
translationtransformation.cpp
#include <gl/glew.h> #include <gl/freeglut.h> #include <stdio.h> #include <math.h> #define num_vertices 3 #define num_indices 3 #define buffer_offset(i) ((char *)null + (i)) gluint vbo, gworldlocation, indexbufferid, shaderprogramid, positionid; static float scale; struct vector3f{ glfloat x; glfloat y; glfloat z; vector3f(){}; vector3f(glfloat _x, glfloat _y, glfloat _z){ x = _x; y = _y; z = _z; } }; struct vector4f{ glfloat x; glfloat y; glfloat z; glfloat w; vector4f(){}; vector4f(glfloat _x, glfloat _y, glfloat _z, glfloat _w){ x = _x; y = _y; z = _z; w = _w; } }; struct matrix4f{ float m[4][4]; }; static char* readfile(const char* filename) { // open file file* fp = fopen (filename, "r"); // move file pointer end of file , determing length fseek(fp, 0, seek_end); long file_length = ftell(fp); fseek(fp, 0, seek_set); char* contents = new char[file_length+1]; // 0 out memory (int = 0; < file_length+1; i++) { contents[i] = 0; } // here's actual read fread (contents, 1, file_length, fp); // how denote end of string in c contents[file_length+1] = '\0'; fclose(fp); return contents; } bool compiledstatus(gluint shaderid){ glint success = 0; gworldlocation = glgetuniformlocation(shaderprogramid, "gworld"); glgetshaderiv(shaderid, gl_compile_status, &success); if(success){ return true; } else{ glint loglength; glgetshaderiv(shaderid, gl_info_log_length, &loglength); char* msgbuffer = new char[loglength]; glgetshaderinfolog(shaderid, loglength, null, msgbuffer); printf("error: %s \n", msgbuffer); delete(msgbuffer); return false; } } gluint makevertexshader(const char* shadersource){ gluint vertexshaderid = glcreateshader(gl_vertex_shader); glshadersource(vertexshaderid, 1, (const glchar**)&shadersource, null); glcompileshader(vertexshaderid); if(compiledstatus(vertexshaderid)==true){ return vertexshaderid; } else return -1; } gluint makefragmentshader(const char* shadersource){ gluint fragmenshaderid = glcreateshader(gl_fragment_shader); glshadersource(fragmenshaderid, 1, (const char**)&shadersource, null); glcompileshader(fragmenshaderid); if(compiledstatus(fragmenshaderid)==true){ return fragmenshaderid; } else return -1; } gluint makeshaderprogram(gluint vertexshaderid, gluint fragmentshaderid){ gluint shaderid = glcreateprogram(); glattachshader(shaderid, vertexshaderid); glattachshader(shaderid, fragmentshaderid); gllinkprogram(shaderid); return shaderid; } void createvertexbuffer(){ vector3f vertices[3]; vertices[0] = vector3f(-1.0f, -1.0f, 0.0f); vertices[1] = vector3f(1.0f, -1.0f, 0.0f); vertices[2] = vector3f(0.0f, 1.0f, 0.0f); glgenbuffers(1, &vbo); glbindbuffer(gl_array_buffer, vbo); glbufferdata(gl_array_buffer, sizeof(vertices), vertices, gl_static_draw); } void render() { glclear(gl_color_buffer_bit); gluseprogram(shaderprogramid); scale += 0.001f; matrix4f world; world.m[0][0] = 1.0f; world.m[0][1] = 0.0f; world.m[0][2] = 0.0f; world.m[0][3] = sinf(scale); world.m[1][0] = 0.0f; world.m[1][1] = 1.0f; world.m[1][2] = 0.0f; world.m[1][3] = 0.0f; world.m[2][0] = 0.0f; world.m[2][1] = 0.0f; world.m[2][2] = 1.0f; world.m[2][3] = 0.0f; world.m[3][0] = 0.0f; world.m[3][1] = 0.0f; world.m[3][2] = 0.0f; world.m[3][3] = 1.0f; gluniformmatrix4fv(gworldlocation, 1, gl_true, &world.m[0][0]); gldrawarrays(gl_triangles, 0, 3); glutswapbuffers(); } int main(int argc, char** argv){ glutinit(&argc, argv); glutinitdisplaymode(glut_rgba|glut_double|glut_depth); glutinitwindowsize(800, 600); glutcreatewindow("index buffers"); glutdisplayfunc(render); glewinit(); createvertexbuffer(); scale = 0.0f; matrix4f world; world.m[0][0] = 1.0f; world.m[0][1] = 0.0f; world.m[0][2] = 0.0f; world.m[0][3] = sinf(scale); world.m[1][0] = 0.0f; world.m[1][1] = 1.0f; world.m[1][2] = 0.0f; world.m[1][3] = 0.0f; world.m[2][0] = 0.0f; world.m[2][1] = 0.0f; world.m[2][2] = 1.0f; world.m[2][3] = 0.0f; world.m[3][0] = 0.0f; world.m[3][1] = 0.0f; world.m[3][2] = 0.0f; world.m[3][3] = 1.0f; char* vertexshadersource = readfile("vertexshader.vsh"); char* fragmentshadersource = readfile("fragmentshader.fsh"); gluint vertexshaderid = makevertexshader(vertexshadersource); gluint fragmenshaderid = makefragmentshader(fragmentshadersource); gluint shaderprogramid = makeshaderprogram(vertexshaderid, fragmenshaderid); // find position of variables in shader gworldlocation = glgetuniformlocation(shaderprogramid, "gworld"); positionid = glgetattriblocation(shaderprogramid, "position"); glvertexattribpointer(positionid, 3, gl_float, gl_false, 0, 0); gluseprogram(shaderprogramid); glenablevertexattribarray(positionid); glutmainloop(); return 0; }
vertexshader.vsh
#version 330 in vec3 position; uniform mat4 gworld; void main() { gl_position = gworld * vec4(position, 1.0); }
fragmentshader.fsh
#version 330 out vec4 fragcolor; void main() { fragcolor = vec4(1.0, 0.0, 0.0, 1.0); }
can me solve problem. thank much!
Comments
Post a Comment