Hi! i have a problem with blending… my code should blend a valume made of 3 “slices” and read the blended value… From what I’ve read, the function GL_ONE_MINUS_SRC_ALPHA should calculate the blended value(for 3 “slices” like in my code), resolving this expression:
((µ1α1)(1-α2)+ µ2α2)(1-α3)+(µ3*α3)
where µ1 is the value of the first “slice” and α1 is the alpha value of the first slice ( and so on…)
so… the result is 2 ( that is a reasonable result). but the result of the program is always half of the expected value. f.e. with 3 slices and the values µ1=1,µ2=2,µ3=3 the result is 1 (instead of 2), if the result should be 40, the program give 20 and so on… so i think that the program is correct because it is wrong in a “correct way”. maybe there’s something that i don’t know about the method, or maybe i’m not considerating all… please help!!!
#include <cstdio>
#include <cstdlib>
using namespace std;
#include <gl\glew.h>
#include <gl\glut.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <windows.h>
//trasforms alpha from double to char and from 0 and 255
char DoubleToChar(double alpha){
return (char)(alpha*255.0);
}
int main(int argc, char **argv) {
//Volume values: it represents a 1x1x3 volume ( 3 slices along z)
unsigned char Volumevalues[12]={1,0,0,1,2,0,0,0,3,0,0,0};
Volumevalues [3]=DoubleToChar(1.0);
Volumevalues [7]=DoubleToChar(1.0/2.0);
Volumevalues [11]=DoubleToChar(1.0/3.0);
unsigned char result [16]; //array for the result
//initialize glut
glutInit (&argc, argv);
glutCreateWindow(“radiographic simulation”);
glewInit();
// create the texture
GLuint tex;
glGenTextures (1, &tex);
glBindTexture(GL_TEXTURE_3D,tex);
// sets texture’s parameters
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);
//allocate the texture
glTexImage3D(GL_TEXTURE_3D,0,GL_RGBA,1,1,4,0,GL_RGBA,GL_UNSIGNED_BYTE, Volumevalues);
//viewport transform for 1:1 pixel=texel=data mapping
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,1,0.0,1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0,0,1,1);
glPushMatrix();
glEnable(GL_TEXTURE_3D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
float a= ((float)0.5)/3.0;
float b =((float)1.5)/3.0;
float c= ((float)2.5)/3.0;
glBegin(GL_QUADS);
glTexCoord3f(0.0,0.0,a); glVertex2f(0,0);
glTexCoord3f(0.0,1.0,a); glVertex2f(0,1);
glTexCoord3f(1.0,1.0,a); glVertex2f(1,1);
glTexCoord3f(1.0,0.0,a); glVertex2f(1,0);
glTexCoord3f(0.0,0.0,b); glVertex2f(0,0);
glTexCoord3f(0.0,1.0,b); glVertex2f(0,1);
glTexCoord3f(1.0,1.0,b); glVertex2f(1,1);
glTexCoord3f(1.0,0.0,b); glVertex2f(1,0);
glTexCoord3f(0.0,0.0,c); glVertex2f(0,0);
glTexCoord3f(0.0,1.0,c); glVertex2f(0,1);
glTexCoord3f(1.0,1.0,c); glVertex2f(1,1);
glTexCoord3f(1.0,0.0,c); glVertex2f(1,0);
glEnd();
glPopMatrix();
glutSwapBuffers();
//reads datas from the frame buffer and write them into the array result
glReadBuffer(GL_FRONT);
glReadPixels(0,0,1,1,GL_RGBA,GL_BYTE,result);
// print out results
printf("Data before roundtrip:
“);
for (int i=0; i<16; i++)
printf(”%d
", Volumevalues [i]);
printf("Data after roundtrip:
“);
for (int i=0; i<4; i++)
printf(”%d
",result[i]);
getchar();
return 0;
}