Textures White In OpenGL

closed account (N36fSL3A)
I don't know why, but textures in OpenGL are white. One day, I must've change something, and now my textures wont load properly. I tried rewriting my code that loads things multiple times, my textures still are white. Here is my code:

(I switched back to the FF pipeline temporarily, in my other projects I use shaders.)

Sprite.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <SDL.h>
#include <SDL_opengl.h>

#include <fstream>
#include <iostream>
#include <string>
#include <vector>

#ifndef _SPRITE_H_
#define _SPRITE_H_

// Use pragma to erase padding
#pragma pack(push, 1)
struct BMPFileHeader
{
	Uint8  id[2];       // BMP Identifier
	Uint32 size;        // Size of image
	Uint8  reserved[4]; // Reserved
	Uint32 imgAddr;     // Address where pixel data starts
};

struct BMPInfoHeader
{
	Uint32 size;    // Size of header, must be 40
	Sint32 w;       // Width of image
	Sint32 h;       // Height of image
	Uint16 p;       // Number of planes in the image
	Uint32 imgSize; // Size of image in bytes
	Sint32 ppMW;    // Pixels per meter width(Unreliable)
	Sint32 ppMH;    // Pixels per meter height(Unreliable)
	Uint32 cN;      // Number of colors in image
	Uint32 cIN;     // Number of important colors in image
};

struct BMPHeader
{
	BMPFileHeader bf;
	BMPInfoHeader bi;
};
#pragma(pop)

struct FRect
{
	float x, y, w, h;
};

struct Vec2
{
	float x, y;
};

class Sprite
{
	public:
		Sprite() {w = 0; h = 0; sprite = 0;}

		void LoadBMP(const std::string location);

		void Render(const Vec2 pos, FRect* rect = nullptr);
		void Render(float x, float y, FRect* rect = nullptr);

		std::vector<Uint8> getPixelData() const{return pixels;}

		unsigned int getW() const{return w;}
		unsigned int getH() const{return h;}

		unsigned int getSprite() const{return sprite;}

		~Sprite() {glDeleteTextures(1, &sprite); sprite = 0;}

	private:
		std::vector<Uint8> pixels;    // Holds pixel data

		unsigned int w;
		unsigned int h;

		unsigned int sprite;
};

#endif//_SPRITE_H_ 


Sprite.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "Sprite.hpp"

void Sprite::LoadBMP(const std::string location)
{
	std::ifstream file(location); // Attempt to open a file
	BMPHeader     header;         // Object that holds bmp information

	// If the file opened
	if(file)
	{
		// Read in the header
		file.read((char*)&header, sizeof(header));

		// Check if the image is a bmp image
		if(!(header.bf.id[0] == 'B' && header.bf.id[1] == 'M')) {throw "Unreadable image.";}

		// I know I'm supposed to check for other stuff to be true, but I could care less about it

		// Make some space for image data
		pixels = std::vector<Uint8>(header.bi.imgSize);

		file.seekg(header.bf.imgAddr); // Jump to location in file where pixel data starts
		file.read((char*)&pixels[0], header.bi.imgSize); // Read in the data


		// Now we have the image loaded into memory, generate the texture object
		glGenTextures(1, &sprite);            // Generate a texture
		glBindTexture(GL_TEXTURE_2D, sprite); // Bind that texture temporarily

		GLint mode = GL_RGB;                   // Set the mode

		//pixels = ColorKey(pixels, w, h, img->pitch, key, mode);

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	 
		// Create the texture. We get the offsets from the image, then we use it with the image's
		// pixel data to create it.
		glTexImage2D(GL_TEXTURE_2D, 0, mode, w, h, 0, mode, GL_UNSIGNED_BYTE, pixels.data());
		
		// Unbind the texture
		glBindTexture(GL_TEXTURE_2D, 0);
	}

	else {throw "Failure opening file";}
}

void Sprite::Render(const Vec2 pos, FRect* rect)
{
	if(sprite == 0) {throw "Sprite ID is equal to 0!";}

	float tTop    = 0.0f;
	float tBottom = 1.0f;
	float tLeft   = 0.0f;
	float tRight  = 1.0f;

	float qWidth  = (float)w;
	float qHeight = (float)h;

	if(rect != nullptr)
	{
		tLeft   = rect->x/w;
		tRight  = (rect->x + rect->w) / w;
		tTop    = rect->y / h;
		tBottom = (rect->y + rect->h ) / h;

		qWidth  = rect->w;
		qHeight = rect->h;
	}

	glPushMatrix();
		// Move to desired position
		glTranslatef(pos.x, pos.y, 0);

		glBindTexture(GL_TEXTURE_2D, sprite);
		glBegin(GL_QUADS);
			glTexCoord2f(tLeft, tTop);     glVertex2f(0.f, 0.0f);
			glTexCoord2f(tRight, tTop);    glVertex2f(qWidth, 0.0f);
			glTexCoord2f(tRight, tBottom); glVertex2f(qWidth, qHeight);
			glTexCoord2f(tLeft, tBottom);  glVertex2f(0.f, qHeight);
		glEnd();
	glPopMatrix();
}

void Sprite::Render(float x, float y, FRect* rect)
{
	Vec2 pos = {x, y};

	Render(pos, rect);
}
Last edited on
closed account (N36fSL3A)
Bah, I fixed it. But now my textures show up weird (2 separate clips of image):
http://prntscr.com/24rxav

if I try drawing a clip then the full image:
http://prntscr.com/24rz0p

My image is 64 by 64, an enlarged version:

http://prntscr.com/24ry0j

This is how I'm using it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void Game::Render()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();

	//glTranslatef(0.5f, 0.5f, 0.0f);

	glEnable(GL_TEXTURE_2D);

	FRect rect  = {0.0f, 0.0f, 32.0f, 32.0f};
	FRect rect2 = {0.0f, 0.0f, 32.0f, 32.0f};

	img.Render(windowWidth/2, windowHeight/2, &rect);
	img.Render(windowWidth/2, (windowHeight/2) + 128, &rect2);

	//img.Render(windowWidth/2, (windowHeight/2) + 128);

	SDL_GL_SwapBuffers();
}
Last edited on
When you say you fixed it did you:

 
glTexImage2D(GL_TEXTURE_2D, 0, mode, w, h, 0, mode, GL_UNSIGNED_BYTE, pixels.data());
change w, h to header.bi.w and header.bi.h

and add missing fields in this struct?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct BMPInfoHeader
{
	Uint32 size;    // Size of header, must be 40
	Sint32 w;       // Width of image
	Sint32 h;       // Height of image
	Uint16 p;       // Number of planes in the image
        // missing colordepth bpp 2 bytes
        // missing  compression method 4 bytes
	Uint32 imgSize; // Size of image in bytes
	Sint32 ppMW;    // Pixels per meter width(Unreliable)
	Sint32 ppMH;    // Pixels per meter height(Unreliable)
	Uint32 cN;      // Number of colors in image
	Uint32 cIN;     // Number of important colors in image
};
Topic archived. No new replies allowed.