Browse Source

使用subroutine

master
blobt 4 years ago
parent
commit
68ed181987
  1. BIN
      .vs/shader2/v14/.suo
  2. 54
      main.cpp
  3. 71
      res/shader/MixLight.fs
  4. 19
      res/shader/MixLight.vs
  5. 4
      shader2.vcxproj
  6. 4
      shader2.vcxproj.filters

BIN
.vs/shader2/v14/.suo

54
main.cpp

@ -91,9 +91,9 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
glewInit();
/*创建program*/
GLuint program = CreateGPUProgram("res/shader/diffuse.vs", "res/shader/diffuse.fs");
GLuint program = CreateGPUProgram("res/shader/MixLight.vs", "res/shader/MixLight.fs");
GLuint posLocation, texcoordLocation, normalLocation, MLocation, VLocation, PLocation, NMLocation, textureLocation, offsetLocation;
GLuint posLocation, texcoordLocation, normalLocation, MLocation, VLocation, PLocation, NMLocation, textureLocation, offsetLocation, surfaceColorLocation;
posLocation = glGetAttribLocation(program, "pos");
texcoordLocation = glGetAttribLocation(program, "texcoord");
normalLocation = glGetAttribLocation(program, "normal");
@ -103,7 +103,12 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
PLocation = glGetUniformLocation(program, "P");
NMLocation = glGetUniformLocation(program, "NM");
textureLocation = glGetUniformLocation(program, "U_MainTexture");
/*使用subroutine*/
surfaceColorLocation = glGetSubroutineUniformLocation(program, GL_FRAGMENT_SHADER, "U_SurfaceColor");
GLuint ambientLightIndex = glGetSubroutineIndex(program, GL_FRAGMENT_SHADER, "Ambient");
GLuint diffuseLightIndex = glGetSubroutineIndex(program, GL_FRAGMENT_SHADER, "Diffuse");
GLuint specularLightIndex = glGetSubroutineIndex(program, GL_FRAGMENT_SHADER, "Specular");
/*load model*/
unsigned int *indexes = nullptr;
@ -126,16 +131,6 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
/*创建纹理*/
GLuint mainTexture = CreateTextureFromFile("res/image/niutou.bmp");
/* 定义了3个点表示3个模型的位置 */
float posOffsets[] = {
-1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f
};
/*创建offset VBO*/
GLuint offsetVBO = CreateBufferObject(GL_ARRAY_BUFFER, sizeof(float)*9, GL_STATIC_DRAW, posOffsets);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glEnable(GL_DEPTH_TEST);
@ -149,7 +144,7 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
0,0,0,1
};
glm::mat4 model = glm::translate(0.0f, -0.5f, -4.0f) * glm::rotate(-90.0f, 0.0f, 1.0f, 0.0f)*glm::scale(0.01f,0.01f,0.01f);
glm::mat4 model = glm::translate(0.0f, -0.5f, -3.0f) * glm::rotate(-90.0f, 0.0f, 1.0f, 0.0f)*glm::scale(0.01f,0.01f,0.01f);
glm::mat4 projection = glm::perspective(45.0f, (float)windowWidth / (float)windowHeight, 0.1f, 1000.0f);
glm::mat4 normalMatrix = glm::inverseTranspose(model);
@ -182,19 +177,31 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
glVertexAttribPointer(normalLocation, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), (void*)(sizeof(float) * 5));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, offsetVBO);
glEnableVertexAttribArray(offsetLocation);
glVertexAttribPointer(offsetLocation, 3, GL_FLOAT, GL_FALSE, sizeof(float)*3, (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glVertexAttribDivisor(offsetLocation, 1);
model = glm::translate(-1.0f, -0.5f, -3.0f) * glm::rotate(-90.0f, 0.0f, 1.0f, 0.0f)*glm::scale(0.01f, 0.01f, 0.01f);
glUniformMatrix4fv(MLocation, 1, GL_FALSE, glm::value_ptr(model));
glUniformSubroutinesuiv(GL_FRAGMENT_SHADER, 1, &ambientLightIndex);/*选的你要使用的subroutine*/
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT,0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
model = glm::translate(0.0f, -0.5f, -3.0f) * glm::rotate(-90.0f, 0.0f, 1.0f, 0.0f)*glm::scale(0.01f, 0.01f, 0.01f);
glUniformMatrix4fv(MLocation, 1, GL_FALSE, glm::value_ptr(model));
glUniformSubroutinesuiv(GL_FRAGMENT_SHADER, 1, &diffuseLightIndex);/*选的你要使用的subroutine*/
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
model = glm::translate(1.0f, -0.5f, -3.0f) * glm::rotate(-90.0f, 0.0f, 1.0f, 0.0f)*glm::scale(0.01f, 0.01f, 0.01f);
glUniformMatrix4fv(MLocation, 1, GL_FALSE, glm::value_ptr(model));
glUniformSubroutinesuiv(GL_FRAGMENT_SHADER, 1, &specularLightIndex);/*选的你要使用的subroutine*/
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glDrawElementsInstanced(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0,3);
glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glUseProgram(0);
};
Timer t2;
while (true) {
if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) {
if (msg.message == WM_QUIT) {
@ -205,12 +212,7 @@ INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
t2.Start();
//model = glm::translate(-1.0f, -0.5f, -4.0f) * glm::rotate(-90.0f, 0.0f, 1.0f, 0.0f)*glm::scale(0.01f, 0.01f, 0.01f);
what();
glFlush();
printf("cost time %f\n",t2.GetPassedTime()*1000.0f);
SwapBuffers(dc);
}

71
res/shader/MixLight.fs

@ -0,0 +1,71 @@
#version 400 core
subroutine vec4 SurfaceColor();
uniform sampler2D U_MainTexture;
subroutine uniform SurfaceColor U_SurfaceColor;
varying vec3 V_Normal;
varying vec4 V_WorldPos;//
varying vec2 V_Texcoord;
subroutine (SurfaceColor) vec4 Ambient(){
//ambient
vec4 AmbientLightColor = vec4(0.4, 0.4, 0.4, 1.0);
vec4 AmbientMaterial = vec4(0.2, 0.2, 0.2, 1.0);
vec4 AmbientColor = AmbientLightColor*AmbientMaterial;
vec4 ret = AmbientColor;
return ret;
}
subroutine (SurfaceColor) vec4 Diffuse(){
vec3 lightPos = vec3(10.0, 10.0, 0.0);
vec3 L=lightPos;
L = normalize(L);
vec3 n = normalize(V_Normal);
//ambient
vec4 AmbientLightColor = vec4(0.4, 0.4, 0.4, 1.0);
vec4 AmbientMaterial = vec4(0.2, 0.2, 0.2, 1.0);
vec4 AmbientColor = AmbientLightColor*AmbientMaterial;
//diffuse
vec4 DiffuseLightColor = vec4(1.0,1.0,1.0,1.0);
vec4 DiffuseMaterial = vec4(0.8, 0.8, 0.8, 1.0);
vec4 diffuseColor = DiffuseLightColor*DiffuseMaterial*max(0.0, dot(L,n));
vec4 ret = AmbientColor + texture2D(U_MainTexture, V_Texcoord) * diffuseColor;
return ret;
}
subroutine (SurfaceColor) vec4 Specular(){
vec3 lightPos = vec3(10.0, 10.0, 0.0);
vec3 L=lightPos;
L = normalize(L);
vec3 n = normalize(V_Normal);
//ambient
vec4 AmbientLightColor = vec4(0.4, 0.4, 0.4, 1.0);
vec4 AmbientMaterial = vec4(0.2, 0.2, 0.2, 1.0);
vec4 AmbientColor = AmbientLightColor*AmbientMaterial;
//diffuse
vec4 DiffuseLightColor = vec4(1.0,1.0,1.0,1.0);
vec4 DiffuseMaterial = vec4(0.8, 0.8, 0.8, 1.0);
vec4 diffuseColor = DiffuseLightColor*DiffuseMaterial*max(0.0, dot(L,n));
//specular
vec4 SpecularLightColor = vec4(1.0,1.0,1.0,1.0);
vec4 SpecularMaterial = vec4(0.9,0.9,0.9,1.0);
vec3 reflectDir = normalize(reflect(-L,n));//-L线线
vec3 viewDir = normalize(vec3(0.0) - V_WorldPos.xyz);//vec3(0.0)线
vec4 specularColor = SpecularLightColor*SpecularMaterial*pow(max(0.0,dot(viewDir,reflectDir)),128.0);
vec4 ret = AmbientColor + texture2D(U_MainTexture, V_Texcoord) * diffuseColor + specularColor;
return ret;
}
void main() {
gl_FragColor = U_SurfaceColor();
}

19
res/shader/MixLight.vs

@ -0,0 +1,19 @@
attribute vec3 pos;
attribute vec2 texcoord;
attribute vec3 normal;
uniform mat4 M;
uniform mat4 V;
uniform mat4 P;
uniform mat4 NM;
varying vec3 V_Normal;
varying vec4 V_WorldPos;//µ±Ç°µÄλÖÃ
varying vec2 V_Texcoord;
void main() {
V_Normal = mat3(NM)*normal;
V_WorldPos = M * vec4(pos, 1.0);
V_Texcoord = texcoord;
gl_Position = P*V*M*vec4(pos,1.0);
}

4
shader2.vcxproj

@ -159,8 +159,8 @@
<ClInclude Include="timer.h" />
</ItemGroup>
<ItemGroup>
<None Include="res\shader\diffuse.fs" />
<None Include="res\shader\diffuse.vs" />
<None Include="res\shader\MixLight.fs" />
<None Include="res\shader\MixLight.vs" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

4
shader2.vcxproj.filters

@ -46,10 +46,10 @@
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="res\shader\diffuse.fs">
<None Include="res\shader\MixLight.fs">
<Filter>源文件</Filter>
</None>
<None Include="res\shader\diffuse.vs">
<None Include="res\shader\MixLight.vs">
<Filter>源文件</Filter>
</None>
</ItemGroup>
Loading…
Cancel
Save