Skip to content

Commit

Permalink
Merge pull request #195 from TaixuEngineGroup/gui
Browse files Browse the repository at this point in the history
增加一个模型
  • Loading branch information
xmmmmmovo committed May 22, 2023
2 parents d691326 + 44c97ed commit c7e95f6
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 15 deletions.
4 changes: 3 additions & 1 deletion engine/runtime/platform/opengl/ogl_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ void OGLRenderer::update(float delta_time) {
auto &trans_comp =
_current_scene->_ecs_coordinator
.getComponent<TransformComponent>(entity);

// spdlog::debug("translate: {} {} {}", trans_comp.translate().x,
// trans_comp.translate().y,
// trans_comp.translate().z);
_lights.pointLights[_lights.pointLightCount] = {
.position = glm::vec4(trans_comp.translate(), 1.0f),
.ambient = light_comp.light_color,
Expand Down
2 changes: 1 addition & 1 deletion engine/runtime/resource/raw_data/material.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct Material final {
glm::vec3 emissive{};
float opacity{1.0f};
float refracti{1.0f};
float shininess{1.0f};
float shininess{32.0f};
float strength{1.0f};

int wireframe{0};
Expand Down
47 changes: 35 additions & 12 deletions engine/shaders/glsl/frag.frag
Original file line number Diff line number Diff line change
Expand Up @@ -73,33 +73,56 @@ layout (std140) uniform LightSource {
SpotLight spotLights[MAX_DIR_LIGHTS];
};

vec3 calcPointLight(PointLight light, vec3 normal, vec3 fragPos, vec3 viewDir) {
vec3 lightPos = light.position.xyz;
vec3 lightDir = normalize(lightPos - fragPos);
// diffuse shading
float diff = max(dot(normal, lightDir), 0.0);
// specular shading
vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), shininess);
// attenuation
float distance = length(lightPos - fragPos);
float attenuation = 1.0 / (light.constant + light.linear * distance + light.quadratic * (distance * distance));
// combine results
vec3 ambient = light.ambient.rgb;
vec3 diffuse = light.diffuse.rgb * diff * vec3(texture(diffuseTexture, fs_in.TexCoords));
vec3 specular = light.specular.rgb * spec;
ambient *= attenuation;
diffuse *= attenuation;
specular *= attenuation;
return (ambient + diffuse + specular);
}

void main()
{
vec3 color = texture(diffuseTexture, fs_in.TexCoords).rgb;
// ambient
vec3 ambient = 0.05 * color;
vec3 normal = normalize(fs_in.Normal);
vec3 objectColor = vec3(texture(diffuseTexture, fs_in.TexCoords));
vec3 normal_norm = normalize(fs_in.Normal);
vec3 viewDir = normalize(camera_pos.xyz - fs_in.FragPos);

vec3 result = vec3(0.0);

for (int i = 0; i < pointLightCount; i++) {
// ambient
float ambientStrength = 0.3;
vec3 ambient = ambientStrength * pointLights[i].ambient.rgb;

// diffuse
vec3 lightDir = normalize(pointLights[i].position.xyz - fs_in.FragPos);

vec3 halfwayDir = normalize(lightDir + viewDir);

float nol = abs(dot(lightDir, normal));
float noh = abs(dot(normal, halfwayDir));
float noh = abs(dot(normal_norm, halfwayDir));

float diff = max(nol, 0.0);
vec3 diffuse = diff * color;
float diff = max(dot(normal_norm, lightDir), 0.0);
vec3 diffuse = diff * pointLights[i].diffuse.rgb;

// specular
float spec = 0.0;
spec = pow(max(noh, 0.0), 32.0);
vec3 specular = vec3(0.3) * spec; // assuming bright white light color
float specularStrength = 0.3;
float spec = pow(max(noh, 0.0), 32.0f);
vec3 specular = specularStrength * spec * pointLights[i].specular.rgb;

result += (ambient + diffuse + specular) * pointLights[i].diffuse.rgb;
result += (ambient + diffuse + specular) * objectColor;
}
FragColor = vec4(result, 1.0);
}
Binary file removed example_proj/assets/models/cube_smooth.fbx
Binary file not shown.
File renamed without changes
13 changes: 13 additions & 0 deletions example_proj/assets/models/planet/planet.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Blender MTL File: 'None'
# Material Count: 1

newmtl Mars
Ns 96.078443
Ka 1.000000 1.000000 1.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ke 0.0 0.0 0.0
Ni 1.000000
d 1.000000
illum 2
map_Kd mars.png
File renamed without changes.
2 changes: 2 additions & 0 deletions example_proj/assets/models/planet/source.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
From TurboSquid, by Gerhald3D: https://www.turbosquid.com/3d-models/realistic-mars-photorealistic-2k-3d-1277433
Slightly modified by Joey de Vries: keep only planet layer, assign diffuse texture to material slot, desaturated texture somewhat.
35 changes: 35 additions & 0 deletions example_proj/gameplay/GO/nanosuit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"Components": {
"RenderableComponent": {
"obj_path": "assets/models/nanosuit/nanosuit.obj",
"visiable": true
},
"RigidBodyComponent": {
"motionType": 1,
"rigid_body_scale": {
"x": 1.0,
"y": 1.0,
"z": 1.0
},
"shapeType": 1
},
"TransformComponent": {
"position": {
"x": -5.0,
"y": 6.0,
"z": -5.0
},
"rotation": {
"x": 0.0,
"y": 0.0,
"z": 0.0,
"w": 1.0
},
"scale": {
"x": 1.0,
"y": 1.0,
"z": 1.0
}
}
}
}
2 changes: 1 addition & 1 deletion example_proj/gameplay/GO/planet.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"Components": {
"RenderableComponent": {
"obj_path": "assets/models/planet.obj",
"obj_path": "assets/models/planet/planet.obj",
"visiable": true
},
"RigidBodyComponent": {
Expand Down
4 changes: 4 additions & 0 deletions example_proj/gameplay/level/level 1-1.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
"GO_path": "gameplay/GO/planet.json",
"name": "planet"
},
{
"GO_path": "gameplay/GO/nanosuit.json",
"name": "nanosuit"
},
{
"GO_path": "gameplay/GO/light1.json",
"name": "light1"
Expand Down

0 comments on commit c7e95f6

Please sign in to comment.