c++ - Open GL texture isnt rendering -


im trying use opengl render texture on quad instance of model class. however, appears black screen. when solid color used, quad renders fine purely texture isn't being rendered properly.

this main method:

#include <gl/glew.h> #include "src/window.h" #include "src/graphics/model.h" #include "src/graphics/texture.h"  int main(){      window window("test", 640, 360);     window.setclearcolor(vec4(0.0f, 0.0f, 0.0f, 1.0f));      const char *vertshader =             "#version 330 core\n"                     "layout (location = 0) in vec2 position;\n"                     "layout (location = 1) in vec4 color;\n"                     "layout (location = 2) in vec2 texcoords;\n"                     "out vec4 color;\n"                     "out vec2 texcoords;\n"                     "void main(){\n"                     "gl_position = vec4(position, 0.0f, 1.0f);\n"                     "texcoords = texcoords;\n"                     "color = color;\n"                     "}";      const char *fragshader =             "#version 330 core\n"                     "in vec4 color;\n"                     "in vec2 texcoords;\n"                     "uniform sampler2d texture;"                     "out vec4 colorfinal;\n"                     "void main(){\n"                     "colorfinal = color;\n"                     "}";      gluint vert = glcreateshader(gl_vertex_shader);     gluint frag = glcreateshader(gl_fragment_shader);      glshadersource(vert, 1, &vertshader, null);     glcompileshader(vert);      int  success;     char infolog[512];     glgetshaderiv(vert, gl_compile_status, &success);     if(!success)     {         glgetshaderinfolog(vert, 512, null, infolog);         std::cout << "error::shader::vertex::compilation_failed\n" << infolog << std::endl;     }       glshadersource(frag, 1, &fragshader, null);     glcompileshader(frag);      glgetshaderiv(frag, gl_compile_status, &success);      if(!success)     {         glgetshaderinfolog(frag, 512, null, infolog);         std::cout << "error::shader::vertex::compilation_failed\n" << infolog << std::endl;     }      gluint program = glcreateprogram();     glattachshader(program, vert);     glattachshader(program, frag);     gllinkprogram(program);     gluseprogram(program);      std::vector<glfloat> vertices = {             // positions             0.5f,  0.5f,             0.5f, -0.5f,             -0.5f, -0.5f,             -0.5f, -0.5f,             -0.5f,  0.5f,             0.5f,  0.5f     };      std::vector<glfloat> texcoords = {1.0f, 1.0f,                                       1.0f, 0.0f,                                       0.0f, 0.0f,                                       0.0f, 0.0f,                                       0.0f, 1.0f,                                       1.0f, 1.0f};      model model(vertices, {1.0f, 0.0f, 0.0f, 1.0f}, "/home/callum/documents/open gl/sandbox/res/test.png");     model.adddata(2, texcoords); #if 0 //    gluint vbo; //    glgenbuffers(1, &vbo); //    glbindbuffer(gl_array_buffer, vbo); //    glbufferdata(gl_array_buffer, sizeof(vertices[0]) * vertices.size() //            , vertices.data(), gl_static_draw); //    glvertexattribpointer(0, 3, gl_float, gl_false, 0, (glvoid*)0); //    glenablevertexattribarray(0); #endif     while(!window.isclosed()){         glgeterror();         window.clear();         model.draw();         glenum error = glgeterror();         if(error != gl_no_error){             std::cout << gluerrorstring(error) << std::endl;         }         window.update();     }      return 0; } 

the model header file:

// // created callum on 15/08/17. //  #ifndef minecraftclone_model_h #define minecraftclone_model_h  #include <vector> #include <gl/glew.h> #include "../glm.h" #include "texture.h"  class model{ public:     model(std::vector<glfloat>vertices, vec4 color, const std::string& filepath);     model();     ~model();      void adddata(int dimensions, std::vector<glfloat> data);      void draw(); protected:     std::vector<glfloat> m_vertices;     vec4 m_color;      gluint m_buffercount;     gluint m_vao;     texture m_texture;     std::vector<gluint> m_buffers; };  #endif //minecraftclone_model_h 

the model implementation file:

// // created callum on 15/08/17. //  #include <iostream> #include "model.h"  model::model(std::vector<glfloat>vertices, vec4 color, const std::string& filepath)         : m_vertices(vertices), m_color(color), m_texture(filepath){     m_buffercount = 0;     glgenvertexarrays(1, &m_vao);     adddata(2, m_vertices);      std::vector<glfloat> colordata;     for(int = 0; < m_vertices.size() / 2; i++){         colordata.push_back(1.0f);         colordata.push_back(0.0f);         colordata.push_back(0.0f);         colordata.push_back(1.0f);     }      adddata(4, colordata); }  model::model(): m_texture(""){     m_buffercount = 0;     glgenvertexarrays(1, &m_vao); }  model::~model(){}  void model::adddata(int dimensions, std::vector<glfloat> data){     glbindvertexarray(m_vao);     gluint vbo;     glgenbuffers(1, &vbo);     glbindbuffer(gl_array_buffer, vbo);     glbufferdata(gl_array_buffer, data.size() * sizeof(data[0]), data.data(), gl_static_draw);     glvertexattribpointer(m_buffercount, dimensions, gl_float, gl_false, 0, (glvoid *) 0);     glenablevertexattribarray(m_buffercount++);     m_buffers.push_back(vbo);     glbindbuffer(gl_array_buffer, 0);     glbindvertexarray(0); }  void model::draw(){ //    glactivetexture(gl_texture0);     m_texture.bind();     glbindvertexarray(m_vao);     gldrawarrays(gl_triangles, 0, 6);     glbindvertexarray(0); } 

the texture header file:

// // created callum on 17/08/17. //  #ifndef minecraftclone_texture_h #define minecraftclone_texture_h  #include <string> #include <gl/glew.h>  class texture{ public:     texture(const std::string& filepath);     ~texture();     void bind() const;     void unbind() const;  private:     gluint m_textureid;     int m_width;     int m_height;     int m_channels; };   #endif //minecraftclone_texture_h 

the texture implementation file:

// // created callum on 17/08/17. //  #include "texture.h"  #include <iostream>  #include <soil/soil.h>  texture::texture(const std::string &filepath){     unsigned char *image = soil_load_image(filepath.c_str(), &m_width, &m_height, &m_channels, soil_load_auto);     if(image == null){         std::cout << "unable load texture image" << std::endl;     }      //check if image contained alpha channel     bool alpha;     alpha = (m_channels != 3);      glgentextures(1, &m_textureid);     glbindtexture(gl_texture_2d, m_textureid);      gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_nearest);     gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_nearest);     gltexparameteri(gl_texture_2d,gl_texture_wrap_s, gl_repeat);     gltexparameteri(gl_texture_2d,gl_texture_wrap_t, gl_repeat); //    glteximage2d(gl_texture_2d, 0, alpha ? gl_rgba : gl_rgb, m_width, m_height, 0,alpha ? gl_rgba : gl_rgb, gl_unsigned_byte, image);     glteximage2d(gl_texture_2d, 0, gl_rgb, m_width, m_height, 0,gl_rgb, gl_unsigned_byte, image);      glbindtexture(gl_texture_2d, 0);      soil_free_image_data(image); }  texture::~texture(){     gldeletetextures(1, &m_textureid); }  void texture::bind() const{     glbindtexture(gl_texture_2d, m_textureid); }  void texture::unbind() const{     glbindtexture(gl_texture_2d, 0); } 


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -