Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 8 of 8

Thread: rotating TRANSPARENT cylinder - looks weird

  1. #1
    Intern Contributor
    Join Date
    Aug 2011
    Posts
    76

    rotating TRANSPARENT cylinder - looks weird

    See this movie: http://www.2shared.com/video/RwpFidK...cylinder.html?
    And this code:

    ---------------------
    # glPush + some translation + rotation....

    # Enable blending / transparancy
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    # Cylinder color (NB: ALPHA MUST BE < 1, DEFAULT IS 0.5)!
    glColor4f(1.0, 1.0, 1.0, 0.2)

    # Draw ends of cylinder:
    cyl_obj = gluNewQuadric();

    cylHgt = 0.2
    glTranslate( 0.0, 0.0, -cylHgt/2 )

    # Draw cylinder:
    # gluDisk (GLUquadricObj, innerRadius, outerRadius, slices, rings)
    gluDisk(cyl_obj, 0.0, 0.1, 12, 1);

    # gluCylinder(gluNewQuadr, Radius_base, Radius_top,
    # height, slices, stacks)
    gluCylinder(cyl_obj, 0.1, 0.1, cylHgt, 12, 1);

    glTranslate( 0.0, 0.0, cylHgt )
    gluDisk(cyl_obj, 0.0, 0.1, 12, 1);

    glPopMatrix() # -- POP PROJECTION MATRIX
    ---------------------

    My question is: Why does the cylinder look like one side of it is different in terms of transparency, compared to the other side of the cylinder ?

    http://www.2shared.com/video/RwpFidK...cylinder.html?

    I mean: When the movie start, you can see that the whole cylinder has the same color everywhere... Then after a few seconds you see a dark spot moving along one of the rotating axes - is it due to lighting ? I don't understand this - I don't think it looks right and I don't think I want this behaviour...

    Thank you for any ideas/comments/suggestions...!

  2. #2
    Intern Contributor
    Join Date
    Aug 2011
    Posts
    76
    Nobody knows?

    Maybe someone can help me (lead me in the right direction) by telling how to get a background color like this: http://tinyurl.com/9h55qy6

    I tried google, but couldn't find the answer... Still, question number 1 is also in play, if you can help...

    Thank you very much - I appreciate anything you might suggest !

  3. #3
    Junior Member Regular Contributor
    Join Date
    Apr 2012
    Posts
    167
    Quote Originally Posted by newsb View Post
    # glPush + some translation + rotation....

    # Enable blending / transparancy
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    # Cylinder color (NB: ALPHA MUST BE < 1, DEFAULT IS 0.5)!
    glColor4f(1.0, 1.0, 1.0, 0.2)

    # Draw ends of cylinder:
    cyl_obj = gluNewQuadric();

    cylHgt = 0.2
    glTranslate( 0.0, 0.0, -cylHgt/2 )

    # Draw cylinder:
    # gluDisk (GLUquadricObj, innerRadius, outerRadius, slices, rings)
    gluDisk(cyl_obj, 0.0, 0.1, 12, 1);

    # gluCylinder(gluNewQuadr, Radius_base, Radius_top,
    # height, slices, stacks)
    gluCylinder(cyl_obj, 0.1, 0.1, cylHgt, 12, 1);

    glTranslate( 0.0, 0.0, cylHgt )
    gluDisk(cyl_obj, 0.0, 0.1, 12, 1);

    glPopMatrix() # -- POP PROJECTION MATRIX
    Help us to help you. When you post code, put in between [ code] and [ /code] tags.
    Make it as clean as possible to scan with the eye (i.e. indent when possible).
    Also - post code that would actually compile.
    There are many syntax errors in the code you posted, such as missing ;,
    and statements like 'glTranslate'. It should be 'glTranslatef'. Bottom line
    is that it looks like you didn't try very hard to do a clean post. You shouldn't
    expect responses that try hard either.

    That being said, I tried to run a cleaned up version of your code on my Windows 7 computer (NVidia Quadro FX 1800 video card), and it ran fine. Could be the difference in our video cards. I did not get any of the artifacts present in your animation. You might want to look at the discussion of blending on p. 215 of the Red Book for some hints on how to eliminate your artifacts.
    Last edited by Carmine; 10-01-2012 at 12:39 PM.

  4. #4
    Intern Contributor
    Join Date
    Aug 2011
    Posts
    76
    Quote Originally Posted by Carmine View Post
    Help us to help you. When you post code, put in between [ code] and [ /code] tags.
    Uh, sorry - you're completely right - thank you for making me remember that. If I could edit the original post, I would do it now - however, seems like I can only "edit post" on my last reply (or is it a timelimit?)...

    Make it as clean as possible to scan with the eye (i.e. indent when possible).
    That's what I tried to do - I even tried to make it so short that people would quickly see the problem and the attached/"linked to" video... This particular code piece didn't need any indentation because that would look wrong (all I showed is at the same level, i.e. in a display()-function or similar place)... I think, sorry - that it's a matter of taste about making a long post (that can compile) or showing only the relevant code lines. However, maybe you're right and maybe I would get better feedback if I showed the complete code. BUT: I think us python-users are a minority here. I didn't have time to convert it all to C/C++/C# as I imagine most people here would use...?

    Also - post code that would actually compile.
    There are many syntax errors in the code you posted, such as missing ;,
    and statements like 'glTranslate'. It should be 'glTranslatef'. Bottom line
    is that it looks like you didn't try very hard to do a clean post. You shouldn't
    expect responses that try hard either.
    Sorry, but you're wrong. I code in python so there's no missing ";". And glTranslate is correct (at least I think so): You can see it here: http://www.opengl.org/sdk/docs/man/x...lTranslate.xml and I also use it directly in my code... However I've also been wondering which version of glTranslate python will choose. Until now, I think it'll automatically choose the best/correct one. Regarding posting code that would compile: I think not so many in here use python so that's why I didn't post my python code. Another reason is that I assumed that someone more experienced than you and me could just look at the movie I posted at see / tell what the problem was/is and/or come up with good suggestions/ideas about what could be wrong...

    That being said, I tried to run a cleaned up version of your code on my Windows 7 computer (NVidia Quadro FX 1800 video card), and it ran fine. Could be the difference in our video cards. I did not get any of the artifacts present in your animation. You might want to look at the discussion of blending on p. 215 of the Red Book for some hints on how to eliminate your artifacts.
    Thank you very much. It's very useful for me to know that it works on another machine and on another OS (I'm on linux with nvidia GTX 260, I think). I suspect maybe my graphics card... Maybe one day, I should try to see how this behaves under windows, if nothing else comes up...

    About the reference to blending on p. 215 of the Red Book: Thank you very much! That's exactly a hint/idea/suggestion that I'm looking for and maybe I'll find the explanation there... I only just learned about blending the other day, so it could also be that I'm initialising my graphics stuff / opengl init()-stuff incorrectly...

    I'm very grateful because you took the time to answer/give me some ideas... If you/anyone have more ideas that can help me solve either the artifact problem or my glclear background color problem/question, then I would really appreciate that too... Anyway, if this is all the feedback I get/deserve: Thanks again for your valuable time and ideas/references, which I'll dig into tomorrow after a bit sleep now...

  5. #5
    Junior Member Regular Contributor
    Join Date
    Apr 2012
    Posts
    167
    Quote Originally Posted by newsb View Post
    I code in python ....
    My apologies. That explains the #'s.

    It's very useful for me to know that it works on another machine and on another OS (I'm on linux with nvidia GTX 260, I think). I suspect maybe my graphics card...
    I've seen artifacts like yours in some of my programs. I eliminated them by following the advice in the Redbook - particularly the DepthMask stuff shown below. Are your polygons two sided? If so, try culling the backfaces. Note that with Classic GL you are not going to be able to make transparency work correctly for lots (i.e. more than 2) objects. You could probably do that with shaders.

    Code :
    void display (void)
    {
       GLfloat mat_solid[] = { 0.75, 0.75, 0.0, 1.0 };
       GLfloat mat_zero[] = { 0.0, 0.0, 0.0, 1.0 };
       GLfloat mat_transparent[] = { 0.0, 0.8, 0.8, 0.6 };
       GLfloat mat_emission[] = { 0.0, 0.3, 0.3, 0.6 };
     
       glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
       glPushMatrix ();
          glTranslatef (-0.15, -0.15, solidZ);
          glMaterialfv(GL_FRONT, GL_EMISSION, mat_zero);
          glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_solid);
          glCallList (sphereList);
       glPopMatrix ();
     
       glPushMatrix ();
          glTranslatef (0.15, 0.15, transparentZ);
          glRotatef (15.0, 1.0, 1.0, 0.0);
          glRotatef (30.0, 0.0, 1.0, 0.0);
          glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
          glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_transparent);
          glEnable (GL_BLEND);
          glDepthMask (GL_FALSE);
          glBlendFunc (GL_SRC_ALPHA, GL_ONE);
          glCallList (cubeList);
          glDepthMask (GL_TRUE);
          glDisable (GL_BLEND);
       glPopMatrix ();
     
       glutSwapBuffers();
    }

  6. #6
    Intern Contributor
    Join Date
    Aug 2011
    Posts
    76
    Quote Originally Posted by Carmine View Post
    I've seen artifacts like yours in some of my programs. I eliminated them by following the advice in the Redbook - particularly the DepthMask stuff shown below.
    Uh, this time it's me that has to be sorry for the long waiting time before I reply back. I checked for an answer but suddely I got called in to a lot of work-related meetings and stuff to do after work, or else I normally try to answer people who help me ASAP - also for helping other people (for the record/archives).

    Are your polygons two sided? If so, try culling the backfaces. Note that with Classic GL you are not going to be able to make transparency work correctly for lots (i.e. more than 2) objects. You could probably do that with shaders.
    Hmm. That was hard questions for a guy like me. I frankly don't know if my polygons are two-sided or not and I once remember doing something with culling, long ago. In any case: I'm really really grateful, because you've helped me solve my problem using the depthmask stuff you show below. Thank you very much for that - it seems to completely solve my problem!

    I don't understand the comment about: "not going to be able to make transparency work correctly for lots (i.e. more than 2) objects" - looks correct to me... Or maybe I'm not using classic GL ? Also, shaders - I've heard/read a lot about them but never really tried them because opengl is not my primary work. I work more with physics and reaction forces (in Matlab) on a daily basis. OpenGL is in my sparetime. Anyway, I think I'm getting better and better now, especially thanks to helpful people like you. I'll not look into shaders for the moment - maybe in 1/2 year I'll try working with them... Right now, I calculate some physics (position, velocity and acceleration) with Matlab and then I load the results into python using scipy and then I use openGL to show it. Impressingly enough, I've now found out how to import *.obj files from my CAD-program and show it in the openGL program that you just helped me with! So this is what I play with at the moment - later, shaders sounds like fun and I'll also learn that within the next 12 months, I hope :-)

    Code :
    void display (void)
    {
       GLfloat mat_solid[] = { 0.75, 0.75, 0.0, 1.0 };
       GLfloat mat_zero[] = { 0.0, 0.0, 0.0, 1.0 };
       GLfloat mat_transparent[] = { 0.0, 0.8, 0.8, 0.6 };
       GLfloat mat_emission[] = { 0.0, 0.3, 0.3, 0.6 };
     
       glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
       glPushMatrix ();
          glTranslatef (-0.15, -0.15, solidZ);
          glMaterialfv(GL_FRONT, GL_EMISSION, mat_zero);
          glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_solid);
          glCallList (sphereList);
       glPopMatrix ();
     
       glPushMatrix ();
          glTranslatef (0.15, 0.15, transparentZ);
          glRotatef (15.0, 1.0, 1.0, 0.0);
          glRotatef (30.0, 0.0, 1.0, 0.0);
          glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
          glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_transparent);
          glEnable (GL_BLEND);
          glDepthMask (GL_FALSE);
          glBlendFunc (GL_SRC_ALPHA, GL_ONE);
          glCallList (cubeList);
          glDepthMask (GL_TRUE);
          glDisable (GL_BLEND);
       glPopMatrix ();
     
       glutSwapBuffers();
    }

    You're completely right. For the record: Just using DepthMask (false) before drawing my object and DepthMask (true) after, solved my problem...

    Thank you SO much, openGL friend ! I really appreciate your time to help me solve this... I'll now read in redbook and play around with this for the whole sunday, where I have nothing else to do :-)
    I'm grateful because I would probably never have figured out this solution myself
    Thanks for a great forum.

  7. #7
    Junior Member Regular Contributor
    Join Date
    Apr 2012
    Posts
    167
    ... don't understand the comment about: "not going to be able to make transparency work correctly for lots (i.e. more than 2) objects" - looks correct to me...
    This technique doesn't work when you have several transparent objects in front of each other, i.e. you are trying to look through 3 or more transparent objects. If you look closely you'll see that it breaks down. You can render 1000 transparent objects correctly as long as you don't have 3 or more along a line of sight. BTW - remember to render the transparent objects last in a scene.

    Or maybe I'm not using classic GL
    You are using Classic GL.

  8. #8
    Intern Contributor
    Join Date
    Aug 2011
    Posts
    76
    Quote Originally Posted by Carmine View Post
    This technique doesn't work when you have several transparent objects in front of each other, i.e. you are trying to look through 3 or more transparent objects. If you look closely you'll see that it breaks down.
    Maybe it's because my transparent cylinders are so simple and I only have a tiny overlap. My 3 cylinders don't overlap very much and only for a short time... At least I cannot see any problems and here's the movie: http://www.2shared.com/video/dghWGJN3/transp.html

    You can render 1000 transparent objects correctly as long as you don't have 3 or more along a line of sight. BTW - remember to render the transparent objects last in a scene.
    Uh, thank you - will do so... I'm almost ready to replace my cylinders with my wavefront OBJ-display-list-stuff... I think (hope) it'll be great, think in 1-2 days I have transparent objects from my CAD-program spinning around instead of these ugly simple grey/white dummy-cylinders :-)

    You are using Classic GL.
    Uh, ok, thank you. Appreciate all your comments, they're very valuable to me in this process - thanks man!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •