Skip to content

Instantly share code, notes, and snippets.

@superwills
Last active September 17, 2021 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superwills/525521e6e62c7e5a616fb4ef85773c2d to your computer and use it in GitHub Desktop.
Save superwills/525521e6e62c7e5a616fb4ef85773c2d to your computer and use it in GitHub Desktop.
Why is glTexImage2D() backwards?
#define _CRT_SECURE_NO_DEPRECATE
#include <windows.h>
#include <stdio.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <map>
#include <vector>
using namespace std;
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
struct Globals
{
HINSTANCE hInstance;
HWND hwnd;
HDC hdc;
HGLRC hglrc;
GLuint texId;
int width, height;
};
Globals g;
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow);
void draw();
// GL ERR
map<int, const char*> createErrMap()
{
map<int, const char*> errmap;
errmap.insert(make_pair(0x0000, "GL_NO_ERROR"));
errmap.insert(make_pair(0x0500, "GL_INVALID_ENUM"));
errmap.insert(make_pair(0x0501, "GL_INVALID_VALUE"));
errmap.insert(make_pair(0x0502, "GL_INVALID_OPERATION"));
errmap.insert(make_pair(0x0503, "GL_STACKOVERFLOW"));
errmap.insert(make_pair(0x0504, "GL_STACK_UNDERFLOW"));
errmap.insert(make_pair(0x0505, "GL_OUTOFMEMORY"));
errmap.insert(make_pair(0x8CD5, "GL_FRAMEBUFFER_COMPLETE"));
errmap.insert(make_pair(0x8CD6, "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT"));
errmap.insert(make_pair(0x8CD7, "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT"));
errmap.insert(make_pair(0x8CD9, "GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS"));
errmap.insert(make_pair(0x8CDD, "GL_FRAMEBUFFER_UNSUPPORTED"));
return errmap;
}
map<int, const char*> glErrName = createErrMap();
inline bool GL_OK(int line, const char* file)
{
GLenum err = glGetError();
if (err != GL_NO_ERROR)
printf("GLERROR %d %s, line=%d of file=%s\n", err, glErrName[err], line, file);
return err == GL_NO_ERROR;
}
#define CHECK_GL GL_OK( __LINE__, __FILE__ )
GLuint CreateTexture()
{
int w = 16, h = 16;
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Supposed to be: 0xrrggbbaa !!
// But actually 0xaabbggrr !!
vector<GLuint> rgba(w * h, 0xaaff0000);//!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
GLuint texId;
glGenTextures(1, &texId); CHECK_GL;
glBindTexture(GL_TEXTURE_2D, texId); CHECK_GL;
glPixelStorei(GL_UNPACK_ALIGNMENT, 4); CHECK_GL;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); CHECK_GL;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); CHECK_GL;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, &rgba[0]); CHECK_GL;
return texId;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow)
{
AllocConsole();
AttachConsole(GetCurrentProcessId());
freopen("CON", "w", stdout);
g.hInstance = hInstance;
const TCHAR* wndclassName = TEXT("opengl");
WNDCLASSEX wcx = { 0 };
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcx.hIconSm = 0;
wcx.hInstance = hInstance;
wcx.lpfnWndProc = WndProc;
wcx.lpszClassName = wndclassName;
wcx.lpszMenuName = 0;
wcx.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
if (!RegisterClassEx(&wcx)) FatalAppExit(0, TEXT("Could not RegisterClassEx()"));
RECT rect = { 50, 50, 850, 650 };
g.width = rect.right - rect.left;
g.height = rect.bottom - rect.top;
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, false);
g.hwnd = CreateWindow(wndclassName,
TEXT("GL WINDOW!"),
WS_OVERLAPPEDWINDOW,
rect.left, rect.top,
rect.right - rect.left, rect.bottom - rect.top,
NULL, NULL,
hInstance, NULL);
if (!g.hwnd) FatalAppExit(NULL, TEXT("CreateWindow() failed!"));
ShowWindow(g.hwnd, iCmdShow);
g.hdc = GetDC(g.hwnd);
PIXELFORMATDESCRIPTOR pfd = { 0 };
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 32;
int chosenPixelFormat = ChoosePixelFormat(g.hdc, &pfd);
if (!chosenPixelFormat) FatalAppExit(NULL, TEXT("ChoosePixelFormat() failed!"));
printf("You got ID# %d as your pixelformat!\n", chosenPixelFormat);
int result = SetPixelFormat(g.hdc, chosenPixelFormat, &pfd);
if (!result) FatalAppExit(NULL, TEXT("SetPixelFormat() failed!"));
g.hglrc = wglCreateContext(g.hdc);
wglMakeCurrent(g.hdc, g.hglrc);
g.texId = CreateTexture();
MSG msg;
while (1)
{
if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT) break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
draw();
}
}
wglMakeCurrent(0, 0);
wglDeleteContext(g.hglrc);
ReleaseDC(g.hwnd, g.hdc);
AnimateWindow(g.hwnd, 200, AW_HIDE | AW_BLEND);
return msg.wParam;
}
void draw()
{
glViewport(0, 0, g.width, g.height); CHECK_GL;
glMatrixMode(GL_PROJECTION); CHECK_GL;
glLoadIdentity(); CHECK_GL;
//gluPerspective(45.0, (float)g.width / (float)g.height, 1, 1000); CHECK_GL;
glMatrixMode(GL_MODELVIEW); CHECK_GL;
glLoadIdentity(); CHECK_GL;
glEnable(GL_TEXTURE_2D); CHECK_GL;
glEnable(GL_BLEND); CHECK_GL;
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); CHECK_GL;
glBindTexture(GL_TEXTURE_2D, g.texId); CHECK_GL;
//gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0); CHECK_GL;
glClearColor(0.5, 0, 0, 0); CHECK_GL;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); CHECK_GL;
// 3--2
// | |
// 4--1
//
glBegin(GL_QUADS);
glColor3f(1, 1, 1);
glTexCoord2f(1, 0);
glVertex3f(1, -1, 0);
glTexCoord2f(1, 1);
glVertex3f(1, 1, 0);
glTexCoord2f(0, 1);
glVertex3f(-1, 1, 0);
glTexCoord2f(0, 0);
glVertex3f(-1, -1, 0);
glEnd();
SwapBuffers(g.hdc);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
switch (message)
{
case WM_CREATE:
Beep(50, 10);
return 0;
case WM_KEYDOWN:
switch (wparam)
{
case VK_ESCAPE:
PostQuitMessage(0);
break;
default:
break;
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wparam, lparam);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment