PDA

View Full Version : Alpha testing



nt
10-22-2007, 04:40 PM
I would like to create an image, I want to draw three squares, one colored blue, one red, and one black. I don't mind the ordering of the squares.
I want the blue line to be above red line but behind the blue line in the entire image, the red line should be above black but behind blue and the black line should be below red but above blue in the entire image. How can I do it, I am attaching my code here, please help, me my hardware is SGI prism and won't support color index mode, I want alpha testing only.

to compile gcc -lGL -lGLU source.c
// gcc -lGL alpha.c
//file name alpha.c
#include <stdio.h>

#include <X11/Xlib.h>
#include <GL/glx.h>
#include <GL/gl.h>
#include <string.h>
#include <stdlib.h>

void openWindow( void );


Display *myDpy;
Window w1, myGfxw[2];
GLXContext myGfxCx;

void updateOrtho(void)
{
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glOrtho(-80.0, 100.0, -80.0, 100.0, 0.50, 10.0);

}

GLvoid drawScene( GLvoid )
{
updateOrtho();

glEnable( GL_BLEND );
glBlendFunc (GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA);

// green background
glPushMatrix();
glColor4f (0., 1., 0., 1.0);
drawMyRect( -125., -125., 350., 350., -3.0);
glPopMatrix();

drawBlueSquare();
drawRedSquare();

// yellow rectangle
glPushMatrix();
glBlendFunc (GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA );
drawBlackSquare();
glPopMatrix();
glFlush();


}

void drawBlueSquare()
{
glColor4f(0.0, 0.0, 1.0, 1.0 );
glLineWidth(10);
glBegin(GL_LINES);
glVertex3f(-40.0, -25.0, -1.);
glVertex3f(-40.0, 25.0, -1.);
glVertex3f( 25.0, 25.0, -1.);
glVertex3f( 25.0, -25.0, -1.);
glVertex3f(-40.0, 25.0, -1.);
glVertex3f( 25.0, 25.0, -1.);
glVertex3f( 25.0, -25.0, -1.);
glVertex3f(-40.0, -25.0, -1.);
glEnd();
}
void drawRedSquare()
{
glColor4f(1.0, 0.0, 0.0, 0.25 );
glLineWidth(10);
glBegin(GL_LINES);
glVertex3f( 0.0, -5.0, -1.);
glVertex3f( 0.0, 50.0, -1.);
glVertex3f( 60.0, 50.0, -1.);
glVertex3f( 60.0, -5.0, -1.);
glVertex3f( 0.0, -5.0, -1.);
glVertex3f( 60.0, -5.0, -1.);
glVertex3f( 0.0, 50.0, -1.);
glVertex3f( 60.0, 50.0, -1.);
glEnd();

}

void drawBlackSquare()
{
glColor4f(0.0, 0.0, 0.0, 0.25 );
glLineWidth(10);

glBegin(GL_LINES);
glVertex3f(-12.5, 12.5, -1.);
glVertex3f(-12.5, 62.5, -1.);
glVertex3f( 37.5, 62.5, -1.);
glVertex3f( 37.5, 12.5, -1.);
glVertex3f(-12.5, 12.5, -1.);
glVertex3f( 37.5, 12.5, -1.);
glVertex3f(-12.5, 62.5, -1.);
glVertex3f( 37.5, 62.5, -1.);
glEnd();
}

int main( int argc, char *argv[] )
{
int cnt;

openWindow();

while (1)
{
glXMakeCurrent(myDpy, myGfxw[0], myGfxCx);
glXSwapBuffers(myDpy, myGfxw[0]);
glXWaitVideoSyncSGI(1, 0, &cnt);
glFlush();
glClearColor( 0.0, 0.0, 1.0, 1.0 );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawScene();

}
return(0);
}

void openWindow( void )
{

int w, h;
static char initFirstPass = 1;
static int dblBuf[ ] =
{
GLX_RGBA, GLX_RED_SIZE, 1, GLX_GREEN_SIZE, 1,GLX_BLUE_SIZE, 1,
GLX_DEPTH_SIZE, 12, GLX_DOUBLEBUFFER, None
};

char * filePath;
XVisualInfo *vi;
Colormap cmap;
XSizeHints myHints;
static XSetWindowAttributes swa;

myHints.flags = USPosition;
myDpy = XOpenDisplay(0);
vi = glXChooseVisual(myDpy, DefaultScreen(myDpy), dblBuf);
myGfxCx = glXCreateContext(myDpy, vi, None, 1);
cmap = XCreateColormap(myDpy, RootWindow(myDpy, vi->screen),
vi->visual, AllocNone);

swa.colormap = cmap;
swa.border_pixel = 0;
swa.event_mask = StructureNotifyMask;
// swa.override_redirect = 1;

myHints.x = 450;
myHints.y = 0;
myHints.width = 100;
myHints.height = 100;

myGfxw[0] = XCreateWindow(myDpy,RootWindow(myDpy,vi->screen), 725,
55,480, 480, 0, vi->depth, InputOutput, vi->visual,
CWBorderPixel | CWColormap | CWEventMask
| CWOverrideRedirect, &swa);
XSetStandardProperties(myDpy, myGfxw[0], "MY", "MY", 0,0,0,
&myHints);

XMapWindow(myDpy, myGfxw[0]);
glXMakeCurrent(myDpy, myGfxw[0], GfxCx);
glViewport(0, 0, 480, 480);
glClearColor( 0.0, 0.0, 1.0, 1.0 );
initFirstPass = 0;
}

please help

Lindley
10-22-2007, 05:52 PM
Alpha testing probably isn't the easiest way to do this. Just set up a glOrtho projection, then just render the GL_QUADS at different depths.

I see you are calling glOrtho, but you're doing it with the modelview matrix. You *can* make this work, but you're really meant to use the GL_PROJECTION matrix for that. Then if you like the modelview matrix as identity, increasing depth is increasing negative z.

Seems you've already got alpha blending figured out....

knackered
10-23-2007, 08:48 AM
does he?
Alpha blending should be:-
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
If you're trying to do some other kind of blending using dst alpha, it can't really be termed 'alpha blending' in the traditional sense.

BTW nt, I have absolutely no idea (from reading and re-reading your description) what you want to achieve. One minute you're talking about squares, the next you're talking about lines.

yooyo
10-23-2007, 08:59 AM
uhm.. very confusing question.. Try to better explain what bother you. Why dont you take piece of paper and colorful pencils and draw manually picture... then take photo or scan it, upload somewhere and give us link.

nt
10-23-2007, 04:31 PM
Hi yooyo, I can email the picture to any one, I use gl_Lines to draw squares, I wanted one color be over another color but under a 3rd color. I made a link to the picture please look at it.

I am using this glBlendFunc, beacuse I saw this in a book, I am not understanding what this glBlendFunc (GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA );
and glBlendFunc(GL_ONE, GL_ZERO); does, I read the man pages ad book, but not really helpful, I will appreciate if some one can belp me on this.

the link is
http://www.hoorray.com/sharedPhotos_pub.shared?sid=fa7YRzz7l2I1BHeY133%2B 7TKYRrkuV%2Byj