gpt4 book ai didi

c++ - OpenGL - 一个简单的程序

转载 作者:行者123 更新时间:2023-11-28 06:48:36 33 4
gpt4 key购买 nike

我正在学习 2D 图形,我正在尝试编写一个简单的程序来显示像 Sprite 一样的位图。我结合了几个代码来得到这个。应用程序运行正常(我的意思是它没有崩溃),我们可以看到一个窗口,但没有显示图像。
这是代码:

#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;
}

请告诉我,我做错了什么?

最佳答案

您确实创建了一个窗口,但没有创建 OpenGL context .没有 GL 上下文就不能使用 GL。在 Windows 上,创建 GL 上下文的 native API 是 wgl .这OpeGL.org wiki article解释了一些使用 wgl 的细节。

但是,您可能想要使用一些 GL 上下文和窗口管理库,它以独立于平台的方式抽象整个过程,这样只需几次调用,就可以创建一个支持 GL 的窗口。看看GLFW .这wiki article列出了更多此类库。

关于c++ - OpenGL - 一个简单的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24477277/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com