|
|
#include <windows.h>
#include "glew.h"
#include "Glm/glm.hpp"
#include "Glm/ext.hpp"
#include <stdio.h>
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib, "glew32.lib")
struct Vertex { float pos[3]; float color[4]; };
/**
* @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Ĭ�ϴ�������
}
char* LoadFileContent(const char *path) { FILE* pFile = fopen(path, "rb"); if (pFile) { fseek(pFile, 0, SEEK_END); int nLen = ftell(pFile); char* buffer = new char[nLen+1]; rewind(pFile); fread(buffer, nLen, 1, pFile); buffer[nLen] = '\0'; fclose(pFile); return buffer; } fclose(pFile); return nullptr; }
/**
* ����һ��GPU���� */ GLuint CreateGPUProgram(const char* vsShaderPath, const char* fsShaderPath) {
//����shader
GLuint vsShader = glCreateShader(GL_VERTEX_SHADER); GLuint fsShader = glCreateShader(GL_FRAGMENT_SHADER); //��ȡshader����
const char* vsCode = LoadFileContent(vsShaderPath); const char* fsCode = LoadFileContent(fsShaderPath); //��shader���� ���ڴ洫���Դ�
glShaderSource(vsShader, 1, &vsCode, nullptr); glShaderSource(fsShader, 1, &fsCode, nullptr); //����shader
glCompileShader(vsShader); GLint compileResult = GL_TRUE; glGetShaderiv(vsShader, GL_COMPILE_STATUS, &compileResult); if (compileResult == GL_FALSE) { char szLog[1024] = { 0 }; GLsizei logLen = 0;//ʵ�ʴ�����־����
glGetShaderInfoLog(vsShader, 1024, &logLen, szLog); printf("Compile shader fail error log is : %s \n shader code :\n %s \n ", szLog, vsCode); glDeleteShader(vsShader); }
glCompileShader(fsShader); compileResult = GL_TRUE; glGetShaderiv(fsShader, GL_COMPILE_STATUS, &compileResult); if (compileResult == GL_FALSE) { char szLog[1024] = { 0 }; GLsizei logLen = 0;//ʵ�ʴ�����־����
glGetShaderInfoLog(fsShader, 1024, &logLen, szLog); printf("Compile shader fail error log is : %s \n shader code :\n %s \n ", szLog, fsCode); glDeleteShader(fsShader); } //����program
GLuint program = glCreateProgram(); //����shader
glAttachShader(program, vsShader); glAttachShader(program, fsShader);
//����
glLinkProgram(program);
GLint nResult; glGetProgramiv(program, GL_LINK_STATUS, &nResult); if (nResult == GL_FALSE) { char log[1024] = { 0 }; GLsizei writed = 0; glGetProgramInfoLog(program, 1024, &writed, log); printf("Create CPU program fail error %s\n", log); glDeleteProgram(program); program = 0; } //����shader
glDetachShader(program, vsShader); glDetachShader(program, fsShader);
//ɾ��shader
glDeleteShader(vsShader); glDeleteShader(fsShader);
return program; } /**
* @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("test.vs", "test.fs");
GLuint posLocation, colorLocation, MLocation, VLocation, PLocation; posLocation = glGetAttribLocation(program, "pos"); colorLocation = glGetAttribLocation(program, "color"); MLocation = glGetUniformLocation(program, "M"); VLocation = glGetUniformLocation(program, "V"); PLocation = glGetUniformLocation(program, "P");
/*����vbo*/ Vertex vertex[3];
vertex[0].pos[0] = 0; vertex[0].pos[1] = 0; vertex[0].pos[2] = -100.0f; vertex[0].color[0] = 0.0f; vertex[0].color[1] = 0.0f; vertex[0].color[2] = 0.0f; vertex[0].color[3] = 1.0f;
vertex[1].pos[0] = 10; vertex[1].pos[1] = 0; vertex[1].pos[2] = -100.0f; vertex[1].color[0] = 0.0f; vertex[1].color[1] = 0.0f; vertex[1].color[2] = 0.0f; vertex[1].color[3] = 1.0f;
vertex[2].pos[0] = 0; vertex[2].pos[1] = 10; vertex[2].pos[2] = -100.0f; vertex[2].color[0] = 0.0f; vertex[2].color[1] = 0.0f; vertex[2].color[2] = 0.0f; vertex[2].color[3] = 1.0f; GLuint vbo; glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*7*3, vertex, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0);
/*����IBO*/ unsigned int indexes[] = {0,1,2}; GLuint ibo; glGenBuffers(1, &ibo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * 3, indexes, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
//����һ����λ������һ��ͶӰ�������������ݵ�shader
float identify[] = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 }; glm::mat4 projection = glm::perspective(45.0f, 800.0f/600.0f, 0.1f, 1000.0f);
/*��ʾ����*/ ShowWindow(hwnd, SW_SHOW); UpdateWindow(hwnd);
/*�����������û�����*/ MSG msg; while (true) { if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) { if (msg.message == WM_QUIT) { break; } TranslateMessage(&msg); DispatchMessage(&msg); }
glClear(GL_COLOR_BUFFER_BIT); glUseProgram(program);
//����mvp������shader
glUniformMatrix4fv(MLocation, 1, GL_FALSE, identify); glUniformMatrix4fv(VLocation, 1, GL_FALSE, identify); glUniformMatrix4fv(PLocation, 1, GL_FALSE, glm::value_ptr(projection));
//��shader��posָ�����ݺ�color����
glBindBuffer(GL_ARRAY_BUFFER, vbo); glEnableVertexAttribArray(posLocation); glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0); glEnableVertexAttribArray(colorLocation); glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(sizeof(float) * 3)); glBindBuffer(GL_ARRAY_BUFFER, 0);
//glDrawArrays(GL_TRIANGLES, 0, 3);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo); glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glUseProgram(0); SwapBuffers(dc); }
return 0; }
|