diff --git a/.vs/shader2/v14/.suo b/.vs/shader2/v14/.suo index 7e7dfc2..1b7abfd 100644 Binary files a/.vs/shader2/v14/.suo and b/.vs/shader2/v14/.suo differ diff --git a/main.cpp b/main.cpp index d028fd1..8563c3b 100644 --- a/main.cpp +++ b/main.cpp @@ -91,9 +91,9 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine glewInit(); /*创建program*/ - GLuint program = CreateGPUProgram("res/shader/ViewDepthMap.vs", "res/shader/ViewDepthMap.fs"); + GLuint program = CreateGPUProgram("res/shader/UI FullScreemQuad.vs", "res/shader/SimpleTexture.fs"); - GLuint posLocation, texcoordLocation, normalLocation, MLocation, VLocation, PLocation; + GLuint posLocation, texcoordLocation, normalLocation, MLocation, VLocation, PLocation, textureLocation; posLocation = glGetAttribLocation(program, "pos"); texcoordLocation = glGetAttribLocation(program, "texcoord"); normalLocation = glGetAttribLocation(program, "normal"); @@ -101,13 +101,14 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine 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); + VertexData* vertexes = LoadObjModel("res/model/Quad.obj", &indexes, vertexCount, indexCount); printf("load model cost %fs %d\n", t.GetPassedTime(), t.GetPassedTickers()); if (vertexes == nullptr) { @@ -119,9 +120,9 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine glBindBuffer(GL_ARRAY_BUFFER, vbo); glEnableVertexAttribArray(posLocation); glVertexAttribPointer(posLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)0); - /*glEnableVertexAttribArray(texcoordLocation); + glEnableVertexAttribArray(texcoordLocation); glVertexAttribPointer(texcoordLocation, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 3)); - glEnableVertexAttribArray(normalLocation); + /*glEnableVertexAttribArray(normalLocation); glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 5));*/ glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -131,6 +132,9 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine /*创建纹理*/ 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)); @@ -164,11 +168,13 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine glUniformMatrix4fv(VLocation, 1, GL_FALSE, identify); glUniformMatrix4fv(PLocation, 1, GL_FALSE, glm::value_ptr(projection)); + glBindTexture(GL_TEXTURE_2D, mainTexture); + glUniform1i(textureLocation, 0); + 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); }; @@ -187,11 +193,23 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine DispatchMessage(&msg); } - //绘制内容到fbo - GL_CALL(glClearColor(0.1f, 0.4f, 0.7f, 1.0f)); + glBindFramebuffer(GL_FRAMEBUFFER, fbo); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); what(); + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, colorBuffer); + + glBegin(GL_QUADS); + glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -0.5f, -2.0f); + glTexCoord2f(1.0f, 0.0f); glVertex3f(0.5f, -0.5f, -2.0f); + glTexCoord2f(1.0f, 1.0f); glVertex3f(0.5f, 0.5f, -2.0f); + glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, 0.5f, -2.0f); + glEnd(); + glFlush(); SwapBuffers(dc); } diff --git a/res/shader/SimpleTexture.fs b/res/shader/SimpleTexture.fs new file mode 100644 index 0000000..40af759 --- /dev/null +++ b/res/shader/SimpleTexture.fs @@ -0,0 +1,8 @@ +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/SimpleTexture.vs b/res/shader/SimpleTexture.vs new file mode 100644 index 0000000..a00db95 --- /dev/null +++ b/res/shader/SimpleTexture.vs @@ -0,0 +1,14 @@ +attribute vec3 pos; +attribute vec2 texcoord; +attribute vec3 normal; + +uniform mat4 M; +uniform mat4 V; +uniform mat4 P; + +varying vec2 V_Texcoord; + +void main() { + V_Texcoord = texcoord; + gl_Position = P*V*M*vec4(pos, 1.0); +} \ No newline at end of file diff --git a/res/shader/UI FullScreemQuad.vs b/res/shader/UI FullScreemQuad.vs index 6fdc552..2abb9c3 100644 --- a/res/shader/UI FullScreemQuad.vs +++ b/res/shader/UI FullScreemQuad.vs @@ -5,7 +5,6 @@ attribute vec3 normal; uniform mat4 M; uniform mat4 V; uniform mat4 P; -uniform mat4 NM; varying vec2 V_Texcoord; diff --git a/shader2.vcxproj b/shader2.vcxproj index bfff430..ce60435 100644 --- a/shader2.vcxproj +++ b/shader2.vcxproj @@ -159,8 +159,9 @@ - - + + + diff --git a/shader2.vcxproj.filters b/shader2.vcxproj.filters index 2a8311c..1578e22 100644 --- a/shader2.vcxproj.filters +++ b/shader2.vcxproj.filters @@ -46,10 +46,13 @@ - + 婧愭枃浠 - + + 婧愭枃浠 + + 婧愭枃浠