A texture problem
Hi all.
I'm having a problem with loading bitmap textures (it's only my first OpenGL program). The problem is that the textures seem to be white, but the file is loading cus the dimension are correct.
(Please excuse the mess of the code)
#include <windows.h>
#include <GL\GL.h>
#include <GL\GLU.h>
#include <GL\GLaux.h>
HGLRC hRC=NULL;
GLuint Tex[1];
void Render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, Tex[1]);
glBegin(GL_QUADS); // Drawing Using Triangles
glTexCoord2f(0.0f, 0.0f);
glVertex3f( 0.0f, 0.0f, 0.0f); // Top
glTexCoord2f(1.0f, 0.0f);
glVertex3f( 128.0f, 0.0f, 0.0f); // Bottom Left
glTexCoord2f(1.0f, 1.0f);
glVertex3f( 128.0f, 128.0f, 0.0f); // Bottom Right
glTexCoord2f(0.0f, 1.0f);
glVertex3f( 0.0f, 128.0f, 0.0f); // Bottom Right
glEnd();
}
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_CLOSE:
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
// Set up a WNDCLASSEX class to store information about the style of window to be created
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_OWNDC | CS_HREDRAW | CS_VREDRAW, MsgProc, 0L,
0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"OpenGL Test", NULL };
RegisterClassEx(&wc);
HWND hWnd = CreateWindow("OpenGL Test", "OpenGL Test 1", WS_OVERLAPPEDWINDOW, 200, 200,
640, 480, NULL, NULL, NULL, NULL);
static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
16, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};
HDC hDC = GetDC(hWnd);
DescribePixelFormat(hDC, GetPixelFormat(hDC), sizeof(PIXELFORMATDESCRIPTOR), &pfd);
GLuint PixelFormat = ChoosePixelFormat(hDC, &pfd);
if(SetPixelFormat(hDC, PixelFormat, &pfd))
{
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
AUX_RGBImageRec *Text[1];
int vPort[4];
glGetIntegerv(GL_VIEWPORT, vPort);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, vPort[2], vPort[3], 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
memset(&Text[0],0,sizeof(void *)*1);
Text[0] = auxDIBImageLoad("B.dib");
glGenTextures(1, &Tex[0]);
glBindTexture(GL_TEXTURE_2D, Tex[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, Text[0]->sizeX, Text[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, Text[0]->data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtering
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
free(Text[0]->data);
free(Text[0]);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
ShowWindow(hWnd, SW_SHOWNORMAL);
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
PeekMessage(&msg, hWnd, NULL, NULL, PM_REMOVE);
DWORD i = WM_QUIT;
while( msg.message != WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
Render();
SwapBuffers(hDC);
}
}
}
wglMakeCurrent(NULL,NULL);
wglDeleteContext(hRC);
ReleaseDC(hWnd,hDC);
DestroyWindow(hWnd);
UnregisterClass("OpenGl Test", hInstance);
return 0;
}
Anyway, I know through debugging that at "Text[0] = auxDIBImageLoad("B.dib");" the data is set to just repeated 255 values. Please help
[edited by - Dougie on March 27, 2004 8:30:20 PM]
What are the dimensions of this image? If you are making a regular texture, the image''s x and y size must be powers of 2 (i.e. 2, 4, 8...).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement