Browse Source

xray

master
blobt 4 years ago
parent
commit
8dd05e8350
  1. BIN
      .vs/shader3/v14/.suo
  2. 169
      main.cpp
  3. 62
      res/shader/test.fs

BIN
.vs/shader3/v14/.suo

169
main.cpp

@ -72,15 +72,6 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
glewInit();
//init fsqgpu program
GPUProgram fsqProgram;
fsqProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/fullscreenquad.vs");
fsqProgram.AttachShader(GL_FRAGMENT_SHADER, "res/shader/fullscreenquad.fs");
fsqProgram.Link();
fsqProgram.DetectAttribute("pos");
fsqProgram.DetectAttribute("texcoord");
fsqProgram.DetectUniform("U_MainTexture");
//init gpu program
GPUProgram gpuProgram;
gpuProgram.AttachShader(GL_VERTEX_SHADER, "res/shader/test.vs");
@ -94,21 +85,11 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
gpuProgram.DetectUniform("V");
gpuProgram.DetectUniform("P");
gpuProgram.DetectUniform("NM");
gpuProgram.DetectUniform("U_AmbientLightColor");
gpuProgram.DetectUniform("U_AmbientMaterial");
gpuProgram.DetectUniform("U_DiffuseLightColor");
gpuProgram.DetectUniform("U_DiffuseMaterial");
gpuProgram.DetectUniform("U_DiffuseIntensity");
gpuProgram.DetectUniform("U_LightPos");
gpuProgram.DetectUniform("U_LightDirection");
gpuProgram.DetectUniform("U_Cutoff");
gpuProgram.DetectUniform("U_SpecularLightColor");
gpuProgram.DetectUniform("U_SpecularMaterial");
gpuProgram.DetectUniform("U_EyePos");
//init 3d model
ObjModel cube;
cube.Init("res/model/Cube.obj");
ObjModel obj;
obj.Init("res/model/Sphere.obj");
float identity[] = {
1.0f,0,0,0,
@ -116,57 +97,15 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
0,0,1.0f,0,
0,0,0,1.0f
};
float ambientLightColor[] = {0.4f,0.4f,0.4f,1.0f};
float ambientMaterial[] = { 0.2f,0.2f,0.2f,1.0f };
float diffuseLightColor[] = { 1.0f,1.0f,1.0f,1.0f };
float diffuseMaterial[] = { 0.8f, 0.8f, 0.8f, 1.0f };
float lightPos[] = {0.0f,1.0f,0.0f,0.0f};
float diffuseIntensity = 1.0f;
float spotLightDirection[] = {0.0f, -1.0f, 0.0f, 128.0f};//把最后一位改成......
float spotLightCutoff = 0.0f;
float specularLightColor[] = { 1.0f,1.0f,1.0f,1.0f };
float specularMaterial[] = { 1.0f,1.0f,1.0f,1.0f };
float eyePos[] = { 0.0f,0.0f,0.0f };
glm::mat4 model1 = glm::translate<float>(-2.0f, 0.0f, -6.0f) * glm::rotate(-30.0f , 1.0f, 1.0f, 1.0f);
glm::mat4 model2 = glm::translate<float>(2.0f, 0.0f, -6.0f) * glm::rotate(-30.0f, 1.0f, 1.0f, 1.0f);
glm::mat4 model3 = glm::translate<float>(6.0f, 0.0f, -6.0f) * glm::rotate(-30.0f, 1.0f, 1.0f, 1.0f);
glm::mat4 viewMatrix1 = glm::lookAt(glm::vec3(-0.0f, 1.5f, -3.0f), glm::vec3(-2.0f, 0.0f, -6.0f), glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 viewMatrix2 = glm::lookAt(glm::vec3(3.0f, 1.5f, -3.0f), glm::vec3(2.0f, 0.0f, -6.0f), glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 viewMatrix3 = glm::lookAt(glm::vec3(7.0f, 1.5f, -3.0f), glm::vec3(6.0f, 0.0f, -6.0f), glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 model = glm::translate<float>(0.0f, 0.0f, -4.0f);
glm::mat4 viewMatrix = glm::lookAt(glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, -2.0f), glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 projectionMatrix = glm::perspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);
glm::mat4 normalMatrix = glm::inverseTranspose(model);
glm::mat4 normalMatrix1 = glm::inverseTranspose(model1);
glm::mat4 normalMatrix2 = glm::inverseTranspose(model2);
glm::mat4 normalMatrix3 = glm::inverseTranspose(model3);
//初始化fsq
FullScreenQuad fsq;
fsq.Init();
//初始化FBO
FBO fboDirectionLight;
fboDirectionLight.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
fboDirectionLight.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
fboDirectionLight.Finish();
FBO fboPointerLight;
fboPointerLight.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
fboPointerLight.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
fboPointerLight.Finish();
FBO fboSpotLight;
fboSpotLight.AttachColorBuffer("color", GL_COLOR_ATTACHMENT0, GL_RGBA, viewportWidth, viewportHeight);
fboSpotLight.AttachDepthBuffer("depth", viewportWidth, viewportHeight);
fboSpotLight.Finish();
glClearColor(41.0f/255.0f, 71.0f/255.0f, 121.0f / 255.0f, 1.0f);
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
@ -184,103 +123,23 @@ INT WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
DispatchMessage(&msg);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glUseProgram(gpuProgram.mProgram);
glUniformMatrix4fv(gpuProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
glUniform4fv(gpuProgram.GetLocation("U_AmbientLightColor"), 1,ambientLightColor);
glUniform4fv(gpuProgram.GetLocation("U_AmbientMaterial"), 1, ambientMaterial);
glUniform4fv(gpuProgram.GetLocation("U_DiffuseLightColor"), 1, diffuseLightColor);
glUniform4fv(gpuProgram.GetLocation("U_DiffuseMaterial"), 1, diffuseMaterial);
glUniform4fv(gpuProgram.GetLocation("U_LightDirection"), 1, spotLightDirection);
glUniform4fv(gpuProgram.GetLocation("U_SpecularLightColor"), 1, specularLightColor);
glUniform4fv(gpuProgram.GetLocation("U_SpecularMaterial"), 1, specularMaterial);
glUniformMatrix4fv(gpuProgram.GetLocation("P"), 1, GL_FALSE, glm::value_ptr(projectionMatrix));
glUniform3fv(gpuProgram.GetLocation("U_EyePos"), 1, eyePos);
//left
fboDirectionLight.Bind();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
lightPos[0] = 0.0f;
lightPos[1] = 1.5f;
lightPos[2] = 0.0f;
lightPos[3] = 0.0f;
diffuseIntensity = 1.0f;
spotLightCutoff = 0.0f;
glUniform4fv(gpuProgram.GetLocation("U_LightPos"), 1, lightPos);
glUniform1f(gpuProgram.GetLocation("U_DiffuseIntensity"), diffuseIntensity);
glUniform1f(gpuProgram.GetLocation("U_Cutoff"), spotLightCutoff);
glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model1));
glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix1));
glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix1));
cube.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
cube.Draw();
fboDirectionLight.Unbind();
//middle
fboPointerLight.Bind();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
lightPos[0] = 0.0f;
lightPos[1] = 3.0f;
lightPos[2] = -6.0f;
lightPos[3] = 1.0f;
diffuseIntensity = 3.0f;
spotLightCutoff = 0.0f;
glUniform4fv(gpuProgram.GetLocation("U_LightPos"), 1, lightPos);
glUniform1f(gpuProgram.GetLocation("U_DiffuseIntensity"), diffuseIntensity);
glUniform1f(gpuProgram.GetLocation("U_Cutoff"), spotLightCutoff);
glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model2));
glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix2));
glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix2));
cube.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
cube.Draw();
fboDirectionLight.Unbind();
//right
fboSpotLight.Bind();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
lightPos[0] = 6.0f;
lightPos[1] = 3.0f;
lightPos[2] = -6.0f;
lightPos[3] = 1.0f;
diffuseIntensity = 4.0f;
spotLightCutoff = 15.0f;
glUniform4fv(gpuProgram.GetLocation("U_LightPos"), 1, lightPos);
glUniform1f(gpuProgram.GetLocation("U_DiffuseIntensity"), diffuseIntensity);
glUniform1f(gpuProgram.GetLocation("U_Cutoff"), spotLightCutoff);
glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model3));
glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix3));
glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix3));
cube.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
cube.Draw();
fboSpotLight.Unbind();
glUniformMatrix4fv(gpuProgram.GetLocation("M"), 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(gpuProgram.GetLocation("V"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
glUniformMatrix4fv(gpuProgram.GetLocation("NM"), 1, GL_FALSE, glm::value_ptr(normalMatrix));
obj.Bind(gpuProgram.GetLocation("pos"), gpuProgram.GetLocation("texcoord"), gpuProgram.GetLocation("normal"));
obj.Draw();
glUseProgram(0);
glFlush();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(fsqProgram.mProgram);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, fboDirectionLight.GetBuffer("color"));
glUniform1i(fsqProgram.GetLocation("U_MainTexture"), 0);
fsq.DrawToLeftTop(fsqProgram.GetLocation("pos"), fsqProgram.GetLocation("texcoord"));
glBindTexture(GL_TEXTURE_2D, fboSpotLight.GetBuffer("color"));
glUniform1i(fsqProgram.GetLocation("U_MainTexture"), 0);
fsq.DrawToLeftBottom(fsqProgram.GetLocation("pos"), fsqProgram.GetLocation("texcoord"));
glBindTexture(GL_TEXTURE_2D, fboPointerLight.GetBuffer("color"));
glUniform1i(fsqProgram.GetLocation("U_MainTexture"), 0);
fsq.DrawToRightTop(fsqProgram.GetLocation("pos"), fsqProgram.GetLocation("texcoord"));
glFlush();
SwapBuffers(dc);
}
return 0;

62
res/shader/test.fs

@ -1,15 +1,4 @@
uniform vec4 U_AmbientLightColor;
uniform vec4 U_AmbientMaterial;
uniform vec4 U_LightPos;
uniform vec4 U_LightDirection;
uniform float U_Cutoff;
uniform float U_DiffuseIntensity;
uniform vec4 U_DiffuseLightColor;
uniform vec4 U_DiffuseMaterial;
uniform vec3 U_EyePos;
uniform vec4 U_SpecularLightColor;
uniform vec4 U_SpecularMaterial;
varying vec3 V_Normal;
varying vec3 V_WorldPos;
@ -17,54 +6,15 @@ varying vec3 V_WorldPos;
void main()
{
//common
vec3 N = normalize(V_Normal);
vec3 L = normalize(U_EyePos - V_WorldPos);
//ambient
vec4 ambientColor = U_AmbientLightColor * U_AmbientMaterial;
//diffuse
vec3 L = vec3(0.0);
float attenuation = 1.0;
float diffuseIntensity = 0.0;
if(U_LightPos.w != 0){//
L = normalize(U_LightPos.xyz - V_WorldPos);
float distance = length(U_LightPos.xyz - V_WorldPos);
float constantFactor=0.5;
float linearFactor=0.3;
float expFactor=0.1;
attenuation = 1.0 / (constantFactor + linearFactor * distance + expFactor * distance * distance);
if( U_Cutoff > 0){//
//
float radianCutoff = U_Cutoff * 3.14 / 180;
float cosThta = cos(radianCutoff);
vec3 spotLightDirection = normalize(U_LightDirection.xyz);
float currentThta = max(0.0, dot(-L, spotLightDirection));
float cosThta = dot(N,L);
float alpha = 0.0;
if(currentThta > cosThta){
if(dot(L,N)>0.0)
{
diffuseIntensity=pow(currentThta,U_LightDirection.w);
}
}
} else {
diffuseIntensity = max(0.0, dot(L,N));
}
} else {
//
L = normalize(U_LightPos.xyz - vec3(0.0));
diffuseIntensity = max(0.0, dot(L,N));
if(cosThta > 0.0){
alpha = 1 - cosThta;
}
vec4 diffuseColor = U_DiffuseLightColor * U_DiffuseMaterial * attenuation * diffuseIntensity * U_DiffuseIntensity;
//specualr
vec3 RD = normalize(reflect(-L,N));
vec3 VD = normalize(U_EyePos - V_WorldPos);
vec4 specularColor = U_SpecularLightColor * U_SpecularMaterial * pow(max(0.0, dot(RD,VD)) ,128);
gl_FragColor = ambientColor + diffuseColor + specularColor;
gl_FragColor = vec4(0.6) * alpha;
}
Loading…
Cancel
Save