PDA

View Full Version : BMP texture Mapping appears pale



Milk
08-27-2006, 06:07 AM
Hi all,

My texture mapping makes the mapped BMP look very pale. This is my code:



glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);Any reason why that would appear pale? Thanks :-)

plasmonster
08-27-2006, 06:55 AM
Just a guess, but perhaps your texture is pale?

Milk
08-27-2006, 07:06 AM
You mean the bitmap being pale? Its not its much darker than when it is displayed using OpenGL?

plasmonster
08-27-2006, 07:10 AM
Can you please post the complete code and perhaps a screenshot (extra points for that :) )?

plasmonster
08-27-2006, 07:14 AM
In the future, when you have trouble like this, it's really helpful if you can paint as complete a picture as possible, to help others identify the trouble you're having as quickly as possible, and with a minimum of Q&A. This saves everyone some time, and helps keeps the discussion productive.

Hope this helps.

Milk
08-27-2006, 07:33 AM
Rendered Scene Code:


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPopMatrix();
glEnable(GL_TEXTURE_2D);
Texture t = Texture();
int i = t.LoadBitmapW("c:/b.bmp");
glBindTexture(GL_TEXTURE_2D, i);

//DRAW THE BACKGROUND
glBegin(GL_POLYGON);
glTexCoord2f(0.0,0.0);glVertex3f(0.0,0.0,0.0);
glTexCoord2f(1.0,0.0);glVertex3f(1.0,0.0,0.0);
glTexCoord2f(1.0,1);glVertex3f(1.0,1,0.0);
glTexCoord2f(0.0,1);glVertex3f(0.0,1,0.0);
glEnd();
glPopMatrix();
glSwapBuffers();
glFlush(); Main Code:

void main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(800,600);
glutCreateWindow("Example");
glutDisplayFunc(renderScene);
// glutIdleFunc(renderScene);
glutReshapeFunc(changeSize);
glutSpecialFunc(processSpecialKeys);

// enable depth testing
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}Texture Loading Code:

int Texture::LoadBitmap(char *filename)
{

unsigned char *l_texture;
int i, j=0;
FILE *l_file;
BITMAPFILEHEADER fileheader;
BITMAPINFOHEADER infoheader;
RGBTRIPLE rgb;
num_texture++;

if( (l_file = fopen(filename, "rb"))==NULL) return (-1);
fread(&fileheader, sizeof(fileheader), 1, l_file);
fseek(l_file, sizeof(fileheader), SEEK_SET);
fread(&infoheader, sizeof(infoheader), 1, l_file);

l_texture = (BYTE *) malloc(infoheader.biWidth * infoheader.biHeight * 4);
memset(l_texture, 0, infoheader.biWidth * infoheader.biHeight * 4);
glPixelStorei( GL_UNPACK_ALIGNMENT, 1);

for (i=0; i < infoheader.biWidth*infoheader.biHeight; i++)
{
fread(&amp;rgb, sizeof(rgb), 1, l_file);

l_texture[j+0] = rgb.rgbtRed; // Red component
l_texture[j+1] = rgb.rgbtRed; // Green component
l_texture[j+2] = rgb.rgbtBlue; // Blue component
l_texture[j+3] = 255; // Alpha value
j += 4; // Go to the next position
}
fclose(l_file);
glBindTexture(GL_TEXTURE_2D, num_texture);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
free(l_texture);
return num_texture;

}Screen View Code:


// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;

float ratio = 1.0* w / h;

// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Set the viewport to be the entire window
glViewport(0,0, w, h);

// Set the correct perspective.
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.5,0.5,1.2, 0.5,0.5,0.0, 0.0,1.0,0.0);Right theres the simplified version of the code because the real code is very long.

This is the ouput I get when i run the program

Image 1 (http://www.securemilk.com/gui.bmp)

This is the texture getting mapped
Image 2 (http://www.securemilk.com/b.bmp)

As i said thats the basic version of the code but its still giving pale images.

And my apologies for the lack of info during my first post. I thought the reason why I was getting pale images was due to the Variables I was using in the original code which were decreasing image quality for faster performance and i thought that someone could quickly point where i was going wrong from the code i posted.

Hope this helps, if not explain what else you require :-)

Thanks for your help

plasmonster
08-27-2006, 07:47 AM
I'm sorry, I guess you missed the point. If you post the complete code, then I can just copy and paste it in an empty project real quick and build it.

I understand if you're unwilling or unable to do that, but unfortunately I don't have time to debug code snippits.

plasmonster
08-27-2006, 07:48 AM
If you want, you can email me the program and I'll look at it for you. I mean complete program, though, paste and build style :)

Milk
08-27-2006, 08:07 AM
Thankyou very much mate.

The following code compiles fine, with unicode off in VS2005.

Lesson1.cpp


#include "stdafx.h"
#include "GL/glut.h"
#include "texture.h"
#include <stdlib.h>


#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
bool first = true;

void changeSize(int w, int h) {

// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;

float ratio = 1.0* w / h;

// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Set the viewport to be the entire window
glViewport(0,0, w, h);

// Set the correct perspective.
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.5,0.5,1.2, 0.5,0.5,0.0, 0.0,1.0,0.0);
}
void renderScene(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();

glEnable(GL_TEXTURE_2D);
if(first){
Texture t = Texture();
int i = t.LoadBitmap("c:/b.bmp");

}

glBindTexture(GL_TEXTURE_2D, 1);
//DRAW THE BACKGROUND
glBegin(GL_POLYGON);
glTexCoord2f(0.0,0.0);
glVertex3f(-0.16,0.0,0.0);
glTexCoord2f(1.0,0.0);
glVertex3f(1.16,0.0,0.0);
glTexCoord2f(1.0,1);
glVertex3f(1.16,1.0,0.0);
glTexCoord2f(0.0,1);
glVertex3f(-0.16,1.0,0.0);
glEnd();
glPopMatrix();

glutSwapBuffers();
glFlush();

}



void main (int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(800,600);
glutCreateWindow("Example");
glutDisplayFunc(renderScene);
// glutIdleFunc(renderScene);
glutReshapeFunc(changeSize);
// glutSpecialFunc(processSpecialKeys);

// enable depth testing
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}Lesson1.h


#pragma onceTexture.h


#pragma once

class Texture
{
public:
Texture(void);
int LoadBitmap(char *filname);
public:
virtual ~Texture(void);
};Texture.cpp


#include "StdAfx.h"
#include "Texture.h"
#include <fstream>
#include "windows.h"
#include "GL/glut.h"

int num_texture = 0;

Texture::Texture(void)
{
}

Texture::~Texture(void)
{
}

int Texture::LoadBitmap(char *filename)
{

unsigned char *l_texture;
int i, j=0;
FILE *l_file;
BITMAPFILEHEADER fileheader;
BITMAPINFOHEADER infoheader;
RGBTRIPLE rgb;
num_texture++;

if( (l_file = fopen(filename, "rb"))==NULL) return (-1);
fread(&amp;fileheader, sizeof(fileheader), 1, l_file);
fseek(l_file, sizeof(fileheader), SEEK_SET);
fread(&amp;infoheader, sizeof(infoheader), 1, l_file);

l_texture = (BYTE *) malloc(infoheader.biWidth * infoheader.biHeight * 4);
memset(l_texture, 0, infoheader.biWidth * infoheader.biHeight * 4);
glPixelStorei( GL_UNPACK_ALIGNMENT, 1);

for (i=0; i < infoheader.biWidth*infoheader.biHeight; i++)
{
fread(&amp;rgb, sizeof(rgb), 1, l_file);

l_texture[j+0] = rgb.rgbtRed; // Red component
l_texture[j+1] = rgb.rgbtRed; // Green component
l_texture[j+2] = rgb.rgbtBlue; // Blue component
l_texture[j+3] = 255; // Alpha value
j += 4; // Go to the next position
}
fclose(l_file);
glBindTexture(GL_TEXTURE_2D, num_texture);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
free(l_texture);
return num_texture;

}You will need the file c:/b.bmp which is on one of the links i sent you in my previous post.

Thanks again for your help mate

Komat
08-27-2006, 08:47 AM
You have error in code loading the bitmap. You are storing value of red component into value of green component.


l_texture[j+0] = rgb.rgbtRed; // Red component
l_texture[j+1] = rgb.rgbtRed; // Green component <-- HERE
l_texture[j+2] = rgb.rgbtBlue; // Blue component

plasmonster
08-27-2006, 08:59 AM
I hope this helps.


#include <stdlib.h>
#include "glut.h"
#include <fstream>
#include "windows.h"
#include "glut.h"

class Texture
{
public:
//
// Retrun a 0 on error (reserved name in OpenGL)
//
GLuint LoadBMP(char *filename)
{
FILE *l_file = fopen(filename, "rb");
if (l_file == NULL)
{
printf("file open failed");
return (0);
}

BITMAPFILEHEADER fileheader;
BITMAPINFOHEADER infoheader;
fread(&amp;fileheader, sizeof(fileheader), 1, l_file);
fseek(l_file, sizeof(fileheader), SEEK_SET);
fread(&amp;infoheader, sizeof(infoheader), 1, l_file);

unsigned char *l_texture = (BYTE *) malloc(infoheader.biWidth * infoheader.biHeight * 4);
unsigned char *pTex = l_texture;
RGBTRIPLE rgb;
for (int i=0; i < infoheader.biWidth*infoheader.biHeight; i++)
{
fread(&amp;rgb, sizeof(rgb), 1, l_file);
*pTex++ = rgb.rgbtRed; // Red component
*pTex++ = rgb.rgbtGreen; // Green component
*pTex++ = rgb.rgbtBlue; // Blue component
*pTex++ = 255; // Alpha value
}
fclose(l_file);

GLuint bind;
glGenTextures(1, &amp;bind);
glBindTexture(GL_TEXTURE_2D, bind);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
//glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
free(l_texture);

return bind;
}
};

GLuint num_texture;

void changeSize(int w, int h)
{
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;

// Set the viewport to be the entire window
glViewport(0, 0, w, h);

// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90, (float)w / h, 1, 1000);

// Set the correct perspective.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,50, 0,0,0, 0,1,0);
}

void renderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Enable stuff
glBindTexture(GL_TEXTURE_2D, num_texture);
glEnable(GL_TEXTURE_2D);

// Draw
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex3f(-16,-16,0);
glTexCoord2f(0,1); glVertex3f(-16,+16,0);
glTexCoord2f(1,1); glVertex3f(+16,+16,0);
glTexCoord2f(1,0); glVertex3f(+16,-16,0);
glEnd();

// Disable stuff
glDisable(GL_TEXTURE_2D);

glutSwapBuffers();
glFlush();
}

void Init()
{
// Set default state
glPixelStorei( GL_UNPACK_ALIGNMENT, 1);
glEnable(GL_DEPTH_TEST);

// Load assets
Texture t = Texture();
num_texture = t.LoadBMP("c:\\Test.bmp");
if (num_texture == 0)
printf("LoadBitmap failed!\n");
}

void main (int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(800,600);
glutCreateWindow("Example");
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);

// Do one-time setup
Init();

glutMainLoop();
}

plasmonster
08-27-2006, 09:01 AM
The code you gave me wouldn't build, since windows defines LoadBitmap (changed it to LoadBMP) ;)

Hope this helps.

Milk
08-27-2006, 09:40 AM
Thanks both very much! Its very much appreciated :-)