|
|
#include <windows.h>
#include "glew.h"
#include "Glm/glm.hpp"
#include "Glm/ext.hpp"
#include <stdio.h>
#include "misc.h"
#include "model.h"
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib, "glew32.lib")
/**
* @hwnd ������Ϣ�Ĵ��� * @msg ��Ϣ���� */ LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_CLOSE: PostQuitMessage(0); return 0; }
return DefWindowProc(hwnd, msg, wParam, lParam);//������Ϣ����windowĬ�ϴ�������
}
/**
* @hinstance ��Ӧ�ó�������ʵ�� * @hPrevInstance ��һ��Ӧ�ó�ʽ��ʵ�� * @IpCmdLine �����еIJ��� * @ShowCmd ��ô��ʾ���� */ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { /*ע�ᴰ��*/ WNDCLASSEX wndclass; wndclass.cbClsExtra = 0; //�������͵Ķ����ռ䣬���ﲻ��Ҫ
wndclass.cbSize = sizeof(WNDCLASSEX); //����ʵ��ռ�õ��ڴ�
wndclass.cbWndExtra = 0; //���ڵĶ����ռ䣬���ﲻ��Ҫ
wndclass.hbrBackground = NULL; //���ڱ���
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //������������
wndclass.hIcon = NULL; //Ӧ�ó���exe�ļ���ʾͼ��
wndclass.hIconSm = NULL; //Ӧ�ó�������ʱ���Ͻ�ͼ��
wndclass.hInstance = hinstance; //��Ӧ�ó���ʵ��
wndclass.lpfnWndProc = GLWindowProc; //�����û������˴��ڣ��������ᱻ����
wndclass.lpszClassName = L"GLWindow";//��������
wndclass.lpszMenuName = NULL;//�˵�����
wndclass.style = CS_VREDRAW | CS_HREDRAW;//���ڸ���ʱ���ػ淽ʽ������ʹ�ô�ֱ�ػ���ˮƽ�ػ�
ATOM atom = RegisterClassEx(&wndclass); if (!atom) { MessageBox(NULL, L"Register failed", L"Error", MB_OK); return 0; }
/*��������*/ //�������ڴ�С
RECT rect; rect.left = 0; rect.right = 800; rect.top = 0; rect.bottom = 600; AdjustWindowRect(&rect, WS_EX_OVERLAPPEDWINDOW, NULL); int windowWidth = rect.right - rect.left; int windowHeight = rect.bottom - rect.top;
//��������һ��Ҫ�ղ�ע�ᴰ�ڵı���һ��
HWND hwnd = CreateWindowEx(NULL, L"GLWindow", L"OpenGL Window", WS_OVERLAPPEDWINDOW, 100, 100, windowWidth, windowHeight, NULL, NULL, hinstance, NULL);
//������Ⱦ����
HDC dc = GetDC(hwnd);//��ȡ�豸������
PIXELFORMATDESCRIPTOR pfd; memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); pfd.nVersion = 1; pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); pfd.cColorBits = 32; //��ɫ������ÿ������Ϊ32����4ͨ��RGBA
pfd.cDepthBits = 24; //���Ȼ�����ÿ�����ش�С��24���ر�ʾһ��������
pfd.cStencilBits = 8; //�ɰ建����ÿ����Ϊ8����
pfd.iPixelType = PFD_TYPE_RGBA; //������������ΪRGBA
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; //��ʾ������������������
//�������ظ�ʽ
int pixelFormat = ChoosePixelFormat(dc, &pfd); SetPixelFormat(dc, pixelFormat, &pfd);
//����OpenGL��Ⱦ����
HGLRC rc = wglCreateContext(dc); wglMakeCurrent(dc, rc);//����OpenGL��Ⱦ������Ч
/*glew��ʼ��*/ glewInit();
/*����program*/ GLuint program = CreateGPUProgram("res/shader/test.vs", "res/shader/test.fs");
GLuint posLocation, texcoordLocation, normalLocation, MLocation, VLocation, PLocation, NMLocation, textureLocation; posLocation = glGetAttribLocation(program, "pos"); texcoordLocation = glGetAttribLocation(program, "texcoord"); normalLocation = glGetAttribLocation(program, "normal"); MLocation = glGetUniformLocation(program, "M"); VLocation = glGetUniformLocation(program, "V"); PLocation = glGetUniformLocation(program, "P"); NMLocation = glGetUniformLocation(program, "NM"); textureLocation = glGetUniformLocation(program, "U_MainTexture");
/*load model*/ unsigned int *indexes = nullptr; int vertexCount = 0, indexCount = 0; VertexData* vertexes = LoadObjModel("res/model/Sphere.obj", &indexes, vertexCount, indexCount);
if (vertexes == nullptr) { printf("load obj model fail\n"); }
/*����vbo*/ GLuint vbo = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(VertexData) * vertexCount, GL_STATIC_DRAW, vertexes);
/*����IBO*/ GLuint ibo = CreateBufferObject(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indexCount, GL_STATIC_DRAW, indexes);
/*��������*/ GLuint mainTexture = CreateTextureFromFile("res/image/stone.jpg");
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glEnable(GL_DEPTH_TEST);
//����һ����λ������һ��ͶӰ�������������ݵ�shader
float identify[] = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 }; glm::mat4 model = glm::translate(0.0f, 0.0f, -4.0f); glm::mat4 projection = glm::perspective(45.0f, 800.0f/600.0f, 0.1f, 1000.0f); glm::mat4 normalMatrix = glm::inverseTranspose(model);
/*��ʾ����*/ ShowWindow(hwnd, SW_SHOW); UpdateWindow(hwnd);
/*�����������û�����*/ MSG msg;
float angle = 0.0; while (true) { if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) { if (msg.message == WM_QUIT) { break; } TranslateMessage(&msg); DispatchMessage(&msg); }
if (angle > 360) { angle = 0; } else { angle += 0.01f; } model = glm::translate(0.0f, 0.0f, -4.0f) * glm::rotate(angle, 0.0f, 1.0f, 0.0f); glm::mat4 normalMatrix = glm::inverseTranspose(model);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(program); glUniformMatrix4fv(MLocation, 1, GL_FALSE, glm::value_ptr(model)); glUniformMatrix4fv(VLocation, 1, GL_FALSE, identify); glUniformMatrix4fv(PLocation, 1, GL_FALSE, glm::value_ptr(projection)); glUniformMatrix4fv(NMLocation, 1, GL_FALSE, glm::value_ptr(normalMatrix));
glBindTexture(GL_TEXTURE_2D, mainTexture); glUniform1i(textureLocation, 0);
glBindBuffer(GL_ARRAY_BUFFER, vbo); glEnableVertexAttribArray(posLocation); glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)0); glEnableVertexAttribArray(texcoordLocation); glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 3)); glEnableVertexAttribArray(normalLocation); glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 5)); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glUseProgram(0);
SwapBuffers(dc); }
return 0; }
|