Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

OpenGL transparent texture messing with GL_Lines

  • 19-05-2011 3:38pm
    #1
    Registered Users, Registered Users 2 Posts: 7,182 ✭✭✭


    Right, messing with GL. When I use a .tga texture with no transparency then everything is fine.
    All my debug GL_Lines draw fine.
    159612.PNG
    But when I tried loading a .tga with an alpha channel all my LG_Lines seem to be drawn in black.
    159613.PNG

    I am setting the colour of the lines with glColor4f();

    Here is the init function for the graphics:
    void InitGraphics()
    {
        hDC = GetDC(hWnd);
    
        SetupPixelFormat();
    
        hRC = wglCreateContext(hDC);
        wglMakeCurrent(hDC, hRC);
    
    	glEnable(GL_TEXTURE_2D);
    	glShadeModel(GL_SMOOTH);
    	glClearColor(0, 0, 0, 0.5);
    	glClearDepth(1.0);
    	glEnable(GL_DEPTH_TEST);
    	glDepthFunc(GL_LEQUAL);
    	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
        
    }
    
    And the draw calls, the gl_Blend doesn't seem to do anything. If I remove them (and they usually are removed) nothing changes.
    void DrawGraphics()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        // Set location in front of camera
        glLoadIdentity();
        glTranslated(0, 0, -10);
    
        // Draw shapes
    	glEnable(GL_BLEND);
    	glBlendFunc(GL_SRC_ALPHA,GL_ONE);
    	ShapeManager::Instance()->DrawShapes();
    	glDisable(GL_BLEND);
    	#ifdef DEBUG
    		DrawDebug();
    	#endif
    
        // Show the new scene
        SwapBuffers(hDC);
    }
    

    DrawShapes is just a loop calling glVertex3f on a load of gl_Quads.

    Any ideas, most of this code is stolen, blatantly, from NeHe (Praised be his name).


Comments

  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    It's ages since I've looked at fixed function OpenGL so I'm guessing there, but what alpha do you set for the GL_Lines?


  • Registered Users, Registered Users 2 Posts: 7,182 ✭✭✭Genghiz Cohen


    fasty wrote: »
    It's ages since I've looked at fixed function OpenGL so I'm guessing there, but what alpha do you set for the GL_Lines?

    1

    Also when I said GL_Quads, I meant GL_Polygon.


  • Registered Users, Registered Users 2 Posts: 7,182 ✭✭✭Genghiz Cohen


    Also,
    If the bottom right corner of a texture is a colour, sometimes the debug grid in the background gets drawn with what appears to be that colour mixed with the glColor4f(0.2,0.2,0.2,0.5); I set the line to.

    Plus, when using multiple shapes with my default texture (.png here because boards doesn't like .tga)
    160303.png

    If one passes over the other, the white sections show through.


  • Registered Users, Registered Users 2 Posts: 7,182 ✭✭✭Genghiz Cohen


    And NOW I notice that a texture with transpatency over one without blends them together.
    6034073


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    Hmm, your blend function setup doesn't seem right to me now I'm looking at it. It should be GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA.

    This means that if a source pixel's alpha is 0, the colour will be source.rgb * 0. This will be added to destination.rgb * (1 - 0), which is whatever you set the clear colour to in this case.
    glBlendFunc(GL_SRC_ALPHA,GL_ONE);
    

    It doesn't matter in in this case though, because you're essentially using the alpha of your textures as a mask.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 7,182 ✭✭✭Genghiz Cohen


    fasty wrote: »
    It doesn't matter in in this case though, because you're essentially using the alpha of your textures as a mask.

    Almost.
    Without the debug lines, everything draws correctly now. So that's a big step, thanks!


  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    Just force alpha to 1 if not using an alpha texture. Or disable blending altogether if you aren't using it.

    Those lines are for an overlay right? If it's an overlay you don't want to blend at all, just use the "painter's algorithm" and draw it last. If it's a screenspace overlay use a special 2d shader with normalised device coordinates, if the lines exist in 3d space then just draw them last and disable depth test.


  • Registered Users, Registered Users 2 Posts: 7,182 ✭✭✭Genghiz Cohen


    srsly78 wrote: »
    if the lines exist in 3d space then just draw them last and disable depth test.

    Will try this tomorrow.
    Is it wrong for a man to hope?
    :p


  • Registered Users, Registered Users 2 Posts: 7,182 ✭✭✭Genghiz Cohen


    Bah, no joy.


  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    Get rid of all transparency, no blending at all. What does it render as then?

    Does your texture have an alpha channel? Is it RGBA?

    When I get stuck I usually go back to basics, and add complexity until it breaks. Try using per-vertex colored quads instead of textures, should be easier to debug.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 7,182 ✭✭✭Genghiz Cohen


    srsly78 wrote: »
    Get rid of all transparency, no blending at all. What does it render as then?

    Does your texture have an alpha channel? Is it RGBA?

    When I get stuck I usually go back to basics, and add complexity until it breaks. Try using per-vertex colored quads instead of textures, should be easier to debug.

    If I take out all texturing and blending code and let a texture load, it's the same result.
    If the texture has Alpha, all lines are drawn black.
    If not, lines are drawn correctly and the GL_Polys come up as filled white (as I set the GL_Color to white)

    Looking into my (NeHe's) tex loading code, I commented out these lines and the issue went away
    // Typical Texture Generation Using Data From The TGA ( CHANGE )
    		glGenTextures(1, &texture->texID);				// Create The Texture ( CHANGE )
    		glBindTexture(GL_TEXTURE_2D, texture->texID);
    		glTexImage2D(GL_TEXTURE_2D, 0, texture->bpp / 8, texture->width, texture->height, 0, texture->type, GL_UNSIGNED_BYTE, texture->imageData);
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    Can you post the line drawing code, including any state you might set? The texture loading code looks fine, proviced the bpp is 32 for RGBA TGAs and 24 for RGB.


  • Registered Users, Registered Users 2 Posts: 7,182 ✭✭✭Genghiz Cohen


    The for loop is temporary until I get around to telling it to only draw on visible space where X % 10 == 0 and Y %10 == 0
    void DrawDebug()
    {
    	glPopMatrix();
    	glBegin(GL_LINES);
    
    	glColor4f(0.5,0.5,0.5,1);
    
    	for(int i = 0; i < 2000; i++)
    	{
    		glVertex3f(-1000 + 10 * i, 1000, 1);
    		glVertex3f(-1000 + 10 * i, -1000, 1); 
    
    		glVertex3f(1000, -1000 + 10 * i, 1);
    		glVertex3f(-1000, -1000 + 10 * i, 1);
    	}
    	
    	glColor4f(1,0,0,1);
    	glVertex3f(0,0,1);
    	glColor4f(1,0,0,1);
    	glVertex3f(1,0,1);
    	
    	glColor4f(0,1,0,1);
    	glVertex3f(0,0,1);
    	glColor4f(0,1,0,1);
    	glVertex3f(0,1,1);
    
    	glColor4f(0,0,1,1);
    	glVertex3f(0,0,1);
    	glColor4f(0,0,1,1);
    	glVertex3f(0,0,2);
    	glEnd();
    	glPopMatrix();
    
    	
    	SManager->DrawDebug(); // Tell the ShapeManager to draw its debug lines
    	
    }
    

    I'm sitting here cringing, waiting for you go to "Um, yeah.. It's that super obvious error on line derp..."


  • Registered Users, Registered Users 2 Posts: 7,182 ✭✭✭Genghiz Cohen


    If I glClearColor(1, 0, 0, 0);
    Then the GL_Lines and polys stay black.


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    Haha, no major derps that I can see so far but I'll have a think about it.

    It's a strange one. For my framework, I have a similar debug drawing interface that lets me draw lines and shapes in 2D or 3D on top of the final scene. All I do is disable depth testing to get stuff to appear on top of everything else, or just disable depth writes to make the debug lines work properly with 3D space.

    I've never had any weird alpha issues like this at all!


  • Registered Users, Registered Users 2 Posts: 7,182 ✭✭✭Genghiz Cohen


    I could put up the source code if that would be useful (keeping in mind it's poorly commented and heavily experimental)


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    Yeah, I'll take a look at the source if you want to post it. Don't worry about comments or experiments, it's impossible to figure things out without hacking out messy code first!


  • Registered Users, Registered Users 2 Posts: 7,182 ✭✭✭Genghiz Cohen


    fasty wrote: »
    Yeah, I'll take a look at the source if you want to post it. Don't worry about comments or experiments, it's impossible to figure things out without hacking out messy code first!


    http://www.filedropper.com/glfirst

    There's a super fancy algo for generating randomly shaped pixely asteroids in there, don't be stealing it :P


  • Registered Users, Registered Users 2 Posts: 7,157 ✭✭✭srsly78


    Umm that's ancient OpenGL :D I haven't seen glBegin/glEnd in years! These days I mostly use OpenGL ES on mobiles or DirectX on desktop. Am in work so can't look too closely right now tho :(

    Your glClearColor() however... Are you using RGBA or ARGB? You realise you set alpha to zero right? That's why everything went black.


  • Registered Users, Registered Users 2 Posts: 7,182 ✭✭✭Genghiz Cohen


    srsly78 wrote: »
    Umm that's ancient OpenGL :D I haven't seen glBegin/glEnd in years! These days I mostly use OpenGL ES on mobiles or DirectX on desktop. Am in work so can't look too closely right now tho :(

    Your glClearColor() however... Are you using RGBA or ARGB? You realise you set alpha to zero right? That's why everything went black.

    It's the GL we learned, I mean to change it to arrays at some point. :p

    Changing the clear colour A value didn't seem to do anything last time I tired, and even if it did, how would that effect the lines getting drawn on top of it?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    I'll hit it with the debug stick later on! A quick look at the code doesn't show anything obvious!


  • Registered Users, Registered Users 2 Posts: 981 ✭✭✭fasty


    161071.png

    The problem was the state machine aspect of OpenGL. GL_TEXTURE_2D was enabled when drawing your debug lines, but you didn't specify texture coordinates or anything.

    Since you'll only be drawing debug lines in debug mode, wrap that drawing with
    void DrawDebug()
    {
    	glDisable(GL_TEXTURE_2D);
    	// your debug drawing code
    	glEnable(GL_TEXTURE_2D);
    }
    

    Try to minimise state changes in actual rendering code if you can! Oh, and you can move all that other stuff to your init function. Just make sure you give the lines and alpha of 1 and you won't need to switch blending on and off.

    BTW, If you want to move on to newer versions of OpenGL and use cool stuff like GLSL, VBOs and FBOs, here are some great sites:

    http://duriansoftware.com/joe/An-intro-to-modern-OpenGL.-Table-of-Contents.html
    http://www.arcsynthesis.org/gltut/

    You lose things like the matrix stack (a good thing IMO), so you'll need a maths library. GLM is worth a look, as is the Configurable Maths Library.

    And there's this article on using GLEW to create a 3.1 context

    http://www.opengl.org/wiki/Tutorial:_OpenGL_3.1_The_First_Triangle_(C%2B%2B/Win)

    or a minimal version of my own device class here:

    http://pastebin.com/mBjR3YLi

    and Swiftless' OpenGL 4 tutorials

    http://www.swiftless.com/opengltuts/opengl4tuts.html

    But don't worry about using fixed function OpenGL for now, it does the job if you just want to play with the gameplay stuff.


  • Registered Users, Registered Users 2 Posts: 7,182 ✭✭✭Genghiz Cohen


    fasty wrote: »
    161071.png

    The problem was the state machine aspect of OpenGL. GL_TEXTURE_2D was enabled when drawing your debug lines, but you didn't specify texture coordinates or anything.

    Since you'll only be drawing debug lines in debug mode, wrap that drawing with
    void DrawDebug()
    {
    	glDisable(GL_TEXTURE_2D);
    	// your debug drawing code
    	glEnable(GL_TEXTURE_2D);
    }
    

    Try to minimise state changes in actual rendering code if you can! Oh, and you can move all that other stuff to your init function. Just make sure you give the lines and alpha of 1 and you won't need to switch blending on and off.

    BTW, If you want to move on to newer versions of OpenGL and use cool stuff like GLSL, VBOs and FBOs, here are some great sites:

    http://duriansoftware.com/joe/An-intro-to-modern-OpenGL.-Table-of-Contents.html
    http://www.arcsynthesis.org/gltut/

    You lose things like the matrix stack (a good thing IMO), so you'll need a maths library. GLM is worth a look, as is the Configurable Maths Library.

    And there's this article on using GLEW to create a 3.1 context

    http://www.opengl.org/wiki/Tutorial:_OpenGL_3.1_The_First_Triangle_(C%2B%2B/Win)

    or a minimal version of my own device class here:

    http://pastebin.com/mBjR3YLi

    and Swiftless' OpenGL 4 tutorials

    http://www.swiftless.com/opengltuts/opengl4tuts.html

    But don't worry about using fixed function OpenGL for now, it does the job if you just want to play with the gameplay stuff.

    In years to come, when developers run into a GL problem, they will whisper a short prayer to fasty.
    You legend.


Advertisement