PDA

View Full Version : texture



Hylke Donker
10-22-2004, 01:56 AM
Hello
I have written a small program that would just show a texture loaded from a file(i got the loading thing from nehe), but somehow the texture looks really weird with much stripes in it.
My code:

#include <GL/glut.h>
#include <stdio.h>

void Init();
void Reshape(int w, int h);
void Render();

GLuint TexName;

struct Image {
unsigned long sizeX;
unsigned long sizeY;
char *data;
};

typedef struct Image Image;

int ImageLoad(char *filename, Image *image);

int ImageLoad(char *filename, Image *image)
{
FILE *file;
unsigned long size; // size of the image in bytes.
unsigned long i; // standard counter.
unsigned short int planes; // number of planes in image (must be 1)
unsigned short int bpp; // number of bits per pixel (must be 24)
char temp; // temporary color storage for bgr-rgb conversion.

// make sure the file is there.
if ((file = fopen(filename, "rb"))==NULL)
{
printf("File Not Found : %s\n",filename);
return 0;
}

// seek through the bmp header, up to the width/height:
fseek(file, 18, SEEK_CUR);

// read the width
if ((i = fread(&amp;image->sizeX, 4, 1, file)) != 1) {
printf("Error reading width from %s.\n", filename);
return 0;
}
printf("Width of %s: %lu\n", filename, image->sizeX);

// read the height
if ((i = fread(&amp;image->sizeY, 4, 1, file)) != 1) {
printf("Error reading height from %s.\n", filename);
return 0;
}
printf("Height of %s: %lu\n", filename, image->sizeY);

// calculate the size (assuming 24 bits or 3 bytes per pixel).
size = image->sizeX * image->sizeY * 3;

// read the planes
if ((fread(&amp;planes, 2, 1, file)) != 1) {
printf("Error reading planes from %s.\n", filename);
return 0;
}
if (planes != 1) {
printf("Planes from %s is not 1: %u\n", filename, planes);
return 0;
}

// read the bpp
if ((i = fread(&amp;bpp, 2, 1, file)) != 1) {
printf("Error reading bpp from %s.\n", filename);
return 0;
}
if (bpp != 24) {
printf("Bpp from %s is not 24: %u\n", filename, bpp);
return 0;
}

// seek past the rest of the bitmap header.
fseek(file, 24, SEEK_CUR);

// read the data.
image->data = (char *) malloc(size);
if (image->data == NULL) {
printf("Error allocating memory for color-corrected image data");
return 0;
}

if ((i = fread(image->data, size, 1, file)) != 1) {
printf("Error reading image data from %s.\n", filename);
return 0;
}

for (i=0;i<size;i+=3) { // reverse all of the colors. (bgr -> rgb)
temp = image->data[i];
image->data[i] = image->data[i+2];
image->data[i+2] = temp;
}

// we're done.
return 1;
}

int main(int argc, char **argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
int window = glutCreateWindow("Texture test");
Init();
glutDisplayFunc(Render);
glutReshapeFunc(Reshape);
glutMainLoop();
return 0;
}

void Init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);

Image *image1;

image1 = (Image *) malloc(sizeof(Image));
if (image1 == NULL) {
printf("Error allocating space for image");
exit(0);
}

if (!ImageLoad("test.bmp", image1)) {
exit(1);
}

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);

glGenTextures(1, &amp;TexName);
glBindTexture(GL_TEXTURE_2D, TexName);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image1->sizeX, image1->sizeY, 0, GL_RGBA, GL_UNSIGNED_BYTE, image1->data);
}

void Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, TexName);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -2.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 2.0, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(2.0, 2.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(2.0, -2.0, 0.0);
glEnd();
glFlush();
}

void Reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-3.0, 3.0, -3.0, 3.0, -5.0, 5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, 1.5);
}Thanx Hylke

mikael_aronsson
10-22-2004, 03:03 AM
Hi !

It does look like the image buffer is using 3 bytes per pixel and you try to create the texture with GL_RGBA....

Mikael

Hylke Donker
10-22-2004, 04:24 AM
Thank you for your reply, that was the problem.
Hylke

Hylke Donker
10-22-2004, 04:32 AM
I got another small question, i see a small white border around the texture, how can i remove it?
Thanx Hylke