Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm a beginner to OpenGL and GLSL especially, and I'm having a lot of trouble getting textures to display via shaders. My shader code compiles without any warnings or errors, but I seem completely unable to render my texture. In fact, nothing's being rendered at all; I'm getting a blank screen. Here's my current, simplified code (written in D, but should function almost exactly as it looks as if it were in C++):

int main(string[] argv) {
    try {
        ilInit();
        iluInit();
        ilutInit();                 // GOTCHA: These two calls need to be in order!
        ilutRenderer(ILUT_OPENGL);  //
        glfwInit();
        if(SDL_Init(SDL_INIT_VIDEO) > 0) writeln("ERROR");

        // Create an OpenGL context and window.
        const int VWIDTH = 1600;
        const int VHEIGHT = 900;
        const int WIDTH = 1280;
        const int HEIGHT = 720;
        glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 8);
        glfwOpenWindowHint(GLFW_AUX_BUFFERS, 1);
        glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
        glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
        glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_FALSE);
        glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);
        glfwOpenWindow(WIDTH, HEIGHT, 8, 8, 8, 8, 0, 0, GLFW_WINDOW /+ GLFW_FULLSCREEN +/);

        GL.bindClassicVersions(true);
        GL.bindModernVersions(true, GLVersion.GL33);
        GL.bindExtensions(true);    // Returns GL_INVALID_ENUM

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, VWIDTH, 0, VHEIGHT, -100, 100);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity(); 

        // Set clear color
        glClearColor(0.392, 0.584, 0.929, 1);

        // Set GL enables/disables
        glShadeModel(GL_SMOOTH);                                // Enables smooth shading
        glEnable(GL_TEXTURE_2D);                                // Enable 2D texturing
        glEnable(GL_BLEND);                                     // Enable alpha blending
        glDisable(GL_DEPTH_TEST);                               // Disable depth testing
        glDisable(GL_CULL_FACE);                                // Disable face culling
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        //glDepthFunc(GL_LEQUAL);                                 // The depth test type

        // Initialize and compile shaders
        GLuint vertShaderId, fragShaderId;
        vertShaderId = glCreateShader(GL_VERTEX_SHADER);
        fragShaderId = glCreateShader(GL_FRAGMENT_SHADER);

        immutable(GLchar)* vertShaderSrc = toStringz("#version 330
in vec3 vertex;
in vec2 texCoord;

out vec2 fragmentTexCoord;

void main(void) {
    fragmentTexCoord = texCoord;
    gl_Position = vec4(vertex, 1.0);
}
");
        immutable(GLchar)* fragShaderSrc = toStringz("#version 330
uniform sampler2D firstTexture;
in vec2 fragmentTexCoord;

layout(location = 0, index = 0) out vec4 fragColor;

void main(void) {
    fragColor = texture2D(firstTexture, fragmentTexCoord);
}
");

        glShaderSource(vertShaderId, 1, &vertShaderSrc, null);
        glShaderSource(fragShaderId, 1, &fragShaderSrc, null);

        glCompileShader(vertShaderId);
        tge.shader.Shader.checkCompileLog(vertShaderId);
        glCompileShader(fragShaderId);
        tge.shader.Shader.checkCompileLog(fragShaderId);

        GLuint programId = glCreateProgram();

        glAttachShader(programId, vertShaderId);
        glAttachShader(programId, fragShaderId);

        glLinkProgram(programId);
        tge.shader.Shader.checkCompileLog(programId);

        glUseProgram(programId);
        checkGLError();

        // Load texture
        auto texture = new Texture2d(r"res/test.png");
        texture.load();

        bool running = true;
        clearGLError();
        while(running) {
            // Clear viewport
            glClear(GL_COLOR_BUFFER_BIT);

            // Bind texture ID
            glActiveTexture(GL_TEXTURE0);
            glBindTexture(GL_TEXTURE_2D, texture.textureId);
            checkGLError();

            // Set texture uniform value
            auto loc = glGetUniformLocation(programId, "firstTexture");
            checkGLError();
            glUniform1i(loc, 0);
            checkGLError();

            // Render
            glBegin(GL_QUADS);
                glTexCoord2d(0, 0); glVertex2d(-0.8, -0.8);
                glTexCoord2d(1, 0); glVertex2d( 0.8, -0.8);
                glTexCoord2d(1, 1); glVertex2d( 0.8,  0.8);
                glTexCoord2d(0, 1); glVertex2d(-0.8,  0.8);
            glEnd();
            checkGLError();

            // Swap front and back rendering buffers
            glfwSwapBuffers();

            // Add a tiny delay if no work is being done in the loop.
            //glfwSleep(double.epsilon);

            // Check if ESC key was pressed or window was closed
            running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
        }
    }
    catch(Throwable t) {
        Logger.log(Severity.Error, t.msg);
    }

    return 0;
}

I'm guessing it's something simple I've overlooked due to being new. Any help would be appreciated!

share|improve this question
    
Some debugging tips: 1. never clear with black (i.e. change the clear color to see if any triangle hits the render target), 2. try to not fill the whole screen for sanity checks (i.e. your quad, that covers the whole window. Use .8 rather than 1). those 2 things will help isolate where the issue is. –  Bahbar Aug 26 '11 at 11:33
    
Good points. I did both of those things, and I'm still getting a blank screen (albeit now colored cornflower blue, instead of black). –  ThatsGobbles Aug 26 '11 at 16:15
add comment

1 Answer

One thing I noticed is that you commented out the actual color finding procedure:

    //fragColor = texture2D(firstTexture, fragmentTexCoord);
    fragColor = vec4(1, 0, 0, 1);

Another Problem:

    glVertex2d(-1, -1); glTexCoord2d(0, 0);

should be:

    glTexCoord2d(0, 0); glVertex2d(-1, -1); 

in all the lines

share|improve this answer
    
My mistake, that was me doing sanity testing. I've edited my original post, but I wasn't able to get that sanity check working either. –  ThatsGobbles Aug 25 '11 at 21:43
1  
@ThatsGobbles: If you couldn't get the sanity check to work, then there's a lack of sanity in your code ;) Or, to put it another way, the problem has nothing to do with texturing. Also, you neglected to say what exactly wasn't working about it. –  Nicol Bolas Aug 26 '11 at 0:23
    
I'm not getting any visual output whatsoever, only a black screen. –  ThatsGobbles Aug 26 '11 at 0:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.