You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

313 lines
9.7 KiB

#include <windows.h>
#include "glew.h"
#include "wglew.h"
#include "Glm/glm.hpp"
#include "Glm/ext.hpp"
#include <stdio.h>
#include "misc.h"
#include "model.h"
#include "timer.h"
#include "frustum.h"
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib, "glew32.lib")
HGLRC CreateNBRC(HDC dc)
{
HGLRC rc;
GLint attribs[]{
WGL_DRAW_TO_WINDOW_ARB,GL_TRUE,
WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,
WGL_DOUBLE_BUFFER_ARB,GL_TRUE,
WGL_RED_BITS_ARB,8,
WGL_GREEN_BITS_ARB,8,
WGL_BLUE_BITS_ARB,8,
WGL_ALPHA_BITS_ARB,8,
WGL_DEPTH_BITS_ARB,24,
WGL_STENCIL_BITS_ARB,8,
WGL_SAMPLE_BUFFERS_ARB,GL_TRUE,
WGL_SAMPLES_ARB,16,
NULL,NULL
};
int pixelFormat[256] = { 0 };
UINT formatNum = 0;
wglChoosePixelFormatARB(dc, attribs, NULL, 256, pixelFormat, &formatNum);
printf("support format number is %u\n", formatNum);
if (formatNum>0)
{
PIXELFORMATDESCRIPTOR pfd;
DescribePixelFormat(dc, pixelFormat[0], sizeof(pfd), &pfd);
SetPixelFormat(dc, pixelFormat[0], &pfd);
int contexAttributes[] = {
WGL_CONTEXT_MAJOR_VERSION_ARB,4,
WGL_CONTEXT_MINOR_VERSION_ARB,3,
WGL_CONTEXT_PROFILE_MASK_ARB,WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
0
};
rc = wglCreateContextAttribsARB(dc, nullptr, contexAttributes);
}
return rc;
}
/**
* @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 命令行的参数
* @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;
}
/*创建窗口*/
int windowWidth = 800;
int windowHeight = 600;
RECT rect;
rect.left = 0;
rect.right = windowWidth;
rect.bottom = windowHeight;
rect.top = 0;
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, false);
//窗口名称一定要和刚才注册窗口的保持一致
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();
if (wglChoosePixelFormatARB)
{
//destroy window
wglMakeCurrent(dc, nullptr);
wglDeleteContext(rc);
rc = nullptr;
ReleaseDC(hwnd, dc);
dc = nullptr;
DestroyWindow(hwnd);
hwnd = CreateWindowEx(NULL, L"GLWindow", L"OpenGL Window", WS_OVERLAPPEDWINDOW, 100, 100, windowWidth, windowHeight, NULL, NULL, hinstance, NULL);
//create msaa rc
dc = GetDC(hwnd);
rc = CreateNBRC(dc);
wglMakeCurrent(dc, rc);
}
/*创建program*/
GLuint program = CreateGPUProgram("res/shader/SimpleTexture.vs", "res/shader/SimpleTexture.fs");
GLuint posLocation, texcoordLocation, normalLocation, MLocation, VLocation, PLocation, 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");
textureLocation = glGetUniformLocation(program, "U_MainTexture");
/*load model*/
unsigned int *indexes = nullptr;
int vertexCount = 0, indexCount = 0;
Timer t;
t.Start();
VertexData* vertexes = LoadObjModel("res/model/niutou.obj", &indexes, vertexCount, indexCount);
printf("load model cost %fs %d\n", t.GetPassedTime(), t.GetPassedTickers());
if (vertexes == nullptr) {
printf("load obj model fail\n");
}
/*创建vbo*/
GLuint vao = CreateVAOWithVBOSettings([&]()->void
{
GLuint vbo = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(VertexData) * vertexCount, GL_STATIC_DRAW, vertexes);
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));
});
/*创建IBO*/
GLuint ibo = CreateBufferObject(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indexCount, GL_STATIC_DRAW, indexes);
glBindBuffer(GL_ARRAY_BUFFER, 0);
/*创建fsqprogram*/
GLuint fsqprogram = CreateGPUProgram("res/shader/UI FullScreemQuad.vs", "res/shader/RenderDepthBuffer.fs");
GLuint fsqposLocation, fsqtexcoordLocation, fsqnormalLocation, fsqMLocation, fsqVLocation, fsqPLocation, fsqtextureLocation;
fsqposLocation = glGetAttribLocation(fsqprogram, "pos");
fsqtexcoordLocation = glGetAttribLocation(fsqprogram, "texcoord");
fsqnormalLocation = glGetAttribLocation(fsqprogram, "normal");
fsqMLocation = glGetUniformLocation(fsqprogram, "M");
fsqVLocation = glGetUniformLocation(fsqprogram, "V");
fsqPLocation = glGetUniformLocation(fsqprogram, "P");
textureLocation = glGetUniformLocation(program, "U_MainTexture");
/*load model*/
unsigned int *fsqindexes = nullptr;
int fsqvertexCount = 0, fsqindexCount = 0;
Timer t2;
t2.Start();
VertexData* fsqvertexes = LoadObjModel("res/model/Quad.obj", &fsqindexes, fsqvertexCount, fsqindexCount);
printf("load model cost %fs %d\n", t2.GetPassedTime(), t2.GetPassedTickers());
if (fsqvertexes == nullptr) {
printf("load obj model fail\n");
}
/*创建fsqvbo*/
GLuint fsqvao = CreateVAOWithVBOSettings([&]()->void
{
GLuint fsqvbo = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(VertexData) * fsqvertexCount, GL_STATIC_DRAW, fsqvertexes);
glBindBuffer(GL_ARRAY_BUFFER, fsqvbo);
glEnableVertexAttribArray(fsqposLocation);
glVertexAttribPointer(fsqposLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)0);
glEnableVertexAttribArray(fsqtexcoordLocation);
glVertexAttribPointer(fsqtexcoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 3));
});
/*创建fsqIBO*/
GLuint fsqibo = CreateBufferObject(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * fsqindexCount, GL_STATIC_DRAW, fsqindexes);
glBindBuffer(GL_ARRAY_BUFFER, 0);
/*创建纹理*/
GLuint mainTexture = CreateTextureFromFile("res/image/niutou.bmp");
GLuint colorBuffer, depthBuffer;
GLuint fbo = CreateFramebufferObject(colorBuffer, depthBuffer, windowWidth, windowHeight);
glViewport(0, 0, windowWidth, windowHeight);
GL_CALL(glClearColor(0.1f, 0.4f, 0.7f, 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.5f, -2.0f) * glm::rotate(-90.0f, 0.0f, 1.0f, 0.0f)*glm::scale(0.01f,0.01f,0.01f);
glm::mat4 projection = glm::perspective(45.0f, (float)windowWidth / (float)windowHeight, 0.1f, 1000.0f);
glm::mat4 normalMatrix = glm::inverseTranspose(model);
/*显示窗口*/
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
/*监听并处理用户请求*/
MSG msg;
auto what = [&]()->void {
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));
glBindTexture(GL_TEXTURE_2D, mainTexture);
glUniform1i(textureLocation, 0);
glBindVertexArray(vao);
glUniformMatrix4fv(MLocation, 1, GL_FALSE, glm::value_ptr(model));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glUseProgram(0);
};
auto RenderFullScreenQuad = [&]()->void {
glUseProgram(fsqprogram);
glBindVertexArray(fsqvao);
glBindTexture(GL_TEXTURE_2D, colorBuffer);
glUniform1i(fsqtextureLocation, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, fsqibo);
glDrawElements(GL_TRIANGLES, fsqindexCount, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glUseProgram(0);
};
/*使用固定管线绘制一个四边形,并把fbo内容贴上*/
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(glm::value_ptr(projection));
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
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 | GL_DEPTH_BUFFER_BIT);
what();
glFlush();
SwapBuffers(dc);
}
return 0;
}