PDA

View Full Version : Enviroment Mapping (need help)



High-Tag
12-04-2003, 06:29 PM
hi people i am new to opengl programming.
I have read up books on opengl and texture mapping on nehe...
I tried some of the tutorials but none work.

I had been trying out the enviroment mapping with these codes.

RenderGL()



void __fastcall TForm1::RenderGLScene()
{
//Place your OpenGL drawing code here
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//Drawing of Background
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-3.5f,-2.5f,-3.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f( 3.5f,-2.5f,-3.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f( 3.5f, 2.5f,-3.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-3.5f, 2.5f,-3.0f);
glEnd();

glTexGenf(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
glTexGenf(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glBindTexture(GL_TEXTURE_2D, texture);

==============================
Initialise()



void __fastcall TForm1::SetupRC()
{
glEnable (GL_DEPTH_TEST);
glEnable (GL_LIGHTING);
glLightModelfv (GL_LIGHT_MODEL_AMBIENT, ambient_light);

glLightfv (GL_LIGHT0, GL_DIFFUSE, source_light);
glLightfv (GL_LIGHT0, GL_POSITION, light_pos);
glEnable (GL_LIGHT0);

glEnable (GL_COLOR_MATERIAL);
glColorMaterial (GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glEnable (GL_CULL_FACE);

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);

glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);
glEnable(GL_TEXTURE_2D);
bitmapData = LoadBitmapFile("texture\\scenary.bmp", &bitmapInfoHeader);
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_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glEnable(GL_TEXTURE_2D);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, bitmapInfoHeader.biWidth, bitmapInfoHeader.biHeight, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth, bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);

glFlush();
}

==============================
LoadBitmapFile()



unsigned char* LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
FILE *filePtr;
BITMAPFILEHEADER bitmapFileHeader;
unsigned char *bitmapImage;
int imageIdx = 0;
unsigned char tempRGB;

filePtr = fopen(filename, "rb");
if(filePtr == NULL)
return NULL;

fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 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;
}


I am using borland C++ builder 3 to do the coding... Nothing comes out from these codes except a purple color layer.


So pls can someone kindly aid me in this. Thank You.

ZbuffeR
12-05-2003, 08:28 AM
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth, bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);

Maybe you could uncomment this line ? ;-) This is the most useful one when dealing with textures...

Tell if its ok.

Mazy
12-05-2003, 09:25 AM
I think you missed the glu call whe you looked at the code http://www.opengl.org/discussion_boards/ubb/smile.gif

first of all, you should bind and enalbe spheremapping before you draw the quad, and you dont need texcoords for the quad, but you do however need normals, since spheremapping is based on those.. Rotating the quad will help to see the mapping.. I do believe that things works to a certain point since you mention 'purple' as a color, but you dont specify that in the code, so i guess that comes from the texture you loaded.

ZbuffeR
12-05-2003, 03:13 PM
Originally posted by Mazy:
I think you missed the glu call whe you looked at the code http://www.opengl.org/discussion_boards/ubb/smile.gif

Oops, you are right http://www.opengl.org/discussion_boards/ubb/tongue.gif
In fact, as gluBuild2DMipmaps is quite slow, I do not use it.

I would add that for env. mapping to appear nice on a single plane you may need to tesselate it a bit. And just as Mazy said, rotate it along time, with normals, and see if it works.

High-Tag
12-05-2003, 03:50 PM
oh sorry people...
I think i had mistaken the meaning of "enviroment mapping", it's actually binding texture to 3d object to make it "reflect" right?
if that is true maybe i am wrong to say it's "enviroment mapping"


But actually i was trying to draw a "square" or "rect" behind a sphere and make it a wallpaper by mapping the texture onto the "square" or "rect".

High-Tag
12-05-2003, 03:59 PM
Actually i was trying to program a bouncing ball ball application by it seems that after i map a "wallpaper" texture behind the ball, it become EXTREMELY SLOW....
from 50 interval, to "about 1500 interval"...
(I was rendering the wallpaper with the ball at 50 interval)

So can anyone kindly tell me how to improve the performance.