c++ - OpengGL Array Texture shows up distorted -


this texture

enter image description here

this how showing up

enter image description here

i getting distorted result when using array textures.

do need create mipmaps? using wrong image type?

bmp's might bot idea images using time being simplicity.

code

#include <iostream> using namespace std; #include <cstdlib> #include <stdio.h>  #include <gl/glew.h> #include <glfw/glfw3.h>  #include <glm/glm.hpp> #include <glm/gtc/matrix_transform.hpp> using namespace glm;  glfwwindow* window;  #define window_height 768 #define window_width 1024  #include "common/shaders.h" #include "common/texture.h" #include "common/controls.h"  #define buffer_offset(i) ((char *)null + (i))   static void error_callback(int error, const char* description); static void key_callback(glfwwindow* window, int key, int scancode, int action, int mods);  int main(void) {    if (!glfwinit()) {         fprintf( stderr, "failed initialize glfw\n" );     exit(exit_failure);   }      glfwwindowhint(glfw_samples, 4);     glfwwindowhint(glfw_context_version_major, 4);     glfwwindowhint(glfw_context_version_minor, 0);   glfwwindowhint(glfw_opengl_profile       , glfw_opengl_core_profile);   glfwwindowhint(glfw_opengl_forward_compat, gl_true);    window = glfwcreatewindow(window_width, window_height, "ortho", null, null);   if (!window) {         fprintf(stderr, "failed create window\n");     glfwterminate();     exit(exit_failure);   }   glfwmakecontextcurrent(window);   glfwsetkeycallback(window, key_callback);   glfwseterrorcallback(error_callback);     glfwsetinputmode(window, glfw_sticky_keys, gl_true);   glfwsetinputmode(window, glfw_cursor, glfw_cursor_normal);     glfwsetcursorpos(window, window_width/2, window_height/2);    glewexperimental = gl_true;     if (glewinit() != glew_ok) {         fprintf(stderr, "failed initialize glew\n");     glfwterminate();     exit(exit_failure);     }   if (gl_ext_texture_array){         fprintf(stderr, "gl_ext_texture_array\n");   }      glclearcolor(0.0f, 0.0f, 255.0f, 0.0f);      glenable(gl_depth_test);     gldepthfunc(gl_less);       gluint vai; // vertex array id     glgenvertexarrays(1, &vai);     glbindvertexarray(vai);    char vert[] = "shaders/triangles.vert";   char frag[] = "shaders/triangles.frag";    gluint program    = load_shaders(vert,frag);      gluint matrix_id  = glgetuniformlocation(program, "mvp");     gluint texture_id = glgetuniformlocation(program, "material");    glsizei width         = 16;   glsizei height        = 16;   glsizei layercount    = 2;   glsizei miplevelcount = 1;    unsigned char* data = bmp_data("tiles.bmp");    glgentextures(1, &texture_id);   glbindtexture(gl_texture_2d_array, texture_id);    //glubyte texels[32] =    //{        ////texels first image.        //0,   0,   0,   255,        //255, 0,   0,   255,        //0,   255, 0,   255,        //0,   0,   255, 255,        ////texels second image.        //255, 255, 255, 255,        //255, 255,   0, 255,        //0,   255, 255, 255,        //255, 0,   255, 255,   //};   //gltexstorage3d(gl_texture_2d_array, miplevelcount, gl_rgba8, width, height, layercount);   //gltexsubimage3d(gl_texture_2d_array, 0, 0, 0, 0, width, height, layercount, gl_rgba, gl_unsigned_byte, texels);    glpixelstorei(gl_unpack_row_length, 16);    gltexstorage3d(gl_texture_2d_array, 1, gl_rgb8, width, height, layercount);   gltexsubimage3d(gl_texture_2d_array, 0, 0, 0, 0, width, height, layercount, gl_rgb, gl_unsigned_byte, data);    gltexparameteri(gl_texture_2d_array,gl_texture_min_filter,gl_linear);   //gltexparameteri(gl_texture_2d_array,gl_texture_mag_filter,gl_linear);   gltexparameteri(gl_texture_2d_array,gl_texture_wrap_s,gl_clamp_to_edge);   gltexparameteri(gl_texture_2d_array,gl_texture_wrap_t,gl_clamp_to_edge);     delete [] data;      gluint vbo;     gluint uvbo;   glgenbuffers(1, &vbo);   glgenbuffers(1, &uvbo);   // prep tile   glfloat vertex[] = {      0, 1, 0,      1, 1, 0,      1, 0, 0,       0, 1, 0,      0, 0, 0,      1, 0, 0,   };   glfloat uv[] = {      0.0f , 16.0f,      16.0f, 16.0f,      16.0f, 0.0f ,       0.0f , 16.0f,      0.0f , 0.0f ,      16.0f, 0.0f ,   };    glbindbuffer(gl_array_buffer, vbo);   glbufferdata(gl_array_buffer, sizeof(vertex), vertex, gl_static_draw);    glbindbuffer(gl_array_buffer, uvbo);   glbufferdata(gl_array_buffer, sizeof(uv), uv, gl_static_draw);    gluseprogram(program);   while (!glfwwindowshouldclose(window)) {         glclear(gl_color_buffer_bit | gl_depth_buffer_bit);          computematricesfrominputs();         glm::mat4 projectionmatrix = getprojectionmatrix();         glm::mat4 viewmatrix       = getviewmatrix();         glm::mat4 modelmatrix      = glm::mat4(1.0);         glm::mat4 mvp              = projectionmatrix * viewmatrix * modelmatrix;      gluniformmatrix4fv(matrix_id, 1, gl_false, &mvp[0][0]);     gluniform1i(texture_id, 0);      // render tile     glenablevertexattribarray(0);     glenablevertexattribarray(1);      glbindbuffer(gl_array_buffer, vbo);     glvertexattribpointer(0,3,gl_float,gl_false,0,(void*)0);      glbindbuffer(gl_array_buffer, uvbo);     glvertexattribpointer(1,2,gl_float,gl_false,0,(void*)0);      gldrawarrays(gl_triangles, 0, 2*3);      gldisablevertexattribarray(0);     gldisablevertexattribarray(1);        glfwswapbuffers(window);     glfwpollevents();   }   glfwdestroywindow(window);      gldeleteprogram(program);     gldeletevertexarrays(2, &vai);    glfwterminate();   exit(exit_success); }  static void error_callback(int error, const char* description){   fputs(description, stderr); }  static void key_callback(glfwwindow* window, int key, int scancode, int action, int mods){   if (key == glfw_key_escape && action == glfw_press)       glfwsetwindowshouldclose(window, 1); } 

fragment shader

#version 400 core in vec2 uv;                      // interpolated values vertex shaders out vec3 color;                  // ouput data uniform sampler2darray material; // values stay constant whole mesh.  void main(){     color = texture(material,vec3(uv,0)).rgb; } 

vert shader

#version 400 core layout(location = 0) in vec3 vertex_pos; layout(location = 1) in vec2 vertex_uv;  out vec2 uv; uniform mat4 mvp;  void main(){     gl_position =  mvp * vec4(vertex_pos,1);   uv          = vertex_uv; } 

i ended paying solve problem me.

i needed update texture vertex's. didn't need call glpixelstorei remove that.

glfloat vertex[] = {    0, 1, 0,    1, 1, 0,    1, 0, 0,     0, 1, 0,    0, 0, 0,    1, 0, 0, }; 

a texture files boundaries (uv coordinates) 0.0 1.0 on x , y axis. if want map whole texture quad set 4 corners quad's 4 corners. following image example: http://www.c-jump.com/bcc/common/talk3/opengl/wk07_texture/const_images/texturemap.png


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -