/*---------------------------------------------------------------------- * texture3d.c GL example of the mesa 3d-texture extention to simulate * procedural texturing, it uses a lattice gradient noise and * perlin turbulence functions. * * Author: Daniel Barrero * barrero@irit.fr * * cc texture3d.c -o texture3d -lglut -lMesaGLU -lMesaGL -lX11 -lXext -lm * *----------------------------------------------------------------------- */ #include #include #include #include #include #include #ifndef M_PI #define M_PI 3.14159265358979323846 #endif #define B 256 #define SIZE 16 float noise[SIZE+1][SIZE+1][SIZE+1]; float rotX=0,rotY=0,rotZ=30; int cube=1; /***** system indepedant well behaved noise function, based on code in: "Random Number Generators: Good Ones are Hard to Find" by Stephen K. Park and Keith W. Miller in Communications of the ACM, 31, 10 (Oct. 1988) pp. 1192-1201. *******/ #define K_A 16807 #define K_M 2147483647 /* Mersenne prime 2^31 -1 */ #define K_Q 127773 /* K_M div K_A */ #define K_R 2836 /* K_M mod K_A */ double drand(long s) { static long seed; long hi, lo; if(s!=0) { seed=s; drand(0); drand(0); drand(0); } hi = seed / K_Q; lo = seed % K_Q; if ((seed = K_A * lo - K_R * hi) <= 0) seed += K_M; return ((float) seed / K_M); } void paintBox ( GLdouble x0, GLdouble x1, GLdouble y0, GLdouble y1, GLdouble z0, GLdouble z1, GLenum type) { static GLdouble n[6][3] = { {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0}, {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} }; static GLint faces[6][4] = { { 0, 1, 2, 3 }, { 3, 2, 6, 7 }, { 7, 6, 5, 4 }, { 4, 5, 1, 0 }, { 5, 6, 2, 1 }, { 7, 4, 0, 3 } }; GLdouble v[8][3], tmp; GLdouble vt[8][3]; GLint i; if (x0 > x1) { tmp = x0; x0 = x1; x1 = tmp; } if (y0 > y1) { tmp = y0; y0 = y1; y1 = tmp; } if (z0 > z1) { tmp = z0; z0 = z1; z1 = tmp; } v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0; v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1; v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0; v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1; v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0; v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1; vt[0][0] = vt[1][0] = vt[2][0] = vt[3][0] = 0.0; vt[4][0] = vt[5][0] = vt[6][0] = vt[7][0] = 1.0; vt[0][1] = vt[1][1] = vt[4][1] = vt[5][1] = 0.0; vt[2][1] = vt[3][1] = vt[6][1] = vt[7][1] = 1.0; vt[0][2] = vt[3][2] = vt[4][2] = vt[7][2] = 0.0; vt[1][2] = vt[2][2] = vt[5][2] = vt[6][2] = 1.0; for (i = 0; i < 6; i++) { glBegin(type); glNormal3dv(&n[i][0]); glVertex3dv(&v[faces[i][0]][0]); glTexCoord3dv(&vt[faces[i][0]][0]); glNormal3dv(&n[i][0]); glVertex3dv(&v[faces[i][1]][0]); glTexCoord3dv(&vt[faces[i][1]][0]); glNormal3dv(&n[i][0]); glVertex3dv(&v[faces[i][2]][0]); glTexCoord3dv(&vt[faces[i][2]][0]); glNormal3dv(&n[i][0]); glVertex3dv(&v[faces[i][3]][0]); glTexCoord3dv(&vt[faces[i][3]][0]); glEnd(); } } void drawCube(float size) { paintBox(-size/2., size/2., -size/2., size/2.,-size/2., size/2.,GL_QUADS); } float noise31(float vec[3]) { long tmp; tmp= 103*vec[0]+107*vec[1]+109*vec[2]; return (float)(((rand()*tmp) % ( B + B)) - B) / B; } void initNoise() { float tmp,u,v; long i,j, k, ii,jj,kk; srand(1); drand(1107); for (i=0; ib)?b:x)) void calcMarble(unsigned char *voxels,int tex_width,int tex_height,int tex_depth) { unsigned char *vp; int i,j,k,l,m; float vec[3],vecs[3],vect[3]; float tmp,tmp1,marble,f; initNoise(); vp=voxels; vecs[2]=1.0/tex_depth; vecs[1]=1.0/tex_height; vecs[0]=1.0/tex_width; for (i=0,vec[0]=0;i Flat",1); break; case 1: glShadeModel(GL_FLAT); glutPostRedisplay(); glutChangeToMenuEntry(1,"Flat ---> Smooth",0); break; case 2: glDisable(GL_TEXTURE_3D_EXT); glutPostRedisplay(); glutChangeToMenuEntry(2,"Flat ---> Texture3D",3); break; case 3: glEnable(GL_TEXTURE_3D_EXT); glutPostRedisplay(); glutChangeToMenuEntry(2,"Texture 3D --> Flat",2); break; case 4: glDisable(GL_TEXTURE_GEN_S); glDisable(GL_TEXTURE_GEN_T); glDisable(GL_TEXTURE_GEN_R); glutPostRedisplay(); glutChangeToMenuEntry(3,"Texture coord Fixed --> Gen",5); break; case 5: glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_GEN_T); glEnable(GL_TEXTURE_GEN_R); glutPostRedisplay(); glutChangeToMenuEntry(3,"Texture coord Gen --> fixed",4); break; case 6: cube=0; glutPostRedisplay(); glutChangeToMenuEntry(4,"Teapot --> Cube",7); break; case 7: cube=1; glutPostRedisplay(); glutChangeToMenuEntry(4,"Cube --> Teapot",6); break; case 20: exit(0); break; default: break; } } void main( int argc, char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutCreateWindow("3d TextureEXT Test"); init(); glutDisplayFunc(display); glutReshapeFunc(resize); glutKeyboardFunc(keyboard); glutCreateMenu(menu); glutAddMenuEntry("Smooth --> Flat ",1); glutAddMenuEntry("Texture3D --> Flat ",2); glutAddMenuEntry("Texture Gen --> Fixed",4); glutAddMenuEntry("Cube --> Teapot",6); glutAddMenuEntry("Exit",20); glutAttachMenu(GLUT_RIGHT_BUTTON); printhelp(); glutMainLoop(); }