PDA

View Full Version : Texture 3D



gramyfill
09-20-2009, 07:47 PM
Hello!
I am new on this forums, I have a poroblem with texture 3D, i need know how to aplicated texture 3D on cube. Here is the source, when i compiler the source don't have errors but the texture don't display on the cube.

void Cuadro (void) {
glBegin (GL_QUADS);
glTexCoord3d(0.0,0.0,-5.0); glVertex2d(-2.0,-1.5);
glTexCoord3d(0.0,1.0,-5.0); glVertex2d(+2.0,-1.5);
glTexCoord3d(1.0,1.0,-5.0); glVertex2d(+2.0,+1.5);
glTexCoord3d(1.0,0.0,-5.0); glVertex2d(-2.0,+1.5);
glEnd();
}
//coordenadas de la textura

//tamaņo de la textura
#define AnchoImagen 256
#define AltoImagen 256
#define zImagen 512
GLfloat CargarImagen [16][16][16][3];
//GLfloat Imagen [AnchoImagen][AltoImagen][zImagen];

//angulos de rotacion
GLfloat rotx = 0.0;
GLfloat roty = 0.0;
GLfloat rotz = 0.0;

//cargar una imagen en formato RAW
void CargaImagen(char *fileName)
{
int i, j,k;
float norm;
FILE *file;
float size;
size=AnchoImagen*AltoImagen*zImagen;
file=fopen(fileName,"rb");
if (file==NULL)
{ printf("Error fichero\n"); exit(1); }

fread(CargarImagen, sizeof(float), size, file);

//obtener el valor maximo en la imagen

for (i = 0; i < 16; i++) {
for (j = 0; j <16 ; j++) {
for (k= 0; k <16 ; k++) {

CargarImagen[i][j][k][0]=k*17;
CargarImagen[i][j][k][1]=j*17;
CargarImagen[i][j][k][2]=i*17;

}
}
}
/*for (i = 0; i < AnchoImagen; i++) {
for (j = 0; j <AltoImagen ; j++) {
for (k= 0; k <zImagen ; k++) {
norm=CargarImagen[i][j][k];
CargarImagen[i][j][k] = norm/imax;

}
}*/



fclose(file);
}




//dibuja la superficie
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_MODELVIEW_MATRIX );
glLoadIdentity();
glPushMatrix();
glTranslated(0.0, 0.0, -12.0);
glRotatef(rotx, 1.0, 0.0, 0.0);
glRotatef(roty, 0.0, 1.0, 0.0);
glRotatef(rotz, 0.0, 0.0, 1.0);

// Cuadro();
printf("imax = %f",imax);
glPopMatrix();
glFlush();
}

void init(void)
{
glClearColor(0.05f, 0.2f, 0.4f, 1.0f);

//carga la textura
CargaImagen("C:\\RatH0006_05.dat");
glEnable(GL_DEPTH_TEST);

glTexImage3D(GL_TEXTURE_3D, //Specifies the target texture.
0, //Specifies the level-of-detail number
4, //Specifies the number of color components in the texture.
AnchoImagen, // Specifies the width of the texture image including the border if any.
AltoImagen, // Specifies the height of the texture image including the border if any
zImagen, //Specifies the depth of the texture image including the border if any.
0, //Specifies the width of the border
GL_ALPHA, //Specifies the format of the pixel data
GL_FLOAT, // Specifies the data type of the pixel data.
CargarImagen); // Specifies a pointer to the image data in memory.

glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glEnable(GL_TEXTURE_3D);
}

please help me.
Thanks

gramyfill
09-20-2009, 07:50 PM
sorry.
I put the wrong source. this is the correct.

#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <GL/glext.h> //libreria para glTextImage3D
#include <math.h>
#include <stdio.h>
PFNGLTEXIMAGE3DPROC glTexImage3D;
float imax=0,valor;
GLfloat angle = 0.0;
//coordenadas de la textura

void Cuadro (void) {

glBegin (GL_QUADS);
//glTexCoord3d(0.0,0.0,0.0); glVertex3d(-2.5,-1.5,0.0);
// glTexCoord3d(0.0,1.0,0.0); glVertex3d(+2.5,-1.5,0.0);
// glTexCoord3d(1.0,1.0,0.0); glVertex3d(+2.5,+1.5,0.0);
// glTexCoord3d(1.0,0.0,0.0); glVertex3d(-2.5,+1.5,0.0);
// Frente
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-2.0f, -2.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 2.0f, -2.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 2.0f, 2.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-2.0f, 2.0f, 1.0f);
// parte de Atras
glNormal3f( 0.0f, 0.0f,-1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-2.0f, -2.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-2.0f, 2.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 2.0f, 2.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 2.0f, -2.0f, -1.0f);
// Arriba
glNormal3f( 0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-2.0f, 2.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-2.0f, 2.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 2.0f, 2.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 2.0f, 2.0f, -1.0f);
// Abajo
glNormal3f( 0.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-2.0f, -2.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 2.0f, -2.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 2.0f, -2.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-2.0f, -2.0f, 1.0f);
// lado Derecho
glNormal3f( 1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 2.0f, -2.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 2.0f, 2.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 2.0f, 2.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 2.0f, -2.0f, 1.0f);
// Lado Izquierdo
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-2.0f, -2.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-2.0f, -2.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-2.0f, 2.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-2.0f, 2.0f, -1.0f);
glEnd();


glEnd();
}
//coordenadas de la textura


//tamaņo de la textura
#define widthImagen 256
#define heigthImagen 256
#define depthImagen 512
GLuint texture;
GLfloat data[widthImagen*heigthImagen*depthImagen];
//angulos de rotacion
GLfloat rotx = 0.0;
GLfloat roty = 0.0;
GLfloat rotz = 0.0;

//cargar una imagen en formato RAW
void CargaImagen(char *fileName)
{
int i;
float norm;
FILE *file;
float size;
size=widthImagen*heigthImagen*depthImagen;

file=fopen(fileName,"rb");
if (file==NULL)
{ printf("Error fichero\n"); exit(1); }
fread(&amp;data,sizeof(float) ,size, file);

//obtener el valor maximo en la imagen
for (i =0; i <size; i++) {
valor=data[i];
//printf("\n%f",valor);
if(valor > imax)
imax = valor;

}
for (i = 0; i <size; i++) {
norm=data[i];
data[i] = norm/imax;

}

fclose(file);


}

//parametros de la fuente de luz y del material de la
//superficie
void initlights(void)
{
GLfloat ambient[] = {0.5, 0.5, 0.5, 1.0};
GLfloat position[] = {0.0, 0.0, 1.0, 0.0};

GLfloat mat_diffuse[] = {0.9, 0.9, 0.9, 1.0};
GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat mat_shininess[] = {100.0};

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_POSITION, position);

glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
}


//dibuja la superficie
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_MODELVIEW_MATRIX );
glLoadIdentity();
glPushMatrix();
glTranslated(0.0, 0.0, -12.0);
glRotatef(rotx, 1.0, 0.0, 0.0);
glRotatef(roty, 0.0, 1.0, 0.0);
glRotatef(rotz, 0.0, 0.0, 1.0);
Cuadro();
//printf("maximo =%f",imax);
glPopMatrix();
glFlush();
}
void init(void)
{
//carga la textura
glClearColor(0.05f, 0.2f, 0.4f, 1.0f);
glTexImage3D = (PFNGLTEXIMAGE3DPROC) wglGetProcAddress("glTexImage3D");
if (glTexImage3D == NULL) {
printf("Error in line %d: Couldn't load glTexImage3D function. Aborting.\n", __LINE__);
}

glEnable(GL_TEXTURE_3D);
glEnable(GL_DEPTH_TEST);

CargaImagen("C:\\RatH0006_05.dat");

glGenTextures( 1, &amp;texture ); //generate the texture with the loaded data
glBindTexture( GL_TEXTURE_3D, texture ); //bind the texture to it's array
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
GL_MODULATE ); //set texture environment parameters
glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
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);

glTexImage3D(GL_TEXTURE_3D, //Specifies the target texture.
0, //Specifies the level-of-detail number
4, //Specifies the number of color components in the texture.
widthImagen, // Specifies the width of the texture image including the border if any.
heigthImagen, // Specifies the height of the texture image including the border if any
depthImagen,
0, //Specifies the width of the border
GL_LUMINANCE, //Specifies the format of the pixel data
GL_FLOAT, // Specifies the data type of the pixel data.
&amp;data); // Specifies a pointer to the image data in memory.


}

void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(18, (GLfloat) w/(GLfloat) h, 1.0, 50.0);
glMatrixMode (GL_MODELVIEW);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);

}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 'x':
case 'X':
rotx = rotx - 10.0f;
glutPostRedisplay();
break;
case 's':
case 'S':
rotx = rotx + 10.0f;
glutPostRedisplay();
break;
case 'y':
case 'Y':
roty = roty + 10.0f;
glutPostRedisplay();
break;
case 't':
case 'T':
roty = roty - 10.0f;
glutPostRedisplay();
break;
case 'a':
case 'A':
rotz = rotz - 10.0f;
glutPostRedisplay();
break;
case 'z':
case 'Z':
rotz = rotz + 10.0f;
glutPostRedisplay();
break;

case 27:
exit(0);
break;
}
}

int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (700, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Proyeccion y1");
init ();
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();

return 0;
}

Heiko
09-21-2009, 12:19 AM
First of all: please just edit your first post if your code is not correct, before people are trying to understand the wrong code.

Second: you have to bind the texture before you are drawing the cube. So in your display function, bind the texture before calling the function that draws the quad.

Third: why do you use a 3D texture for texturing a cube? If you want each side of the cube to have a different texture, use a cubemap (that has 6 sides). If you want each side using the same texture, use a 2D texture. If you really must use a 3D texture, at least provide 3d texture coordinates (you are using 2d texture coordinates).

gramyfill
09-21-2009, 04:16 PM
Ok. sorry again for wrong source.

I have an volumetric image and i think a metod for display that images is the texture 3D. Do you have any idea how i do display this images in opengl.

DmitryM
09-21-2009, 04:45 PM
If you want to display a volumetric image as a cube, then you need to render it slice by slice from far to near one with blending.

gramyfill
09-21-2009, 04:58 PM
sorry DmitryM but i confuse.
I have in a raw file the image and i need display them. Please you can be more specifically for that I do not understand your response

DmitryM
09-21-2009, 05:47 PM
Sorry for being so brief - it consumes too much time to describe in detail and post code....

So, you have a volumetric image in raw format. You managed to load it into OpenGL's Texture3D. Now I suggest you to draw it slice by slice in order from far to near. Without blending you'll see just a nearest slice, so you need to enable some sort of blending while drawing them.

Slice, for example, can be defined as a texel set specified by equations: 0<=x,y<=1, z=z0, where z0 defines slice's depth. So the main loop will look like this:

-enable blending
-for z from 1 to 0 with step (1/size_Z):
-draw the quad with texture coordinates (0,0,z0),(1,0,z0),(1,1,z0),(0,1,z0)
-disable blending

ZbuffeR
09-22-2009, 01:21 AM
gramyfill, you post on the 'advanced' forum, so people expect that you have a somewhat advanced background on OpenGL :)