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 4 of 4

Thread: Blending not yielding the results as expected, trying to make images overlap...

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Junior Member Newbie
    Join Date
    Feb 2018
    Posts
    6

    Question Blending not yielding the results as expected, trying to make images overlap...

    Basically I'm trying to avoid using depth sorting by using blending and I've tried the familiar

    Code :
    glEnable(GL_BLEND);
             glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    but no luck.

    The goal I'm trying to get to is something I managed to solve in Irrlicht using the opengl driver so I know this should work. Here's the thread and the final image with blending enabled. http://irrlicht.sourceforge.net/foru...eb86385ff21b57



    Here's the source code for the GL version using GLUT and SOIL for image loading, I'm using Xubuntu ( not sure it matters).

    Code :
    #include <GL/glut.h>
    #include <GL/glu.h>
    #include <cmath>
    #include <iostream>
    #include <string>
    #include "SOIL.h"
    // angle of rotation for the camera direction
    float angle=0.0;
    // actual vector representing the camera's direction
    float lx=0.0f,lz=-1.0f;
    // XZ position of the camera
    float x=0.0f,z=35.0f;
    // all variables initialized to 1.0, meaning
    // the triangle will initially be white
    float red=1.0f, blue=1.0f, green=1.0f;
     
    GLuint TextureID = 0;
    GLuint GetTexture(std::string Filename)
    {
    	GLuint tex_ID;
     
    	tex_ID = SOIL_load_OGL_texture
    	(
    				Filename.c_str(),
    				SOIL_LOAD_AUTO,
                    SOIL_CREATE_NEW_ID,
                    SOIL_FLAG_MIPMAPS |
                    SOIL_FLAG_INVERT_Y |
                    SOIL_FLAG_NTSC_SAFE_RGB |
                    SOIL_FLAG_COMPRESS_TO_DXT
            );
     
    		if( tex_ID > 0 )
    		{
    			glEnable( GL_TEXTURE_2D );
    			glBindTexture( GL_TEXTURE_2D, tex_ID );
    			std::cout<<"texture loaded!"<<std::endl;
     
    			return tex_ID;
    		}
    		else
    			return 0;
    }
    void processNormalKeys(unsigned char key, int x, int y)
    {
    	if (key == 27)
    		exit(0);
    }
    void changeSize(int w, int h)
    {
    	// Prevent a divide by zero, when window is too short
    	// (you cant make a window of zero width).
    	if (h == 0)
    		h = 1;
    	float ratio =  w * 1.0 / h;
     
            // Use the Projection Matrix
    	glMatrixMode(GL_PROJECTION);
     
            // Reset Matrix
    	glLoadIdentity();
     
    	// Set the viewport to be the entire window
    	glViewport(0, 0, w, h);
     
    	// Set the correct perspective.
    	gluPerspective(45.0f, ratio, 0.1f, 100.0f);
     
    	// Get Back to the Modelview
    	glMatrixMode(GL_MODELVIEW);
    }
    void renderScene(void)
    {
    	// Clear Color and Depth Buffers
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	// Reset transformations
    	glLoadIdentity();
    	// Set the camera
    	gluLookAt(	x, 1.0f, z,
    			x+lx, 1.0f,  z+lz,
    			0.0f, 1.0f,  0.0f);
     
    			 glEnable(GL_BLEND);
     
    			 //tried a whole bunch of other stuff
      //glBlendFunc(GL_ONE, GL_ONE);
    //glEnable(GL_BLEND_COLOR);
    //glEnable(GL_DEPTH_TEST);
    //glBlendEquation( GL_FUNC_ADD);
    //glBlendFunc(GL_DST_ALPHA, GL_ZERO);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, TextureID);
    	glColor3f(0.9f, 0.9f, 0.9f);
    	glBegin(GL_QUADS);
            glTexCoord2d(0.0,0.0);
    		glVertex3f(-5.0f, -5.0f, 0.0f);
    		glTexCoord2d(1.0,0.0);
    		glVertex3f(-5.0f, 5.0f, 0.0f  );
    		glTexCoord2d(1.0,1.0);
    		glVertex3f( 5.0f, 5.0f, 0.0f);
    		glTexCoord2d(0.0,1.0);
    		glVertex3f( 5.0f, -5.0f, 0.0f );
    	glEnd();
     
    	glColor3f(0.9f, 0.9f, 0.9f);
    	glBegin(GL_QUADS);
            glTexCoord2d(0.0,0.0);
    		glVertex3f(-10.0f, -5.0f, 0.0f);
    		glTexCoord2d(1.0,0.0);
    		glVertex3f(-10.0f, 5.0f, 0.0f  );
    		glTexCoord2d(1.0,1.0);
    		glVertex3f( 5.0f, 5.0f, 0.0f);
    		glTexCoord2d(0.0,1.0);
    		glVertex3f( 5.0f, -5.0f, 0.0f );
    	glEnd();
        glDisable(GL_TEXTURE_2D);
     
    glDisable(GL_BLEND);
     
    	glutSwapBuffers();
    }
    void processSpecialKeys(int key, int xx, int yy)
    {
    	float fraction = 0.1f;
     
    	switch (key)
    	{
    		case GLUT_KEY_LEFT :
    			angle -= 0.01f;
    			lx = sin(angle);
    			lz = -cos(angle);
    			break;
    		case GLUT_KEY_RIGHT :
    			angle += 0.01f;
    			lx = sin(angle);
    			lz = -cos(angle);
    			break;
    		case GLUT_KEY_UP :
    			x += lx * fraction;
    			z += lz * fraction;
    			break;
    		case GLUT_KEY_DOWN :
    			x -= lx * fraction;
    			z -= lz * fraction;
    			break;
    	}
    }
    int main(int argc, char **argv)
    {
    	// init GLUT and create window
    	glutInit(&argc, argv);
    	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    	glutInitWindowPosition(100,100);
    	glutInitWindowSize( 640, 640);
    	glutCreateWindow("SOIL image Test");
     
    	// register callbacks
    	glutDisplayFunc(renderScene);
    	glutReshapeFunc(changeSize);
    	glutIdleFunc(renderScene);
    	glutKeyboardFunc(processNormalKeys);
    	glutSpecialFunc(processSpecialKeys);
     
    	// OpenGL init
     
    	glEnable(GL_DEPTH_TEST);
        TextureID = GetTexture( "../../../textures/particlewhite.png");
    	// enter GLUT event processing cycle
    	glutMainLoop();
     
    	return 1;
    }

    and the png image Click image for larger version. 

Name:	particlewhite.png 
Views:	52 
Size:	15.4 KB 
ID:	2666
    Last edited by pandoragami; 02-11-2018 at 05:11 PM. Reason: removed unneeded code.

Tags for this Thread

Posting Permissions

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