OpenGL multiple textures with VBOs -
i'm trying figure out how render object (a cube) different textures each face. simplicities sake, have 2 textures applied 3 faces of cube each. understand should using texture arrays 3 coordinates represent relevant texture used. i'm unsure of how , how code fragment shader.
here relevant part of init()
function:
final string texturename = model.gettextures().get(i).texturename; final filetexture texturegenerator = new filetexture(this.getclass().getresourceasstream(texturename), true, context); textureid = texturegenerator.gettextureid(); width = texturegenerator.getwidth(); height = texturegenerator.getheight(); texturemap.put(model.gettextures().get(i).matname, textureid); context.getgl().glactivetexture(gl.gl_texture0 + i); context.getgl().glbindtexture(gl.gl_texture_2d, textureid);
i confused here because orange book (opengl shading language) gives examples in glactivetexture
, glbindtexture
used glsl common mistakes says shouldn't this.
from there, display()
function looks this:
gl.glbindbuffer(gl.gl_array_buffer, getvertexbufferobject()); gl.glbufferdata(gl.gl_array_buffer, getnoofvertices() * 3 * 4, getvertices(), gl.gl_stream_draw); gl.glbindbuffer(gl.gl_array_buffer, gettexcoordbufferobject()); gl.glbufferdata(gl.gl_array_buffer, getnoofvertices() * 2 * 4, gettexcoords(), gl.gl_stream_draw); gl.glbindbuffer(gl.gl_element_array_buffer, getindicesbufferobject()); gl.glbufferdata(gl.gl_element_array_buffer, getnoofindices() * 4, getindices(), gl.gl_stream_draw); gl.glbindbuffer(gl.gl_array_buffer, getcolorbufferobject()); gl.glbufferdata(gl.gl_array_buffer, getnoofvertices() * 4 * 4, getcolors(), gl.gl_stream_draw); layertextureshader.use(gl); gl.glenableclientstate(gl.gl_vertex_array); gl.glbindbuffer(gl.gl_array_buffer, getvertexbufferobject()); gl.glvertexpointer(3, gl.gl_float, 0, 0); gl.glenableclientstate(gl.gl_color_array); gl.glbindbuffer(gl.gl_array_buffer, mask ? getmaskcolorbufferobject() : getcolorbufferobject()); gl.glcolorpointer(4, gl.gl_float, 0, 0); gl.glclientactivetexture(gl.gl_texture0); gl.glenableclientstate(gl.gl_texture_coord_array); gl.gltexcoordpointer(3, gl.gl_float, 0, 0); gl.glclientactivetexture(gl.gl_texture1); gl.glenableclientstate(gl.gl_texture_coord_array); gl.gltexcoordpointer(3, gl.gl_float, 0, 0); gl.glbindbuffer(gl.gl_element_array_buffer, getindicesbufferobject()); final int count = getnoofindices(); gl.gldrawelements(gl.gl_triangles, count, gl.gl_unsigned_int, 0); gl.glbindbuffer(gl.gl_element_array_buffer, 0); gl.glbindbuffer(gl.gl_array_buffer, 0); gl.glclientactivetexture(gl.gl_texture0); gl.gldisableclientstate(gl.gl_texture_coord_array); gl.glclientactivetexture(gl.gl_texture1); gl.gldisableclientstate(gl.gl_texture_coord_array); gl.gldisableclientstate(gl.gl_vertex_array); gl.gldisableclientstate(gl.gl_color_array); gl.gldisableclientstate(gl.gl_texture_coord_array); layertextureshader.release(gl);
i unsure of put in glsl shaders. vertex shader has standard gl_texcoord[0] = gl_multitexcoord0;
, fragment shader looks like:
uniform sampler2d texture; void main() { gl_fragcolor = texture2d(texture, gl_texcoord[0].st); }
how instruct fragment shader on texture use? assume it's when i'm populating vertex, index, textures buffers etc , passing in 3rd texture coordinate each point? value of 3rd coordinate value of relevant texture coordinate? hope question makes sense , help. chris
what looking cube map. in opengl, can define 6 textures @ once (representing size sides of cube) , map them using 3d texture coordinates instead of common 2d texture coordinates. simple cube, texture coordinates same vertices' respective normals. (if texturing plane cubes in manner, can consolidate normals , texture coordinates in vertex shader, too!) cube maps simpler trying bind 6 distinct textures simultaneously way doing right now.
gluint mhandle; glgentextures(1, &mhandle); // create texture // note target being used instead of gl_texture_2d! gltextparameteri(gl_texture_cube_map, gl_texture_mag_filter, gl_linear); gltextparameteri(gl_texture_cube_map, gl_texture_wrap_r, gl_clamp_to_edge); gltextparameteri(gl_texture_cube_map, gl_texture_wrap_s, gl_clamp_to_edge); gltextparameteri(gl_texture_cube_map, gl_texture_wrap_t, gl_clamp_to_edge); glbindtexture(gl_texture_cube_map, mhandle); // now, load in 6 distinct images. need same dimensions! // notice targets being specified: 6 sides of cube map. glteximage2d(gl_texture_cube_map_positive_x, 0, gl_rgba, width, height, 0, format, gl_unsigned_byte, data1); glteximage2d(gl_texture_cube_map_negative_x, 0, gl_rgba, width, height, 0, format, gl_unsigned_byte, data2); glteximage2d(gl_texture_cube_map_positive_y, 0, gl_rgba, width, height, 0, format, gl_unsigned_byte, data3); glteximage2d(gl_texture_cube_map_negative_y, 0, gl_rgba, width, height, 0, format, gl_unsigned_byte, data4); glteximage2d(gl_texture_cube_map_positive_z, 0, gl_rgba, width, height, 0, format, gl_unsigned_byte, data5); glteximage2d(gl_texture_cube_map_negative_z, 0, gl_rgba, width, height, 0, format, gl_unsigned_byte, data6); glgeneratemipmap(gl_texture_cube_map); gltextparameteri(gl_texture_cube_map, gl_texture_min_filter, gl_linear_mipmap_linear); // , of course, after done using textures... gldeletetextures(1, &mhandle);
now, when doing shaders, need vertex shader accept and/or pass 3d coordinates (vec3) instead of 2d coordinates (vec2).
// old glsl style attribute vec3 intexturecoordinate; varying vec3 vtexturecoordinate; // more recent glsl in vec3 intexturecoordinate; out vec3 vtexturecoordinate;
in example, vertex shader assign vtexturecoordinate = intexturecoordinate. fragment shader needs accept texture coordinate , sample cube map uniform.
uniform samplercube cubemap; ... gl_fragcolor = texturecube(cubemap, vtexturecoordinate);
whew! lot. did leave out?
Comments
Post a Comment