PDA

View Full Version : OpenGL 3D Texture Simple Cube with glsl



Iblues76
05-30-2011, 10:07 AM
Hi,
I'm looking for a simple 3d Texture example with a cube. Hopefully one that uses glsl.

thanks,
Francisco

The Little Body
05-30-2011, 03:08 PM
Hi,

You want several things in the same time :)

1) draw an (animated) cube

2) add texture mapping

3) use vertex/fragment instead of the fixed OpenGL pipeline

4) extend 2D texture mapping to 3D texture mapping


What do you think about making this step by step for a good understanding of all this?

For example for the two first steps, I propose you to begin with this link :

http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=06

What is you OpenGL dev platform : Windows, Linux, MacOS, another OS ?

Iblues76
05-30-2011, 04:16 PM
@The Little Buddy.

thanks for the reply.

I have no problems with steps 3 to 4. However, I have a problem figuring out how to extend the coords in 3d.
Most examples, show 2d texturing mapping each side the same. However, in 3d is different.

I would love to see if I could get help with step 4 that you mention.

I'm using windows.
thanks,
Francisco

The Little Body
05-30-2011, 04:49 PM
For example, here a sample of the first step on Linux
(the NeHe lesson 05 that I have a little simplifiate for only display the rotated colored cube)


The cube.cpp file :


//
// This code was created by Jeff Molofee '99 (ported to Linux/GLUT by Richard Campbell '99)
//
// If you've found this code useful, please let me know.
//
// Visit me at www.demonews.com/hosted/nehe
// (email Richard Campbell at ulmont@bellsouth.net)
//
// YLP 31/05/2011 : modify it for only display a cube

#include <GL/glut.h> // Header File For The GLUT Library
#include <GL/gl.h> // Header File For The OpenGL32 Library
#include <GL/glu.h> // Header File For The GLu32 Library
#include <unistd.h> // needed to sleep

/* ASCII code for the escape key. */
#define ESCAPE 27

/* The number of our GLUT window */
int window;

/* rotation angle for the cube. */
float rcube = 0.0f;

/* A general OpenGL initialization function. Sets all of the initial parameters. */
void InitGL(int Width, int Height) // We call this right after our OpenGL window is created.
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading

glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Reset The Projection Matrix

gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f); // Calculate The Aspect Ratio Of The Window

glMatrixMode(GL_MODELVIEW);
}

/* The function called when our window is resized (which shouldn't happen, because we're fullscreen) */
void ReSizeGLScene(int Width, int Height)
{
if (Height==0) // Prevent A Divide By Zero If The Window Is Too Small
Height=1;

glViewport(0, 0, Width, Height); // Reset The Current Viewport And Perspective Transformation

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
}

/* The main drawing function. */
void DrawGLScene()
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); // Clear The Screen And The Depth Buffer

glLoadIdentity(); // Reset the transformation matrix.
glTranslatef(0.0f,0.0f,-7.0f); // Move Right 3 Units, and back into the screen 7

glRotatef(rcube,1.0f,1.0f,1.0f); // Rotate The Cube On X, Y, and Z

// draw a cube (6 quadrilaterals)
glBegin(GL_QUADS); // start drawing the cube.

// top of cube
glColor3f(0.0f,1.0f,0.0f); // Set The Color To Blue
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Top)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Top)
glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)

// bottom of cube
glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange
glVertex3f( 1.0f,-1.0f, 1.0f); // Top Right Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f, 1.0f); // Top Left Of The Quad (Bottom)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Bottom)
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Bottom)

// front of cube
glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Front)
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Front)

// back of cube.
glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow
glVertex3f( 1.0f,-1.0f,-1.0f); // Top Right Of The Quad (Back)
glVertex3f(-1.0f,-1.0f,-1.0f); // Top Left Of The Quad (Back)
glVertex3f(-1.0f, 1.0f,-1.0f); // Bottom Left Of The Quad (Back)
glVertex3f( 1.0f, 1.0f,-1.0f); // Bottom Right Of The Quad (Back)

// left of cube
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
glVertex3f(-1.0f, 1.0f,-1.0f); // Top Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f,-1.0f); // Bottom Left Of The Quad (Left)
glVertex3f(-1.0f,-1.0f, 1.0f); // Bottom Right Of The Quad (Left)

// Right of cube
glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet
glVertex3f( 1.0f, 1.0f,-1.0f); // Top Right Of The Quad (Right)
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f, 1.0f); // Bottom Left Of The Quad (Right)
glVertex3f( 1.0f,-1.0f,-1.0f); // Bottom Right Of The Quad (Right)
glEnd(); // Done Drawing The Cube

rcube-=1.0f; // Decrease The Rotation Variable For The Cube

// swap the buffers to display, since double buffering is used.
glutSwapBuffers();
}

/* The function called whenever a key is pressed. */
void keyPressed(unsigned char key, int x, int y)
{
/* avoid thrashing this call */
usleep(100);

/* If escape is pressed, kill everything. */
if (key == ESCAPE)
{
/* shut down our window */
glutDestroyWindow(window);

/* exit the program...normal termination. */
exit(0);
}
}

int main(int argc, char **argv)
{
/* Initialize GLUT state - glut will take any command line arguments that pertain to it or
X Windows - look at its documentation at http://reality.sgi.com/mjk/spec3/spec3.html */
glutInit(&amp;argc, argv);

/* Select type of Display mode:
Double buffer
RGBA color
Alpha components supported
Depth buffered for automatic clipping */
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);

/* get a 640 x 480 window */
glutInitWindowSize(640, 480);

/* the window starts at the upper left corner of the screen */
glutInitWindowPosition(0, 0);

/* Open a window */
window = glutCreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe '99");

/* Register the function to do all our OpenGL drawing. */
glutDisplayFunc(&amp;DrawGLScene);

/* Go fullscreen. This is as soon as possible. */
glutFullScreen();

/* Even if there are no events, redraw our gl scene. */
glutIdleFunc(&amp;DrawGLScene);

/* Register the function called when our window is resized. */
glutReshapeFunc(&amp;ReSizeGLScene);

/* Register the function called when the keyboard is pressed. */
glutKeyboardFunc(&amp;keyPressed);

/* Initialize our window. */
InitGL(640, 480);

/* Start Event Processing Engine */
glutMainLoop();

return 1;
}



And the Makefile :


all : cube

cube : cube.cpp
gcc cube.cpp -o cube -lGL -lGLU -lglut


for to compile it and execute



make
./cube


=> are you ready for the second step ?
(cf. the 2D texture mapping) ?

The Little Body
05-30-2011, 04:53 PM
So, escuse-me for the last post :)

In 2D mapping you have the s and t texture coordinate (cf. a coordinate in a 2D plane)

In 3D mapping, the 3d texture coordinate r is the "height"

The Little Body
05-30-2011, 04:55 PM
A 3D texture is a volum when a 2D texture is only a plane

The Little Body
05-30-2011, 05:02 PM
Have you already 2D texture mapping that work ?

The Little Body
05-30-2011, 05:03 PM
How load/bind you the texture ?

From a .bmp or .tga file or you compute it ?

The Little Body
05-30-2011, 05:08 PM
In // to your response, I extend the cube.cpp example for to add the 2D texture mapping facility

And after your response, I begin to extend it for to use a 3D texture

The Little Body
05-30-2011, 06:02 PM
The 2D texture mapping is now added :)



//
// This code was created by Jeff Molofee '99 (ported to Linux/GLUT by Richard Campbell '99)
//
// If you've found this code useful, please let me know.
//
// Visit me at www.demonews.com/hosted/nehe
// (email Richard Campbell at ulmont@bellsouth.net)
//
// YLP 31/05/2011 : modify it for only display a cube
#include <GL/glut.h> // Header File For The GLUT Library
#include <GL/gl.h> // Header File For The OpenGL32 Library
#include <GL/glu.h> // Header File For The GLu32 Library
#include <unistd.h> // needed to sleep

/* ASCII code for the escape key. */
#define ESCAPE 27

/* The number of our GLUT window */
int window;

/* rotation angle for the cube. */
float rcube = 0.0f;

/* Texture */
GLuint texid;
GLuint texwidth=2;
GLuint texheight=2;
// GLuint texdepth=1;
GLubyte texData[16] =
{
0xFF,0x00,0x00,0xFF, // red
0x00,0xFF,0x00,0xFF, // rgreen
0x00,0x00,0xFF,0xFF, // blue
0xFF,0xFF,0xFF,0xFF // black
};

void DrawCube()
{
glColor3f(1.0f,1.0f,1.0f);

// draw a cube (6 quadrilaterals)
glBegin(GL_QUADS); // start drawing the cube.

// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad

// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad

// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad

// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad

// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad

// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad

glEnd(); // Done Drawing The Cube

}

void InitTexture()
{

// Bind the texture
glEnable(GL_TEXTURE_2D);
glGenTextures(1,&amp;texid);
glBindTexture(GL_TEXTURE_2D,texid); //Sélectionne ce n°
glTexImage2D (
GL_TEXTURE_2D, //Type : texture 2D
0, //Mipmap : aucun
4, //Couleurs : 4 (red,green,blue,alpha)
texwidth, //Largeur : 2
texheight, //Hauteur : 2
0, //Largeur du bord : 0
GL_RGBA, //Format : RGBA
GL_UNSIGNED_BYTE, //Type des couleurs
texData //Addresse de l'image
);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}


/* A general OpenGL initialization function. Sets all of the initial parameters. */
void InitGL(int Width, int Height) // We call this right after our OpenGL window is created.
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black
glClearDepth(1.0); // Enables Clearing Of The Depth Buffer
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading

glMatrixMode(GL_PROJECTION);
glLoadIdentity(); // Reset The Projection Matrix

gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f); // Calculate The Aspect Ratio Of The Window

glMatrixMode(GL_MODELVIEW);

InitTexture();


}

/* The function called when our window is resized (which shouldn't happen, because we're fullscreen) */
void ReSizeGLScene(int Width, int Height)
{
if (Height==0) // Prevent A Divide By Zero If The Window Is Too Small
Height=1;

glViewport(0, 0, Width, Height); // Reset The Current Viewport And Perspective Transformation

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
}

/* The main drawing function. */
void DrawGLScene()
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); // Clear The Screen And The Depth Buffer

glLoadIdentity(); // Reset the transformation matrix.
glTranslatef(0.0f,0.0f,-7.0f); // Move Right 3 Units, and back into the screen 7

glRotatef(rcube,1.0f,1.0f,1.0f); // Rotate The Cube On X, Y, and Z

DrawCube();

rcube-=1.0f; // Decrease The Rotation Variable For The Cube

// swap the buffers to display, since double buffering is used.
glutSwapBuffers();
}

/* The function called whenever a key is pressed. */
void keyPressed(unsigned char key, int x, int y)
{
/* avoid thrashing this call */
usleep(100);

/* If escape is pressed, kill everything. */
if (key == ESCAPE)
{
/* shut down our window */
glutDestroyWindow(window);

/* exit the program...normal termination. */
exit(0);
}
}

int main(int argc, char **argv)
{
/* Initialize GLUT state - glut will take any command line arguments that pertain to it or
X Windows - look at its documentation at http://reality.sgi.com/mjk/spec3/spec3.html */
glutInit(&amp;argc, argv);

/* Select type of Display mode:
Double buffer
RGBA color
Alpha components supported
Depth buffered for automatic clipping */
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);

/* get a 640 x 480 window */
glutInitWindowSize(640, 480);

/* the window starts at the upper left corner of the screen */
glutInitWindowPosition(0, 0);

/* Open a window */
window = glutCreateWindow("Jeff Molofee's GL Code Tutorial ... NeHe '99");

/* Register the function to do all our OpenGL drawing. */
glutDisplayFunc(&amp;DrawGLScene);

/* Go fullscreen. This is as soon as possible. */
glutFullScreen();

/* Even if there are no events, redraw our gl scene. */
glutIdleFunc(&amp;DrawGLScene);

/* Register the function called when our window is resized. */
glutReshapeFunc(&amp;ReSizeGLScene);

/* Register the function called when the keyboard is pressed. */
glutKeyboardFunc(&amp;keyPressed);

/* Initialize our window. */
InitGL(640, 480);

/* Start Event Processing Engine */
glutMainLoop();

return 1;
}

The Little Body
05-30-2011, 06:09 PM
The texture definition is this



/* Texture */
GLuint texid;
GLuint texwidth=2;
GLuint texheight=2;
// GLuint texdepth=1;
GLubyte texData[16] =
{
0xFF,0x00,0x00,0xFF, // red
0x00,0xFF,0x00,0xFF, // rgreen
0x00,0x00,0xFF,0xFF, // blue
0xFF,0xFF,0xFF,0xFF // black
};


The initialisation of the texture is this :



void InitTexture()
{

// Bind the texture
glEnable(GL_TEXTURE_2D);
glGenTextures(1,&amp;texid);
glBindTexture(GL_TEXTURE_2D,texid); //Sélectionne ce n°
glTexImage2D (
GL_TEXTURE_2D, //Type : texture 2D
0, //Mipmap : aucun
4, //Couleurs : 4 (red,green,blue,alpha)
texwidth, //Largeur : 2
texheight, //Hauteur : 2
0, //Largeur du bord : 0
GL_RGBA, //Format : RGBA
GL_UNSIGNED_BYTE, //Type des couleurs
texData //Addresse de l'image
);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}


InitTexture() is added to the end of the InitGL func

And the display of the cube is now implemented into DrawCube()
(used in DrawGLScene() )

The Little Body
05-30-2011, 06:12 PM
So, now that I have the InitTexture() and DrawCube() funcs, I have only to modify this 2 funcs (+ the texture definition) for to add the 3D texture mapping :)

Can compile this on you Windows platform and see the textured rotated cube when you execute it ?

The Little Body
05-30-2011, 07:51 PM
This work now with 3D texturing :)



/* Texture */
GLuint texid;
int texwidth=16;
int texheight=16;
int texdepth=16;




void DrawCube()
{
glColor3f(1.0f,1.0f,1.0f);

// draw a cube (6 quadrilaterals)
glBegin(GL_QUADS); // start drawing the cube.

// Front Face
glTexCoord3f(0.0f, 0.0f,1.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord3f(1.0f, 0.0f,1.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord3f(1.0f, 1.0f,1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord3f(0.0f, 1.0f,1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad

// Back Face
glTexCoord3f(0.0f, 0.0f,0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord3f(0.0f, 1.0f,0.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord3f(1.0f, 1.0f,0.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord3f(1.0f, 0.0f,0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad

// Top Face
glTexCoord3f(0.0f, 1.0f,0.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord3f(0.0f, 1.0f,1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord3f(1.0f, 1.0f,1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord3f(1.0f, 1.0f,0.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad

// Bottom Face
glTexCoord3f(0.0f, 0.0f,0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord3f(1.0f, 0.0f,0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord3f(1.0f, 0.0f,1.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord3f(0.0f, 0.0f,1.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad

// Right face
glTexCoord3f(1.0f, 0.0f,0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord3f(1.0f, 1.0f,0.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord3f(1.0f, 1.0f,1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
glTexCoord3f(1.0f, 0.0f,1.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad

// Left Face
glTexCoord3f(0.0f, 0.0f,0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
glTexCoord3f(0.0f, 0.0f,1.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord3f(0.0f, 1.0f,1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord3f(0.0f, 1.0f,0.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad

glEnd();
// Done Drawing The Cube
}




void InitTexture()
{
int s,t,p;
GLubyte *ptr;


// Create the 3D texture
ptr=(GLubyte *) malloc(texwidth*texheight*texdepth*4);
texData=ptr;
printf("malloc = %x, texData = %x, size=%d \n", ptr, texData, texwidth*texheight*texdepth*4);

for(p=0;p<texdepth;p++)
{
// printf("\ndepth=%d\n", p);
for(t=0;t<texheight;t++)
{
// printf("\nheight=%d ",t);
for(s=0;s<texwidth;s++)
{
// printf(".");
ptr[0]=s*16; // red
ptr[1]=t*16; // green
ptr[2]=p*16; // blue
ptr[3]=0xFF; // alpha
ptr+=4;
}
}
}

glEnable(GL_TEXTURE_3D);
glGenTextures(1,&amp;texid);
glBindTexture(GL_TEXTURE_3D,texid); //Sélectionne ce n°
glTexImage3D (
GL_TEXTURE_3D, //Type : texture 3D
0, //Mipmap : aucun
GL_RGBA, // (red,green,blue,alpha)
texwidth, // Largeur
texheight, // Hauteur
texdepth, // profondeur
0, //Largeur du bord : 0
GL_RGBA, //Format : RGBA
GL_UNSIGNED_BYTE, //Type des couleurs
texData //Addresse de l'image
);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}


=> I add glsl vertex/fragments shaders support tomorow :)

This reply help you or not ???

The Little Body
05-30-2011, 08:13 PM
Note that with texwidth=texheight=texdepth=256 (and ptr[x]=x instead ptr[x]=x*16 of course) I have a crash :(
(Yeah, I have found a method for to crash my Linux box, this was a long time it had not happened :) )

Perhaps because 3D textures are obligatory inferiors to 256x256x256 on my graphics card (Geforce 7100 GS) ???

And the (s,t,p) 3D texture coordinate mapping into faces isn't good :(
=> I correct this tomorow too

Iblues76
05-31-2011, 08:38 AM
thank you so much. I'm going to try to build this now. I'm leaving tomorrow and I will be out for one week. but I will try to post back a reply today.

Iblues76
05-31-2011, 09:51 AM
I tried your sample. I place it into my own windows implementation of opengl and I can see the cube.
I'm going to work with that you set for the textures in 3d to see if it works.
I will post my code when I'm done. However, it maybe until next week,since I'm going out of town tomorrow morning.

thanks,
Francisco

doug107
07-29-2011, 03:14 AM
Thank you, The Little Body! I managed to have 3D texture work by following your example. Since vc++ don't support 3D texture so I included glew.h and glewInit(). Anyway, it works nearly perfect.

Only when I draw something which do not have texture mapped, they just invisible except texture mapped items. I can only see them when GL_TEXTURE_3D is not enabled.

Anyone come across it or having any suggestion?

thanks,
Doug

doug107
07-29-2011, 04:45 AM
Sorry for my earlier post. I managed to work with non-texture items.

I enable texture and disable texture then other items like this

/******************** texturing ******************/
glEnable(GL_TEXTURE_3D);
glBindTexture(GL_TEXTURE_3D, texid);

glBegin(GL_QUADS);

glColor4f(.....

glTexCoord3f(...);
glVertex3fv(...);
glTexCoord3f(...);
glVertex3fv(...);
glTexCoord3f(...);
glVertex3fv(...);
glTexCoord3f(...);
glVertex3fv(...);

glEnd();

glBindTexture(GL_TEXTURE_3D, 0);
glDisable(GL_TEXTURE_3D);

/********************* no texture items ****************************/
glBegin(GL_QUADS);

glColor4f(.....

glTexCoord3f(...);
glVertex3fv(...);
glTexCoord3f(...);
glVertex3fv(...);
glTexCoord3f(...);
glVertex3fv(...);
glTexCoord3f(...);
glVertex3fv(...);

glEnd();