[DEV] better management of light and material for 3D mesh

This commit is contained in:
Edouard DUPIN 2013-03-07 21:37:14 +01:00
parent 1c8ce9488c
commit 19c162eff2
13 changed files with 308 additions and 47 deletions

2
build

@ -1 +1 @@
Subproject commit cb8010af47373f5d5b2c319153b2585b074b5b68
Subproject commit 57ef468e1cbca4e6a6c4a9804024e7a684017441

View File

@ -2,12 +2,57 @@
precision mediump float;
precision mediump int;
#endif
struct DirectionalLight {
vec3 direction;
vec3 halfplane;
vec4 ambientColor;
vec4 diffuseColor;
vec4 specularColor;
};
struct Material {
vec4 ambientFactor;
vec4 diffuseFactor;
vec4 specularFactor;
float shininess;
};
// Light
uniform DirectionalLight EW_directionalLight;
// Material
uniform Material EW_material;
// Input :
uniform sampler2D EW_texID;
varying vec2 f_texcoord;
varying vec3 v_ecNormal;
void main(void) {
gl_FragColor = texture2D(EW_texID, f_texcoord);
vec4 tmpElementColor = texture2D(EW_texID, f_texcoord);
// Normalize v_ecNormal
vec3 ecNormal = v_ecNormal / length(v_ecNormal);
float ecNormalDotLightDirection = max(0.0, dot(ecNormal, EW_directionalLight.direction));
float ecNormalDotLightHalfplane = max(0.0, dot(ecNormal, EW_directionalLight.halfplane));
// Calculate ambient light
vec4 ambientLight = EW_directionalLight.ambientColor * EW_material.ambientFactor;
// Calculate diffuse light
vec4 diffuseLight = ecNormalDotLightDirection * EW_directionalLight.diffuseColor * EW_material.diffuseFactor;
// Calculate specular light
vec4 specularLight = vec4(0.0);
if (ecNormalDotLightHalfplane > 0.0) {
specularLight = pow(ecNormalDotLightHalfplane, EW_material.shininess) * EW_directionalLight.specularColor * EW_material.specularFactor;
specularLight = EW_directionalLight.specularColor * EW_material.specularFactor;
}
vec4 light = ambientLight + diffuseLight + specularLight;
gl_FragColor = tmpElementColor * light;
}

View File

@ -6,14 +6,22 @@ precision mediump int;
attribute vec3 EW_coord3d;
attribute vec2 EW_texture2d;
attribute vec3 EW_normal;
attribute vec3 EW_faceNormal;
uniform mat4 EW_MatrixTransformation;
uniform mat4 EW_MatrixPosition;
// output :
varying vec2 f_texcoord;
varying vec3 v_ecNormal;
void main(void) {
gl_Position = EW_MatrixTransformation * EW_MatrixPosition * vec4(EW_coord3d, 1.0);
// set texture output coord
f_texcoord = EW_texture2d;
mat4 MatrixPosition = EW_MatrixPosition;
MatrixPosition[3][0] = 0.0;
MatrixPosition[3][1] = 0.0;
MatrixPosition[3][2] = 0.0;
//v_ecNormal = vec3(MatrixPosition * vec4(EW_normal, 1.0) );
v_ecNormal = vec3(MatrixPosition * vec4(EW_faceNormal, 1.0) );
}

View File

@ -0,0 +1,13 @@
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif
// Input :
uniform sampler2D EW_texID;
varying vec2 f_texcoord;
void main(void) {
gl_FragColor = texture2D(EW_texID, f_texcoord);
}

View File

@ -0,0 +1,2 @@
texturedNoMaterial.vert
texturedNoMaterial.frag

View File

@ -0,0 +1,18 @@
#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif
// Input :
attribute vec3 EW_coord3d;
attribute vec2 EW_texture2d;
uniform mat4 EW_MatrixTransformation;
uniform mat4 EW_MatrixPosition;
// output :
varying vec2 f_texcoord;
void main(void) {
gl_Position = EW_MatrixTransformation * EW_MatrixPosition * vec4(EW_coord3d, 1.0);
// set texture output coord
f_texcoord = EW_texture2d;
}

View File

@ -53,7 +53,7 @@ namespace ewol
*/
void Draw(void);
/**
* @brief Clear alll tre registered element in the current element
* @brief Clear alll the registered element in the current element
*/
void Clear(void);
/**

View File

@ -31,6 +31,9 @@ ewol::Light::~Light(void)
void ewol::Light::Link(ewol::Program* prog, const etk::UString& baseName)
{
if (NULL == prog) {
return;
}
m_GL_direction = prog->GetUniform(baseName+".direction");
m_GL_halfplane = prog->GetUniform(baseName+".halfplane");
m_GL_ambientColor = prog->GetUniform(baseName+".ambientColor");

View File

@ -29,6 +29,9 @@ ewol::Material::~Material(void)
void ewol::Material::Link(ewol::Program* prog, const etk::UString& baseName)
{
if (NULL == prog) {
return;
}
m_GL_ambientFactor = prog->GetUniform(baseName+".ambientFactor");
m_GL_diffuseFactor = prog->GetUniform(baseName+".diffuseFactor");
m_GL_specularFactor = prog->GetUniform(baseName+".specularFactor");

View File

@ -60,20 +60,37 @@ class VertexNode {
ewol::Mesh::Mesh(etk::UString genName, etk::UString shaderName) :
ewol::Resource(genName),
m_enableFaceNormal(false),
m_enableFaceNormal(true),
m_enableVertexNormal(true),
m_numberOfElments(0),
m_texture0(NULL)
{
// get the shader resource :
m_GLPosition = 0;
// set the element material properties :
m_material.SetAmbientFactor(vec4(0.200000,0.200000,0.200000, 1.0));
m_material.SetDiffuseFactor(vec4(0.640000, 0.640000, 0.640000, 1.0));
m_material.SetSpecularFactor(vec4(0.500000, 0.500000, 0.500000, 1.0));
m_material.SetShininess(0.96078431);
m_light.SetDirection(vec3(0,cos(M_PI/4),sin(M_PI/4)));
m_light.SetHalfPlane(vec3(1,0,0));
m_light.SetAmbientColor(vec4(1,1,1,1));
m_light.SetDiffuseColor(vec4(1.0,1.0,1.0,1));
m_light.SetSpecularColor(vec4(0.0,0.0,0.0,1));
if (true == ewol::resource::Keep(shaderName, m_GLprogram) ) {
m_GLPosition = m_GLprogram->GetAttribute("EW_coord3d");
m_GLtexture = m_GLprogram->GetAttribute("EW_texture2d");
m_GLNormal = m_GLprogram->GetAttribute("EW_normal");
m_GLNormalFace = m_GLprogram->GetAttribute("EW_faceNormal");
m_GLMatrix = m_GLprogram->GetUniform("EW_MatrixTransformation");
m_GLMatrixPosition = m_GLprogram->GetUniform("EW_MatrixPosition");
m_GLtexID0 = m_GLprogram->GetUniform("EW_texID");
// Link material and Lights
m_material.Link(m_GLprogram, "EW_material");
m_light.Link(m_GLprogram, "EW_directionalLight");
}
// this is the properties of the buffer requested : "r"/"w" + "-" + buffer type "f"=flaot "i"=integer
ewol::resource::Keep("w-fff", m_verticesVBO);
@ -121,6 +138,12 @@ void ewol::Mesh::Draw(mat4& positionMatrix)
m_GLprogram->SendAttributePointer(m_GLtexture, 2/*u,v*/, m_verticesVBO, MESH_VBO_TEXTURE);
// position :
m_GLprogram->SendAttributePointer(m_GLNormal, 3/*x,y,z*/, m_verticesVBO, MESH_VBO_VERTICES_NORMAL);
// position :
m_GLprogram->SendAttributePointer(m_GLNormalFace, 3/*x,y,z*/, m_verticesVBO, MESH_VBO_FACE_NORMAL);
// draw materials :
m_material.Draw(m_GLprogram);
m_light.Draw(m_GLprogram);
// Request the draw od the elements :
glDrawArrays(GL_TRIANGLES, 0, m_numberOfElments);
m_GLprogram->UnUse();
@ -196,6 +219,9 @@ void ewol::Mesh::GenerateVBO(void)
if(true==m_enableVertexNormal) {
m_verticesVBO->PushOnBuffer(MESH_VBO_VERTICES_NORMAL,m_listVertexNormal[m_listFaces[iii].m_vertex[indice]]);
}
if(true==m_enableFaceNormal) {
m_verticesVBO->PushOnBuffer(MESH_VBO_FACE_NORMAL,m_listFacesNormal[iii]);
}
indice = 1;
tmpUV = m_listUV[m_listFaces[iii].m_uv[indice]];
@ -204,6 +230,9 @@ void ewol::Mesh::GenerateVBO(void)
if(true==m_enableVertexNormal) {
m_verticesVBO->PushOnBuffer(MESH_VBO_VERTICES_NORMAL,m_listVertexNormal[m_listFaces[iii].m_vertex[indice]]);
}
if(true==m_enableFaceNormal) {
m_verticesVBO->PushOnBuffer(MESH_VBO_FACE_NORMAL,m_listFacesNormal[iii]);
}
indice = 2;
tmpUV = m_listUV[m_listFaces[iii].m_uv[indice]];
@ -212,6 +241,9 @@ void ewol::Mesh::GenerateVBO(void)
if(true==m_enableVertexNormal) {
m_verticesVBO->PushOnBuffer(MESH_VBO_VERTICES_NORMAL,m_listVertexNormal[m_listFaces[iii].m_vertex[indice]]);
}
if(true==m_enableFaceNormal) {
m_verticesVBO->PushOnBuffer(MESH_VBO_FACE_NORMAL,m_listFacesNormal[iii]);
}
#ifndef PRINT_HALF
if (m_listFaces[iii].m_nbElement==4) {
indice = 0;
@ -221,6 +253,9 @@ void ewol::Mesh::GenerateVBO(void)
if(true==m_enableVertexNormal) {
m_verticesVBO->PushOnBuffer(MESH_VBO_VERTICES_NORMAL,m_listVertexNormal[m_listFaces[iii].m_vertex[indice]]);
}
if(true==m_enableFaceNormal) {
m_verticesVBO->PushOnBuffer(MESH_VBO_FACE_NORMAL,m_listFacesNormal[iii]);
}
indice = 2;
tmpUV = m_listUV[m_listFaces[iii].m_uv[indice]];
@ -229,6 +264,9 @@ void ewol::Mesh::GenerateVBO(void)
if(true==m_enableVertexNormal) {
m_verticesVBO->PushOnBuffer(MESH_VBO_VERTICES_NORMAL,m_listVertexNormal[m_listFaces[iii].m_vertex[indice]]);
}
if(true==m_enableFaceNormal) {
m_verticesVBO->PushOnBuffer(MESH_VBO_FACE_NORMAL,m_listFacesNormal[iii]);
}
indice = 3;
tmpUV = m_listUV[m_listFaces[iii].m_uv[indice]];
@ -237,6 +275,9 @@ void ewol::Mesh::GenerateVBO(void)
if(true==m_enableVertexNormal) {
m_verticesVBO->PushOnBuffer(MESH_VBO_VERTICES_NORMAL,m_listVertexNormal[m_listFaces[iii].m_vertex[indice]]);
}
if(true==m_enableFaceNormal) {
m_verticesVBO->PushOnBuffer(MESH_VBO_FACE_NORMAL,m_listFacesNormal[iii]);
}
}
#endif
}
@ -245,7 +286,7 @@ void ewol::Mesh::GenerateVBO(void)
}
void ewol::Mesh::CreateCube(void)
void ewol::Mesh::CreateCube(float size)
{
m_listVertex.Clear();
m_listUV.Clear();
@ -276,14 +317,14 @@ void ewol::Mesh::CreateCube(void)
o---------------------o
0 3
*/
m_listVertex.PushBack(vec3( 1.0, -1.0, -1.0)); // 0
m_listVertex.PushBack(vec3( 1.0, -1.0, 1.0)); // 1
m_listVertex.PushBack(vec3(-1.0, -1.0, 1.0)); // 2
m_listVertex.PushBack(vec3(-1.0, -1.0, -1.0)); // 3
m_listVertex.PushBack(vec3( 1.0, 1.0, -1.0)); // 4
m_listVertex.PushBack(vec3( 1.0, 1.0, 1.0)); // 5
m_listVertex.PushBack(vec3(-1.0, 1.0, 1.0)); // 6
m_listVertex.PushBack(vec3(-1.0, 1.0, -1.0)); // 7
m_listVertex.PushBack(vec3( size, -size, -size)); // 0
m_listVertex.PushBack(vec3( size, -size, size)); // 1
m_listVertex.PushBack(vec3(-size, -size, size)); // 2
m_listVertex.PushBack(vec3(-size, -size, -size)); // 3
m_listVertex.PushBack(vec3( size, size, -size)); // 4
m_listVertex.PushBack(vec3( size, size, size)); // 5
m_listVertex.PushBack(vec3(-size, size, size)); // 6
m_listVertex.PushBack(vec3(-size, size, -size)); // 7
m_listUV.PushBack(vec2(0.0, 0.0));
m_listUV.PushBack(vec2(1.0, 0.0));
@ -299,6 +340,89 @@ void ewol::Mesh::CreateCube(void)
}
void ewol::Mesh::CreateViewBox(float size)
{
m_listVertex.Clear();
m_listUV.Clear();
m_listFaces.Clear();
m_numberOfElments = 0;
// This is the direct generation basis on the .obj system
/*
5 6
o---------------------o
/. /|
/ . / |
/ . / |
/ . / |
/ . / |
4 / . / |
o---------------------o |
| . |7 |
| . | |
| . | |
| . | |
| o..............|......o
| . 1 | / 2
| . | /
| . | /
| . | /
| . | /
|. |/
o---------------------o
0 3
*/
m_listVertex.PushBack(vec3( size, -size, -size)); // 0
m_listVertex.PushBack(vec3( size, -size, size)); // 1
m_listVertex.PushBack(vec3(-size, -size, size)); // 2
m_listVertex.PushBack(vec3(-size, -size, -size)); // 3
m_listVertex.PushBack(vec3( size, size, -size)); // 4
m_listVertex.PushBack(vec3( size, size, size)); // 5
m_listVertex.PushBack(vec3(-size, size, size)); // 6
m_listVertex.PushBack(vec3(-size, size, -size)); // 7
/*
o----------o----------o----------o
|8 |9 |10 |11
| | | |
| | | |
| 0 | 1 | 2 |
| | | |
| | | |
| | | |
| | | |
o----------o----------o----------o
|4 |5 |6 |7
| | | |
| | | |
| 3 | 4 | 5 |
| | | |
| | | |
| | | |
| | | |
o----------o----------o----------o
0 1 2 3
*/
m_listUV.PushBack(vec2(0.0 , 0.0 )); // 0
m_listUV.PushBack(vec2(1.0/3.0, 0.0 )); // 1
m_listUV.PushBack(vec2(2.0/3.0, 0.0 )); // 2
m_listUV.PushBack(vec2(1.0 , 0.0 )); // 3
m_listUV.PushBack(vec2(0.0 , 0.5 )); // 4
m_listUV.PushBack(vec2(1.0/3.0, 0.5 )); // 5
m_listUV.PushBack(vec2(2.0/3.0, 0.5 )); // 6
m_listUV.PushBack(vec2(1.0 , 0.5 )); // 7
m_listUV.PushBack(vec2(0.0 , 1.0 )); // 8
m_listUV.PushBack(vec2(1.0/3.0, 1.0 )); // 9
m_listUV.PushBack(vec2(2.0/3.0, 1.0 )); // 10
m_listUV.PushBack(vec2(1.0 , 1.0 )); // 11
m_listFaces.PushBack(Face(0,1, 1,5, 2,6, 3,2)); // 4
m_listFaces.PushBack(Face(4,4, 0,0, 3,1, 7,5)); // 3
m_listFaces.PushBack(Face(2,6, 6,10, 7,11, 3,7)); // 2
m_listFaces.PushBack(Face(4,2, 7,3, 6,7, 5,6)); // 5
m_listFaces.PushBack(Face(1,5, 5,9, 6,10, 2,6)); // 1
m_listFaces.PushBack(Face(0,4, 4,8, 5,9, 1,5)); // 0
}
void ewol::Mesh::SetTexture(const etk::UString& myTexture)
{
ivec2 tmpSize(256, 256);
@ -568,3 +692,17 @@ void ewol::Mesh::InternalSubdivide(bool smooth)
}
listVertex.Clear();
}
void ewol::Mesh::LoadMaterial(const etk::UString& name)
{
}
void ewol::Mesh::DisplaceElement(const ewol::DisplacementTable& displacement)
{
}

View File

@ -15,18 +15,55 @@
#include <ewol/renderer/resources/Shader.h>
#include <ewol/renderer/resources/Program.h>
#include <ewol/renderer/resources/VirtualBufferObject.h>
#include <ewol/renderer/Light.h>
#include <ewol/renderer/Material.h>
// 3 "float" elements
#define MESH_VBO_VERTICES (0)
// 2 "float" elements
#define MESH_VBO_TEXTURE (1)
// 3 "float" elements
#define MESH_VBO_VERTICES_NORMAL (2)
#define MESH_VBO_FACE_NORMAL (3)
// 4 "float" elements
#define MESH_VBO_COLOR (3)
#define MESH_VBO_COLOR (4)
namespace ewol
{
class DisplacementTable
{
private:
ivec2 m_size;
public:
etk::Vector<float> m_data;
DisplacementTable(const ivec2& size) :
m_size(size),
m_data(size.x()*size.y())
{
// TODO : Check input ...
m_data.ReSize(m_size.x()*m_size.y(), 0);
for(int32_t iii=0; iii<m_size.x()*m_size.y(); iii++) {
m_data[iii] = 0;
}
}
float Get(int32_t x, int32_t y) const
{
// We increment of the size to prevent the <0 result due to the "%" methode ...
x += m_size.x();
y += m_size.y();
x %= m_size.x();
y %= m_size.y();
return m_data[x + y*m_size.x()];
}
void Set(int32_t x, int32_t y, float val)
{
// We increment of the size to prevent the <0 result due to the "%" methode ...
x += m_size.x();
y += m_size.y();
x %= m_size.x();
y %= m_size.y();
m_data[x + y*m_size.x()] = val;
}
};
class Face
{
public:
@ -74,10 +111,13 @@ namespace ewol
int32_t m_GLMatrix;
int32_t m_GLMatrixPosition;
int32_t m_GLNormal;
int32_t m_GLNormalFace;
int32_t m_GLtexture;
int32_t m_GLtexID0;
int32_t m_bufferOfset;
int32_t m_numberOfElments;
ewol::Material m_material;
ewol::Light m_light;
protected:
etk::Vector<vec3> m_listVertex; //!< List of all vertex in the element
etk::Vector<vec2> m_listUV; //!< List of all UV point in the mesh (for the specify texture)
@ -95,9 +135,7 @@ namespace ewol
void GenerateVBO(void);
public:
// some addition basic funtion that permit to create or overwrite some caracterstics :
void CreateCube(void);
void SetTexture(const etk::UString& myTexture);
void Subdivide(int32_t numberOfTime, bool smooth);
protected:
void InternalSubdivide(bool smooth);
public:
@ -110,6 +148,16 @@ namespace ewol
private:
void CalculateNormaleFace(void);
void CalculateNormaleEdge(void);
public:
void LoadMaterial(const etk::UString& name);
/*
* Element modification area :
*/
public :
void CreateCube(float size=1.0);
void CreateViewBox(float size=1.0);
void Subdivide(int32_t numberOfTime, bool smooth);
void DisplaceElement(const ewol::DisplacementTable& displacement);
};
};

View File

@ -34,7 +34,7 @@ namespace ewol
* @brief Constructor of this VBO.
* @param[in] accesMode Acces mode : ???
*/
VirtualBufferObject(const etk::UString& accesMode, int32_t nbElement=3);
VirtualBufferObject(const etk::UString& accesMode, int32_t nbElement=4);
/**
* @brief Destructor of this VBO.
*/

View File

@ -88,36 +88,19 @@ FILE_LIST+= ewol/widget/Scene.cpp \
ewol/game/Camera.cpp
LOCAL_COPY_FILES := ../data/textured3D.prog:textured3D.prog \
../data/textured3D.frag:textured3D.frag \
../data/textured3D.vert:textured3D.vert \
\
../data/textured3D2.prog:textured3D2.prog \
../data/textured3D2.frag:textured3D2.frag \
../data/textured3D2.vert:textured3D2.vert \
\
../data/color.prog:color.prog \
../data/color.frag:color.frag \
../data/color.vert:color.vert \
\
../data/color3.prog:color3.prog \
../data/color3.frag:color3.frag \
../data/color3.vert:color3.vert \
\
../data/simple3D.prog:simple3D.prog \
../data/simple3D.frag:simple3D.frag \
../data/simple3D.vert:simple3D.vert \
\
../data/textured.prog:textured.prog \
../data/textured.frag:textured.frag \
../data/textured.vert:textured.vert \
\
../data/text.prog:text.prog \
../data/text.frag:text.frag \
../data/text.vert:text.vert
LOCAL_COPY_FOLDERS := ../data/theme/default/widgetEntry.*:theme/default \
../data/theme/rounded/widgetEntry.*:theme/rounded \
../data/theme/default/widgetButton.*:theme/default \
../data/theme/rounded/widgetButton.*:theme/rounded \
../data/textured.*: \
../data/texturedNoMaterial.*: \
../data/text.*: \
../data/simple3D.*: \
../data/color.*: \
../data/color3.*: \
../data/textured3D2.*: \
../data/textured3D.*: