c++ - Texture not showing up with SOIL and OpenGL 3.2 -
i using soil load textures, , trying "display" them on triangle using opengl 3.2. having problem, though: nothing showing up.
this code:
#include <iostream> #include "gl/soil.h" #include "math/vec3.hpp" #include "math/mat3.hpp" #include "setup.hpp" #include "graphics/shader.hpp" #include "graphics/texture.hpp" int main() { if(!setup::initopengl()) return -1; if(!setup::openwindow("testing", 800, 600)) return -1; glfloat vertices[] = { 0.0f, 0.5f, -1.0f, 0.5f, -0.5f, -1.0f, -0.5f, -0.5f, -1.0f }; glfloat uv[] = { 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f }; shader shader("", ""); shader.compile("#version 330\nlayout(location = 0) in vec3 vertexposition_modelspace;\nlayout(location = 1) in vec2 vertexuv;\nout vec2 uv;\nvoid main() { gl_position.xyz = vertexposition_modelspace;gl_position.w = 1.0;\nuv = vertexuv; }", "#version 330 core\nout vec3 color;\nin vec2 uv;\nuniform sampler2d tex_sampler;\nvoid main() { color = texture(tex_sampler, uv).rgb; }"); gluint vertexbuffer; glgenbuffers(1, &vertexbuffer); glbindbuffer(gl_array_buffer, vertexbuffer); glbufferdata(gl_array_buffer, sizeof(vertices), vertices, gl_static_draw); gluint texbuffer; glgenbuffers(1, &texbuffer); glbindbuffer(gl_array_buffer, texbuffer); glbufferdata(gl_array_buffer, sizeof(uv), uv, gl_static_draw); gluint _tex = soil_load_ogl_texture("test.jpg", soil_load_auto, soil_create_new_id, soil_flag_ntsc_safe_rgb | soil_flag_mipmaps); std::cout << "tex = " << _tex << std::endl; glbindtexture(gl_texture_2d, _tex); gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_nearest); gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_nearest); { glclear(gl_color_buffer_bit | gl_depth_buffer_bit); shader.bind(); glenablevertexattribarray(0); glenablevertexattribarray(1); glbindbuffer(gl_array_buffer, vertexbuffer); glvertexattribpointer(0, 3, gl_float, gl_false, 0, (void*) 0); glbindbuffer(gl_array_buffer, texbuffer); glvertexattribpointer(1, 2, gl_float, gl_false, 0, (void*) 0); glactivetexture(gl_texture0); glbindtexture(gl_texture_2d, _tex); gluniform1i(glgetuniformlocation(shader.getid(), "tex_sampler"), 0); gldrawarrays(gl_triangles, 0, 3); gldisablevertexattribarray(0); gldisablevertexattribarray(1); shader.unbind(); glfwswapbuffers(); } while(glfwgetkey(glfw_key_esc) != glfw_press && glfwgetwindowparam(glfw_opened)); setup::done(); return 0; }
and readable version of shaders:
// vertex shader: #version 330 layout(location = 0) in vec3 vertexposition_modelspace; layout(location = 1) in vec2 vertexuv; out vec2 uv; void main() { gl_position.xyz = vertexposition_modelspace; gl_position.w = 1.0; uv = vertexuv; } //////////////////// // fragment shader: #version 330 core out vec3 color; in vec2 uv; uniform sampler2d tex_sampler; void main() { color = texture(tex_sampler, uv).rgb; }
when code used, nothing shows up. when use constant colour in fragment shader, color = vec3(1.0, 0.0, 0.0);
, works perfectly.
Comments
Post a Comment