Hi! my program creates a chessboard using 3d texture; the program creates 3 different chessboard and display the blending. When I use an Ortho projection is all ok,( if I change the visible plane it works perfectly). The problem is with glFrustum: if I move znear or zfar the chessboard is /isn’t visible but if I change left/right I always see the chessboard ( that shouldn’t be correct!). Suggestions?
#include <windows.h>
#include <stdio.h>
#include "glew.h"
#include "glut.h"
static void display(void);
int main(int argc, char **argv)
{
int Err=0;
glutInitWindowSize(400, 400);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInit(&argc, argv);
glutCreateWindow("Chessboard");
glutDisplayFunc(display);
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s
", glewGetErrorString(err));
}
glutMainLoop();
return Err;
}
static void display(void)
{
// Create texture data
float *pData = new float[32*32*3*3];
int nx,ny,nz,nIndex;
for (nIndex=0; nIndex<32*32*3*3; nIndex++)
pData[nIndex]=0;
nIndex=0;
for (nz=0; nz<3; nz++)
for (nx=0; nx<32; nx++)
for (ny=0; ny<32; ny++)
{
if (((nx+ny)%((nz+1)*2))==0)
pData[nIndex]=0.0;
else
pData[nIndex]=0.3;
nIndex+=3;
}
// Enable texture, declare and init texture
glEnable(GL_TEXTURE_3D);
GLuint tex;
glGenTextures (1, &tex);
// Activate texture mapping
glBindTexture(GL_TEXTURE_3D,tex);
// Set dimension
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP);
GLenum errCode;
const GLubyte *errString;
if ((errCode = glGetError()) != GL_NO_ERROR) //check for glut errors
{
errString = gluErrorString(errCode);
fprintf (stderr, "OpenGL Error: %s
", errString);
getchar();
}
// Set texture data
glTexImage3D(GL_TEXTURE_3D,0,GL_RGB,32,32,3,0,GL_RGB,GL_FLOAT,pData);
// Clear buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set geometry
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0,1.0,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//set blending
glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFunc(GL_CONSTANT_COLOR,GL_ONE);
glBlendColor(1.0,1.0,1.0,1.0);
// Draw quad
glBegin(GL_QUADS);
for (nz=0; nz<3; nz++)
{
glTexCoord3f(0.0,0.0,nz); glVertex3f(-0.8, -0.8, 0.1+0.1*nz);
glTexCoord3f(1.0,0.0,nz); glVertex3f(0.8, -0.8, 0.1+0.1*nz);
glTexCoord3f(1.0,1.0,nz); glVertex3f(0.8, 0.8, 0.1+0.1*nz);
glTexCoord3f(0.0,1.0,nz); glVertex3f(-0.8, 0.8, 0.1+0.1*nz);
}
glEnd();
// Disable texture
glDisable(GL_TEXTURE_3D);
glDisable(GL_BLEND);
delete [] pData;
glutSwapBuffers();
}