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

Thread: Using SOIL to load textures onto blended polygons?

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

    Using SOIL to load textures onto blended polygons?

    Hello all,

    I'm brand new to this forum and wanted to thank ahead anyone likely to help me. I'm having a bit of trouble getting textures to load onto polygons, the code below is very simple, but as simple as it can be it seems to not show anything except two flag shaped triangles.

    I'm basically using the file "img_test.png" from the SOIL library that is included in the following code.
    Just for the sake of reference I added this to imgur Click image for larger version. 

Name:	YMF1knr.png 
Views:	16 
Size:	2.2 KB 
ID:	2664

    Code :
    #include <GL/gl.h>
    #include <GL/glu.h>
    #include <GL/glut.h>
    #include <stdlib.h>
    #include <iostream>
    #include <string>
    #include <stdio.h>
    #include "src/SOIL.h"
     
    static int leftFirst = GL_TRUE;
    typedef GLuint texture;
    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_POWER_OF_TWO
    				| SOIL_FLAG_MIPMAPS
    				| SOIL_FLAG_MULTIPLY_ALPHA
    				| SOIL_FLAG_COMPRESS_TO_DXT
    				| SOIL_FLAG_DDS_LOAD_DIRECT
    				| SOIL_FLAG_INVERT_Y
    				);
     
    		if( tex_ID > 0 )
    		{
    			glEnable( GL_TEXTURE_2D );
    			glBindTexture( GL_TEXTURE_2D, tex_ID );
     
    			return tex_ID;
    		}
    		else
    			return 0;
    }
    /*  Initialize alpha blending function.  */
    static void init(void)
    {
       glEnable (GL_BLEND);
       glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
       glShadeModel (GL_FLAT);
       glClearColor (1.0, 1.0, 1.0, 0.0);
       TextureID = GetTexture( "img_test.png");
    }
    static void drawLeftTriangle(void)
    {
    /* draw yellow triangle on LHS of screen */
        // For Ortho mode, of course
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, TextureID);
        glBegin (GL_TRIANGLES);
          glColor4f(1.0, 1.0, 0.0, 0.75);
          glVertex3f(0.1, 0.9, 0.0);
          glVertex3f(0.1, 0.1, 0.0);
        glVertex3f(0.7, 0.5, 0.0);
        glEnd();
        glDisable(GL_TEXTURE_2D);
    }
    static void drawRightTriangle(void)
    {
        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, TextureID);
    /* draw cyan triangle on RHS of screen */
       glBegin (GL_TRIANGLES);
          glColor4f(0.0, 1.0, 1.0, 0.75);
          glVertex3f(0.9, 0.9, 0.0);
          glVertex3f(0.3, 0.5, 0.0);
          glVertex3f(0.9, 0.1, 0.0);
       glEnd();
       glDisable(GL_TEXTURE_2D);
    }
    void display(void)
    {
       glClear(GL_COLOR_BUFFER_BIT);
       drawLeftTriangle();
       drawRightTriangle();
       glFlush();
    }
     
    void reshape(int w, int h)
    {
       glViewport(0, 0, (GLsizei) w, (GLsizei) h);
       glMatrixMode(GL_PROJECTION);
       glLoadIdentity();
       if (w <= h)
          gluOrtho2D (0.0, 1.0, 0.0, 1.0*(GLfloat)h/(GLfloat)w);
       else
          gluOrtho2D (0.0, 1.0*(GLfloat)w/(GLfloat)h, 0.0, 1.0);
    }
     
    void keyboard(unsigned char key, int x, int y)
    {
       switch (key)
       {
          case 27:  /*  Escape key  */
             exit(0);
             break;
          default:
             break;
       }
    }
    void FreeTexture( GLuint texture )
    {
      glDeleteTextures( 1, &texture );
    }
    int main(int argc, char** argv)
    {
       glutInit(&argc, argv);
       glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
       glutInitWindowSize ( 480, 480);
     
       glutCreateWindow (argv[0]);
     
       init();
     
       glutReshapeFunc (reshape);
       glutKeyboardFunc (keyboard);
       glutDisplayFunc (display);
       glutMainLoop();
     
       FreeTexture( TextureID);
     
       return 0;
    }


    I managed to solve it myself using another thread on here https://www.opengl.org/discussion_bo...-drawing/page2

    This works for bmp, png, and jpg images!

    Code :
    #include "SOIL.h"
    #include "GL/glew.h"
    #include "GL/glut.h"
     
    GLuint texture;
     
    void display (void)
    {
     
    //    if these lines were still there, i get a black screen
        glClearColor (0.0,0.0,0.0,1.0);
        glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
     
        // background render
     
        glDisable( GL_DEPTH_TEST ) ;///!!!!
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0.0f, 1024.0, 512.0, 0.0, 0.0, 1.f);
     
        glEnable( GL_TEXTURE_2D ); //
        glBindTexture( GL_TEXTURE_2D, texture );
    glColor3f(1.0f,1.0f,1.0f);
    glBegin (GL_QUADS);
        glTexCoord2d(0.0,0.0); glVertex2d(0.0,0.0);
        glTexCoord2d(1.0,0.0); glVertex2d(1024.0,0.0);
        glTexCoord2d(1.0,1.0); glVertex2d(1024.0,512.0);
        glTexCoord2d(0.0,1.0); glVertex2d(0.0,512.0);
    glEnd();
        glDisable(GL_TEXTURE_2D);
     
        // foreground render
     
        // re-enable depth writes and testing
        glEnable( GL_DEPTH_TEST );
        glDepthMask(GL_TRUE);
     
        glLoadIdentity();
        //gluPerspective (60, (GLfloat)winWidth / (GLfloat)winHeight, 0.0, 1.0);
        glMatrixMode(GL_PROJECTION); // changed this here to be consistent w/ conventions
     
        glOrtho(0.0f, 1024.0, 512.0, 0.0, 0.0, 1.f);
     
        glColor3f(0.0, 0.0, 1.0);
        glBegin (GL_QUADS);
            glVertex2d(400.0,100.0);
            glVertex2d(400.0,300.0);
            glVertex2d(700.0,100.0);
            glVertex2d(700.0,300.0);
        glEnd();
     
        glutSwapBuffers();
    }
    void reshape (int w, int h)
    {
        glViewport (0, 0, (GLsizei)w, (GLsizei)h);
        glMatrixMode (GL_PROJECTION);
        glLoadIdentity ();
        gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
        glMatrixMode (GL_MODELVIEW);
    }
    int main (int argc, char **argv)
    {
        glutInit (&argc, argv);
        glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
        glutInitWindowSize (1024, 512);
        glutInitWindowPosition (100, 100);
        glutCreateWindow ("A basic OpenGL Window");
        glutDisplayFunc (display);
        glutIdleFunc (display);
        glutReshapeFunc (reshape);
     
        glEnable( GL_TEXTURE_2D );
     
        texture = SOIL_load_OGL_texture
    	(
    		"img_test.jpg",
    		SOIL_LOAD_AUTO,
    		SOIL_CREATE_NEW_ID,
    		SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
    	);
     
        glutMainLoop ();
     
        return 0;
    }
    Last edited by pandoragami; 02-11-2018 at 11:27 AM. Reason: Solved the 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
  •