效果图
1,取两幅图像的最大像素值合成新的图像
2,取两幅图像的最小像素值合成新的图像
实现shader
varying vec2 V_Texcoord;
uniform sampler2D U_BaseTexture;
uniform sampler2D U_BlendTexture;
void main()
{
vec4 blendColor=texture2D(U_BlendTexture,V_Texcoord);
vec4 baseColor=texture2D(U_BaseTexture,V_Texcoord);
//颜色变亮
gl_FragColor=max(blendColor,baseColor);
//颜色变暗
//gl_FragColor=min(blendColor,baseColor);
}
#include
#include "glew.h"
#include
#include
#include "utils.h"
#include "GPUProgram.h"
#include "ObjModel.h"
#include "FBO.h"
#include "FullScreenQuad.h"
#include "Glm/glm.hpp"
#include "Glm/ext.hpp"
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glew32.lib")
FullScreenQuad fsq;
LRESULT CALLBACK GLWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CLOSE:
PostQuitMessage(0);
break;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}
INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ 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;
wndClass.hIconSm = NULL;
wndClass.hInstance = hInstance;
wndClass.lpfnWndProc=GLWindowProc;
wndClass.lpszClassName = L"OpenGL";
wndClass.lpszMenuName = NULL;
wndClass.style = CS_VREDRAW | CS_HREDRAW;
ATOM atom = RegisterClassEx(&wndClass);
RECT rect;
rect.left = 0;
rect.top = 0;
rect.right = 800;
rect.bottom = 600;
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
HWND hwnd = CreateWindowEx(NULL, L"OpenGL", L"RenderWindow", WS_OVERLAPPEDWINDOW, 100, 100, rect.right-rect.left, rect.bottom-rect.top, NULL, NULL, hInstance, NULL);
HDC dc = GetDC(hwnd);
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_TYPE_RGBA | PFD_DOUBLEBUFFER;
pfd.iLayerType = PFD_MAIN_PLANE;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 24;
pfd.cStencilBits = 8;
int pixelFormatID = ChoosePixelFormat(dc, &pfd);
SetPixelFormat(dc,pixelFormatID,&pfd);
HGLRC rc = wglCreateContext(dc);
wglMakeCurrent(dc, rc);
GetClientRect(hwnd, &rect);
int viewportWidth = rect.right - rect.left, viewportHeight = rect.bottom - rect.top;
glewInit();
GPUProgram originalProgram;
originalProgram.AttachShader(GL_VERTEX_SHADER, "Debug/res/shader/fullscreenquad.vs");
originalProgram.AttachShader(GL_FRAGMENT_SHADER, "Debug/res/shader/fullscreenquad.fs");
originalProgram.Link();
originalProgram.DetectAttribute("pos");
originalProgram.DetectAttribute("texcoord");
originalProgram.DetectUniform("U_MainTexture");
GPUProgram combineProgram;
combineProgram.AttachShader(GL_VERTEX_SHADER, "Debug/res/shader/fullscreenquad.vs");
combineProgram.AttachShader(GL_FRAGMENT_SHADER, "Debug/res/shader/darker.fs");
combineProgram.Link();
combineProgram.DetectAttribute("pos");
combineProgram.DetectAttribute("texcoord");
combineProgram.DetectUniform("U_BaseTexture");//fbo -> color buffer
combineProgram.DetectUniform("U_BlendTexture");//src -> color glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
//init 3d model
ObjModel cube,quad,sphere;
quad.Init("Debug/res/model/Quad.obj");
float identity[] = {
1.0f,0,0,0,
0,1.0f,0,0,
0,0,1.0f,0,
0,0,0,1.0f
};
glm::mat4 quadModel = glm::translate(6.0f, 0.0f, -6.0f)*glm::rotate(-90.0f, 1.0f, 0.0f, 0.0f)*glm::scale(5.0f,5.0f,5.0f);
glm::mat4 quadNormalMatrix = glm::inverseTranspose(quadModel);
glm::mat4 projectionMatrix = glm::perspective(50.0f, (float)viewportWidth / (float)viewportHeight, 0.1f, 1000.0f);
glm::mat4 viewMatrix3 = glm::lookAt(glm::vec3(14.0f, 2.5f, -10.0f), glm::vec3(6.0f, 1.0f, -6.0f), glm::vec3(0.0f, 1.0f, 0.0f));
fsq.Init();
GLuint head = CreateTextureFromFile("Debug/res/image/wood.bmp");
GLuint grass = CreateTextureFromFile("Debug/res/image/earth.bmp");
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
MSG msg;
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);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//normal rgba
glUseProgram(originalProgram.mProgram);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, head);
glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0);
fsq.DrawToLeftTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord"));
//hdr
glUseProgram(originalProgram.mProgram);
glBindTexture(GL_TEXTURE_2D,grass);
glUniform1i(originalProgram.GetLocation("U_MainTexture"), 0);
fsq.DrawToRightTop(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord"));
//combine
glUseProgram(combineProgram.mProgram);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, head);//base image
glUniform1i(combineProgram.GetLocation("U_BaseTexture"), 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, grass);//blend image
glUniform1i(combineProgram.GetLocation("U_BlendTexture"), 1);
fsq.DrawToRightBottom(combineProgram.GetLocation("pos"), combineProgram.GetLocation("texcoord"));
glDisable(GL_BLEND);
glFlush();
SwapBuffers(dc);
}
return 0;
}