Recent

Author Topic: 32bit (transparent) color in OpenGL control component?  (Read 14784 times)

Peiman

  • New Member
  • *
  • Posts: 41
32bit (transparent) color in OpenGL control component?
« on: July 08, 2011, 08:35:23 pm »
Hi,
I am a newbie in OpenGL. I have started with TOpenGLControl component. I wonder whether I can draw "transparent" surfaces with it similar to 2D gdi+ by changing the 4th byte of the color (alpha)?

Carver413

  • Full Member
  • ***
  • Posts: 119
Re: 32bit (transparent) color in OpenGL control component?
« Reply #1 on: July 08, 2011, 11:53:00 pm »
yes it is possible. how you go about it depends on whether you are using old opengl or new. most likely you are using old since lazarus doesn't implement any new code. check out
http://nehe.gamedev.net/
http://www.pascalgamedevelopment.com/
Lazarus doesn't do much in the way of opengl.
I am working on some modern code but haven't got a real transparency shader yet. just one that clips transparent bits from view. if your planing to display a bunch of 3d objects with transparency this can get quite difficult do to the limitations of opengl. I guess it messes up the Z buffer and things might not turn out the way you would expect.   

Carver413

  • Full Member
  • ***
  • Posts: 119
Re: 32bit (transparent) color in OpenGL control component?
« Reply #2 on: July 09, 2011, 06:22:19 am »
digging through some of my old code I think this is what your looking for

Code: [Select]
    glBindTexture(GL_TEXTURE_2D, Font.Texture);// bind the texture
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);// set the blend mode
    glEnable(GL_DEPTH_TEST);//turn on depth testing
    glEnable(GL_BLEND);// turn on blending   
you must use a RGBA texture and this code is useless if your using a shader. if your not using a texture then I guess you would set the alpha in
Code: [Select]
glColor4f(1.0,0.7,0.3,0.5);

Peiman

  • New Member
  • *
  • Posts: 41
Re: 32bit (transparent) color in OpenGL control component?
« Reply #3 on: July 09, 2011, 09:38:36 am »
Thanks all,

I found this on http://www.opengl.org/resources/faq/technical/transparency.htm:
 
Code: [Select]
glEnable (GL_BLEND);
  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

The only important thing is that after this you should never use glcolor3* function.
« Last Edit: July 09, 2011, 10:04:53 am by Peiman »

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: 32bit (transparent) color in OpenGL control component?
« Reply #4 on: July 09, 2011, 12:29:45 pm »
You can still use glColor3* functions, they will work normally. It is basically exactly same thing as glColor4f(r, g, b, 1). As you see it will always set color alpha to fully visible, but it doesn't disable transparency from the texture alpha channel itself.

Peiman

  • New Member
  • *
  • Posts: 41
Re: 32bit (transparent) color in OpenGL control component?
« Reply #5 on: July 09, 2011, 02:35:24 pm »
Sorry,
I think my problem with OpenGL is more than I have imagined. Now neither the 4f nor 3f colors won't work! Ironically the only thing I need now is simple transparent surfaces! I think it is easier to design a new engine rather use opengl for this.

Even with the provided example of opengl component (with 3f colors). it mess up the depth and colors of the cube.
Code: [Select]

  glClearColor(1.0, 1.0, 1.0, 1.0);
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(45.0, double(width) / height, 0.1, 100.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  glTranslatef(0.0, 0.0,-6.0);
  glRotatef(cube_rotationz, cube_rotationx, cube_rotationy,1);
  glBegin(GL_QUADS);
          glColor3f(0.0,1.0,0.0);                              // Set The Color To Green
          glVertex3f( 1.0, 1.0,-1.0);                  // Top Right Of The Quad (Top)
          glVertex3f(-1.0, 1.0,-1.0);                  // Top Left Of The Quad (Top)
          glVertex3f(-1.0, 1.0, 1.0);                  // Bottom Left Of The Quad (Top)
          glVertex3f( 1.0, 1.0, 1.0);                  // Bottom Right Of The Quad (Top)
  glEnd();
  glBegin(GL_QUADS);
          glColor3f(1.0,1.0,0.0);                              // Set The Color To Orange
          glVertex3f( 1.0,-1.0, 1.0);                  // Top Right Of The Quad (Bottom)
          glVertex3f(-1.0,-1.0, 1.0);                  // Top Left Of The Quad (Bottom)
          glVertex3f(-1.0,-1.0,-1.0);                  // Bottom Left Of The Quad (Bottom)
          glVertex3f( 1.0,-1.0,-1.0);                  // Bottom Right Of The Quad (Bottom)
  glEnd();
  glBegin(GL_QUADS);
          glColor3f(1.0,0.0,0.0);                              // Set The Color To Red
          glVertex3f( 1.0, 1.0, 1.0);                  // Top Right Of The Quad (Front)
          glVertex3f(-1.0, 1.0, 1.0);                  // Top Left Of The Quad (Front)
          glVertex3f(-1.0,-1.0, 1.0);                  // Bottom Left Of The Quad (Front)
          glVertex3f( 1.0,-1.0, 1.0);                  // Bottom Right Of The Quad (Front)
  glEnd();
  glBegin(GL_QUADS);
          glColor3f(1.0,1.0,0.0);                              // Set The Color To Yellow
          glVertex3f( 1.0,-1.0,-1.0);                  // Bottom Left Of The Quad (Back)
          glVertex3f(-1.0,-1.0,-1.0);                  // Bottom Right Of The Quad (Back)
          glVertex3f(-1.0, 1.0,-1.0);                  // Top Right Of The Quad (Back)
          glVertex3f( 1.0, 1.0,-1.0);                  // Top Left Of The Quad (Back)
  glEnd();
  glBegin(GL_QUADS);
          glColor3f(0.0,0.0,1.0);                              // Set The Color To Blue
          glVertex3f(-1.0, 1.0, 1.0);                  // Top Right Of The Quad (Left)
          glVertex3f(-1.0, 1.0,-1.0);                  // Top Left Of The Quad (Left)
          glVertex3f(-1.0,-1.0,-1.0);                  // Bottom Left Of The Quad (Left)
          glVertex3f(-1.0,-1.0, 1.0);                  // Bottom Right Of The Quad (Left)
  glEnd();
  glBegin(GL_QUADS);
          glColor3f(1.0,0.0,1.0);                              // Set The Color To Violet
          glVertex3f( 1.0, 1.0,-1.0);                  // Top Right Of The Quad (Right)
          glVertex3f( 1.0, 1.0, 1.0);                  // Top Left Of The Quad (Right)
          glVertex3f( 1.0,-1.0, 1.0);                  // Bottom Left Of The Quad (Right)
          glVertex3f( 1.0,-1.0,-1.0);                  // Bottom Right Of The Quad (Right)
  glEnd();

  Speed := double(OpenGLControl1.FrameDiffTimeInMSecs)/10;

  cube_rotationx += 1.2 * Speed;
  cube_rotationy += 1 * Speed;
  cube_rotationz += 1 * Speed;

  OpenGLControl1.SwapBuffers;

end;

 

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: 32bit (transparent) color in OpenGL control component?
« Reply #6 on: July 09, 2011, 04:28:15 pm »
These should only be called once when the program starts:
Quote
  glClearColor(1.0, 1.0, 1.0, 1.0);
  ..
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(45.0, double(width) / height, 0.1, 100.0);
  glMatrixMode(GL_MODELVIEW)

You didn't post the actual transparency code... The actual rendering there looks just fine, if with glColor3f(0.0,1.0,0.0); you assume to fully use green color and rely on texture alpha-channel.

Also what kind of texture are you loading and how? Simplest format i imagine is with PNG that supports alpha. But when it comes to displaying anything transparently, if you use for example glColor4f(1,1,1,0.5), you could even load a JPEG with simple RGB and show it half transparent. Is it texture alpha-channel you are after or both...?

Peiman

  • New Member
  • *
  • Posts: 41
Re: 32bit (transparent) color in OpenGL control component?
« Reply #7 on: July 10, 2011, 12:36:50 pm »
I don't want to use any texture image. As I've mentioned, I only need COLOR, like the simple solidbrush type in GDI+. That's why I think OpenGL is not the best choice for me. (specially as I don't also need a high speed in rendering).

I think the problem is that I have misunderstood opengl. I supposed it also sorts depths by its own. While its official website has stated I have to do this prior using color4f (without textures). (Does GLScene does it automatically?). So I started to write/find sorting algorithms.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: 32bit (transparent) color in OpenGL control component?
« Reply #8 on: July 10, 2011, 10:07:35 pm »
OpenGL doesn't automatically sort polygons, and if it did, it could be extremely memory consuming and slow. Also the way color and depthbuffer works, everything is always plot on top. So you can't for example draw a wall behind a glass window, but you have to draw the wall first.

glColor4f works equally well with "solidbrush" you mean, which is simply a untextured polygon.

Also, your faces were supposed to be colored like this:

Code: [Select]
  glBegin(GL_QUADS);
          glColor4f(0.0,1.0,0.0, 0.5);                // Set The Color To half transparent Green
          glVertex3f( 1.0, 1.0,-1.0);                  // Top Right Of The Quad (Top)
          glVertex3f(-1.0, 1.0,-1.0);                  // Top Left Of The Quad (Top)
          glVertex3f(-1.0, 1.0, 1.0);                  // Bottom Left Of The Quad (Top)
          glVertex3f( 1.0, 1.0, 1.0);                  // Bottom Right Of The Quad (Top)
  glEnd();

Quote
I think it is easier to design a new engine rather use opengl for this.
OpenGL is a graphics API like windows SDK is. Every hardware accelerated graphics engine uses either OpenGL or DirectX (or both). Doing it non-accelerated with just CPU will be a huge drop in performance.
« Last Edit: July 10, 2011, 10:14:32 pm by User137 »

 

TinyPortal © 2005-2018