c++ - OpenGL Trying to render 2 textures, but only 1 does? -
i trying render 2 textures in opengl 3. created 2 arrays of vertices of glfloat type,generated , bound buffers etc.
note: texture loading function working fine,i have loaded texture before, need 2 textures rendered @ same time.
then load textures this:
gluint grass = texture.loadtexture("grass.bmp"); gluint grassloc = glgetuniformlocation(programid, "grasssampler"); gluniform1i(grassloc, 0); gluint crate = texture.loadtexture("crate.bmp"); gluint crateloc = glgetuniformlocation(programid, "cratesampler"); gluniform1i(crateloc, 1);
this how draw them:
glactivetexture(gl_texture0); glbindtexture(gl_texture_2d, grass); gldrawarrays(gl_triangles, 0, 6); glactivetexture(gl_texture1); glbindtexture(gl_texture_2d, crate); gldrawarrays(gl_triangles, 2, 6);
vertex shader:
#version 330 core layout(location = 0) in vec3 grassposition; layout(location = 1) in vec2 grassuvposition; layout(location = 2) in vec3 crateposition; layout(location = 3) in vec2 crateuvposition; out vec2 grassuv; out vec2 crateuv; uniform mat4 mvp; void main(){ gl_position = mvp * vec4(grassposition,1); gl_position = mvp * vec4(crateposition,1); grassuv = grassuvposition; crateuv = crateuvposition; }
fragment shader:
#version 330 core in vec2 grassuv; in vec2 crateuv; out vec3 grasscolor; out vec3 cratecolor; uniform sampler2d grasssampler; uniform sampler2d cratesampler; void main(){ cratecolor = texture(grasssampler, grassuv).rgb; grasscolor = texture(cratesampler, crateuv).rgb; }
can see doing wrong?
edit: trying render 2 different textures on 2 different vaos
you're kinda doing everything wrong; it's hard pick out 1 thing.
your shaders they're tying take 2 positions , 2 texture coordinates, presumably generate 2 triangles, sample 2 textures , write colors 2 different images.
that's not how works. unless use geometry shader (and please not take endorsement), call gldrawarrays(gl_triangles, 0, 6);
render 2 triangles, no matter vs or fs's say.
a vertex has 1 position. writing gl_position
twice overwrite previous value, writing variable twice in c++ would. , number of triangles rendered defined number of vertices. vertex shader cannot create vertices. can't destroy them (though, through gl_culldistance
, can potentially cull whole primitives).
it not clear mean "i need 2 textures rendered @ same time." or more point, "at same time" refers to. don't know code ought trying do.
given data vertex shader expects, looks have 2 separate sets of triangles, own positions , texture coordinates. want render 1 set of triangles 1 texture, render set different texture.
so... that. instead of having vaos send 2 positions , 2 texture coordinates, send one. vs should take 1 position/texcoord, , fs should take single texture , write single output. difference determined vao active , texture bound texture unit 0 @ time issue render call.
if intend write different output images, way fs suggests, change fbos between rendering well.
if however, goal have same triangle use 2 textures 2 mappings, writing separate results 2 images, can too. difference provide single position, , textures must bound both texture units 0 , 1 when issue rendering command.
Comments
Post a Comment