OpenGL2, Почему не отображается текстура на квадрате?
Вот код, текстуры не видно, вместо нее какая-то текстура как шахматная доска
#include <GL/glut.h>
#include <GL/gl.h>
#include <stdio.h>
unsigned char* loadBMPRaw(const char* imagepath, unsigned int& outWidth, unsigned int& outHeight, bool flipY) {
printf("Reading image %s\n", imagepath);
outWidth = -1;
outHeight = -1;
unsigned char header[54];
unsigned int dataPos;
unsigned int imageSize;
unsigned char* data;
FILE* file = fopen(imagepath, "rb");
if (!file) { printf("Image could not be opened\n"); return NULL; }
if (fread(header, 1, 54, file) != 54) {
printf("Not a correct BMP file\n");
return NULL;
}
if (header[0] != 'B' || header[1] != 'M') {
printf("Not a correct BMP file\n");
return NULL;
}
if (*(int*)&(header[0x1E]) != 0) { printf("Not a correct BMP file\n"); return NULL; }
if (*(int*)&(header[0x1C]) != 24) { printf("Not a correct BMP file\n"); return NULL; }
dataPos = *(int*)&(header[0x0A]);
imageSize = *(int*)&(header[0x22]);
outWidth = *(int*)&(header[0x12]);
outHeight = *(int*)&(header[0x16]);
if (imageSize == 0) imageSize = outWidth * outHeight * 3;
if (dataPos == 0) dataPos = 54;
data = new unsigned char[imageSize];
fread(data, 1, imageSize, file);
fclose(file);
if (flipY) {
unsigned char* tmpBuffer = new unsigned char[outWidth * 3];
int size = outWidth * 3;
for (int i = 0; i < outHeight / 2; i++) {
memcpy_s(tmpBuffer, size, data + outWidth * 3 * i, size);
memcpy_s(data + outWidth * 3 * i, size, data + outWidth * 3 * (outHeight - i - 1), size);
memcpy_s(data + outWidth * 3 * (outHeight - i - 1), size, tmpBuffer, size);
}
delete[] tmpBuffer;
}
return data;
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(800, 600);
glutCreateWindow("windowname");
unsigned int x, y;
unsigned char* texDat = loadBMPRaw("wood.bmp", x, y, false);
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 8, 8, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, texDat);
glMatrixMode(GL_PROJECTION);
glOrtho(0, 800, 0, 600, -1, 1);
glMatrixMode(GL_MODELVIEW);
glClear(GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, tex);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex2i(100, 100);
glTexCoord2i(0, 1); glVertex2i(100, 500);
glTexCoord2i(1, 1); glVertex2i(500, 500);
glTexCoord2i(1, 0); glVertex2i(500, 100);
glEnd();
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
glFlush();
system("pause");
return 0;
}