All pastes #234257 Raw Edit

Stuff

public text v1 · immutable
·published 2006-11-02 11:54 UTC
rendered paste body
//Includes and Define's:
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>


#define TGGL_WINDOW_CAPTION "TheGeek's OpenGL Template"
#define TGGL_WINDOW_CLASS_NAME "TheGeek_OpenGL"
#define TGGL_WIDTH 800
#define TGGL_HEIGHT 600

typedef struct{
	float x;
	float y;
	float z;
}TGGL_Vertix;
typedef struct{
	float r;
	float g;
	float b;
}TGGL_Color;
typedef struct{
	TGGL_Vertix v1;
	TGGL_Vertix v2;
	TGGL_Vertix v3;
	TGGL_Color color;
}TGGL_Poly;

//Function Definitions:
LRESULT				CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
void				RenderPoly(TGGL_Poly);

//Globals:
char g_szClassName[] = TGGL_WINDOW_CLASS_NAME;
HGLRC g_hrc;
HDC g_hdc;

//Functions:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
	WNDCLASSEX wndclassex;
	MSG msg;
	HWND hwnd;
	PIXELFORMATDESCRIPTOR pfd;
	int iFormat, iWindowWidth, iWindowHeight;
	RECT rcClient, rcWindow;

	ZeroMemory(&wndclassex, sizeof(wndclassex));
	wndclassex.cbSize			= sizeof(WNDCLASSEX);
	wndclassex.style			= CS_HREDRAW | CS_VREDRAW;
	wndclassex.lpfnWndProc		= WindowProc;
	wndclassex.hInstance		= hInstance;
	wndclassex.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	wndclassex.hIconSm			= LoadIcon(NULL, IDI_APPLICATION);
	wndclassex.hCursor			= LoadCursor(NULL, IDC_ARROW);
	wndclassex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wndclassex.lpszClassName	= g_szClassName;

	if(!RegisterClassEx(&wndclassex)){
		char szError[150] = "Line %d: Class registration failed.";
		sprintf(szError, szError, __LINE__ - 2);
		MessageBox(NULL, szError, "Fatal Error", MB_OK | MB_ICONERROR);
		return 0;
	}

	hwnd = CreateWindowEx(WS_EX_APPWINDOW, g_szClassName, TGGL_WINDOW_CAPTION, WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_MINIMIZEBOX | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, TGGL_WIDTH, TGGL_HEIGHT, NULL, NULL, hInstance, NULL);
	if(hwnd == NULL){
		char szError[150] = "Line %d: Window creation failed.";
		sprintf(szError, szError, __LINE__ - 3);
		MessageBox(NULL, szError, "Fatal Error", MB_OK | MB_ICONERROR);
		return 0;
	}

	//Adjust window dimensions
	GetWindowRect(hwnd, &rcWindow);
	GetClientRect(hwnd, &rcClient);
	iWindowWidth = ((rcWindow.right - rcWindow.left) - rcClient.right) + TGGL_WIDTH;
	iWindowHeight = ((rcWindow.bottom - rcWindow.top) - rcClient.bottom) + TGGL_HEIGHT;
	SetWindowPos(hwnd, NULL, 0, 0, iWindowWidth, iWindowHeight, SWP_NOZORDER | SWP_NOMOVE);

	g_hdc = GetDC(hwnd);

	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 = 32;
	pfd.iLayerType = PFD_MAIN_PLANE;
	iFormat = ChoosePixelFormat(g_hdc, &pfd);
	SetPixelFormat(g_hdc, iFormat, &pfd);

	g_hrc = wglCreateContext(g_hdc);
	wglMakeCurrent(g_hdc, g_hrc);

	//Set OpenGL options
	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
	glClearDepth(1.0f);
	glDepthFunc(GL_LEQUAL);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_TEXTURE_2D);
	glShadeModel(GL_SMOOTH);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	ShowWindow(hwnd, SW_SHOW);
	UpdateWindow(hwnd);
	SetTimer(hwnd, 1, 30, NULL);

	while(GetMessage(&msg, NULL, 0, 0) > 0){
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	wglMakeCurrent(NULL, NULL);
	wglDeleteContext(g_hrc);
	ReleaseDC(hwnd, g_hdc);

	return (int)msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam){
	static TGGL_Poly poly = {
		{0.0f, 1.0f, 0.0f},		//v1
		{0.87f, -0.5f, 0.0f},	//v2 
		{-0.87f, -0.5f, 0.0f},	//v3
		{1.0f, 0.0f, 0.0f}		//color
	};
	static float fRotation = 0.0f;
	static UINT uTexture;
	switch(msg){
		case WM_CREATE:
			{
				HFILE hRawTexture;
				BYTE *pbData;
				DWORD dwBytesRead;

				pbData = GlobalAlloc(GPTR, 256*256*3);
				
				hRawTexture = CreateFile("C:\\Documents and Settings\\cesar\\Bureaublad\\texture.raw", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
				ReadFile(hRawTexture, pbData, 256*256*3, &dwBytesRead, NULL);
				
				CloseHandle(hRawTexture);

				/*glGenTextures(1, &uTexture);
				glBindTexture(GL_TEXTURE_2D, uTexture);
				glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
				glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
				glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
				glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
				glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
				gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 256, 256, GL_RGB, GL_UNSIGNED_BYTE, pbData);*/
				glGenTextures(1, &uTexture);
				glBindTexture(GL_TEXTURE_2D, uTexture);
				glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, pbData);
				glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
				glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

				GlobalFree(pbData);
			}
		break;
		case WM_DESTROY:
			PostQuitMessage(0);
		break;
		case WM_PAINT:
			{
				PAINTSTRUCT ps;

				BeginPaint(hwnd, &ps);
				glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
				glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

				glLoadIdentity();
				glRotatef(fRotation, 1.0f, 1.0f, 0.0f);
				//RenderPoly(poly);
				//Quad with texture:
				glBindTexture(GL_TEXTURE_2D, uTexture);
				glBegin(GL_QUADS);
				glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -0.5f,  0.0f);
				glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f, -0.5f,  0.0f);
				glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f,  0.5f,  0.0f);
				glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f,  0.5f,  0.0f);
				glEnd();

				SwapBuffers(g_hdc);
				EndPaint(hwnd, &ps);
			}
		break;
		case WM_TIMER:
			if(wparam == 1){
				if(fRotation >= 360){
					fRotation = 0;
				}else{
					fRotation += 1;
				}
				RedrawWindow(hwnd, NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
			}
		break;
		case WM_ERASEBKGND:
			return TRUE;
		break;
		default:
			return DefWindowProc(hwnd, msg, wparam, lparam);
	}
	return 0;
}
void RenderPoly(TGGL_Poly poly){
	glBegin(GL_TRIANGLES);
	glColor3f(poly.color.r, poly.color.g, poly.color.b);
	glVertex3f(poly.v1.x, poly.v1.y, poly.v1.z);
	glVertex3f(poly.v2.x, poly.v2.y, poly.v2.z);
	glVertex3f(poly.v3.x, poly.v3.y, poly.v3.z);
	glEnd();
}