diff --git a/.vs/shader2/v14/.suo b/.vs/shader2/v14/.suo index bfba0eb..cefb1ec 100644 Binary files a/.vs/shader2/v14/.suo and b/.vs/shader2/v14/.suo differ diff --git a/main.cpp b/main.cpp index c3f5906..deaea6c 100644 --- a/main.cpp +++ b/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); + //获取实际窗口大小 + RECT rect; + GetClientRect(hwnd, &rect); + windowWidth = rect.right - rect.left; + windowHeight = rect.bottom - rect.top; + //设置渲染环境 HDC dc = GetDC(hwnd);//获取设备上下文 PIXELFORMATDESCRIPTOR pfd; @@ -88,7 +87,7 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine glewInit(); /*创建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; posLocation = glGetAttribLocation(program, "pos"); @@ -104,12 +103,33 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine /*load model*/ unsigned int *indexes = nullptr; 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) { 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*/ 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); glEnable(GL_DEPTH_TEST); + glViewport(0,0, windowWidth, windowHeight); //创建一个单位矩阵和一个投影矩阵,后面用来传递到shader float identify[] = { @@ -130,8 +151,9 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine 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 projection = glm::perspective(45.0f, (float)windowWidth / (float)windowHeight, 0.1f, 1000.0f); glm::mat4 normalMatrix = glm::inverseTranspose(model); /*显示窗口*/ @@ -141,7 +163,6 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine /*监听并处理用户请求*/ MSG msg; - float angle = 0.0; while (true) { if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) { if (msg.message == WM_QUIT) { @@ -151,13 +172,6 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine 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); @@ -168,7 +182,7 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine glUniformMatrix4fv(PLocation, 1, GL_FALSE, glm::value_ptr(projection)); glUniformMatrix4fv(NMLocation, 1, GL_FALSE, glm::value_ptr(normalMatrix)); - glBindTexture(GL_TEXTURE_2D, secondTexture); + glBindTexture(GL_TEXTURE_2D, mainTexture); glUniform1i(textureLocation, 0); glBindBuffer(GL_ARRAY_BUFFER, vbo); diff --git a/res/shader/UI.fs b/res/shader/UI.fs new file mode 100644 index 0000000..6324833 --- /dev/null +++ b/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); +} \ No newline at end of file diff --git a/res/shader/UI.vs b/res/shader/UI.vs new file mode 100644 index 0000000..5d2859e --- /dev/null +++ b/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); +} \ No newline at end of file diff --git a/shader2.vcxproj b/shader2.vcxproj index acb980e..dc94119 100644 --- a/shader2.vcxproj +++ b/shader2.vcxproj @@ -157,6 +157,8 @@ + + diff --git a/shader2.vcxproj.filters b/shader2.vcxproj.filters index b857b09..238dbbd 100644 --- a/shader2.vcxproj.filters +++ b/shader2.vcxproj.filters @@ -40,5 +40,11 @@ 婧愭枃浠 + + 婧愭枃浠 + + + 婧愭枃浠 + \ No newline at end of file