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