diff options
Diffstat (limited to 'glgfx.cpp')
-rw-r--r-- | glgfx.cpp | 114 |
1 files changed, 88 insertions, 26 deletions
@@ -6,8 +6,10 @@ GLuint ebhandler; //handlers for the perspective transforms GLint projhandler; GLint tranhandler; +GLint rothandler; GLint camhandler; -glm::mat4 tran; +GLint camposhandler; +GLint lightposhandler; //device coordinates // ^ // (y+) @@ -38,6 +40,20 @@ int initglgfx() { printf("%u\n",glnum); //vertex data load of the box model float vertexdata[] = { +-0.500000,0.500000,-0.500000, 0.000000,0.000000,-1.000000, 0.500000,0.500000,-0.500000, 0.000000,0.000000,-1.000000, -0.500000,-0.500000,-0.500000, 0.000000,0.000000,-1.000000, +-0.500000,-0.500000,-0.500000, 0.000000,0.000000,-1.000000, 0.500000,0.500000,-0.500000, 0.000000,0.000000,-1.000000, 0.500000,-0.500000,-0.500000, 0.000000,0.000000,-1.000000, +0.500000,0.500000,-0.500000, 1.000000,0.000000,0.000000, 0.500000,0.500000,0.500000, 1.000000,0.000000,0.000000, 0.500000,-0.500000,-0.500000, 1.000000,0.000000,0.000000, +0.500000,-0.500000,-0.500000, 1.000000,0.000000,0.000000, 0.500000,0.500000,0.500000, 1.000000,0.000000,0.000000, 0.500000,-0.500000,0.500000, 1.000000,0.000000,0.000000, +0.500000,0.500000,0.500000, 0.000000,0.000000,1.000000, -0.500000,0.500000,0.500000, 0.000000,0.000000,1.000000, 0.500000,-0.500000,0.500000, 0.000000,0.000000,1.000000, +-0.500000,0.500000,0.500000, 0.000000,0.000000,1.000000, -0.500000,-0.500000,0.500000, 0.000000,0.000000,1.000000, 0.500000,-0.500000,0.500000, 0.000000,0.000000,1.000000, +-0.500000,0.500000,0.500000, -1.000000,0.000000,0.000000, -0.500000,0.500000,-0.500000, -1.000000,0.000000,0.000000, -0.500000,-0.500000,0.500000, -1.000000,0.000000,0.000000, +-0.500000,0.500000,-0.500000, -1.000000,0.000000,0.000000, -0.500000,-0.500000,-0.500000, -1.000000,0.000000,0.000000, -0.500000,-0.500000,0.500000, -1.000000,0.000000,0.000000, +-0.500000,-0.500000,-0.500000, 0.000000,-1.000000,0.000000, 0.500000,-0.500000,0.500000, 0.000000,-1.000000,0.000000, -0.500000,-0.500000,0.500000, 0.000000,-1.000000,0.000000, +0.500000,-0.500000,-0.500000, 0.000000,-1.000000,0.000000, 0.500000,-0.500000,0.500000, 0.000000,-1.000000,0.000000, -0.500000,-0.500000,-0.500000, 0.000000,-1.000000,0.000000, +-0.500000,0.500000,-0.500000, 0.000000,1.000000,0.000000, -0.500000,0.500000,0.500000, 0.000000,1.000000,0.000000, 0.500000,0.500000,0.500000, 0.000000,1.000000,0.000000, +0.500000,0.500000,0.500000, 0.000000,1.000000,0.000000, 0.500000,0.500000,-0.500000, 0.000000,1.000000,0.000000, -0.500000,0.500000,-0.500000, 0.000000,1.000000,0.000000, + }; +/* float vertexdata[] = { -0.5, 0.5, -.5, // x y z 0.5, 0.5, -.5, 0.5, -0.5, -.5, @@ -45,24 +61,41 @@ int initglgfx() { -0.5, 0.5, 0.5, // x y z 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, - -0.5, -0.5, 0.5 + -0.5, -0.5, 0.5, + //normal positions + 1.0, 0.0, 0.0, + -1.0, 0.0, 0.0, + 0.0, 1.0, 0.0, + 0.0, -1.0, 0.0, + 0.0, 0.0, 1.0, + 0.0, 0.0, -1.0, }; +*/ //polygons of the box mode based on the vertex data. //they must be clockwise from the front so that they are culled when not //seen +/* GLuint elementdata[] = { + 0,12, 1,12, 3,12, + 3,12, 1,12, 2,12, + + 1,8, 5,8, 2,8, + 2,8, 5,8, 6,8, + + 5,13, 4,13, 6,13, + 4,13, 7,13, 6,13, + + 4,9, 0,9, 7,9, + 0,9, 3,9, 7,9, + + 3,11, 6,11, 7,11, + 2,11, 6,11, 3,11, + + 0,10, 4,10, 5,10, + 5,10, 1,10, 0,10, + }; +*/ GLuint elementdata[] = { - 0,1,3, - 3,1,2, - 1,5,2, - 2,5,6, - 5,4,6, - 4,7,6, - 4,0,7, - 0,3,7, - 3,6,7, - 2,6,3, - 0,4,5, - 5,1,0, + 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35 }; //binds to the element buffer glGenBuffers(1,&vbhandler); @@ -131,9 +164,21 @@ int genshaders() { pos = glGetAttribLocation(shaderprogramhandle, "position"); //the actual atribute, number of values, type, whether they need to be normalied to -1 1, //number of bytes between values, offset from beginning in bytes - glVertexAttribPointer(pos, 3, GL_FLOAT, GL_FALSE, 0, 0); + glVertexAttribPointer(pos, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), 0); glEnableVertexAttribArray(pos); - + GLint vnorm; + vnorm = glGetAttribLocation(shaderprogramhandle, "vnormal"); + //the actual atribute, number of values, type, whether they need to be normalied to -1 1, + //number of bytes between values, offset from beginning in bytes + glVertexAttribPointer(vnorm, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *) (3 * sizeof(float)) ); + glEnableVertexAttribArray(vnorm); + //getting uniform handler + projhandler = glGetUniformLocation(shaderprogramhandle, "proj"); + camhandler = glGetUniformLocation(shaderprogramhandle, "cam"); + tranhandler = glGetUniformLocation(shaderprogramhandle, "tran"); + rothandler = glGetUniformLocation(shaderprogramhandle, "rot"); + lightposhandler = glGetUniformLocation(shaderprogramhandle, "lightpos"); + camposhandler = glGetUniformLocation(shaderprogramhandle, "campos"); } //opens a filebuffer stream and reads every char std::string readfile(char const * filename) { @@ -152,6 +197,18 @@ std::string readfile(char const * filename) { filebuffer.close(); return rstr; } +int setcam (float cpx, float cpy, float cpz, float clx, float cly, float clz, float cux, float cuy, float cuz) { + //lookat eye position of camera, center center of screen, up up direction + //glm::mat4 cam = glm::lookAt(glm::vec3(0,0,1), glm::vec3(0,0,0),glm::vec3(0,1,0)); + glm::mat4 cam = glm::lookAt(glm::vec3(cpx, cpy, cpz), glm::vec3(clx, cly, clz), glm::vec3(cux, cuy, cuz)); + glUniformMatrix4fv(camhandler, 1, GL_FALSE, glm::value_ptr(cam)); + glUniform3fv(camposhandler, 1, glm::value_ptr(glm::vec3(cpx,cpy,cpz))); + return 0; +} +int setlight (float x, float y, float z) { + glUniform3fv(camposhandler, 1, glm::value_ptr(glm::vec3(x,y,z))); + return 0; +} //draws the model, setting perspective transform and position transform //done with magic numbers for now for testing purposes int drawmodel(float x, float y, float z, float xr, float yr, float zr) { @@ -159,12 +216,8 @@ int drawmodel(float x, float y, float z, float xr, float yr, float zr) { //order of application ir right to left //fov, aspect ratio, size of near and far planes distance glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)maxx/(float)maxy, 0.1f, 100.0f); - projhandler = glGetUniformLocation(shaderprogramhandle, "proj"); glUniformMatrix4fv(projhandler, 1, GL_FALSE, glm::value_ptr(proj)); - //lookat eye position of camera, center center of screen, up up direction - glm::mat4 cam = glm::lookAt(glm::vec3(0,0,1), glm::vec3(0,0,0),glm::vec3(0,1,0)); - camhandler = glGetUniformLocation(shaderprogramhandle, "cam"); - glUniformMatrix4fv(camhandler, 1, GL_FALSE, glm::value_ptr(cam)); + //a valid tranlation matrix showing the colun first conventions of glm::mat4 //tran = glm::mat4( // glm::vec4(1.0f,0.0f,0.0f,0.0f), //x transform is the one on the last column @@ -178,14 +231,23 @@ int drawmodel(float x, float y, float z, float xr, float yr, float zr) { tran = glm::rotate(tran,glm::radians(xr),glm::vec3(1,0,0)); // axis of rotation x y z tran = glm::rotate(tran,glm::radians(yr),glm::vec3(0,1,0)); tran = glm::rotate(tran,glm::radians(zr),glm::vec3(0,0,1)); + glm::mat4 rot; + tran = glm::translate(tran, glm::vec3(0,0,0)); + rot = glm::rotate(rot,glm::radians(xr),glm::vec3(1,0,0)); // axis of rotation x y z + rot = glm::rotate(rot,glm::radians(yr),glm::vec3(0,1,0)); + rot = glm::rotate(rot,glm::radians(zr),glm::vec3(0,0,1)); //deprecated debug stuff - //printf("translated vector x%f\n",(tran * glm::vec4(0.0f,0.0f,0.0f,1.0f)).x); - //printf("translated vector y%f\n",(tran * glm::vec4(0.0f,0.0f,0.0f,1.0f)).y); - //printf("translated vector z%f\n",(tran * glm::vec4(0.0f,0.0f,0.0f,1.0f)).z); + //printf("translated vector x%f\n",(tran * glm::vec4(1.0f,0.0f,0.0f,1.0f)).x); + //printf("translated vector y%f\n",(tran * glm::vec4(0.0f,1.0f,0.0f,1.0f)).y); + //printf("translated vector z%f\n",(tran * glm::vec4(0.0f,0.0f,1.0f,1.0f)).z); + //printf("translated vector x%f\n",(rot * glm::vec4(1.0f,0.0f,0.0f,1.0f)).x); + //printf("translated vector y%f\n",(rot * glm::vec4(0.0f,1.0f,0.0f,1.0f)).y); + //printf("translated vector z%f\n",(rot * glm::vec4(0.0f,0.0f,1.0f,1.0f)).z); //loads the transaltion stuff - tranhandler = glGetUniformLocation(shaderprogramhandle, "tran"); glUniformMatrix4fv(tranhandler, 1, GL_FALSE, glm::value_ptr(tran)); - glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0); + glUniformMatrix4fv(rothandler, 1, GL_FALSE, glm::value_ptr(rot)); + //glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0); + glDrawArrays(GL_TRIANGLES,0,36); } int quitglgfx() { } |