|
@ -1,7 +1,9 @@ |
|
|
#include <windows.h>
|
|
|
#include <windows.h>
|
|
|
|
|
|
#include "glew.h"
|
|
|
#include <gl/GL.h>
|
|
|
#include <gl/GL.h>
|
|
|
#include <stdio.h>
|
|
|
#include <stdio.h>
|
|
|
#pragma comment(lib,"opengl32.lib")
|
|
|
#pragma comment(lib,"opengl32.lib")
|
|
|
|
|
|
#pragma comment(lib, "glew32.lib")
|
|
|
|
|
|
|
|
|
/**
|
|
|
/**
|
|
|
* @hwnd 发起消息的窗口 |
|
|
* @hwnd 发起消息的窗口 |
|
@ -17,6 +19,59 @@ LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) |
|
|
return DefWindowProc(hwnd, msg, wParam, lParam);//其他消息调用window默认处理函数
|
|
|
return DefWindowProc(hwnd, msg, wParam, lParam);//其他消息调用window默认处理函数
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
char* LoadFileContent(const char *path) { |
|
|
|
|
|
char* fileContent = nullptr; |
|
|
|
|
|
int filesize = 0; |
|
|
|
|
|
FILE*pFile = fopen(path, "rb"); |
|
|
|
|
|
if (pFile) { |
|
|
|
|
|
fseek(pFile, 0, SEEK_END); |
|
|
|
|
|
int nLen = ftell(pFile); |
|
|
|
|
|
if (nLen > 0) { |
|
|
|
|
|
rewind(pFile); |
|
|
|
|
|
fileContent = new char[nLen + 1]; |
|
|
|
|
|
fread(fileContent, sizeof(char), nLen, pFile); |
|
|
|
|
|
fileContent[nLen] = '\0'; |
|
|
|
|
|
filesize = nLen; |
|
|
|
|
|
} |
|
|
|
|
|
fclose(pFile); |
|
|
|
|
|
} |
|
|
|
|
|
return fileContent; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
GLuint CreateGPUProgram(const char* vsShaderPath, const char* fsShaderPath) { |
|
|
|
|
|
//创建program
|
|
|
|
|
|
GLuint program = glCreateProgram(); |
|
|
|
|
|
|
|
|
|
|
|
//创建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); |
|
|
|
|
|
glCompileShader(fsShader); |
|
|
|
|
|
|
|
|
|
|
|
//绑定shader
|
|
|
|
|
|
glAttachShader(program, vsShader); |
|
|
|
|
|
glAttachShader(program, fsShader); |
|
|
|
|
|
|
|
|
|
|
|
//连接
|
|
|
|
|
|
glLinkProgram(program); |
|
|
|
|
|
|
|
|
|
|
|
//解绑shader
|
|
|
|
|
|
glDetachShader(program, vsShader); |
|
|
|
|
|
glDetachShader(program, fsShader); |
|
|
|
|
|
|
|
|
|
|
|
//删除shader
|
|
|
|
|
|
glDeleteShader(vsShader); |
|
|
|
|
|
glDeleteShader(fsShader); |
|
|
|
|
|
|
|
|
|
|
|
return program; |
|
|
|
|
|
} |
|
|
/**
|
|
|
/**
|
|
|
* @hinstance 本应用程序启动实例 |
|
|
* @hinstance 本应用程序启动实例 |
|
|
* @hPrevInstance 上一次应用程式的实例 |
|
|
* @hPrevInstance 上一次应用程式的实例 |
|
@ -78,6 +133,11 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine |
|
|
HGLRC rc = wglCreateContext(dc); |
|
|
HGLRC rc = wglCreateContext(dc); |
|
|
wglMakeCurrent(dc, rc);//设置OpenGL渲染环境生效
|
|
|
wglMakeCurrent(dc, rc);//设置OpenGL渲染环境生效
|
|
|
|
|
|
|
|
|
|
|
|
/*glew初始化*/ |
|
|
|
|
|
glewInit(); |
|
|
|
|
|
|
|
|
|
|
|
GLuint program = CreateGPUProgram("test.vs", "test.fs"); |
|
|
|
|
|
|
|
|
glClearColor(1.0f, 1.0f, 0.0f, 1.0f); |
|
|
glClearColor(1.0f, 1.0f, 0.0f, 1.0f); |
|
|
|
|
|
|
|
|
/*显示窗口*/ |
|
|
/*显示窗口*/ |
|
|