/**************************
* Includes
*
**************************/
#include <windows.h>
#include <gl/gl.h>
/**************************
* Function Declarations
*
**************************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
bool LoadBMP(LPTSTR filename, GLuint &texid);
int initGL();
GLuint texture[1];
/**************************
* WinMain
*
**************************/
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;
float theta = 0.0f;
/* register window class */
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "GLSample";
RegisterClass (&wc);
/* create main window */
hWnd = CreateWindow (
"GLSample", "OpenGL Sample",
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
0, 0, 256, 256,
NULL, NULL, hInstance, NULL);
/* enable OpenGL for the window */
EnableOpenGL (hWnd, &hDC, &hRC);
initGL();
/* program main loop */
while (!bQuit)
{
/* check for messages */
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
/* handle or dispatch messages */
if (msg.message == WM_QUIT)
{
bQuit = TRUE;
}
else
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
else
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, texture[0]);
glPushMatrix();
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex2f(1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex2f(1.0f, 0.0f);
glEnd();
glPopMatrix();
SwapBuffers(hDC);
}
}
/* shutdown OpenGL */
DisableOpenGL (hWnd, hDC, hRC);
/* destroy the window explicitly */
DestroyWindow (hWnd);
return msg.wParam;
}
/********************
* Window Procedure
*
********************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
return 0;
case WM_CLOSE:
PostQuitMessage (0);
return 0;
case WM_DESTROY:
return 0;
case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
}
return 0;
default:
return DefWindowProc (hWnd, message, wParam, lParam);
}
}
/*******************
* Enable OpenGL
*
*******************/
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
/* get the device context (DC) */
*hDC = GetDC (hWnd);
/* set the pixel format for the DC */
ZeroMemory (&pfd, sizeof (pfd));
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat (*hDC, &pfd);
SetPixelFormat (*hDC, iFormat, &pfd);
/* create and enable the render context (RC) */
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );
}
/******************
* Disable OpenGL
*
******************/
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent (NULL, NULL);
wglDeleteContext (hRC);
ReleaseDC (hWnd, hDC);
}
bool LoadBMP(LPTSTR filename, GLuint &texid)
{
HBITMAP hBMP;
BITMAP BMP;
glGenTextures(1, &texid);
hBMP=(HBITMAP)LoadImage(GetModuleHandle(NULL), filename, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE );
if (!hBMP)
return FALSE;
GetObject(hBMP, sizeof(BMP), &BMP);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glBindTexture(GL_TEXTURE_2D, texid);
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, 3, BMP.bmWidth, BMP.bmHeight, 0, NULL, GL_UNSIGNED_BYTE, BMP.bmBits);
DeleteObject(hBMP);
return TRUE;
}
int initGL()
{
if(!LoadBMP("crap.bmp", texture[0]))
{
MessageBox(NULL, "h", "h", NULL);
return 0;
}
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
return 1;
}
Texture not appearing
Hello everyone! Up to this point I've been creating graphics inside of openGL. I think it would be much easier if I could load them out of files, so I went on a venture to load BMPs into openGL. I have a program that everything is working on, except it's not mapping the texture to the quad. Can someone please point out what I'm doing wrong?
Here's the code.
The "texture" is a blue box, with a green outlined white circle in the center.
Thanks.
Quick examination shows me that you do not have viewport configured. Also you try putting BMP in before you have textures enabled.
I'm looking into how you can fix it. wait a while.
I'm looking into how you can fix it. wait a while.
The line
may be incorrect. The parameter for the texture format (which is NULL in your case) should be set to the format of your texture in the file (for example GL_RGB).
glTexImage2D(GL_TEXTURE_2D, 0, 3, BMP.bmWidth, BMP.bmHeight, 0, NULL, GL_UNSIGNED_BYTE, BMP.bmBits);
may be incorrect. The parameter for the texture format (which is NULL in your case) should be set to the format of your texture in the file (for example GL_RGB).
int initGL(GLdouble display_width, GLdouble display_height){ glEnable(GL_TEXTURE_2D); if(!LoadBMP("crap.bmp", texture[0])) { MessageBox(NULL, "h", "h", NULL); return 0; } glShadeModel(GL_SMOOTH); glClearColor(0.0f, 0.0f, 0.0f, 0.0f);//// this initialises projection and modelview matrix. glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, display_width, 0.0, display_height, -1.0, 1.0) glMatrixMode(GL_MODELVIEW); glLoadIdentity();//// test these later, I remember they could also affect it.// glClearDepth(1.0f);// glEnable(GL_DEPTH_TEST);// glDepthFunc(GL_LEQUAL);// glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); return 1;}
hope this works.
Edit: nmi's right too.
alright I have init set to:
But it still shows a white box
int initGL(){ glEnable(GL_TEXTURE_2D); if(!LoadBMP("crap.bmp", texture[0])) { MessageBox(NULL, "h", "h", NULL); return 0; } glShadeModel(GL_SMOOTH); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); return 1;}
But it still shows a white box
Didn't you fix the thing what nmi said too?
either:
glTexImage2D(GL_TEXTURE_2D, 0, 3, BMP.bmWidth, BMP.bmHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, BMP.bmBits);
or
glTexImage2D(GL_TEXTURE_2D, 0, 3, BMP.bmWidth, BMP.bmHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, BMP.bmBits);
I remember windows bitmaps being BGR from some reason...
either:
glTexImage2D(GL_TEXTURE_2D, 0, 3, BMP.bmWidth, BMP.bmHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, BMP.bmBits);
or
glTexImage2D(GL_TEXTURE_2D, 0, 3, BMP.bmWidth, BMP.bmHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, BMP.bmBits);
I remember windows bitmaps being BGR from some reason...
GL_RGB doesn't give a compiler error, but GL_BGR does, says it's unefined. GL_RGB still gives a white quad.
Is the size of your texture a power of two (i.e. 256x256 pixels) ?
Check if drawing operations work as expected by drawing untextured quads of different size and color.
Check if the texture was loaded correctly by displaying it without OpenGL, but as a simple image in a window.
Check if drawing operations work as expected by drawing untextured quads of different size and color.
Check if the texture was loaded correctly by displaying it without OpenGL, but as a simple image in a window.
It looks like you are not even generating an id for you texture.
you supposed to call glGenTexture which generate an id number which you can bind and used for texture purpose. There is no where in your code that you have glGenTexture but i see that you are binding to some id that you specify yourself do not do that
you supposed to call glGenTexture which generate an id number which you can bind and used for texture purpose. There is no where in your code that you have glGenTexture but i see that you are binding to some id that you specify yourself do not do that
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement