I have the following code that displays a red and a green sphere in orthographic view with the green sphere shifted a bit in X. I used the depth function always to get an idea of the shift in case it changes, no other depth function gave me a correct result. Anyway what I need o do more is to have the color of the common part with a blend of red and green that is yellow, I set an alpha =0.5 to both colors but to no avail… Any idea how this can be achieved and would it be then necessary to change the depth function?
Any solution involving fragment shaders is OK…
#include <GL/freeglut.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
double radius = 1.0;
double trans = 0.1;
int delay = 160;
int sd = 1;
int sphere;
void computeLocation(double r) {
glMatrixMode(GL_PROJECTION); // Set projection parameters.
glLoadIdentity();
glOrtho(-radius-trans , radius+trans , -radius-trans , radius+trans , -radius - trans , radius + trans);
}
void drawSphere(double r, int lats, int longs) {
int i, j;
for(i = 0; i <= lats; i++) {
double lat0 = M_PI * (-0.5 + (double) (i - 1) / lats);
double z0 = sin(lat0);
double zr0 = cos(lat0);
double lat1 = M_PI * (-0.5 + (double) i / lats);
double z1 = sin(lat1);
double zr1 = cos(lat1);
glBegin(GL_QUAD_STRIP);
for(j = 0; j <= longs; j++) {
double lng = 2 * M_PI * (double) (j - 1) / longs;
double x = cos(lng);
double y = sin(lng);
glNormal3f(x * zr0, y * zr0, z0);
glVertex3f(r*x * zr0, r*y * zr0, r*z0);
glNormal3f(x * zr1, y * zr1, z1);
glVertex3f(r*x * zr1, r*y * zr1, r*z1);
}
glEnd();
}
}
// Initializes information for drawing within OpenGL.
void init() {
glClearColor(1.0, 1.0, 1.0, 0.0); // Set window color to white.
glEnable(GL_DEPTH_TEST); // Draw only closest surfaces
glDepthFunc(GL_ALWAYS);
computeLocation(radius);
}
// Draws the current image.
void draw() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear window.
glShadeModel(GL_SMOOTH);
if (sd==1||sd==3 || (sd==2 && sphere==1)) {
glColor4f(1.0, 0.0, 0.0,0.5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawSphere(radius, 40, 40); // glutSolidSphere(1.0, 10, 10);
}
if (sd==3 || (sd==2 && sphere==2)) {
glColor4f(0.0, 1.0, 0.0,0.5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslated(trans, 0.0, 0.0);
drawSphere(radius, 40, 40);
}
glutSwapBuffers();
}
// Arranges that the window will be redrawn roughly every 40 ms.
void idle() {
static int lastTime = 0; // time of last redraw
int time = glutGet(GLUT_ELAPSED_TIME); // current time
if(lastTime == 0 || time >= lastTime + delay) {
lastTime = time;
if (sd==2)
if (sphere==1) sphere=2;
else sphere=1;
glutPostRedisplay();
}
}
// When window becomes visible, we want the window to
// continuously repaint itself.
void visible(int vis) {
glutIdleFunc(vis == GLUT_VISIBLE ? idle : NULL);
}
void keyboard (unsigned char k, int x, int y)
{
switch (k) {
case '1': sd = 1;break;
case '2': sd = 2;sphere=1;break;
case '3': sd = 3;
}
}
int main(int argc, char **argv) {
// radius = strtod(argv[1], NULL);
if (argc==2) {
delay = strtod(argv[1], NULL);
}
else if (argc==3) {
int t = strtod(argv[1], NULL);
if (t > 0) delay = t;
trans = strtod(argv[2], NULL);
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(50, 100); // Set up display window.
glutInitWindowSize(600, 600);
glutCreateWindow("Sphere");
init();
glutDisplayFunc(draw);
glutVisibilityFunc(visible);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}