2
\$\begingroup\$

I don't understand why my texture("texture.bmp"), is not showing on the screen. Please help me.

The whole drawing function:

///HERE EVERYTHING IS BEING RENDERED
void Main::display(){

    int startTime = GetTickCount();

    ///BACKGROUND COLOR
    glClearColor(0.3f, 0.3f, 2.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    ///LOAD THE IDENTITY MATRIX FOR THE DRAWING
    glLoadIdentity();

    ///SO EVERYTHING HAS THE CORRECT SIZE
    glTranslatef(0.0f, 0.0f, -5.0f);

    ///CAMERA MOVEMENT
    glTranslatef(cameraX, 0.0f, 0.0f);

    ///THE SUN
    glBegin(GL_QUADS);
        glColor3f(1.0f,1.0f,0.0f);

        glVertex2f(-3.0f, 2.0f); // The bottom left corner
        glVertex2f(-3.0f, 2.5f); // The top left corner
        glVertex2f(-2.5f, 2.5f); // The top right corner
        glVertex2f(-2.5f, 2.0f); // The bottom right corner
    glEnd();

    ///THE GROUND
    glBegin(GL_QUADS);
        glColor3f(0.5f,2.0f,0.5f);

        glVertex2f(-20.0f,-4.0f);
        glVertex2f(-20.0f,-1.0f);
        glVertex2f(130.0f,-1.0f);
        glVertex2f(130.0f,-4.0f);

        glColor3f(0.0f,1.0f,0.0f);

        for(float i = 0.0f ; i < 100.0f ; i += 5.0f){
            glVertex2f(i,-1.0f);
            glVertex2f(i,ranFloat[static_cast<int>(i-4.0f)]);
            glVertex2f(i+1.0f,ranFloat[static_cast<int>(i-4.0f)]);
            glVertex2f(i+1.0f,-1.0f);
        }
    glEnd();

    ///THE GRASS
    glBegin(GL_QUADS);
        glColor3f(0.0f,0.5f,0.0f);

        glVertex2f(-20.0f,-1.2f);
        glVertex2f(-20.0f,-1.0f);
        glVertex2f(130.0f,-1.0f);
        glVertex2f(130.0f,-1.2f);
    glEnd();

    ///THE PLAYER
    glTranslatef(playerX,playerY,0.0f);
    glBegin(GL_QUADS);
        glColor3f(1.0f,0.0f,0.0f);

        glVertex2f(-3.0f,-1.0f);
        glVertex2f(-3.0f,0.0f);
        glVertex2f(-2.8f,0.0f);
        glVertex2f(-2.8f,-1.0f);
    glEnd();


    ///TEXTUREEEEEEEEEEEEEEEEEEEEEE
    glBindTexture(GL_TEXTURE_2D,texture);
    glEnable(GL_TEXTURE_2D);

    int X = 0;
    int Y = 0;
    int Width = 1;
    int Height = 1;

    glBegin(GL_QUADS);
        glTexCoord2f(0, 0); glVertex3f(X, Y, 0);
        glTexCoord2f(0, 1); glVertex3f(X + Width, Y, 0);
        glTexCoord2f(1, 1); glVertex3f(X + Width, Y + Height, 0);
        glTexCoord2f(1, 0); glVertex3f(X, Y + Height, 0);
    glEnd();
    glDisable(GL_TEXTURE_2D);

    ///CLOUDS
    glBegin(GL_QUADS);
        glColor3f(1.0f,1.0f,1.0f);
        for(float i = 0.0f ; i < 100.0f ; i += 5.0f){
            glVertex2f(i,3.0f);
            glVertex2f(i,4.0f);
            glVertex2f(i+4.0f,3.0f);
            glVertex2f(i+4.0f,4.0f);
        }
    glEnd();

    if(Input::flying){
        Input::returnFly(0.007f);
    }

    elapsedMS = GetTickCount() - startTime; // Time since start of loop
    //std::cout << "FPS: " << elapsedMS << std::endl;

    ///THE ACTUAL MOVEMENT
    playerY += playerFly;//*elapsedMS;
    playerX += cameraSpeed*-1;//*elapsedMS;
    cameraX += cameraSpeed;//*elapsedMS;

    ///SWAP THE BUFFERS FOR SMOOTH RENDERING
    glutSwapBuffers();
  }

The rest of my code has nothing to do with the texture loading. The loading is not the problem, because I didn't get any errors.

\$\endgroup\$
  • \$\begingroup\$ why on earth are you using sdl and glut togther? \$\endgroup\$ – The Light Spark Nov 3 '15 at 16:29
  • \$\begingroup\$ SDL for loading the image and glut for opengl, to draw the texture(SDL image). \$\endgroup\$ – OpenGLmaster1992 Nov 3 '15 at 16:30
  • 1
    \$\begingroup\$ SDL has both opengl context creation and texture loading functionality. You should not have required two frameworks that do the same things, (and mess up each other's work?) \$\endgroup\$ – The Light Spark Nov 3 '15 at 16:37
  • \$\begingroup\$ I actually prefer glut, but I don't know if I can load images with glut. \$\endgroup\$ – OpenGLmaster1992 Nov 3 '15 at 16:40
0
\$\begingroup\$

Are you checking that your SDL_Surface* is valid? Are you sure that your "Mode" is RGB/RGBA and not BGRA/BGR?

I think the problem is that you need to pass the Surface->format->BytesPerPixel as the third argument to glTexImage2D instead of Mode.

Also, I would set the Min/Mag filter before calling glTexImage2D.

Like this:

void Main::loadStuff() {
    GLuint texture = 0;

    SDL_Surface* Surface = SDL_LoadBMP("texture.bmp");
    if (!Surface) {
        // error, report?
        return;
    }

    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);

    int Mode = GL_RGB;

    if(Surface->format->BytesPerPixel == 4) {
        Mode = GL_RGBA;
    }
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, Surface->format->BytesPerPixel, Surface->w, Surface->h, 0, Mode, GL_UNSIGNED_BYTE, Surface->pixels);


    SDL_FreeSurface(Surface);
}

Bonus points if you want to work in std::unique_ptr for you surface.

\$\endgroup\$
  • \$\begingroup\$ Can't edit, but, also noticed that GLuint texture is local to void Main::loadStuff() - that could certainly be part of the problem, too (when you try to rebind in "The drawing of the texture:" portion, where does that texture come from? \$\endgroup\$ – morandy Nov 3 '15 at 19:27
  • \$\begingroup\$ It is a static variable I defined in the Main header file \$\endgroup\$ – OpenGLmaster1992 Nov 3 '15 at 19:44
  • \$\begingroup\$ Ah! The local one is taking over, that's probably why. Get rid of the one at the top of void Main::loadStuff() then, it's taking over. \$\endgroup\$ – morandy Nov 3 '15 at 19:53
  • \$\begingroup\$ But how can I draw now. I need the texture variable but now it is only defined in de loadStuff function, and I need in in the display function. \$\endgroup\$ – OpenGLmaster1992 Nov 3 '15 at 19:56
  • \$\begingroup\$ Sorry, I wasn't clear, get rid of the one local to loadStuff and have loadStuff set your static variable in the Main header file. \$\endgroup\$ – morandy Nov 3 '15 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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