c++ - OpenGL point sprites render as points and not textures -
here's code
vertex shader
#version 430 in vec3 position; uniform mat4 mvp; void main() { gl_position = mvp * vec4(position, 1.0); }
fragment shader
#version 430 out vec4 outputcolor; uniform sampler2d tex; void main() { outputcolor = texture(tex, gl_pointcoord); }
init
void init() { glpointsize(20.0f); gluint tex; glgentextures(1, &tex); glbindtexture(gl_texture_2d, tex); gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_linear); gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_linear); bmp.open("c:\\users\\alon\\desktop\\star.bmp"); bmp.readpixels(); unsigned char *pixels = new unsigned char[bmp.neededbuffersize()]; bmp.assignpixels(pixels); glteximage2d(gl_texture_2d, 0, gl_rgb, bmp.getwidth(), bmp.getheight(), 0, gl_rgb, gl_unsigned_byte, pixels); delete[] pixels; glclearcolor(0.2f, 0.2f, 0.2f, 1.0f); glenable(gl_depth_test); glpolygonmode(gl_front_and_back, gl_fill); glblendfunc(gl_src_alpha, gl_one_minus_src_alpha); gluint vaoid; glgenvertexarrays(1, &vaoid); glbindvertexarray(vaoid); position_index = glgetattriblocation(program, "position"); mvp_location = glgetuniformlocation(program, "mvp"); // x y z s t glfloat vertices[] = {-0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.5f, 0.5f, 0.0f, 0.5f, 0.5f, 0.0f, -0.5f, 0.5f, 0.0f, -0.5f, -0.5f, 0.0f}; glgenbuffers(1, &vbo); glbindbuffer(gl_array_buffer, vbo); glbufferdata(gl_array_buffer, sizeof(vertices), vertices, gl_static_draw); glvertexattribpointer(position_index, 3, gl_float, gl_false, 0, 0); glbindbuffer(gl_array_buffer, 0); }
render
void render() { glclear(gl_color_buffer_bit | gl_depth_buffer_bit); glenablevertexattribarray(position_index); glbindbuffer(gl_array_buffer, vbo); float c; if(btime) c = (float)std::clock() * 2.0f / clocks_per_sec; else c = 0.0f; glm::mat4 modelview = glm::rotate(-c * 50.0f, glm::vec3(0.0f, 1.0f, 0.0f)); modelview = glm::translate(glm::vec3(0.0f + ftranslatex, 0.0f, -1.75f + ftranslatez)) * modelview; glm::mat4 projection = glm::perspective(60.0f, 16.0f/9.0f, 0.10f, 100.0f); //perspective glm::mat4 mvp = projection * modelview; gluniformmatrix4fv(mvp_location, 1, gl_false, &mvp[0][0]); gldrawarrays(gl_points, 0, 6); glbindbuffer(gl_array_buffer, 0); gldisablevertexattribarray(position_index); glutswapbuffers(); if(glgeterror() != gl_no_error) exit(1); }
the star image is:
output:
is there i'm doing wrong?
in compatbility profiles (or version of gl before 3.1), behavior want occurs when gl_point_sprite
enabled. in core profile, state removed, , gl_pointcoord
behaves if gl_point_sprite
enabled.
to fix problem, must call glenable (gl_point_sprite)
.
Comments
Post a Comment