Search code examples
opengltextures

How do you put textures on square with OpenGL?


I tried to apply textures to squares with the following code, but they didn't apply. Is there a problem with the code above? "dd" is printed on the console window. I think it is read the bmp file. but I only see white square like the following pictures.

white square

Bitmap files are located in the project folder.

path

LoadBMP() is a function that opens the bmp file. LoadGLTextures() is a function to load textures.

ps. I am not good at English. I'm sorry.

#define _CRT_SECURE_NO_DEPRECATE
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <GL/GLAUX.H>

#pragma comment (lib,"glaux.lib")

unsigned int MyTextureObiect[1];
AUX_RGBImageRec *pTextureImage[1];

AUX_RGBImageRec *LoadBMP(char *szFilename) {
    FILE * pFile = NULL;
    if (!szFilename) {
        return NULL;
    }
    pFile = fopen(szFilename, "r");
    if (pFile) {
        fclose(pFile);
        return auxDIBImageLoad(szFilename);
    }
    return NULL;
}

int LoadGLTextures() {
    int Status = FALSE;
    memset(pTextureImage, 0, sizeof(void *) * 1);
    if (pTextureImage[0] = LoadBMP("butterflyans.bmp")) {
        printf("dd");
        Status = TRUE;
        glGenTextures(1, &MyTextureObiect[0]);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexImage2D(GL_TEXTURE_2D, 0, 3, pTextureImage[0]->sizeX, pTextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, pTextureImage[0]->data);
        glEnable(GL_TEXTURE_2D);
    }
    if (pTextureImage[0]) {
        if (pTextureImage[0]->data) {
            free(pTextureImage[0]->data);
        }
        free(pTextureImage[0]);
    }
    return Status;
}




void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glBindTexture(GL_TEXTURE_2D, MyTextureObiect[0]);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-0.5, -0.5, 0.0);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(0.5, -0.5, 0.0);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(0.5, 0.5, 0.0);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-0.5, 0.5, 0.0);
    glEnd();
    glFlush();
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("OpenGL Drawing Example");
    glutDisplayFunc(myDisplay);
    if (LoadGLTextures()) {
        glEnable(GL_TEXTURE_2D);
    }
    glutMainLoop();

    return 0;
}

Solution

  • You missed to bind glBindTexture the texture object, before setting the texture parameters and specifying the texture.
    Note, glTexParameter and glTexImage2D apply to the currently bound texture:

    glGenTextures(1, &MyTextureObiect[0]);
    
    glBindTexture(GL_TEXTURE_2D, MyTextureObiect[0]); // <---- bind the texture
    
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D( .... );
    

    By the way, you set GL_TEXTURE_MIN_FILTER twice, but you never set GL_TEXTURE_MAG_FILTER.