提示画像ですが板ポリにPhongのシェーディングを行いたいのですが上手く反射が付きません。シェーダーは参考サイトを移しました。またライトの座標を(0,1000,-500)といったオブジェクトのずっと上で先の座標を指定しましたが見え方が同じです。下記の実現したいこと部の右の画像ような光沢を出したいです。
現状
カメラ座標(0,0,10) 向き(0,0,-1)
オブジェクト座標(0,0,-200) Scale(100,100,0)
#####試したこと
ライト座標をスプライトに限りなく近ずけた。
板ポリを適当な角度に回転させて光沢が出るかどうか確認
#####実現したいこと(右の画像部ような)
参考サイト: https://learnopengl.com/Lighting/Basic-Lighting
参考サイト: http://tegetegeosprey.g1.xrea.com/opengl/opengl_normalvecotr.htm
Sprite
cpp
// ##################################### 頂点属性 ##################################### void FrameWork::D2::Sprite::setAttribute() { size = endSize - startSize; vertex->resize(6); // 頂点座標 vertex->at(0).position[0] = -0.5f; vertex->at(0).position[1] = 0.5f; vertex->at(0).position[2] = 0; vertex->at(1).position[0] = -0.5f; vertex->at(1).position[1] = -0.5f; vertex->at(1).position[2] = 0; vertex->at(2).position[0] = 0.5f; vertex->at(2).position[1] = 0.5f; vertex->at(2).position[2] = 0; vertex->at(3).position[0] = 0.5f; vertex->at(3).position[1] = 0.5f; vertex->at(3).position[2] = 0; vertex->at(4).position[0] = -0.5f; vertex->at(4).position[1] = -0.5f; vertex->at(4).position[2] = 0; vertex->at(5).position[0] = 0.5f; vertex->at(5).position[1] = -0.5f; vertex->at(5).position[2] = 0; //UV座標 float sizeX = 1.0f / (float)size.x; float sizeY = 1.0f / (float)size.y; vertex->at(0).uv[0] = sizeX * startSize.x; vertex->at(0).uv[1] = sizeY * endSize.y; vertex->at(1).uv[0] = sizeX * startSize.x; vertex->at(1).uv[1] = sizeY * startSize.y; vertex->at(4).uv[0] = sizeX * startSize.x; vertex->at(4).uv[1] = sizeY * startSize.y; vertex->at(2).uv[0] = sizeX * endSize.x; vertex->at(2).uv[1] = sizeY * endSize.y; vertex->at(3).uv[0] = sizeX * endSize.x; vertex->at(3).uv[1] = sizeY * endSize.y; vertex->at(5).uv[0] = sizeX * endSize.x; vertex->at(5).uv[1] = sizeY * startSize.y; } // 面法線 glm::vec3 GetSurfaceNormal(glm::vec3 v1, glm::vec3 v2, glm::vec3 v3) { return glm::cross(v2 - v1,v3 - v2); } // ##################################### 法線 設定 ##################################### void FrameWork::D2::Sprite::setNormal() { glm::vec3 sur1 = GetSurfaceNormal( glm::vec3(vertex->at(0).position[0], vertex->at(0).position[1], vertex->at(0).position[2]), glm::vec3(vertex->at(1).position[0], vertex->at(1).position[1], vertex->at(1).position[2]), glm::vec3(vertex->at(2).position[0], vertex->at(2).position[1], vertex->at(2).position[2])); glm::vec3 sur2 = GetSurfaceNormal( glm::vec3(vertex->at(3).position[0], vertex->at(3).position[1], vertex->at(3).position[2]), glm::vec3(vertex->at(4).position[0], vertex->at(4).position[1], vertex->at(4).position[2]), glm::vec3(vertex->at(5).position[0], vertex->at(5).position[1], vertex->at(5).position[2])); glm::vec3 s = sur1 + sur2; s = glm::normalize(s); glm::vec3 vec1 = glm::normalize(sur1); glm::vec3 vec2 = glm::normalize(sur2); //printf("%f\n",vec1.z); vertex->at(0).normal[0] = vec1.x; vertex->at(0).normal[1] = vec1.y; vertex->at(0).normal[2] = vec1.z; vertex->at(1).normal[0] = s.x; vertex->at(1).normal[1] = s.y; vertex->at(1).normal[2] = s.z; vertex->at(2).normal[0] = s.x; vertex->at(2).normal[1] = s.y; vertex->at(2).normal[2] = s.z; vertex->at(3).normal[0] = s.x; vertex->at(3).normal[1] = s.y; vertex->at(3).normal[2] = s.z; vertex->at(4).normal[0] = s.x; vertex->at(4).normal[1] = s.y; vertex->at(4).normal[2] = s.z; vertex->at(5).normal[0] = vec2.x; vertex->at(5).normal[1] = vec2.y; vertex->at(5).normal[2] = vec2.z; } // ##################################### テクスチャ描画 ##################################### void FrameWork::D2::Sprite::Draw(glm::mat4 projection,glm::vec3 position, int texNum, float r, glm::vec2 s, glm::vec2 start, glm::vec2 end) { shaderTexture.setEnable(); glBindVertexArray(vao); glBindBuffer(GL_ARRAY_BUFFER, vbo); startSize = start; //テクスチャ始点 endSize = end; //テクスチャ終点 vertex->resize(6); setAttribute(); //頂点属性 設定 setNormal(); //法線 設定 glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(VertexAttribute) * vertex->size(), vertex->data()); //Transform glm::vec3 pos = position; pos.y = (float)FrameWork::windowContext->getSize().y - pos.y - (glm::vec2(end - start).y / 2); pos.x = pos.x + (glm::vec2(end - start).x / 2); //setPosition(pos); //座標 setPosition(position); // setScale(glm::vec2(100,100)); //スケール setRotate(pi); //回転 pi += PI / 100; //描画 shaderTexture.setUniform3f("uLightPosition", glm::vec3(0, 100, -200)); //shaderTexture.setUniform3f("uLightPosition", glm::vec3(0, 0, 10)); shaderTexture.setUniform3f("uLightColor", glm::vec3(1, 1, 1)); shaderTexture.setUniform3f("uObjectColor", glm::vec3(1, 0, 0)); shaderTexture.setUniform3f("uViewPosition", glm::vec3(0, 0, 10)); shaderTexture.setUniform1f("uAmbientStrength",0.2f); shaderTexture.setUniform1f("uSpecularStrength",0.5); shaderTexture.setUniform1f("uShininessStrength",32.0); shaderTexture.setUniformMatrix4fv("uTranslate",getMatTranslation()); shaderTexture.setUniformMatrix4fv("uRotate", getMatRotate()); shaderTexture.setUniformMatrix4fv("uScale", getMatScale()); shaderTexture.setUniformMatrix4fv("uViewProjection", projection); //shaderTexture.setUniformMatrix4fv("uViewProjection", glm::ortho(0.0f, (float)FrameWork::windowContext->getSize().x, 0.0f, (float)FrameWork::windowContext->getSize().y)); //printf("%d\n",textureID.at(texNum)); //glActiveTexture(GL_TEXTURE0); //テクスチャ有効 //glBindTexture(GL_TEXTURE_2D, textureID.at(texNum)); //テクスチャバインド glDrawArrays(GL_TRIANGLES, 0, vertex->size()); //描画 //バインド解除 glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); //glBindTexture(GL_TEXTURE_2D, 0); shaderTexture.setDisable(); }
GLSL
/*######################################################################### # フラグメントシェーダー ###########################################################################*/ #version 420 //#extension GL_ARB_explicit_uniform_location : require layout(location = 2) in vec3 vPosition; layout(location = 3) in vec3 vNormal; uniform vec3 uLightPosition; //ライト座標 uniform vec3 uLightColor; //ライト色 uniform vec3 uObjectColor; //オブジェクトの色 uniform vec3 uViewPosition; //カメラ座標 uniform float uAmbientStrength; //環境光の強さ uniform float uSpecularStrength; //鏡面反射の強さ uniform float uShininessStrength; //反射輝度の強さ layout (location = 0) out vec4 fragColor; //out vec4 fragment; void main() { // ambient // float ambientStrength = 0.1; vec3 ambient = uAmbientStrength * uLightColor; // diffuse vec3 norm = normalize(vNormal); vec3 lightDir = normalize(uLightPosition - vPosition); float diff = max(dot(norm, lightDir), 0.0); vec3 diffuse = diff * uLightColor; // specular //float specularStrength = 0.5; vec3 viewDir = normalize(uViewPosition - vPosition); vec3 reflectDir = reflect(-lightDir, norm); //float uShininessStrength = 32; float spec = pow(max(dot(viewDir, reflectDir), 0.0), uShininessStrength); vec3 specular = uSpecularStrength * spec * uLightColor; vec3 result = (ambient + diffuse + specular) * uObjectColor; fragColor = vec4(result, 1.0); }
/*######################################################################### # 頂点シェーダー ###########################################################################*/ #version 420 //#extension GL_ARB_explicit_uniform_location : require layout(location = 0) in vec3 vertexPosition; layout(location = 1) in vec3 vertexNormal; uniform mat4 uTranslate; uniform mat4 uRotate; uniform mat4 uScale; uniform mat4 uViewProjection; layout(location = 2) out vec3 vPosition; layout(location = 3) out vec3 vNormal; void main() { vec4 vertex = vec4(vertexPosition,1.0); mat4 model = uTranslate * uRotate * uScale; vPosition = vec3(model * vec4(vertexPosition,1.0)); vNormal = mat3(transpose(inverse(model))) * vertexNormal; gl_Position = (uViewProjection * model) * vertex; }

まだ回答がついていません
会員登録して回答してみよう