hi,
this is a very late response from me and i apologize. well, i reconstructed the program and brought up with another code. everything fine except the blending. if i change the apha value, i get the object to be drawn in a darker shade but still not transparent. can you tell me the problem with the below code:
#include<windows.h>
#include<GL/glut.h>
#include<math.h>
#include<stdio.h>
#include"Texture.h"
static float xRot=0.0f,yRot=0.0f;
float bottom_normal[3],back_normal[3],lside_normal[3],front_normal[3],out[3];
GLfloat all_normals[12];
int i;
GLubyte *bits;
BITMAPINFO *info;
bool bp, blend;
void renderScene(void);
float bottom_vertices[][3] = { 20.0f,0.0f,0.0f , 0.0f,0.0f,60.0f , -20.0f,0.0f,0.0f};
float back_vertices[][3] = {20.0f,0.0f,0.0f , -20.0f,0.0f,0.0f , 0.0f,40.0f,30.0f};
float lside_vertices[][3] = {-20.0f,0.0f,0.0f , 0.0f,0.0f,60.0f , 0.0f,40.0f,30.0f};
float front_vertices[][3] = {0.0f,0.0f,60.0f , 20.0f,0.0f,0.0f , 0.0f,40.0f,30.0f};
void setupRC()
{
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
bits = (GLubyte*)TextureLoadBitmap("pyramid.bmp");
GLfloat ambient[] = {0.3f,0.3f,0.3f,1.0f};
GLfloat diffuse[] = {0.7f,0.7f,0.7f,0.0f};
GLfloat specular[]= {1.0f,1.0f,1.0f,1.0f};
GLfloat specRef[] = {1.0f,1.0f,1.0f,1.0f};
GLfloat lightPos[] = { -50.f, 50.0f, 100.0f, 1.0f };
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0,GL_AMBIENT,ambient);
glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuse);
glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT,GL_SPECULAR,specRef);;
glMateriali(GL_FRONT,GL_SHININESS,128);
glShadeModel(GL_SMOOTH);
glFrontFace(GL_CCW);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
//glClearDepth(1.0f);
glDepthFunc(GL_LEQUAL);
ShowCursor(FALSE);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
//glColor4f(1.0f, 1.0f, 1.0f, 0.5);
//glBlendFunc(GL_SRC_ALPHA,GL_ONE);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glOrtho(-30.0f,30.0f,-10.0f,60.0f,-100.0f,100.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glClearColor(0.0f,0.0f,0.0f,0.0f);
}
void calcUnitNormals(float out[3])
{
float length;
length = (float)sqrt((out[0]*out[0]) + (out[1]*out[1]) + (out[2]*out[2]));
if(length==0.0f)
length=1.0f;
out[0] /= length;
out[1] /= length;
out[2] /= length;
//printf("norm0: %f
norm1: %f
norm2: %f
",out[0],out[1],out[2]);
}
void calcNormals(GLfloat v[3][3],GLfloat out[3])
{
GLfloat v1[3],v2[3];
static const int x=0,y=1,z=2;
v1[x] = v[0][x] - v[1][x];
v1[y] = v[0][y] - v[1][y];
v1[z] = v[0][z] - v[1][z];
v2[x] = v[1][x] - v[2][x];
v2[y] = v[1][y] - v[2][y];
v2[z] = v[1][z] - v[2][z];
out[x] = v1[y]*v2[z] - v2[y]*v1[z];
out[y] = v1[z]*v2[x] - v1[x]*v2[z];
out[z] = v1[x]*v2[y] - v1[y]*v2[x];
calcUnitNormals(out);
}
void specialKeys(int key,int x,int y)
{
if(key==GLUT_KEY_UP)
xRot +=5.0f;
if(key==GLUT_KEY_DOWN)
xRot -= 5.0f;
if(key==GLUT_KEY_RIGHT)
yRot += 5.0f;
if(key==GLUT_KEY_LEFT)
yRot -= 5.0f;
if(key<0.0f)
xRot=359.0f;
if(key>359.0f)
xRot=0.0f;
if(key>359.0f)
yRot=0.0f;
if(key<0.0f)
yRot=359.0f;
glutPostRedisplay();
}
/*void combineNormals(float xNormal[])
{
static int x=0;
for(int i=0;i<=2;i++)
{
all_normals[x] = xNormal[i];
x=x+1;
}
}*/
void keyboard(unsigned char key, int x, int y)
{printf(“within function”);
switch(key)
{
case 27:
exit(0);
break;
case 'b':
blend = TRUE;
if(blend)
{
printf("this is in blen");
glEnable(GL_BLEND); // Turn Blending On
glDisable(GL_DEPTH_TEST); // Turn Depth Testing Off
blend = !blend;
}
else
{
glDisable(GL_BLEND); // Turn Blending Off
glEnable(GL_DEPTH_TEST); // Turn Depth Testing On
blend = !blend;
}
break;
default: // Now Wrap It Up
break;
}
}
void renderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor4f(1.0f, 1.0f, 1.0f, 0.2);
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
glDepthMask(GL_FALSE);
glEnable(GL_TEXTURE_2D);
glTexImage2D(GL_TEXTURE_2D,0,3,256,256,0,GL_RGBA,GL_UNSIGNED_BYTE,bits);
GLfloat pyramid_vertex_colors[][3] = {{1.0f,0.0f,0.0f},
{0.0f,1.0f,0.0f},
{0.0f,0.0f,1.0f},
{1.0f,1.0f,0.0f},
{1.0f,0.0f,1.0f},
{0.0f,1.0f,1.0f},
{0.25f,0.25f,0.25f},
{0.75f,0.75f,0.75f},
{0.60f,0.40f,0.12f},
{0.98f,0.625f,0.12f},
{0.98f,0.04f,0.7f},
{0.60f,0.40f,0.70f}};
glPushMatrix();
glRotatef(xRot,1.0f,0.0f,0.0f);
glRotatef(yRot,0.0f,1.0f,0.0f);
//glColor3ub(128,128,128);
glBegin(GL_TRIANGLES);
calcNormals(bottom_vertices,out);
// Bottom Triangle
glNormal3fv(out);
glTexCoord2f(1.0f,0.0f);
glVertex3fv(bottom_vertices[0]);
glTexCoord2f(0.5f,1.0f);
glVertex3fv(bottom_vertices[1]);
glTexCoord2f(0.0f,0.0f);
glVertex3fv(bottom_vertices[2]);
calcNormals(back_vertices,back_normal);
/*for(int j=0;j<=2;j++);
printf("back_normal: %f
",back_normal[j]);*/
// Back triangle
glNormal3fv(back_normal);
glTexCoord2f(0.0f,0.0f);
glVertex3fv(back_vertices[0]);
glTexCoord2f(1.0f,0.0f);
glVertex3fv(back_vertices[1]);
glTexCoord2f(0.5f,1.0f);
glVertex3fv(back_vertices[2]);
calcNormals(lside_vertices,lside_normal);
/*for(int k=0;k<=2;k++);
printf("lside_normal: %f
",lside_normal[k]);*/
//LSide triangle
glNormal3fv(lside_normal);
glTexCoord2f(0.0f,0.0f);
glVertex3fv(lside_vertices[0]);
glTexCoord2f(1.0f,0.0f);
glVertex3fv(lside_vertices[1]);
glTexCoord2f(0.5f,1.0f);
glVertex3fv(lside_vertices[2]);
calcNormals(front_vertices,front_normal);
/*for(int l=0;l<=2;l++);
printf("front_normal: %f
",front_normal[l]);*/
glNormal3fv(front_normal);
glTexCoord2f(0.0f,0.0f);
glVertex3fv(front_vertices[0]);
glTexCoord2f(1.0f,0.0f);
glVertex3fv(front_vertices[1]);
glTexCoord2f(0.5f,1.0f);
glVertex3fv(front_vertices[2]);
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();
glutSwapBuffers();
}
void changeSize(GLsizei w, GLsizei h)
{
glViewport(0,0,w,h);
if(h==0)
h=1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-30.0f,30.0f,-10.0f,60.0f,-100.0f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void main()
{
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow(“Hello”);
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutSpecialFunc(specialKeys);
glutKeyboardFunc(keyboard);
setupRC();
glutMainLoop();
}
thanx a lot