tags:

views:

196

answers:

1

I'm having trouble getting a texture loaded with SOIL to show up properly on this quad. In case it's not clear, I'm just writing up a little 2D sprite engine, and this is the rendering portion (needs a bit of optimization without a doubt). I haven't done any OpenGL in a couple months, and I'm admittedly quite rusty.

#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#include "SOIL.h"

#include <stdio.h>

GLuint linktex;

void drawSprite(GLint left, GLint right, GLint bottom, GLint top, GLuint texture){  
    //Draw clockwise
    glColor3f(1.0, 1.0, 1.0);
    glBindTexture(GL_TEXTURE_2D, texture);

    glBegin(GL_QUADS);
    glTexCoord2i(1,1); glVertex2i(right , top);
    glTexCoord2i(1,0); glVertex2i(right , bottom);
    glTexCoord2i(0,0); glVertex2i(left , bottom);
    glTexCoord2i(0,1); glVertex2i(left , top);
    glEnd();
}

void display(void){
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    glOrtho(0.0, 240.0, 0.0, 160.0, -1.0, 1.0);
    drawSprite(50, 82, 50, 82, linktex);
    glFlush();
}

void reshape(int w, int h){
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
}

void init(){
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glFrontFace(GL_CW);

    GLuint linktex = SOIL_load_OGL_texture(
        "link.png",
        SOIL_LOAD_AUTO,
        SOIL_CREATE_NEW_ID,
        SOIL_FLAG_INVERT_Y
    );

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);

    if( 0 == linktex )
    {
        printf( "SOIL loading error: '%s'\n", SOIL_last_result());
    }
}

int main (int argc, char **argv) {
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE);
    glutInitWindowSize (240, 160);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("Test");

    glutDisplayFunc (display);
    glutReshapeFunc (reshape);
    glutMainLoop();
    init();
    return 0;
}
+2  A: 

It looks like when you are loading the texture, you are assigning the id to a local variable linktex instead of the global you declared at the beginning of the file.

When you reference linktex in the void display(void); method, the texture is un-initialized.

Try changing your call to load the texture to :

// comment out the type declaration, to assign to the global instead of a local
/*GLuint*/ linktex = SOIL_load_OGL_texture(
        "link.png",
        SOIL_LOAD_AUTO,
        SOIL_CREATE_NEW_ID,
        SOIL_FLAG_INVERT_Y
    );
John Weldon