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

Thread: SOIL library wont display a png file

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2013
    Posts
    4

    SOIL library wont display a png file

    Hello,

    Im using SOIL library to display a random png image and learn this process in openGL with Codeblocks. Everything compiles fine and without errors but then nothing comes up in the created black screen. Ive checked that the png file is in the required folder where the exe is and everything seems correct but image wont show up. Any idea why this happens? This is the code:

    #include <GL/glut.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <SOIL.h>

    void LoadPNG()
    {
    GLuint tex_2d = SOIL_load_OGL_texture(

    "C:\\Users\\Serrano\\Documents\\Sources\\HelloWorl d2\\.binsd\\img_test.png",
    //"img_test.png",
    SOIL_LOAD_AUTO,
    SOIL_CREATE_NEW_ID,
    SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT);

    /* check for an error during the load process */
    if( 0 == tex_2d )
    {
    printf( "SOIL loading error: '%s'\n", SOIL_last_result() );
    }
    }

    void init(void)
    {
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_FLAT);
    }

    void display(void)
    {
    glClear(GL_COLOR_BUFFER_BIT),
    glColor3f(1.0, 1.0, 1.0);
    glLoadIdentity();
    LoadPNG();
    glFlush();
    }

    void reshape(int w, int h)
    {
    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode(GL_PROJECTION),
    glLoadIdentity();
    //glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);*/
    gluPerspective(100.0, 1.0, 1.5, 20.0);
    glMatrixMode(GL_MODELVIEW);
    }
    /* Program entry point */

    int main(int argc, char *argv[])
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(200,200);
    glutCreateWindow(argv[0]);
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
    }

    Thank you!

  2. #2
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    449
    Loading a texture isn't enough, you need to draw some triangles too. Instead of loading the texture every frame (which will cause you too run out of memory the way you have done it), load the texture once + issue some draw calls using some vertices that have texture coordinates.
    With modern OpenGL you need to understand buffer objects, vertex attributes + everything shader related before you can draw anything, but using legacy OpenGL getting started drawing stuff is fairly easy, as you can use:
    Code :
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, tex_2d);
    glBegin(GL_TRIANGLES);
    glTexCoord2f(0.0f, 0.0f);glVertex3f(0.0f, 0.0f, 0.0f);
    glTexCoord2f(1.0f, 0.0f);glVertex3f(1.0f, 0.0f, 0.0f);
    glTexCoord2f(0.0f, 1.0f);glVertex3f(0.0f, 1.0f, 0.0f);
    glEnd();

  3. #3
    Junior Member Newbie
    Join Date
    Oct 2013
    Posts
    4
    Hello, thank you very much. Im fairly new to Open GL and thought it was possible to display the image without a support object sort to say. So then again I tried to wrap my 256x256 .png texture into a square created with GL_QUADS. But still window is black:

    /*Trying to attach a texture to a square drawn with quads*/


    #include <windows.h>
    #include <GL/glut.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <SOIL.h>
    #include <gl\gl.h>
    #include <gl\glu.h>

    GLuint texture[1];

    int LoadGLTextures()
    {
    texture[0] = SOIL_load_OGL_texture
    (
    "img_test.png",
    SOIL_LOAD_AUTO,
    SOIL_CREATE_NEW_ID,
    SOIL_FLAG_INVERT_Y
    );

    if(texture[0] == 0)
    return false;

    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR );
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);

    return true;
    }


    void init()
    {

    glEnable(GL_TEXTURE_2D);
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_FLAT);

    }

    void display(void)
    {
    glClear(GL_COLOR_BUFFER_BIT);
    //glEnable(GL_TEXTURE_2D);
    glLoadIdentity();
    glColor3f(0.0, 0.0, 0.0);
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glBegin(GL_QUADS);
    glTexCoord2f(0.25f, 0.25f); glVertex3f(0.25f, 0.25f, 0.0f);
    glTexCoord2f(0.75f, 0.25f); glVertex3f(0.75f, 0.25f, 0.0f);
    glTexCoord2f(0.75f, 0.75f); glVertex3f(0.75f, 0.75f, 0.0f);
    glTexCoord2f(0.25f, 0.75f); glVertex3f(0.25f, 0.75f, 0.0f);
    glEnd();
    LoadGLTextures();
    glFlush();
    }

    void reshape(int w, int h)
    {
    glViewport(0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode(GL_PROJECTION),
    glLoadIdentity();
    //glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);*/
    gluPerspective(30.0, 1.0, 1.5, 20.0);
    glMatrixMode(GL_MODELVIEW);
    }
    /* Program entry point */

    int main(int argc, char *argv[])
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(200,200);
    glutCreateWindow(argv[0]);
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
    }

  4. #4
    Intern Contributor
    Join Date
    Jun 2013
    Posts
    69
    i think you should look over some tutorials. the code youre using is from a very old opengl by the way. if you want to learn old opengl then visit nehe.gamedev.net

  5. #5
    Member Regular Contributor
    Join Date
    Aug 2008
    Posts
    449
    Yeah, a complete tutorial would be helpful, but 2 things that will be causing a black screen:

    1) You need to load the texture before drawing the triangle, not after (and only load it once).
    2) Don't draw a black triangle on a black background. You have glColor3f(0.0, 0.0, 0.0) but probably instead want glColor3f(1.0f, 1.0f, 1.0f).

  6. #6
    Junior Member Newbie
    Join Date
    Oct 2013
    Posts
    4
    Thank you very much, it worked! I think problem was on loading texture after instead of before.

Posting Permissions

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