我正在学习2D图形,我正在尝试编写一个显示像精灵这样的位图的简单程序。我结合了几个代码来获得这个。应用程序运行正常运行(我的意思是它没有崩溃),我们可以看到一个窗口,但图像没有显示 这是代码:
#include <stdio.h>
#include <windows.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include <GL/glu.h>
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
GLuint LoadBMP(const char *fileName);
char szClassName[ ] = "CodeBlocksWindowsApp";
int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{
HWND hwnd;
MSG Msg;
WNDCLASSEX wincl = {sizeof (WNDCLASSEX), CS_DBLCLKS, WindowProcedure, 0, 0, hThisInstance, LoadIcon (NULL, IDI_APPLICATION), LoadCursor (NULL, IDC_ARROW), (HBRUSH) COLOR_BACKGROUND, NULL, szClassName, LoadIcon (NULL, IDI_APPLICATION)};
if (!RegisterClassEx(&wincl))
return 0;
hwnd = CreateWindowEx(0, szClassName, "Code::Blocks Template Windows App", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 544, 375, HWND_DESKTOP, NULL, hThisInstance, NULL);
ShowWindow (hwnd, nCmdShow);
GLuint texture = LoadBMP("image.bmp");
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_REPLACE);
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
glTexCoord2i(0, 0); glVertex2i(0, 0);
glTexCoord2i(0, 1); glVertex2i(0, 64);
glTexCoord2i(1, 1); glVertex2i(64, 64);
glTexCoord2i(1, 0); glVertex2i(64, 0);
glEnd();
glDisable(GL_TEXTURE_2D);
while (GetMessage (&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the Msg */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for Msg that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
GLuint LoadBMP(const char *fileName)
{
FILE *file;
unsigned char header[54];
unsigned int dataPos;
unsigned int size;
unsigned int width, height;
unsigned char *data;
file = fopen(fileName, "rb");
if (file == NULL)
{
MessageBox(NULL, "Error: Invaild file path!", "Error", MB_OK);
return false;
}
if (fread(header, 1, 54, file) != 54)
{
MessageBox(NULL, "Error: Invaild file!", "Error", MB_OK);
return false;
}
if (header[0] != 'B' || header[1] != 'M')
{
MessageBox(NULL, "Error: Invaild file!", "Error", MB_OK);
return false;
}
dataPos = *(int*)&(header[0x0A]);
size = *(int*)&(header[0x22]);
width = *(int*)&(header[0x12]);
height = *(int*)&(header[0x16]);
if (size == NULL)
size = width * height * 3;
if (dataPos == NULL)
dataPos = 54;
data = new unsigned char[size];
fread(data, 1, size, file);
GLuint texture;
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, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
return texture;
}
请告诉我,我做错了什么?
答案 0 :(得分:1)
您确实创建了一个窗口,但没有创建OpenGL context。 没有GL上下文,您不能使用GL。在Windows上,用于创建GL上下文的本机API为wgl。这个OpeGL.org wiki article解释了使用wgl的一些细节。
但是,您可能希望使用一些GL上下文和窗口管理库,它以独立于平台的方式抽象整个过程,因此只需几次调用,就可以创建一个支持GL的窗口。看看GLFW。这个wiki article列出了更多这样的库。