PDA

View Full Version : Why does this not work?



geohoffman49431
07-09-2004, 01:39 PM
I want to write a class that displays terrain. This is my humble beginning and for now it is hard coded to the terrain in the DrawTerrain function. The loadTexture function works fine if if it is not inside a class but when I put it inside this class the textures do not load correctly. So if I create an instance of the Terrain class it should load the two textures METAL4.bmp and copper.bmp. But after the Terrain class is instantiated

textures[0] == 0 && textures[1] == 0

So the textures are not being generated correctly. The only thing I see is a gray ground with no texture.


#ifndef __TERRAIN_H
#define __TERRAIN_H



#include <GL/glut.h>
#include <iostream>
#include <iomanip>
#include <windows.h>
#include <windowsx.h>
#include <iostream>
#include <math.h>
using namespace std;








class Terrain
{
public:


Terrain()
{
textures [0] = loadTexture("METAL4.bmp");
textures [1] = loadTexture("copper.bmp");
}
void DrawTerrain()
{
glEnable(GL_TEXTURE_2D);
glBindTexture( GL_TEXTURE_2D, textures[2]);
glPushMatrix();
glBegin(GL_QUADS);
glColor3f(.8,.8,.8);
for(int x = -10; x <= 10; x++)
{
for(int z = -10; z <= 10; z++)
{
glTexCoord2f(0,0); glVertex3f(x << 5,0,z << 5);
glTexCoord2f(10,0); glVertex3f((x+1) << 5,0,z << 5);
glTexCoord2f(10,10); glVertex3f((x+1) << 5,0,(z+1) << 5);
glTexCoord2f(0,10); glVertex3f(x << 5,0,(z+1) << 5);
}
}
glEnd();
glPopMatrix();
}




GLubyte *loadBitmap(LPCSTR filename, BITMAPINFO **info)
{
BITMAPFILEHEADER header;
GLubyte *bits;
int bitsize;
int infosize;
FILE *fp = fopen(filename,"rb");
if (fp == NULL) {return NULL;}
if (fread(&amp;header, sizeof(BITMAPFILEHEADER), 1, fp) < 1) {fclose(fp); return 0;}
if (header.bfType != 'MB'){fclose(fp);return NULL;}
infosize = header.bfOffBits - sizeof(BITMAPFILEHEADER);
if ((*info = (BITMAPINFO *)malloc(infosize)) == NULL){fclose(fp);return NULL;}
if (fread(*info, 1, infosize, fp) < infosize){free(*info);fclose(fp);return (NULL);}
if ((bitsize = (*info)->bmiHeader.biSizeImage) == 0)
bitsize = ((*info)->bmiHeader.biWidth * (*info)->bmiHeader.biBitCount + 7) / 8 * abs((*info)->bmiHeader.biHeight);
if ((bits = (GLubyte*)malloc(bitsize)) == NULL){free(*info);fclose(fp);return (NULL);}
if (fread(bits, 1, bitsize, fp) < bitsize){free(*info);free(bits);fclose(fp);return (NULL);}
return (bits);
fclose(fp);
}

GLuint loadTexture(LPCSTR filename, bool useMipmaps = true, GLenum mag_filter = GL_LINEAR, GLenum min_filter = GL_LINEAR, GLenum wrap_s = GL_REPEAT, GLenum wrap_t = GL_REPEAT, GLenum env_mode = GL_MODULATE)
{
BITMAPINFO *info;
GLubyte *BitmapBits;
GLubyte temp;
GLuint textureID = NULL;
glGenTextures(1,&amp;textureID);
glBindTexture( GL_TEXTURE_2D, textureID);

if ((BitmapBits = loadBitmap(filename,&amp;info)) == NULL) return NULL;
for (int i = 0; i < sizeof(GLubyte) * 3 * (info->bmiHeader.biWidth * info->bmiHeader.biHeight); i +=3 )
{
temp = *(BitmapBits + (i*sizeof(GLubyte)));
*(BitmapBits + (i*sizeof(GLubyte))) = *(BitmapBits + ((i+2)*sizeof(GLubyte)));
*(BitmapBits + ((i+2)*sizeof(GLubyte))) = temp;
}

if (!useMipmaps)
{
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R, mag_filter);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R, min_filter);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, wrap_s) ;
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, wrap_t);
glTexEnvi(GL_TEXTURE_2D,GL_TEXTURE_ENV_MODE, env_mode);
glTexImage2D(GL_TEXTURE_2D,0,3,info->bmiHeader.biWidth,info->bmiHeader.biHeight,0,GL_RGB,GL_UNSIGNED_BYTE,Bitma pBits);
}

if (useMipmaps)
{
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, wrap_s) ;
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, wrap_t);
glTexEnvi(GL_TEXTURE_2D,GL_TEXTURE_ENV_MODE, env_mode);
gluBuild2DMipmaps(GL_TEXTURE_2D,3,info->bmiHeader.biWidth,info->bmiHeader.biHeight,GL_RGB,GL_UNSIGNED_BYTE,BitmapB its);
}

return textureID;
}

GLuint textures[5];
};


#endif

3B
07-09-2004, 02:15 PM
I'm guessing you are probably creating the terrain object before you set up GL, so it can't create the textures in the constructor...
Either allocate the Terrain dynamically after you have a valid GL context, or move the texture loading out of the constructor.

geohoffman49431
07-09-2004, 02:21 PM
Der. I am such an idiot sometimes. Thanks a bunch.