티스토리 뷰
OpenGL ES 2.0 Practice 4 : Simple Light
3차원 공간에서 Triangle 표면에 라이팅(Lighting) 처리를 통해 보다 실질적인 효과를 나타내게 합니다.
Lighting 처리는 Light 의 감도 조절을 위해 각각의 Vertex(position)에 대한 Normal (법선벡터) 과 Light Direction 을 이용하며, 색상은 Light Color 와 Triangle 의 색상을 의미하는 Material Diffuser 값으로 연산합니다.
Lighting 원리에 대해서는 http://www.opengl-tutorial.org/beginners-tutorials/tutorial-8-basic-shading/ 를 참조 하세요..
"precision mediump float;\n" +
"attribute vec3 aPosition;\n" +
"attribute vec3 aNormal;\n" +
"uniform mat4 uProjMatrix;\n" +
"uniform mat4 uViewMatrix;\n" +
"uniform mat4 uModelMatrix;\n" +
"uniform vec3 uLightColor;\n" +
"uniform vec3 uLightDir;\n" +
"uniform vec3 uMaterialDiffuse;\n" +
"varying vec3 vColor;\n" +
"void main() {\n" +
" gl_Position = uProjMatrix * uViewMatrix * uModelMatrix * vec4(aPosition, 1.0);\n" +
" vec3 normal_cameraspace = (uViewMatrix * uModelMatrix * vec4(aNormal, 1.0)).xyz;\n" +
" vec3 lightdir_cameraspace = (uViewMatrix * vec4(uLightDir, 1.0)).xyz;" +
" vec3 n = normalize(normal_cameraspace);\n" +
" vec3 l = normalize(lightdir_cameraspace);\n" +
" float cosTheta = clamp( dot(n, l), 0.0, 1.0);" +
" vColor = uMaterialDiffuse * uLightColor * cosTheta;" +
"}\n"
여기서는 태양광 같은 방향만 가지는 Directional Lighting 개념으로 구현 하였습니다.
그리고 View Matrix와 Model Matrix 를 분리하여 물체(Triangle) 만 회전되도록 구현 하였습니다. (즉, Lighting 방향을 고정하고 물체만 회전)
다음 코드는 attribute 및 uniform 변수를 설정하는 코드 입니다.
int aPositionLoc = GLES20.glGetAttribLocation(mProgram, "aPosition");
GLES20.glVertexAttribPointer(aPositionLoc, 3, GLES20.GL_FLOAT, false, 0, mPosition);
int aNormalLoc = GLES20.glGetAttribLocation(mProgram, "aNormal");
GLES20.glVertexAttribPointer(aNormalLoc, 3, GLES20.GL_FLOAT, false, 0, mNormal);
// light color
GLES20.glUniform3f(GLES20.glGetUniformLocation(mProgram, "uLightColor"), 1.0f, 1.0f, 1.0f);
// triangle material color
GLES20.glUniform3f(GLES20.glGetUniformLocation(mProgram, "uMaterialDiffuse"), 0.2f, 0.7f, 0.3f);
// light dir
GLES20.glUniform3f(GLES20.glGetUniformLocation(mProgram, "uLightDir"), 1.0f, -1.0f, -1.0f);
// triangle model matrix
// no transform (identity matrix)
// y-axis, 1 degree rotation every frames
Matrix.rotateM(mModelMatrix, 0, 1.0f, 0.0f, 1.0f, 0.0f);
GLES20.glUniformMatrix4fv(GLES20.glGetUniformLocation(mProgram, "uModelMatrix"), 1, false, mModelMatrix, 0);
// view matrix
GLES20.glUniformMatrix4fv(GLES20.glGetUniformLocation(mProgram, "uViewMatrix"), 1, false, mViewMatrix, 0);
// projection matrix
GLES20.glUniformMatrix4fv(GLES20.glGetUniformLocation(mProgram, "uProjMatrix"), 1, false, mProjMatrix, 0);
GLES20.glEnableVertexAttribArray(aPositionLoc);
GLES20.glEnableVertexAttribArray(aNormalLoc);
결과 화면... (Neuxs 7 2nd, Android 6.0)
'Programming > OpenGL ES' 카테고리의 다른 글
OpenGL ES 2.0 Practice 5 : Simple Texture (0) | 2016.10.23 |
---|---|
OpenGL ES 2.0 Practice 3 : Simple Projection (0) | 2016.10.09 |
OpenGL ES 2.0 Practice 2 : Simple Matrix (0) | 2016.10.09 |
OpenGL ES 2.0 Practice 1 : Simple Triangle (0) | 2016.10.08 |
- Total
- Today
- Yesterday
- TOOL
- OPENGLES
- 프로세스
- triangle
- OpenGL ES 2.0
- smapler2D
- aglie
- texture
- 실전
- practices
- 프랙티스
- 스크럼
- OpenGL ES
- 토크
- 애자일
- Scrum
- Android
- XP
- Matrix
- 툴
- light
- talk
- Practice
- projection
- OpenGL
- process
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |