Browse Source

使用cpu绘制全屏四边型

master
blobt 4 years ago
parent
commit
88a208b5b9
  1. BIN
      .vs/shader2/v14/.suo
  2. 56
      main.cpp
  3. 6
      res/shader/UI.fs
  4. 16
      res/shader/UI.vs
  5. 2
      shader2.vcxproj
  6. 6
      shader2.vcxproj.filters

BIN
.vs/shader2/v14/.suo

56
main.cpp

@ -52,19 +52,18 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
} }
/*创建窗口*/ /*创建窗口*/
//调整窗口大小
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;
int windowWidth = 800;
int windowHeight = 600;
//窗口名称一定要和刚才注册窗口的保持一致 //窗口名称一定要和刚才注册窗口的保持一致
HWND hwnd = CreateWindowEx(NULL, L"GLWindow", L"OpenGL Window", WS_OVERLAPPEDWINDOW, 100, 100, windowWidth, windowHeight, NULL, NULL, hinstance, NULL); HWND hwnd = CreateWindowEx(NULL, L"GLWindow", L"OpenGL Window", WS_OVERLAPPEDWINDOW, 100, 100, windowWidth, windowHeight, NULL, NULL, hinstance, NULL);
//获取实际窗口大小
RECT rect;
GetClientRect(hwnd, &rect);
windowWidth = rect.right - rect.left;
windowHeight = rect.bottom - rect.top;
//设置渲染环境 //设置渲染环境
HDC dc = GetDC(hwnd);//获取设备上下文 HDC dc = GetDC(hwnd);//获取设备上下文
PIXELFORMATDESCRIPTOR pfd; PIXELFORMATDESCRIPTOR pfd;
@ -88,7 +87,7 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
glewInit(); glewInit();
/*创建program*/ /*创建program*/
GLuint program = CreateGPUProgram("res/shader/test.vs", "res/shader/test.fs");
GLuint program = CreateGPUProgram("res/shader/UI.vs", "res/shader/UI.fs");
GLuint posLocation, texcoordLocation, normalLocation, MLocation, VLocation, PLocation, NMLocation, textureLocation; GLuint posLocation, texcoordLocation, normalLocation, MLocation, VLocation, PLocation, NMLocation, textureLocation;
posLocation = glGetAttribLocation(program, "pos"); posLocation = glGetAttribLocation(program, "pos");
@ -104,12 +103,33 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
/*load model*/ /*load model*/
unsigned int *indexes = nullptr; unsigned int *indexes = nullptr;
int vertexCount = 0, indexCount = 0; int vertexCount = 0, indexCount = 0;
VertexData* vertexes = LoadObjModel("res/model/Sphere.obj", &indexes, vertexCount, indexCount);
VertexData* vertexes = LoadObjModel("res/model/Quad.obj", &indexes, vertexCount, indexCount);
if (vertexes == nullptr) { if (vertexes == nullptr) {
printf("load obj model fail\n"); printf("load obj model fail\n");
} }
float z = 4.0f;
float halfFOV = 22.5f;
float randianHalfFOV = 3.14 * halfFOV / 180.0f;//角度转弧度
float tanHalfFOV = sinf(randianHalfFOV) / cosf(randianHalfFOV);
float y = tanHalfFOV * z;
float aspect = (float)windowWidth / (float)windowHeight;
float x = y * aspect;
vertexes[0].position[0] = -x;
vertexes[0].position[1] = -y;
vertexes[1].position[0] = x;
vertexes[1].position[1] = -y;
vertexes[2].position[0] = -x;
vertexes[2].position[1] = y;
vertexes[3].position[0] = x;
vertexes[3].position[1] = y;
/*创建vbo*/ /*创建vbo*/
GLuint vbo = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(VertexData) * vertexCount, GL_STATIC_DRAW, vertexes); GLuint vbo = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(VertexData) * vertexCount, GL_STATIC_DRAW, vertexes);
@ -122,6 +142,7 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glViewport(0,0, windowWidth, windowHeight);
//创建一个单位矩阵和一个投影矩阵,后面用来传递到shader //创建一个单位矩阵和一个投影矩阵,后面用来传递到shader
float identify[] = { float identify[] = {
@ -130,8 +151,9 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
0,0,1,0, 0,0,1,0,
0,0,0,1 0,0,0,1
}; };
glm::mat4 model = glm::translate(0.0f, 0.0f, -4.0f); 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 projection = glm::perspective(45.0f, (float)windowWidth / (float)windowHeight, 0.1f, 1000.0f);
glm::mat4 normalMatrix = glm::inverseTranspose(model); glm::mat4 normalMatrix = glm::inverseTranspose(model);
/*显示窗口*/ /*显示窗口*/
@ -141,7 +163,6 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
/*监听并处理用户请求*/ /*监听并处理用户请求*/
MSG msg; MSG msg;
float angle = 0.0;
while (true) { while (true) {
if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) { if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) {
if (msg.message == WM_QUIT) { if (msg.message == WM_QUIT) {
@ -151,13 +172,6 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
DispatchMessage(&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); glm::mat4 normalMatrix = glm::inverseTranspose(model);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@ -168,7 +182,7 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
glUniformMatrix4fv(PLocation, 1, GL_FALSE, glm::value_ptr(projection)); glUniformMatrix4fv(PLocation, 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(NMLocation, 1, GL_FALSE, glm::value_ptr(normalMatrix)); glUniformMatrix4fv(NMLocation, 1, GL_FALSE, glm::value_ptr(normalMatrix));
glBindTexture(GL_TEXTURE_2D, secondTexture);
glBindTexture(GL_TEXTURE_2D, mainTexture);
glUniform1i(textureLocation, 0); glUniform1i(textureLocation, 0);
glBindBuffer(GL_ARRAY_BUFFER, vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo);

6
res/shader/UI.fs

@ -0,0 +1,6 @@
uniform sampler2D U_MainTexture;
varying vec2 V_Texcoord;
void main() {
gl_FragColor = texture2D(U_MainTexture, V_Texcoord);
}

16
res/shader/UI.vs

@ -0,0 +1,16 @@
attribute vec3 pos;
attribute vec2 texcoord;
attribute vec3 normal;
uniform mat4 M;
uniform mat4 V;
uniform mat4 P;
uniform mat4 NM;
varying vec2 V_Texcoord;
void main() {
//V_Texcoord = vec2(texcoord.x, 1.0-texcoord.y);
V_Texcoord = vec2(texcoord.x, texcoord.y);
gl_Position = P*V*M*vec4(pos,1.0);
}

2
shader2.vcxproj

@ -157,6 +157,8 @@
<ItemGroup> <ItemGroup>
<None Include="res\shader\test.fs" /> <None Include="res\shader\test.fs" />
<None Include="res\shader\test.vs" /> <None Include="res\shader\test.vs" />
<None Include="res\shader\UI.fs" />
<None Include="res\shader\UI.vs" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

6
shader2.vcxproj.filters

@ -40,5 +40,11 @@
<None Include="res\shader\test.vs"> <None Include="res\shader\test.vs">
<Filter>源文件</Filter> <Filter>源文件</Filter>
</None> </None>
<None Include="res\shader\UI.fs">
<Filter>源文件</Filter>
</None>
<None Include="res\shader\UI.vs">
<Filter>源文件</Filter>
</None>
</ItemGroup> </ItemGroup>
</Project> </Project>
Loading…
Cancel
Save