From 21f3329340ccd9a8c42335f84b34d3f7e4a2e99a Mon Sep 17 00:00:00 2001 From: knolax <1339802534.kk@gmail.com> Date: Wed, 1 Mar 2017 12:01:41 -0500 Subject: fixed reflection shading and added colored light. --- example/fragmentshader.glsl | 19 ++++++++++++++++--- example/main | Bin 54184 -> 54360 bytes example/main.cpp | 16 ++++++++-------- example/main.o | Bin 5152 -> 5904 bytes example/textures/elevold.bmp | Bin 0 -> 4000138 bytes example/textures/frederickelev.png | Bin 0 -> 19336 bytes example/textures/frederickelev.xcf | Bin 0 -> 111015 bytes glgfx.cpp | 13 ++++++++----- glgfx.h | 4 ++-- glgfx.o | Bin 62104 -> 62960 bytes libglgfx.a | Bin 66420 -> 67374 bytes 11 files changed, 34 insertions(+), 18 deletions(-) create mode 100644 example/textures/elevold.bmp create mode 100644 example/textures/frederickelev.png create mode 100644 example/textures/frederickelev.xcf diff --git a/example/fragmentshader.glsl b/example/fragmentshader.glsl index 631ea69..fc7b4c8 100644 --- a/example/fragmentshader.glsl +++ b/example/fragmentshader.glsl @@ -2,6 +2,7 @@ out vec4 outpix; uniform vec3 campos; uniform vec3 lightpos; +uniform vec4 lightattrib; uniform sampler2D tex; uniform sampler2D elev; //varing blends them it is transformed to world space @@ -18,18 +19,30 @@ void main() float diffuse = dot(lightnormal, fragnormal.xyz); if (diffuse < 0.0) { diffuse = 0.0; + } else { } vec3 camnormal = normalize(campos - refpos); - lightnormal = -normalize((lightpos - refpos)); // this time it is an incoming vector + lightnormal = normalize((refpos - lightpos)); // this time it is an incoming vector vec3 refnormal = lightnormal - (2 * dot(lightnormal, fragnormal.xyz) * fragnormal.xyz); float reflect = dot(camnormal, refnormal); + if (reflect < 0.0) { + reflect = 0.0; + } else { + } diffuse = pow(diffuse, 2.0); reflect = pow(reflect, 2.0); - float light = (elevpix.b) * reflect + elevpix.a * diffuse; + float dist = abs(length(difpos - lightpos)) / lightattrib.a; + if (dist > 1.0) { + dist = 1.0; + } + dist = 1 - pow(dist, 4.0); + float lightr = (elevpix.b) * 1.0 * lightattrib.r * reflect + elevpix.a * 1.0 * dist * diffuse; + float lightg = (elevpix.b) * 1.0 * lightattrib.g * reflect + elevpix.a * 1.0 * dist * diffuse; + float lightb = (elevpix.b) * 1.0 * lightattrib.b * reflect + elevpix.a * 1.0 * dist * diffuse; vec4 texpix = texture(tex, fragtexcoord); //this should be the vertex in camera space without the w value //outpix = gl_FragCoord; - outpix = light * texpix; + outpix = vec4(lightr * texpix.r, lightg * texpix.g, lightb * texpix.b, texpix.a); //outpix = vec4(light * 1.0,light * 1.0,light * 1.0, 1.0); //outpix = vec4(fragpos.x ,fragpos.y ,fragpos.z, 0.5); //outpix = vec4(fragnormal.x, fragnormal.y, fragnormal.z, 0.5); diff --git a/example/main b/example/main index 248dc85..9f2411e 100755 Binary files a/example/main and b/example/main differ diff --git a/example/main.cpp b/example/main.cpp index a92ce4b..6b7975c 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -11,7 +11,7 @@ int main (int argc, char * argv[]) { //inits program initgfx(400,600, "rotating cube"); initglgfx(); - setlight(0,10,10); + setlight(2.0,10.0,3.0, 1.0, 0.5, 0.5, 15.0); setcam(0.0,0.0,1.0, 0.0,0.0,0.0, 0.0,1.0,0.0); //main loop, this is defined in main.h and global loop = 1; @@ -40,19 +40,19 @@ int main (int argc, char * argv[]) { } switch (rot) { case 0: - drawmodel(0.0f, 0.0f, -2.0f, dir, 0.0f, 0.0f, 1.0); - drawmodel(0.5f, 3.0f, -10.0f, dir, 0.0f, 0.0f, 1.0); + drawmodel(0.0f, 0.0f, -2.0f, dir, 0.0f, 0.0f, 1.0, 1.0, 1.0); + drawmodel(0.5f, 3.0f, -10.0f, dir, 0.0f, 0.0f, 1.0, 1.0, 1.0); break; case 1: - drawmodel(0.0f, 0.0f, -2.0f, 0.0f, dir, 0.0f, 1.0); - drawmodel(0.5f, 3.0f, -10.0f, 0.0f, dir, 0.0f, 1.0); + drawmodel(0.0f, 0.0f, -2.0f, 0.0f, dir, 0.0f, 1.0, 1.0, 1.0); + drawmodel(0.5f, 3.0f, -10.0f, 0.0f, dir, 0.0f, 1.0, 1.0, 1.0); break; case 2: - drawmodel(0.0f, 0.0f, -2.0f, 0.0f, 0.0f, dir, 1.0); - drawmodel(0.5f, 3.0f, -10.0f, 0.0f, 0.0f, dir, 1.0); + drawmodel(0.0f, 0.0f, -2.0f, 0.0f, 0.0f, dir, 1.0, 1.0, 1.0); + drawmodel(0.5f, 3.0f, -10.0f, 0.0f, 0.0f, dir, 1.0, 1.0, 1.0); break; } - drawmodel(0.0f, -2.0, -5.0,0.0,0.0,0.0,1.0 ); + drawmodel(0.0f, -2.0, -5.0,0.0,0.0,0.0,10.0, 1.0, 10.0 ); updategfx(); } quitgfx(); diff --git a/example/main.o b/example/main.o index 0a41d2f..c6267ba 100644 Binary files a/example/main.o and b/example/main.o differ diff --git a/example/textures/elevold.bmp b/example/textures/elevold.bmp new file mode 100644 index 0000000..cb9fec6 Binary files /dev/null and b/example/textures/elevold.bmp differ diff --git a/example/textures/frederickelev.png b/example/textures/frederickelev.png new file mode 100644 index 0000000..b3f13b5 Binary files /dev/null and b/example/textures/frederickelev.png differ diff --git a/example/textures/frederickelev.xcf b/example/textures/frederickelev.xcf new file mode 100644 index 0000000..8b7cd36 Binary files /dev/null and b/example/textures/frederickelev.xcf differ diff --git a/glgfx.cpp b/glgfx.cpp index 7b54254..f3cdec9 100644 --- a/glgfx.cpp +++ b/glgfx.cpp @@ -11,6 +11,7 @@ GLint rothandler; GLint camhandler; GLint camposhandler; GLint lightposhandler; +GLint lightattribhandler; //device coordinates // ^ // (y+) @@ -211,6 +212,7 @@ int genshaders() { rothandler = glGetUniformLocation(shaderprogramhandle, "rot"); lightposhandler = glGetUniformLocation(shaderprogramhandle, "lightpos"); camposhandler = glGetUniformLocation(shaderprogramhandle, "campos"); + lightattribhandler = glGetUniformLocation(shaderprogramhandle, "lightattrib"); } //opens a filebuffer stream and reads every char std::string readfile(char const * filename) { @@ -237,13 +239,14 @@ int setcam (float cpx, float cpy, float cpz, float clx, float cly, float clz, fl 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))); +int setlight (float x, float y, float z, float r, float g, float b, float lim) { + glUniform3fv(lightposhandler, 1, glm::value_ptr(glm::vec3(x,y,z))); + glUniform4fv(lightattribhandler, 1, glm::value_ptr(glm::vec4(r,g,b,lim))); 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, float s) { +int drawmodel(float x, float y, float z, float xr, float yr, float zr, float xs, float ys, float zs) { // the transformation matrix = rotation matrix of object * scaling matrix * translaton matrix representing difference in camera and object location * -rotation matrix of the camera * perspective transform //order of application ir right to left //fov, aspect ratio, size of near and far planes distance @@ -258,12 +261,12 @@ int drawmodel(float x, float y, float z, float xr, float yr, float zr, float s) // glm::vec4(0.0f,0.0f,0.5f,1.0f) //w just make it 1 //); //the translation matrix, matrixes added last are applied first - glm::mat4 tran; - tran = glm::scale(tran, glm::vec3(s,s,s)); + glm::mat4 tran; tran = glm::translate(tran, glm::vec3(x,y,z)); 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)); + tran = glm::scale(tran, glm::vec3(xs, ys, zs)); glm::mat4 rot; 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)); diff --git a/glgfx.h b/glgfx.h index ec40f88..8d0137a 100644 --- a/glgfx.h +++ b/glgfx.h @@ -11,9 +11,9 @@ #include int initglgfx(); int genshaders(); -int drawmodel(float x, float y, float z, float xr, float yr, float zr, float s); +int drawmodel(float x, float y, float z, float xr, float yr, float zr, float xs, float ys, float zs); int setcam (float cpx, float cpy, float cpz, float clx, float cly, float clz, float cux, float cuy, float cuz); -int setlight (float x, float y, float z); +int setlight (float x, float y, float z, float r, float g, float b, float lim); int quitglgfx(); int clearglgfx(); int glgfxloadtexture (const char * file); diff --git a/glgfx.o b/glgfx.o index 9dcb566..f5a2f42 100644 Binary files a/glgfx.o and b/glgfx.o differ diff --git a/libglgfx.a b/libglgfx.a index ed6181a..0dadadc 100644 Binary files a/libglgfx.a and b/libglgfx.a differ -- cgit v1.1