效果图
天空盒顶点shader
attribute vec3 pos;
attribute vec2 texcoord;
attribute vec3 normal;
uniform mat4 M;
uniform mat4 P;
uniform mat4 V;
varying vec3 V_Texcoord;
void main()
{
V_Texcoord=pos;
gl_Position=P*V*M*vec4(pos,1.0);
}
天空盒片元shader
varying vec3 V_Texcoord;
uniform samplerCube U_MainTexture;
void main()
{
gl_FragColor=textureCube(U_MainTexture,V_Texcoord);
}
反射顶点shader
attribute vec3 pos;
attribute vec2 texcoord;
attribute vec3 normal;
uniform mat4 M;
uniform mat4 P;
uniform mat4 V;
uniform mat4 NM;
varying vec4 V_WorldPos;
varying vec3 V_Normal;
void main()
{
V_WorldPos=M*vec4(pos,1.0);
V_Normal=mat3(NM)*normal;//将法线坐标转换到世界空间
gl_Position=P*V*M*vec4(pos,1.0);
}
反射片元shader
varying vec4 V_WorldPos;
varying vec3 V_Normal;
uniform samplerCube U_MainTexture;
void main()
{
//视线方向向量
vec3 eyeVec=normalize(V_WorldPos.xyz-vec3(0.0));
vec3 n=normalize(V_Normal);
//求视线方向的反射向量
vec3 r=reflect(eyeVec,n);
vec4 color=textureCube(U_MainTexture,r);
gl_FragColor=color;
}
折射片元shader
varying vec4 V_WorldPos;
varying vec3 V_Normal;
uniform samplerCube U_MainTexture;
void main()
{
vec3 eyeVec=normalize(V_WorldPos.xyz-vec3(0.0));
vec3 n=normalize(V_Normal);
vec3 r=refract(eyeVec,n,1.0/1.52);
vec4 color=textureCube(U_MainTexture,r);
gl_FragColor=color;
}
渲染函数
#include <windows.h>
#include "glew.h"
#include <stdio.h>
#include <math.h>
#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")
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 = 1280;
rect.bottom = 720;
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/skybox.vs");
originalProgram.AttachShader(GL_FRAGMENT_SHADER, "Debug/res/shader/skybox.fs");
originalProgram.Link();
originalProgram.DetectAttribute("pos");
originalProgram.DetectAttribute("texcoord");
originalProgram.DetectAttribute("normal");
originalProgram.DetectUniform("M");
originalProgram.DetectUniform("V");
originalProgram.DetectUniform("P");
originalProgram.DetectUniform("U_MainTexture");
GPUProgram reflectionProgram;
reflectionProgram.AttachShader(GL_VERTEX_SHADER, "Debug/res/shader/reflection.vs");
reflectionProgram.AttachShader(GL_FRAGMENT_SHADER, "Debug/res/shader/reflection.fs");
reflectionProgram.Link();
reflectionProgram.DetectAttribute("pos");
reflectionProgram.DetectAttribute("texcoord");
reflectionProgram.DetectAttribute("normal");
reflectionProgram.DetectUniform("M");
reflectionProgram.DetectUniform("V");
reflectionProgram.DetectUniform("P");
reflectionProgram.DetectUniform("NM");
reflectionProgram.DetectUniform("U_MainTexture");
GPUProgram refractionProgram;
refractionProgram.AttachShader(GL_VERTEX_SHADER, "Debug/res/shader/refraction.vs");
refractionProgram.AttachShader(GL_FRAGMENT_SHADER, "Debug/res/shader/refraction.fs");
refractionProgram.Link();
refractionProgram.DetectAttribute("pos");
refractionProgram.DetectAttribute("texcoord");
refractionProgram.DetectAttribute("normal");
refractionProgram.DetectUniform("M");
refractionProgram.DetectUniform("V");
refractionProgram.DetectUniform("P");
refractionProgram.DetectUniform("NM");
refractionProgram.DetectUniform("U_MainTexture");
//init 3d model
ObjModel cube,sphere;
cube.Init("Debug/res/model/Cube.obj");
sphere.Init("Debug/res/model/Sphere.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 cubeModel;
glm::mat4 sphereModel = glm::translate(-1.5f, 0.0f, -4.0f);
glm::mat4 sphereNormalMatrix = glm::inverseTranspose(sphereModel);
glm::mat4 sphereModel1 = glm::translate(1.5f, 0.0f, -4.0f);
glm::mat4 sphereNormalMatrix1 = glm::inverseTranspose(sphereModel1);
glm::mat4 cubeNormalMatrix = glm::inverseTranspose(cubeModel);
glm::mat4 projectionMatrix = glm::perspective(50.0f, (float)viewportWidth / (float)viewportHeight, 0.1f, 1000.0f);
glClearColor(0.6f, 0.6f, 0.6f, 1.0f);
GLuint mainTexture = SOIL_load_OGL_cubemap(
"Debug/res/image/right.png",
"Debug/res/image/left.png",
"Debug/res/image/top.png",
"Debug/res/image/bottom.png",
"Debug/res/image/back.png",
"Debug/res/image/front.png",
0, 0, SOIL_FLAG_POWER_OF_TWO);
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
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);
glDisable(GL_DEPTH_TEST);
//normal rgba
glUseProgram(originalProgram.mProgram);
glUniformMatrix4fv(originalProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(cubeModel));
glUniformMatrix4fv(originalProgram.GetLocation("V"), 1, GL_FALSE, identity);
glUniformMatrix4fv(originalProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
//绘制天空盒
glBindTexture(GL_TEXTURE_CUBE_MAP, mainTexture);
glUniform1i(originalProgram.GetLocation("U_MainTexture"),0);
cube.Bind(originalProgram.GetLocation("pos"), originalProgram.GetLocation("texcoord"), originalProgram.GetLocation("normal"));
cube.Draw();
glEnable(GL_DEPTH_TEST);
//绘制反射球
glUseProgram(reflectionProgram.mProgram);
glUniformMatrix4fv(reflectionProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(sphereModel));
glUniformMatrix4fv(reflectionProgram.GetLocation("V"), 1, GL_FALSE, identity);
glUniformMatrix4fv(reflectionProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
glUniformMatrix4fv(reflectionProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(sphereNormalMatrix));
glBindTexture(GL_TEXTURE_CUBE_MAP, mainTexture);
glUniform1i(reflectionProgram.GetLocation("U_MainTexture"), 0);
sphere.Bind(reflectionProgram.GetLocation("pos"), reflectionProgram.GetLocation("texcoord"), reflectionProgram.GetLocation("normal"));
sphere.Draw();
//绘制折射球
glUseProgram(refractionProgram.mProgram);
glUniformMatrix4fv(refractionProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(sphereModel1));
glUniformMatrix4fv(refractionProgram.GetLocation("V"), 1, GL_FALSE, identity);
glUniformMatrix4fv(refractionProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
glUniformMatrix4fv(refractionProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(sphereNormalMatrix1));
glBindTexture(GL_TEXTURE_CUBE_MAP, mainTexture);
glUniform1i(refractionProgram.GetLocation("U_MainTexture"), 0);
sphere.Bind(refractionProgram.GetLocation("pos"), refractionProgram.GetLocation("texcoord"), refractionProgram.GetLocation("normal"));
sphere.Draw();
glFlush();
SwapBuffers(dc);
}
return 0;
}