PDA

View Full Version : second time not the charm texture going away



omega arts
09-02-2002, 06:56 PM
im making a very simp game engine as i learn OpenGl and i am trying to make a textured object but when i render it the second time it goes away. If I write the programme to render once it stays on the object here is the loader and render. Do i have to store the bitmap in some OpenGL function?if you know plz they are if you need all the code just ask. thx for any help in this problem.

#include "main.h"
BITMAPFILEHEADER bitmapFileHeader;
unsigned char *bitmapImage;
unsigned int imageIdx=0;
unsigned char tempRGB;
unsigned char* bitmapData;
unsigned int texture;
BITMAPINFOHEADER bitmapInfoHeader;
unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
FILE *filePtr;

filePtr = fopen(filename, "rb");

if (filePtr == NULL)
return NULL;

fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);


//if (bitmapFileHeader.bfType == BITMAP_ID)
// {
// MessageBox(NULL, "image for you maybe", NULL, MB_OK);
//
// }


fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1,filePtr);
fseek(filePtr, bitmapFileHeader.bfOffBits,SEEK_SET);
bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);

if (!bitmapImage)
{
free(bitmapImage);
fclose(filePtr);
return NULL;
}

fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage,filePtr);

if (bitmapImage == NULL)
{
fclose(filePtr);
return NULL;
}


for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3)
{
tempRGB = bitmapImage[imageIdx];
bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
bitmapImage[imageIdx + 2] = tempRGB;
}
fclose(filePtr);

return bitmapImage;
}

void setuptexture()
{
glClearColor(0.0f,0.0f,0.0f,0.0f);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
bitmapData = LoadBitmapFile("checker.bmp",&bitmapInfoHeader);
if (!bitmapData)
MessageBox(NULL, "No image for you", NULL, MB_OK);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D,0, GL_RGB, bitmapInfoHeader.biWidth, bitmapInfoHeader.biHeight, 0, GL_RGB,GL_UNSIGNED_BYTE, bitmapData);

}

#include "main.h"
#include "texturemap.h"
void drawQuads()
{
glBegin(GL_QUADS);
glTexCoord2f(0.0f,0.0f); glVertex3f(0.5f,-0.5f,0.5f);
glTexCoord2f(1.0f,0.0f); glVertex3f(0.5f,0.5f,0.5f);
glTexCoord2f(1.0f,1.0f); glVertex3f(-0.5f,0.5f,0.5f);
glTexCoord2f(0.0f,1.0f); glVertex3f(-0.5f,-0.5f,0.5f);
glEnd();

}

int render(HWND hwnd)
{

glClearColor(0.0f,0.0f,0.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.5f,100.0f);

gluLookAt(myCam.posx,myCam.posy,myCam.posz,
myCam.eyex,myCam.eyey,myCam.eyez,
myCam.upx,myCam.upy,myCam.upz);


drawQuads();
glBegin(GL_TRIANGLES);
glTexCoord2f(0.5f,1.0f); glVertex3f(0.0f,10.0f,-10.0f);
glTexCoord2f(0.0f,0.0f); glVertex3f(-10.f,0.0f,-10.0f);
glTexCoord2f(1.0f,0.0f); glVertex3f(10.0f,0.0f,-10.0f);
glEnd();
glBegin(GL_TRIANGLES);
glTexCoord2f(0.5f,1.0f); glVertex3f(10.0f,0.0f,-10.0f);
glTexCoord2f(0.0f,0.0f); glVertex3f(-10.0f,0.0f,-10.0f);
glTexCoord2f(1.0f,0.0f); glVertex3f(0.0f,10.0f, -10.0f);
glEnd();

glColor3f(0.0f, 0.5f, 0.05f);


for(float i = -50; i <= 50; i += 1)
{

glBegin(GL_LINES);


glVertex3f(-50, -10, i);
glVertex3f(50, -10, i);


glVertex3f(i, -10, -50);
glVertex3f(i, -10, 50);


glEnd();
}
//glEnable(GL_LIGHTING);
glColor3f(0.0f,0.0f,0.0f);
//auxSolidSphere(1.0f);

//glEnable(GL_LIGHTING);

SwapBuffers(g_HDC);


//fps(hwnd);



return 0;
}

tinak
09-03-2002, 03:23 AM
I usually place the set up texture(s) routine to run when the rest of the GL stuff is initialised in say an InitGL routine:

void InitGL()
{
SetUpMainSceneGLSettings();
SetUpTexture();
}

Then in the Render routine bind the texture ID before calling the begin/end drawing segment. The texture variable you use to bind will need to be global for it to be useable in both the setup and render routines. Note that when you set up the texture in your routine the first 4 lines could be placed in the InitGL routine as they affect the scene as well as the texture.

GLuint texture[1];

void RenderScene()
{
glBindTexture(GL_TEXTURE_2D,texture);
DrawSquare();
DrawCube();
DrawTriangle();
etc.. // each object drawn will use the texture in each scene.
}

A look at Nehe's tutorials will see this in practice.

Hope that helps you in some way.

Tina http://programming.swangen.co.uk/opengl/