0

I have this cylinder object below:

glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, cylinder_mat);
cylinder();

Where static GLfloat cylinder_mat[] = {0.f, .5f, 1.f, 1.f}; dictates the color of my cylinder.

Is there anyway to use glMaterialfv in a way to make the object transparent?

genpfault
  • 51,148
  • 11
  • 85
  • 139
UndefinedReference
  • 1,223
  • 4
  • 22
  • 52
  • If I remember well, transparency is handled by blending color, from your primitives with the framebuffer. You need to enable BLEND and specify how the color will be blended, usually by using alpha channel. – Amadeus May 03 '13 at 04:26
  • Is there any example code of a transparent sphere or cylinder, cube, whatever that you can show me? – UndefinedReference May 03 '13 at 04:33
  • don't forget that OpenGL isn't a raytracer, you will need to submit your graphic primitives in depth order (ie draw background first then foreground objects) to achieve blending. – isti_spl May 03 '13 at 05:12

1 Answers1

1

I guess this example will clarify a bit for you.

#include <GL/freeglut.h>

void init()
{
  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  glClearColor(1.0, 1.0, 1.0, 1.0);
}

void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
  glLoadIdentity();

  glColor4f(0.5, 0.5, 0.5, 1.0);
  glutSolidCube(0.5);

  glTranslatef(-0.1, 0, 0);
  glEnable(GL_BLEND);
  glColor4f(1, 0, 0, 0.3);
  glutSolidCube(0.4);
  glDisable(GL_BLEND);

  glFlush();
}

int main(int argc, char *argv[])
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);
  glutInitWindowSize(600,600);
  glutInitWindowPosition(200,50);
  glutCreateWindow("glut test");
  glutDisplayFunc(display);
  init();
  glutMainLoop();
  return 0;
}

Note that this example is very, very simplicist. So, its main purpose is just to demostrate how to use BLEND functions. I did not take care about DEPTH removal or camera position.

I used the program posted here (How to use alpha transparency in OpenGL?) to construct this one.

Community
  • 1
  • 1
Amadeus
  • 10,199
  • 3
  • 25
  • 31