Hey all, I'm trying to make my first CPP/OpenGL game, wherein the primary graphics are 2D vector-based polygons (so basically just using opengl's primitive drawing functions). However, I want some of the filled in shapes to have a measure of translucency, and so I want to use alpha blending. However, no code that I've used so far seems to do the trick, and I've tried all manner of functions, flags, and finagling to get it working.
Basically this little bit of code is supposed to draw one circle half overlapping a second circle, but no matter what I set the alpha values to, and how I set my flags, the circles are always completely solid.
Here is the main.cpp file:
#include <gl/gl.h>
#include <gl/glu.h>
#include "glut.h"
#include <vector>
#include <math.h>
#include <stdio.h>
#include "EngineDrawGL.h"
int w1;
int h1;
void orthogonalStart (void) {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, w1, 0, h1);
glScalef(1, -1, 1);
glTranslatef(0, -h1, 0);
glMatrixMode(GL_MODELVIEW);
}
void orthogonalEnd (void) {
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
void display (void) {
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor (0.0,0.0,0.0,1.0);
glLoadIdentity();
orthogonalStart();;
//drawshit
glColor4f(1, 0, 0, .5);
fill_circle_GL(100, 100, 30, 360);
glColor4f(0, 0, 1, .5);
fill_circle_GL(100, 120, 20, 360);
orthogonalEnd();
glutSwapBuffers();
}
void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 1000.0);
w1 = w;
h1 = h;
glMatrixMode (GL_MODELVIEW);
glutPostRedisplay();
}
void idle() {
glutPostRedisplay();
}
int main (int argc, char **argv) {
//////set up window//////////////
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
//glEnable(GL_DEPTH_TEST);
//glEnable(GL_ALPHA_TEST);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glEnable(GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glutInitWindowSize (800, 600);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Model Part Editor");
glutIdleFunc( idle );
glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutMainLoop ();
return 0;
}
So basically, it sets up an openGL window, attempts to set up alpha blending, and then the draw function pops in and out of orthogonal mode, sandwhitching the circle drawing in between.
There is also my circle drawing function which looks like this:
void fill_circle_GL(float x1, float y1, float radius, int degrees) {
glBegin(GL_POLYGON);
int i;
for (i=0; i < degrees; i++)
{
float degInRad = i*DEG2RAD;
glVertex2f((cos(degInRad)*radius)+x1,(sin(degInRad)*radius)+y1);
}
glEnd();
}
So I don't really know about the inner workings of OpenGL yet, but most of this code was stitched together from various examples. If anyone has any clue why the circles are still solid, please teach me :)
Thanks for your time
guest42