From cf3f284a5f24bd02006e9ff929cc29d10c428a3e Mon Sep 17 00:00:00 2001 From: knolax <1339802534.kk@gmail.com> Date: Tue, 28 Feb 2017 11:05:31 -0500 Subject: textures loaded properly now but without functional alpha layer. --- example/fragmentshader.glsl | 8 +++++- example/main | Bin 49568 -> 54072 bytes example/makefile | 2 +- example/textures/elev.bmp | Bin 0 -> 4000138 bytes example/textures/elev.png | Bin 0 -> 198073 bytes example/textures/tex.bmp | Bin 0 -> 4000138 bytes example/textures/tex.png | Bin 0 -> 164345 bytes example/vertexshader.glsl | 3 +++ gen.cpp | 37 +++++++++++++++------------ gennew | Bin 0 -> 8496 bytes glgfx.cpp | 61 +++++++++++++++++++++++++++++++++++++++++--- glgfx.h | 2 ++ glgfx.o | Bin 58104 -> 60880 bytes libglgfx.a | Bin 62288 -> 65108 bytes 14 files changed, 91 insertions(+), 22 deletions(-) create mode 100644 example/textures/elev.bmp create mode 100644 example/textures/elev.png create mode 100644 example/textures/tex.bmp create mode 100644 example/textures/tex.png create mode 100755 gennew diff --git a/example/fragmentshader.glsl b/example/fragmentshader.glsl index 70c55b5..6e8c4c5 100644 --- a/example/fragmentshader.glsl +++ b/example/fragmentshader.glsl @@ -2,11 +2,15 @@ out vec4 outpix; uniform vec3 campos; uniform vec3 lightpos; +uniform sampler2D tex; +uniform sampler2D elev; //varing blends them it is transformed to world space varying vec4 fragpos; varying vec4 fragnormal; +varying vec2 fragtexcoord; void main() { + vec4 elevpix = texture(elev, fragtexcoord); vec3 lightnormal = normalize((lightpos - vec3(fragpos.x, fragpos.y, fragpos.z))); float diffuse = dot(lightnormal, vec3(fragnormal.x, fragnormal.y, fragnormal.z)); if (diffuse < 0.0) { @@ -17,9 +21,11 @@ void main() diffuse = pow(diffuse, 2.0); reflect = pow(reflect, 2.0); float light = .4 * reflect + .6 * diffuse; + vec4 texpix = texture(tex, fragtexcoord); //this should be the vertex in camera space without the w value //outpix = gl_FragCoord; - outpix = vec4(light * 1.0,light * 1.0,light * 1.0, 1.0); + outpix = texpix; + //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 51bb73a..14fd1e1 100755 Binary files a/example/main and b/example/main differ diff --git a/example/makefile b/example/makefile index 578059f..4a86980 100644 --- a/example/makefile +++ b/example/makefile @@ -1,5 +1,5 @@ main : main.o - g++ main.o -o main -L/home/knolax/code/cpp/gfx -lgui -lgfx -lSDL2 -lSDL2_ttf -L/home/knolax/code/cpp/glgfx -lglgfx -lGLEW -lGL + g++ main.o -o main -L/home/knolax/code/cpp/gfx -lgui -lgfx -lSDL2 -lSDL2_ttf -L/home/knolax/code/cpp/glgfx -lglgfx -lGLEW -lGL -lSOIL main.o : main.cpp g++ -c main.cpp -I/home/knolax/code/cpp/gfx -I/home/knolax/code/cpp/glgfx clean : diff --git a/example/textures/elev.bmp b/example/textures/elev.bmp new file mode 100644 index 0000000..cb9fec6 Binary files /dev/null and b/example/textures/elev.bmp differ diff --git a/example/textures/elev.png b/example/textures/elev.png new file mode 100644 index 0000000..68c4308 Binary files /dev/null and b/example/textures/elev.png differ diff --git a/example/textures/tex.bmp b/example/textures/tex.bmp new file mode 100644 index 0000000..620d364 Binary files /dev/null and b/example/textures/tex.bmp differ diff --git a/example/textures/tex.png b/example/textures/tex.png new file mode 100644 index 0000000..79d9a1c Binary files /dev/null and b/example/textures/tex.png differ diff --git a/example/vertexshader.glsl b/example/vertexshader.glsl index f60ec9f..8ba8228 100644 --- a/example/vertexshader.glsl +++ b/example/vertexshader.glsl @@ -5,12 +5,15 @@ uniform mat4 cam; uniform mat4 rot; in vec3 position; in vec3 vnormal; +in vec3 texcoord; varying vec4 fragpos; varying vec4 fragnormal; // if this needs to not be blended jsut have all vertexes in a poly be the same +varying vec2 fragtexcoord; void main() { //this is the actual position gl_Position = proj * cam * tran * vec4(position, 1.0f);// - vec4(gl_Normal, 1.0f); fragpos = tran * vec4(position, 1.0f); fragnormal = (tran * vec4(vnormal, 1.0f)) - (tran * vec4(0.0, 0.0, 0.0, 1.0)); + fragtexcoord = texcoord.xy; } diff --git a/gen.cpp b/gen.cpp index 167d9e7..5ac79d0 100644 --- a/gen.cpp +++ b/gen.cpp @@ -9,43 +9,48 @@ int main () { 0.5, 0.5, 0.5, 0.5, -0.5, 0.5, -0.5, -0.5, 0.5, - //normal positions + //normal positions : 8 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, + //texcoords : 14 + 0.0, 0.0, 0.0, + 1.0, 0.0, 0.0, + 0.0, 1.0, 0.0, + 1.0, 1.0, 0.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 int elementdata[] = { - 0,13, 1,13, 3,13, - 3,13, 1,13, 2,13, + 0,13,16, 1,13,17, 3,13,14, + 3,13,14, 1,13,17, 2,13,15, - 1,8, 5,8, 2,8, - 2,8, 5,8, 6,8, + 1,8,16, 5,8,17, 2,8,14, + 2,8,14, 5,8,17, 6,8,15, - 5,12, 4,12, 6,12, - 4,12, 7,12, 6,12, + 5,12,16, 4,12,17, 6,12,14, + 4,12,17, 7,12,15, 6,12,14, - 4,9, 0,9, 7,9, - 0,9, 3,9, 7,9, + 4,9,16, 0,9,17, 7,9,14, + 0,9,17, 3,9,15, 7,9,14, - 3,11, 6,11, 7,11, - 2,11, 6,11, 3,11, + 3,11,14, 6,11,17, 7,11,16, + 2,11,15, 6,11,17, 3,11,14, - 0,10, 4,10, 5,10, - 5,10, 1,10, 0,10, + 0,10,14, 4,10,15, 5,10,17, + 5,10,17, 1,10,16, 0,10,14, }; int i = 0; printf("float vertexdata[] = {\n"); - while (i < 72) { + while (i < 108) { printf("%f,%f,%f, ",vertexdata[(elementdata[i] * 3)],vertexdata[(elementdata[i] * 3) + 1],vertexdata[(elementdata[i] * 3 )+ 2]); - if (((i + 1) % 6) == 0) { + if (((i + 1) % 9) == 0) { printf("\n"); - } else if (((i + 1) % 2) == 0) { + } else if (((i + 1) % 3) == 0) { printf("\t"); } i++; diff --git a/gennew b/gennew new file mode 100755 index 0000000..903dd60 Binary files /dev/null and b/gennew differ diff --git a/glgfx.cpp b/glgfx.cpp index 9393d66..bed50d4 100644 --- a/glgfx.cpp +++ b/glgfx.cpp @@ -3,6 +3,7 @@ //buffer data is the data you bind to opengl GLuint vbhandler; //vertex buffer handle GLuint ebhandler; +GLuint texhandlers[2]; //handlers for the perspective transforms GLint projhandler; GLint tranhandler; @@ -41,7 +42,7 @@ int initglgfx() { glGenBuffers(1, &glnum); printf("%u\n",glnum); //vertex data load of the box model - float vertexdata[] = { +/* 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, @@ -54,6 +55,20 @@ int initglgfx() { 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.500000,0.500000,-0.500000, 0.000000,0.000000,-1.000000, 0.000000,1.000000,0.000000, 0.500000,0.500000,-0.500000, 0.000000,0.000000,-1.000000, 1.000000,1.000000,0.000000, -0.500000,-0.500000,-0.500000, 0.000000,0.000000,-1.000000, 0.000000,0.000000,0.000000, +-0.500000,-0.500000,-0.500000, 0.000000,0.000000,-1.000000, 0.000000,0.000000,0.000000, 0.500000,0.500000,-0.500000, 0.000000,0.000000,-1.000000, 1.000000,1.000000,0.000000, 0.500000,-0.500000,-0.500000, 0.000000,0.000000,-1.000000, 1.000000,0.000000,0.000000, +0.500000,0.500000,-0.500000, 1.000000,0.000000,0.000000, 0.000000,1.000000,0.000000, 0.500000,0.500000,0.500000, 1.000000,0.000000,0.000000, 1.000000,1.000000,0.000000, 0.500000,-0.500000,-0.500000, 1.000000,0.000000,0.000000, 0.000000,0.000000,0.000000, +0.500000,-0.500000,-0.500000, 1.000000,0.000000,0.000000, 0.000000,0.000000,0.000000, 0.500000,0.500000,0.500000, 1.000000,0.000000,0.000000, 1.000000,1.000000,0.000000, 0.500000,-0.500000,0.500000, 1.000000,0.000000,0.000000, 1.000000,0.000000,0.000000, +0.500000,0.500000,0.500000, 0.000000,0.000000,1.000000, 0.000000,1.000000,0.000000, -0.500000,0.500000,0.500000, 0.000000,0.000000,1.000000, 1.000000,1.000000,0.000000, 0.500000,-0.500000,0.500000, 0.000000,0.000000,1.000000, 0.000000,0.000000,0.000000, +-0.500000,0.500000,0.500000, 0.000000,0.000000,1.000000, 1.000000,1.000000,0.000000, -0.500000,-0.500000,0.500000, 0.000000,0.000000,1.000000, 1.000000,0.000000,0.000000, 0.500000,-0.500000,0.500000, 0.000000,0.000000,1.000000, 0.000000,0.000000,0.000000, +-0.500000,0.500000,0.500000, -1.000000,0.000000,0.000000, 0.000000,1.000000,0.000000, -0.500000,0.500000,-0.500000, -1.000000,0.000000,0.000000, 1.000000,1.000000,0.000000, -0.500000,-0.500000,0.500000, -1.000000,0.000000,0.000000, 0.000000,0.000000,0.000000, +-0.500000,0.500000,-0.500000, -1.000000,0.000000,0.000000, 1.000000,1.000000,0.000000, -0.500000,-0.500000,-0.500000, -1.000000,0.000000,0.000000, 1.000000,0.000000,0.000000, -0.500000,-0.500000,0.500000, -1.000000,0.000000,0.000000, 0.000000,0.000000,0.000000, +-0.500000,-0.500000,-0.500000, 0.000000,-1.000000,0.000000, 0.000000,0.000000,0.000000, 0.500000,-0.500000,0.500000, 0.000000,-1.000000,0.000000, 1.000000,1.000000,0.000000, -0.500000,-0.500000,0.500000, 0.000000,-1.000000,0.000000, 0.000000,1.000000,0.000000, +0.500000,-0.500000,-0.500000, 0.000000,-1.000000,0.000000, 1.000000,0.000000,0.000000, 0.500000,-0.500000,0.500000, 0.000000,-1.000000,0.000000, 1.000000,1.000000,0.000000, -0.500000,-0.500000,-0.500000, 0.000000,-1.000000,0.000000, 0.000000,0.000000,0.000000, +-0.500000,0.500000,-0.500000, 0.000000,1.000000,0.000000, 0.000000,0.000000,0.000000, -0.500000,0.500000,0.500000, 0.000000,1.000000,0.000000, 1.000000,0.000000,0.000000, 0.500000,0.500000,0.500000, 0.000000,1.000000,0.000000, 1.000000,1.000000,0.000000, +0.500000,0.500000,0.500000, 0.000000,1.000000,0.000000, 1.000000,1.000000,0.000000, 0.500000,0.500000,-0.500000, 0.000000,1.000000,0.000000, 0.000000,1.000000,0.000000, -0.500000,0.500000,-0.500000, 0.000000,1.000000,0.000000, 0.000000,0.000000,0.000000, }; /* float vertexdata[] = { -0.5, 0.5, -.5, // x y z @@ -106,9 +121,18 @@ int initglgfx() { glGenBuffers(1,&ebhandler); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ebhandler); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elementdata), elementdata, GL_DYNAMIC_DRAW); + //loads textures + glGenTextures(2, texhandlers); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, texhandlers[0]); + glgfxloadtexture("textures/tex.bmp"); + glActiveTexture(GL_TEXTURE1); + glBindTexture(GL_TEXTURE_2D, texhandlers[1]); + glgfxloadtexture("textures/elev.bmp"); //loads the shaders genshaders(); - + glUniform1i(glGetUniformLocation(shaderprogramhandle, "tex"), 0); + glUniform1i(glGetUniformLocation(shaderprogramhandle, "elev"), 1); } //loads shaders and models int genshaders() { @@ -166,14 +190,20 @@ 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, 6 * sizeof(float), 0); + glVertexAttribPointer(pos, 3, GL_FLOAT, GL_FALSE, 9 * 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)) ); + glVertexAttribPointer(vnorm, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void *) (3 * sizeof(float)) ); glEnableVertexAttribArray(vnorm); + GLint texcoord; + texcoord = glGetAttribLocation(shaderprogramhandle, "texcoord"); + //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(texcoord, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), (void *) (6 * sizeof(float)) ); + glEnableVertexAttribArray(texcoord); //getting uniform handler projhandler = glGetUniformLocation(shaderprogramhandle, "proj"); camhandler = glGetUniformLocation(shaderprogramhandle, "cam"); @@ -257,3 +287,26 @@ int clearglgfx() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); return 0; } +int glgfxloadtexture (const char * file) { + SDL_Surface * tsp; + tsp = SDL_LoadBMP(file); + //we're just gonna forget their handlers since loading texture data is a + //one time thing anyways + //texture to bind to, detail,format, + printf("num of pixels\n %d \n", tsp->format->BitsPerPixel); //32 bits per pixel + printf("Rmask\n %04x \n", tsp->format->Rmask); // ff000000 + printf("Gmask\n %04x \n", tsp->format->Gmask); // 00ff0000 + printf("Bmask\n %04x \n", tsp->format->Bmask); // 0000ff00 + printf("Amask\n %04x \n", tsp->format->Amask); // 000000ff + tsp = SDL_ConvertSurfaceFormat(tsp, SDL_PIXELFORMAT_RGBA32, 0); + //int w; + //int h; + //int c; + //unsigned char * data =SOIL_load_image(file, &w, &h,&c, SOIL_LOAD_BMP); + //printf("w%d\n",h); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tsp->w, tsp->h, 0, GL_RGBA, GL_UNSIGNED_BYTE,(void *) tsp->pixels); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + //drawimg(0,0,100,100,file); + SDL_FreeSurface(tsp); +} diff --git a/glgfx.h b/glgfx.h index eccb33b..34677cf 100644 --- a/glgfx.h +++ b/glgfx.h @@ -1,6 +1,7 @@ #ifndef GLGFX_H #define GLGFX_H #include "GL/glew.h" +#include "SOIL/SOIL.h" #include "GL/gl.h" #include "gfx.h" #include @@ -15,5 +16,6 @@ int setcam (float cpx, float cpy, float cpz, float clx, float cly, float clz, fl int setlight (float x, float y, float z); int quitglgfx(); int clearglgfx(); +int glgfxloadtexture (const char * file); std::string readfile(const char *); #endif diff --git a/glgfx.o b/glgfx.o index db4367d..7067275 100644 Binary files a/glgfx.o and b/glgfx.o differ diff --git a/libglgfx.a b/libglgfx.a index bad183b..0ec416b 100644 Binary files a/libglgfx.a and b/libglgfx.a differ -- cgit v1.1