diff options
author | knolax <1339802534.kk@gmail.com> | 2017-02-28 11:05:31 -0500 |
---|---|---|
committer | knolax <1339802534.kk@gmail.com> | 2017-02-28 11:05:31 -0500 |
commit | cf3f284a5f24bd02006e9ff929cc29d10c428a3e (patch) | |
tree | e23fabd9a851ffdce0b48783c1903f40b6bfe573 | |
parent | 0c2c626fbdfd7836f086eb8f4039a394368b7974 (diff) |
textures loaded properly now but without functional alpha layer.
-rw-r--r-- | example/fragmentshader.glsl | 8 | ||||
-rwxr-xr-x | example/main | bin | 49568 -> 54072 bytes | |||
-rw-r--r-- | example/makefile | 2 | ||||
-rw-r--r-- | example/textures/elev.bmp | bin | 0 -> 4000138 bytes | |||
-rw-r--r-- | example/textures/elev.png | bin | 0 -> 198073 bytes | |||
-rw-r--r-- | example/textures/tex.bmp | bin | 0 -> 4000138 bytes | |||
-rw-r--r-- | example/textures/tex.png | bin | 0 -> 164345 bytes | |||
-rw-r--r-- | example/vertexshader.glsl | 3 | ||||
-rw-r--r-- | gen.cpp | 37 | ||||
-rwxr-xr-x | gennew | bin | 0 -> 8496 bytes | |||
-rw-r--r-- | glgfx.cpp | 61 | ||||
-rw-r--r-- | glgfx.h | 2 | ||||
-rw-r--r-- | glgfx.o | bin | 58104 -> 60880 bytes | |||
-rw-r--r-- | libglgfx.a | bin | 62288 -> 65108 bytes |
14 files changed, 91 insertions, 22 deletions
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 Binary files differindex 51bb73a..14fd1e1 100755 --- a/example/main +++ b/example/main 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 Binary files differnew file mode 100644 index 0000000..cb9fec6 --- /dev/null +++ b/example/textures/elev.bmp diff --git a/example/textures/elev.png b/example/textures/elev.png Binary files differnew file mode 100644 index 0000000..68c4308 --- /dev/null +++ b/example/textures/elev.png diff --git a/example/textures/tex.bmp b/example/textures/tex.bmp Binary files differnew file mode 100644 index 0000000..620d364 --- /dev/null +++ b/example/textures/tex.bmp diff --git a/example/textures/tex.png b/example/textures/tex.png Binary files differnew file mode 100644 index 0000000..79d9a1c --- /dev/null +++ b/example/textures/tex.png 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; } @@ -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++; Binary files differ@@ -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); +} @@ -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 <iostream> @@ -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 Binary files differBinary files differ |