opengl - GLSL, combining 2D and 3D textures -
i trying blend 3d texture 2d 1 make terrain. 3d texture has moss, sand, snow , like, interpolated enhance illusion of heights. 2d texture has orange line across meant "road". fragment shader:
# version 420 uniform sampler3d maintexture; uniform sampler2d roadtexture; void main() { vec4 diffuse3d = texture3d(maintexture, gl_texcoord[0].stp); vec4 diffuse2d = texture2d(roadtexture, gl_texcoord[1].st); // yes, aware returning 2d texture value // testing purposes // doing gl_fragcolor = diffuse3d + diffuse2d; // or other operation returns 3d texture gl_fragcolor = diffuse2d; }
and drawing call:
void terrain::draw() { glenableclientstate(gl_vertex_array); glvertexpointer(3, gl_float, sizeof(glm::vec3), &v[0].x); glenableclientstate(gl_normal_array); glnormalpointer(gl_float, sizeof(glm::vec3), &n[0].x); s.enable(); // simple gluseprogram call within shader object glclientactivetexture(gl_texture0); glenableclientstate(gl_texture_coord_array); glenable(gl_texture_3d); glbindtexture(gl_texture_3d, id_texture); s.setsampler("maintexture",0); // calls glgetuniformlocation , gluniform1i gltexcoordpointer(3, gl_float, sizeof(glm::vec3), &t[0].x); glclientactivetexture(gl_texture1); glenableclientstate(gl_texture_coord_array); glenable(gl_texture_2d); glbindtexture(gl_texture_2d, id_texture_road); s.setsampler("roadtexture",1); // same above gltexcoordpointer(2, gl_float, sizeof(glm::vec2), &t2[0].x); glpushmatrix(); glscalef(scalex,scaley,scalez); gldrawelements(gl_triangles, sizei, gl_unsigned_int, index); glpopmatrix(); s.disable(); // gluseprogram(0) gldisableclientstate(gl_vertex_array); gldisableclientstate(gl_normal_array); gldisableclientstate(gl_texture_coord_array); gldisable(gl_texture_3d); gldisable(gl_texture_2d); }
here code setsampler()
method:
void shader::setsampler(std::string name, glint value) { gluint loc = glgetuniformlocation(program, name.c_str()); if (loc>0) { gluniform1i(loc, value); } }
the result solid black color upon whole terrain. have sadly been unable find information on sampler3d, diffuse3d variable in fragment shader compute correct texture, , texture coordinates 2d texture being correcly sent fragment shader (i know because used them color terrain testing , got smooth gradinent green red, expect using first 2 coordinates). checked values passed setsampler()
method , 0 , 1, , 1 , 2 locations corresponding them.
all of can find on issue around vicinity of advice provided here, have implemented).
can assist?
edit: so, kicks, swapped texture units 2d texture became unit 0 , 3d became unit 1. 2d texture rendered. texture units passed correctly (at least in appearence) shader. clues?
vec4 diffuse3d = texture3d(maintexture, gl_texcoord[0].stp); vec4 diffuse2d = texture2d(roadtexture, gl_texcoord[1].st); gl_fragcolor = diffuse2d;
let's pretend wasn't using shaders. let's pretend writing function in c++ returns value.
int funcname(int val1, int val2) { int test1 = compute(val1); int test2 = compute(val2); return test2; }
what function return? obviously, returns compute(val2)
, completely ignoring value of test1
. won't magically combine test1
, test2
. they're separate values, , therefore, remain separate unless explicitly combine them.
just fragment shader.
shaders aren't magic; they're programming. tell them to. if say, "get value texture , don't it", dutifully that. though odds compiler optimize out texture fetch entirely.
if want "blend" of 2 textures, must blend them. must fetch each texture, use both values compute new color.
how depends entirely on you. maybe 2d texture has alpha represents how of 2d texture show. don't know; didn't describe texture looks or how plan show road in places , not in others.
Comments
Post a Comment