Commit a775a586 authored by Ruben Garcia Hernndez's avatar Ruben Garcia Hernndez

Move some more code to NOMADVRLib to easen transparency shading.

Untested
parent d80fee84
......@@ -51,53 +51,81 @@ const char *const IsoTransparentShaders [] = {"Iso Transparent Renderer",
#else
"#version 300 es\n"
#endif
"uniform mat4 matrix;\n"
"layout(location = 0) in vec4 position;\n"
"layout(location = 1) in vec3 normalsIn;\n"
"layout(location = 2) in vec4 colorIn;\n"
// "layout(location = 3) in vec2 uvIn;\n"
"out vec4 color;\n"
"out vec3 n;\n"
"out highp vec4 pos;\n"
// "out vec2 uv;\n"
"void main()\n"
"{\n"
" color = vec4(colorIn.rgba);\n"
" n=normalize(normalsIn);\n"
// " uv=uvIn;\n"
"int i=gl_InstanceID / " GRIDSTR ";\n"
"int j=gl_InstanceID % " GRIDSTR ";\n"
" pos = matrix * (position + vec4 (float(i)*0.15*101.0, 0, float(j)*0.15*101.0, 0));\n"
" gl_Position = pos;\n"
//" gl_Position = matrix * position;\n"
"}\n",
"uniform mat4 matrix;\n"
"layout(location = 0) in vec4 position;\n"
"layout(location = 1) in vec3 normalsIn;\n"
"layout(location = 2) in vec4 colorIn;\n"
// "layout(location = 3) in vec2 uvIn;\n"
"out vec4 color;\n"
"out vec3 n;\n"
"out highp vec4 pos;\n"
// "out vec2 uv;\n"
"void main()\n"
"{\n"
" color = vec4(colorIn.rgba);\n"
" n=normalize(normalsIn);\n"
// " uv=uvIn;\n"
"int i=gl_InstanceID / " GRIDSTR ";\n"
"int j=gl_InstanceID % " GRIDSTR ";\n"
" pos = matrix * (position + vec4 (float(i)*0.15*101.0, 0, float(j)*0.15*101.0, 0));\n"
" gl_Position = pos;\n"
//" gl_Position = matrix * position;\n"
"}\n",
// Fragment Shader
#if defined(WIN32) || defined(CAVE)
"#version 410 core\n"
"#version 410 core\n"
#else
"#version 300 es\n"
"#version 300 es\n"
#endif
"uniform sampler2D diffuse;\n" //now extra depth texture for peeling
"in vec4 color;\n"
"in vec3 n;\n"
"in highp vec4 pos;\n"
"out vec4 outputColor;\n"
"void main()\n"
"{\n"
"vec4 mytex=texture(diffuse, vec2(pos.x/pos.w*0.5+0.5, pos.y/pos.w*0.5+0.5));\n"
//http://www.gamedev.net/topic/556521-glsl-manual-shadow-map-biasscale/
//"vec2 d=vec2(dFdx(pos.z), dFdy(pos.z));\n"
//"highp float m=sqrt(d.x*d.x + d.y*d.y);\n"
"if ((pos.z/pos.w+1)/2 <= mytex.r+0.0001 ) discard;\n"
"uniform sampler2D diffuse;\n" //now extra depth texture for peeling
"in vec4 color;\n"
"in vec3 n;\n"
"in highp vec4 pos;\n"
"out vec4 outputColor;\n"
"void main()\n"
"{\n"
"vec4 mytex=texture(diffuse, vec2(pos.x/pos.w*0.5+0.5, pos.y/pos.w*0.5+0.5));\n"
//http://www.gamedev.net/topic/556521-glsl-manual-shadow-map-biasscale/
//"vec2 d=vec2(dFdx(pos.z), dFdy(pos.z));\n"
//"highp float m=sqrt(d.x*d.x + d.y*d.y);\n"
"if ((pos.z/pos.w+1)/2 <= mytex.r+0.0001 ) discard;\n"
"lowp vec3 nn=normalize(n);"
"lowp float a=max(0.0, dot(nn, vec3(0,sqrt(2.0)/2.0,sqrt(2.0)/2.0)));\n"
"lowp float b=max(0.0, dot(nn, vec3(0,0,1)));\n"
"highp vec4 res=color;\n"
//"outputColor = vec4(pos.x/pos.w*0.5+0.5, pos.y/pos.w*0.5+0.5, 0,1);\n"
" outputColor = vec4 ((res.rgb) * (0.2 + 0.2*a + 0.3*b), color.a);\n"
"}\n",
nullptr
};
\ No newline at end of file
"lowp vec3 nn=normalize(n);"
"lowp float a=max(0.0, dot(nn, vec3(0,sqrt(2.0)/2.0,sqrt(2.0)/2.0)));\n"
"lowp float b=max(0.0, dot(nn, vec3(0,0,1)));\n"
"highp vec4 res=color;\n"
//"outputColor = vec4(pos.x/pos.w*0.5+0.5, pos.y/pos.w*0.5+0.5, 0,1);\n"
" outputColor = vec4 ((res.rgb) * (0.2 + 0.2*a + 0.3*b), color.a);\n"
"}\n",
nullptr
};
const char *const IsoBlendShaders [] = {
// vertex shader
#if defined(WIN32) || defined(CAVE)
"#version 410 core\n"
#else
"#version 300 es\n"
#endif
"uniform mat4 matrix;\n"
"layout(location = 0) in vec4 position;\n"
"layout(location = 1) in vec2 v2TexCoordsIn;\n"
"out vec2 v2TexCoord;\n"
"void main()\n"
"{\n"
" v2TexCoord = v2TexCoordsIn;\n"
" gl_Position = matrix * vec4(position.xyz, 1);\n"
"}\n",
//fragment shader
"#version 410 core\n"
"uniform sampler2D diffuse;\n"
"in vec2 v2TexCoord;\n"
"out vec4 outputColor;\n"
"void main()\n"
"{\n"
" outputColor = texture( diffuse, v2TexCoord);\n"
"}\n",
nullptr
};
extern const char * const IsoShaders[];
extern const char *const IsoTransparentShaders [];
extern const char *const IsoBlendShaders [];
......@@ -91,45 +91,47 @@ bool AddModelToScene( const float *mat/*[16]*/, std::vector<float> &vertdata,
}
ply_close(ply);
for (int i = 0; i < 3* ntriangles; i++) {
vertindices.push_back(CubeIndices[i]);
}
for (int i = 0; i < 3* ntriangles; i++) {
vertindices.push_back(CubeIndices[i]);
}
for (int i = 0; i < nvertices; i++) {
//pos
float V1[4];
for (int j=0;j<3;j++)
V1[j] = CubeVertices[i * numComponents +j];
V1[3]=1;
float V[4];
mult(V, mat, V1);
for (int j=0;j<3;j++)
vertdata.push_back(V[j]);
for (int i = 0; i < nvertices; i++) {
//pos
float V1[4];
for (int j=0;j<3;j++)
V1[j] = CubeVertices[i * numComponents +j];
V1[3]=1;
float V[4];
mult(V, mat, V1);
for (int j=0;j<3;j++)
vertdata.push_back(V[j]);
//normals (FIXME should transform with inverse transform, but we think the matrix has uniform scaling and inv transpose = m)
//rgh beware: normals are (nx, ny, nz, 0) to avoid being translated !!!
for (int j=0;j<3;j++)
V1[j] = 0.f; //CubeVertices[i * numComponents + 3+j];
V1[3]=0;
mult (V, mat, V1);
normalize(V);
for (int j=0;j<3;j++)
vertdata.push_back(V[j]);
//colors (untransformed)
if (!colours) {
for (int j = 0; j < 4; j++)
vertdata.push_back(isocolours[set][j]);
} else {
for (int j = 0; j < 4; j++)
vertdata.push_back(CubeVertices[i * numComponents + 6 + j]);
}
//normals (FIXME should transform with inverse transform, but we think the matrix has uniform scaling and inv transpose = m)
//rgh beware: normals are (nx, ny, nz, 0) to avoid being translated !!!
for (int j=0;j<3;j++)
V1[j] = CubeVertices[i * numComponents + 3+j];
V1[3]=0;
mult (V, mat, V1);
normalize(V);
for (int j=0;j<3;j++) {
vertdata.push_back(V[j]);
//eprintf ("normal %d %d %f", i, j, V[j]);
}
delete[] CubeVertices;
delete[] CubeIndices;
CubeVertices=0;
CubeIndices=0;
return true;
//colors (untransformed)
if (!colours) {
for (int j = 0; j < 4; j++)
vertdata.push_back(isocolours[set][j]);
} else {
for (int j = 0; j < 4; j++)
vertdata.push_back(CubeVertices[i * numComponents + 6 + j]);
}
}
delete[] CubeVertices;
delete[] CubeIndices;
CubeVertices=0;
CubeIndices=0;
return true;
}
int vertex_cb(p_ply_argument argument) {
......@@ -265,7 +267,7 @@ bool SetupDepthPeeling(int renderWidth, int renderHeight, int zlayers, GLuint *t
GLenum e;
if ((e = glGetError()) != GL_NO_ERROR)
eprintf("opengl error %d, start SetupDepthPeeling\n", e);
GLuint clearColor = 0;
for (int i = 0; i < 2; i++) {
glBindTexture(GL_TEXTURE_2D, textures[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
......@@ -280,7 +282,7 @@ bool SetupDepthPeeling(int renderWidth, int renderHeight, int zlayers, GLuint *t
if ((e = glGetError()) != GL_NO_ERROR)
eprintf("opengl error %d, SetupDepthPeeling b\n", e);
glClearTexImage(textures[i], 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, &clearColor);
CleanDepthTexture(textures[i]);
if ((e = glGetError()) != GL_NO_ERROR)
eprintf("opengl error %d, SetupDepthPeeling c\n", e);
......@@ -312,4 +314,36 @@ bool SetupDepthPeeling(int renderWidth, int renderHeight, int zlayers, GLuint *t
return (e == GL_NO_ERROR);
}
#endif
\ No newline at end of file
void CleanDepthTexture (GLuint t)
{
int e;
GLuint clearColor = 0;
//glBindTexture(GL_TEXTURE_2D, t);
glClearTexImage(t, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, &clearColor);
if ((e = glGetError()) != GL_NO_ERROR)
eprintf("Gl error after glClearTexImage: %d, %s\n", e, gluErrorString(e));
}
GLenum EnableDepthFB(unsigned int zl, const GLuint transP,
const GLuint peelingFramebuffer, const GLuint *texture /*[2+ZLAYERS]*/)
{
GLenum e;
glUseProgram(transP);
if ((e = glGetError()) != GL_NO_ERROR) {
eprintf("Gl error after useprogram: %d, %s\n", e, gluErrorString(e));
return e;
}
glBindFramebuffer(GL_FRAMEBUFFER, peelingFramebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
texture[2 + zl], 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D,
texture[1-zl%2], 0);
glBindTexture(GL_TEXTURE_2D, texture[zl%2]);
glClearColor(BACKGROUND[0], BACKGROUND[1], BACKGROUND[2], 1);
glDepthMask(GL_TRUE);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
return glGetError();
}
#endif
......@@ -27,5 +27,8 @@ GLenum PrepareISOTransShader (GLuint *p, GLint *mat);
#if defined(WIN32) || defined (CAVE)
bool SetupDepthPeeling(int renderWidth, int renderHeight, int zlayers, GLuint *textures /*[zlayers+2 (2 depth, zlayers colour)]*/,
GLuint *peelingFramebuffer);
void CleanDepthTexture (GLuint t);
GLenum EnableDepthFB(unsigned int zl, const GLuint transP,
const GLuint peelingFramebuffer, const GLuint *texture /*[2+ZLAYERS]*/);
#endif
#endif // __ISOSURFACESGL_H
......@@ -2069,12 +2069,7 @@ void CMainApplication::RenderAtoms(const vr::Hmd_Eye &nEye)
void CMainApplication::CleanDepthTexture ()
{
int e;
GLuint clearColor = 0;
glBindTexture(GL_TEXTURE_2D, m_iTexture[1]);
if ((e = glGetError()) != GL_NO_ERROR)
dprintf("Gl error after bindtexture: %d, %s\n", e, gluErrorString(e));
glClearTexImage(m_iTexture[1], 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, &clearColor);
::ClearDepthTexture(m_iTexture[1]);
}
//-----------------------------------------------------------------------------
......@@ -2117,19 +2112,8 @@ void CMainApplication::RenderScene(vr::Hmd_Eye nEye)
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &dfb);
for (int zl = 0; zl < ZLAYERS; zl++) {
glUseProgram(m_unSceneProgramID);
if ((e = glGetError()) != GL_NO_ERROR)
dprintf("Gl error after useprogram: %d, %s\n", e, gluErrorString(e));
glBindFramebuffer(GL_FRAMEBUFFER, peelingFramebuffer);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, m_iTexture[3 + zl], 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_iTexture[2-zl%2], 0);
glBindTexture(GL_TEXTURE_2D, m_iTexture[1+zl%2]);
glClearColor(BACKGROUND[0], BACKGROUND[1], BACKGROUND[2], 1);
glDepthMask(GL_TRUE);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
EnableDepthFB(zl, m_unSceneProgramID,
peelingFramebuffer, m_iTexture+1);
for (int i = ISOS - 1; i >= 0; i--) {
PaintGrid(nEye, i);
......@@ -2148,7 +2132,7 @@ void CMainApplication::RenderScene(vr::Hmd_Eye nEye)
glDisable(GL_DEPTH_TEST);
glUseProgram(m_unRenderModelProgramID);
float mat[] = { 1, 0, 0, 0,
const float mat[] = { 1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 };
......@@ -2163,7 +2147,7 @@ void CMainApplication::RenderScene(vr::Hmd_Eye nEye)
glDisable(GL_CULL_FACE);
float z = 0.0f; //(m_fNearClip + m_fFarClip) / 2.0f;
float points[] = {
const float points[] = {
-1, -1, z, 1, 0, 0, -1, 0, 0,
-1, 1, z, 1, 0, 0, -1, 0, 1,
1, 1, z, 1, 0, 0, -1, 1, 1,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment