c++ - OpenGL 3.3 texture mapping triangle -
i have had little luck texturing triangle in opengl 3.3 (core) on last few days. can render vertices , colors, texturing seems problematic. current task/goal vertices , texture coordinates (no colors).
since create test texture hand, should show 16x16 white/red checkered triangle.
strange behavior: if use gl_clamp_to_edge, it's white. if use gl_repeat, comes out pink (white + red)...
vertex shader:
#version 330 layout (location = 0) in vec3 vertpos; layout (location = 1) in vec2 texcoord; out vec2 tex; uniform mat4 viewmatrix, projmatrix; void main() { gl_position = projmatrix * viewmatrix * vec4(vertpos,1.0); tex = texcoord; };
fragment shader:
#version 330 out vec4 color; in vec2 tex; uniform sampler2d texsampler; void main() { color = texture(texsampler, tex); };
state initialization code (these in order shown):
glpixelstorei(gl_unpack_alignment,1); //glpixelstorei(gl_pack_alignment,1); glfrontface(gl_cw); // clockwise oriented polys front faces glcullface(gl_back); // cull out inner polygons... set culling property appropriately gllinewidth(1.0f); // wireframe glblendfunc(gl_src_alpha,gl_one_minus_src_alpha); // color 1.0f opaque, 0.0f transparent gldepthfunc(gl_lequal); gldepthrange(0,1); // default gldisable(gl_cull_face); glclearcolor(0.0f,0.0f,0.0f,1.0f); // set clear color black glpolygonmode(gl_front_and_back,gl_fill); // filled polys
texture initialization:
glgentextures(1,&texid1); glbindtexture (gl_texture_2d, texid1); gltexparameteri(gl_texture_2d,gl_texture_mag_filter,gl_linear); gltexparameteri(gl_texture_2d,gl_texture_min_filter,gl_linear); gltexparameteri(gl_texture_2d, gl_texture_base_level, 0); gltexparameteri(gl_texture_2d, gl_texture_max_level, 0); gltexparameteri(gl_texture_2d, gl_texture_wrap_s, gl_clamp_to_edge); gltexparameteri(gl_texture_2d, gl_texture_wrap_t, gl_clamp_to_edge); uint32 buf[16*16]; uint32 x; (x=0;x<16*16;) { buf[x++] = 0xffffffff; buf[x++] = 0xffff0000; } glteximage2d (gl_texture_2d, 0, gl_rgba8 , 16, 16, 0, gl_bgra, gl_unsigned_byte, (void*)buf); glbindtexture (gl_texture_2d, 0);
shader initialization:
shader1 = new_gl_shader_program(vert_src,frag_src); if (shader1) { if (link_gl_shader_program(shader1)) { projmatrixloc = glgetuniformlocation(shader1, "projmatrix"); viewmatrixloc = glgetuniformlocation(shader1, "viewmatrix"); sampler2dloc = glgetuniformlocation(shader1, "texsampler"); glbindfragdatalocation( shader1, 0, "color" ); // added. } else { print("link error: %s\n",get_gl_shader_msg()); delete_gl_shader_program(shader1); shader1 = 0; } } else print("compile/misc error: %s\n",get_gl_shader_msg()); if (!shader1) return;
buffer data initialization:
typedef struct mesh_vert_3d { float x,y,z; float u,v; }mesh_vert_3d; mesh_vert_3d meshbuf[3]; // total of 15 floats meshbuf[0].x = 0.0f; meshbuf[0].y = 0.0f; meshbuf[0].z = 0.0f; meshbuf[1].x = 1.0f; meshbuf[1].y = 0.0f; meshbuf[1].z = 0.0f; meshbuf[2].x = 1.0f; meshbuf[2].y = 0.0f; meshbuf[2].z = 1.0f; meshbuf[0].u = 0.0f; meshbuf[0].v = 1.0f; meshbuf[1].u = 1.0f; meshbuf[1].v = 1.0f; meshbuf[2].u = 1.0f; meshbuf[2].v = 0.0f; glgenbuffers(1, &vboid); glbindbuffer(gl_array_buffer, vboid); glbufferdata(gl_array_buffer, sizeof(meshbuf), meshbuf, gl_static_draw); glbindbuffer(gl_array_buffer, 0); glgenvertexarrays(1, &vaohandle); glbindvertexarray(vaohandle); glbindbuffer(gl_array_buffer, vboid); glvertexattribpointer(0, 3, gl_float, gl_false, sizeof(float)*5, null); glenablevertexattribarray(0); glvertexattribpointer(1, 2, gl_float, gl_false,sizeof(float)*5, (void*)(sizeof(float)*3)); glenablevertexattribarray(1); glbindvertexarray(0); gldisablevertexattribarray(0); gldisablevertexattribarray(1); glbindbuffer(gl_array_buffer, 0);
render:
gls_begin_3d(&glscene); use_gl_shader_program(shader1); gluniformmatrix4fv(projmatrixloc, 1, gl_false, gls_get_projection_matrix()); gluniformmatrix4fv(viewmatrixloc, 1, gl_false, gls_get_modelview_matrix()); glactivetexture(gl_texture0); glbindtexture (gl_texture_2d, texid1); gluniform1i(sampler2dloc, 0); glbindvertexarray(vaohandle); gldrawarrays(gl_triangles, 0, 3 ); glbindtexture (gl_texture_2d, 0); use_gl_shader_program(0); gls_end_3d(&glscene);
issue 1:
you should bind fragment shader output variable color
fragment data location:
glbindfragdatalocation( shader1, 0, "color" );
otherwise opengl not know results of shader program located.
issue 2:
you create second buffer object texture coordinates: glgenbuffers(1, &tboid);
however, should put single vbo.
issue 3:
you pass null
glvertexattribpointer(1, 2, gl_float, gl_false,sizeof(float)*5, null);
however, beginning of texture coordinates not 0. sizeof(float)*3
.
issue 4:
in vertex shader, have out vec2 texcoordout;
. however, in fragment shader, use in vec2 tex;
. names should match.
Comments
Post a Comment