I need to render some textured axis aligned cubes. Currently I'm using a 2D texture.
I want to use a 3D texture and let OpenGL automatically generate texture coordinates, to simplify things a bit.
I have some code to generate a 2x2x2 checker pattern and then render cubes with it:
#include <stdlib.h>#include <stdint.h>#include <string.h>#include <GL/gl.h>#include "image.h"#include "checkers3d.h"GLuint make_3d_checkers( GLubyte color1[3], GLubyte color2[3] ){ GLuint tex; uint8_t pixels[8*3]; /* color1, color2, color2, color1, color2, color1, color1, color2 */ memcpy( pixels, color1, 3 ); memcpy( pixels+3, color2, 3 ); memcpy( pixels+6, color2, 3 ); memcpy( pixels+9, color1, 3 ); memcpy( pixels+12, color2, 3 ); memcpy( pixels+15, color1, 3 ); memcpy( pixels+18, color1, 3 ); memcpy( pixels+21, color2, 3 ); glGenTextures( 1, &tex ); glBindTexture( GL_TEXTURE_3D, tex ); glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT ); glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT ); // Set the texture's stretching properties glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexParameteri( GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); // apply color and lighting effects to texture glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); // upload texture glPixelStorei( GL_UNPACK_ALIGNMENT, 1 ); glTexImage3D( GL_TEXTURE_3D, 0, 3, 2, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels ); return tex;}
static void draw_map_3d( void ){ glEnable( GL_TEXTURE_GEN_S ); glEnable( GL_TEXTURE_GEN_T ); glEnable( GL_TEXTURE_GEN_R ); GLenum mode = GL_OBJECT_LINEAR; glTexGeni( GL_S, GL_TEXTURE_GEN_MODE, mode ); glTexGeni( GL_T, GL_TEXTURE_GEN_MODE, mode ); glTexGeni( GL_R, GL_TEXTURE_GEN_MODE, mode ); glEnable( GL_TEXTURE_3D ); int n; Box *b; static GLuint tex = 0; if ( tex == 0 ) { GLubyte color1[3] = {DARK}; GLubyte colo2[3] = {BRIGHT}; tex = make_3d_checkers( color1, colo2 ); } glBindTexture( GL_TEXTURE_3D, tex ); for( n=0; n<map.num_boxes; n++ ) { b = &map.boxes[n]; glPushMatrix(); glTranslatef( b->centre[0], b->centre[1], b->centre[2] ); glScalef( b->size[0], b->size[1], b->size[2] ); glMatrixMode( GL_TEXTURE ); glLoadIdentity(); glTranslatef( b->centre[0], b->centre[1], b->centre[2] ); glScalef( b->size[0], b->size[1], b->size[2] ); glMatrixMode( GL_MODELVIEW ); glutSolidCube( 2.0f ); glPopMatrix(); } glMatrixMode( GL_TEXTURE ); glLoadIdentity(); glMatrixMode( GL_MODELVIEW ); glDisable( GL_TEXTURE_3D ); glDisable( GL_TEXTURE_GEN_S ); glDisable( GL_TEXTURE_GEN_T ); glDisable( GL_TEXTURE_GEN_R );}
But texture coordinates aren't generated correctly for Z axis
<image removed>
What am I doing wrong?
[Edited by - usbman3 on December 30, 2010 9:44:30 AM]