PDA

View Full Version : C++ Builder and OpenGL



soconne
08-16-2003, 09:06 AM
I've been able to find tutorials for implementing OpenGL with BCB using forms, but I'd like to have things Rendered to the Image control, or the Panel control. I know both of these have their own HDC's, but when I try and execute MakeCurrent(hdc, hgrc), or whatever the syntax is, I get NULL returned. Anyone know of any good sites showing how to implement this?

PanzerSchreck
08-16-2003, 10:44 AM
I'm not coding in C(++) and I'm using Delphi, but as C-Builder is based on the Delphi-IDE, I may help you :
If the HDC of a panel gives you an error when trying to use it, you'll have to get a valid DC from that TPanel-Component (MyDC := GetDC(MyPanel.Handle)) and use this as the DC for your RD.A TImage-Control's DC AFAIK can't be used for OpenGL-Output...

[This message has been edited by PanzerSchreck (edited 08-16-2003).]

[This message has been edited by PanzerSchreck (edited 08-16-2003).]

soconne
08-16-2003, 01:45 PM
Sweet, thanks a lot. I got it to work witht he Panel......now onto my level editor..

soconne
08-17-2003, 04:51 AM
One more question, does anybody know why you can't load textures in OpenGL with BCB?
Here's my code

BITMAPINFOHEADER biHeader;
BITMAPFILEHEADER header;
unsigned char *data;
unsigned char color;

FILE *pfile;

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

fread(&header, sizeof(BITMAPFILEHEADER), 1, pfile);
if(header.bfType != BITMAP_ID)
{
fclose(pfile);
return 0;
}

fread(&biHeader, sizeof(BITMAPINFOHEADER), 1, pfile);
fseek(pfile, header.bfOffBits, SEEK_SET);

data = new unsigned char[biHeader.biSizeImage];
fread(data, 1, biHeader.biSizeImage, pfile);

int index;
for(index=0; index < (int)biHeader.biSizeImage; index+=3)
{
color = data[index];
data[index] = data[index+2];
data[index+2] = color;
}
fclose(pfile);

glPixelStorei(GL_PACK_ALIGNMENT, 2);
glPixelStorei(GL_UNPACK_ALIGNMENT, 2);

glGenTextures(1, temp);
glBindTexture(GL_TEXTURE_2D, *temp);
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_RGB, biHeader.biWidth, biHeader.biHeight,
0, GL_RGB, GL_UNSIGNED_BYTE, data);
delete [] data;

For some reason all I get is a white triangle. I'm using glEnable(GL_TEXTURE_2D), and I am binding the correct texture name. Any Help?

zeckensack
08-17-2003, 05:12 AM
FAQ you: http://www.opengl.org/developers/faqs/technical/texture.htm#text0020


If you're using a mipmapping filter (e.g., you've called glTexParameter*(), setting a min or mag filter that has MIPMAP in its name), make sure you've set all levels of the mipmap pyramid. All levels must be set, or texture mapping won't occur. You can set all levels at the same time with the gluBuild2DMipmaps() function. All levels of the mipmap pyramid must have the same number of components.

soconne
08-17-2003, 07:10 AM
So why would the same code work in Visual C++ and not in Borland C++ Builder if it was copied over exactly the same? Are there different settings and properties that must be set in Builder?

Machiavelli
08-18-2003, 06:28 AM
oconnellseanm,

First thing, it looks like you are using a loop to swap the pixels from BGR to RGB. IIRC, you can instead use the enum GL_BGR_EXT in your call to gluBuild2DMipmaps() or glTexImage2D().

The only thing that stands out as a possible issue is your call to glPixelStorei(GL_UNPACK_ALIGNMENT, 2). I use a 4 byte alignment, not 2, and it works well. Try it and see if it helps.

MikeM