Move the transparency shaders to the NOMADVRLib library to enable their

use in other platforms.
Code is not OpenGL ES ready yet.
parent ff174423
#include "MyGL.h"
#define SHADERNAME 0
#define SHADERVERTEX 1
#define SHADERFRAGMENT 2
#define SHADERTESSEVAL 3
//-----------------------------------------------------------------------------
// Purpose: Compiles a GL shader program and returns the handle. Returns 0 if
// the shader couldn't be compiled for some reason.
......
......@@ -113,7 +113,7 @@ while (*file!='\0') {
int loadConfigFile(const char * f)
{
//default values
eprintf ("load config file start");
//eprintf ("load config file start");
bool nonperiodic=false;
char base_url[1024]="http://enc-testing-nomad.esc.rzg.mpg.de/v1.0/materials/";
char material[1024]="";
......@@ -313,9 +313,9 @@ int loadConfigFile(const char * f)
char url[2048];
sprintf (url, "%s%s", base_url, material);
//rgh fixme, we know only one
eprintf ("load config file start, before readAtomsJsonURL");
//eprintf ("load config file start, before readAtomsJsonURL");
e = readAtomsJsonURL (url, &numAtoms, &timesteps, &atoms, abc, &clonedAtoms, token);
eprintf ("load config file start, after readAtomsJsonURL");
//eprintf ("load config file start, after readAtomsJsonURL");
if (e<0)
return e-300;
numClonedAtoms=clonedAtoms[0].size()/4;
......
#include "IsoShaders.h"
#define GRIDSTR "1"
const char * const IsoShaders [] = {"Iso Renderer",
//vertex
#if defined(WIN32) || defined(CAVE)
......@@ -41,3 +43,61 @@ const char * const IsoShaders [] = {"Iso Renderer",
//tess
nullptr
};
const char *const IsoTransparentShaders [] = {"Iso Transparent Renderer",
//vertex
#if defined(WIN32) || defined(CAVE)
"#version 410\n"
#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",
// Fragment Shader
#if defined(WIN32) || defined(CAVE)
"#version 410 core\n"
#else
"#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"
"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
extern const char * const IsoShaders[];
extern const char *const IsoTransparentShaders [];
//#if 0
//#if 0
#include <vector>
#include <math.h>
......@@ -177,28 +177,25 @@ GLenum PrepareGLiso (GLuint vao, GLuint vertbuffer, const std::vector<float> &ve
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * vertdata.size(), &vertdata[0], GL_STATIC_DRAW);
if ((e = glGetError()) != GL_NO_ERROR)
eprintf("opengl error %d, glBufferData, l %d\n", e, __LINE__);
int offset = 0;
//now pos[3], normal[3], color[4]
//pos
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (const void *)offset);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (const void *)(0));
if (glGetError() != GL_NO_ERROR)
eprintf("opengl error attrib pointer 0\n");
//normal
offset += 3 * sizeof(GLfloat); //3 floats
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (const void *)offset);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (const void *)(3 * sizeof(float)));
if ((e=glGetError()) != GL_NO_ERROR)
eprintf("opengl error attrib pointer 1\n");
//color
offset += 3 * sizeof(GLfloat); //6 floats
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, stride, (const void *)offset);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, stride, (const void *)(6 * sizeof(float)));
if (glGetError() != GL_NO_ERROR)
eprintf("opengl error attrib pointer 2\n");
......@@ -239,3 +236,80 @@ GLenum PrepareISOShader (GLuint *p, GLint *mat) {
}
return glGetError();
}
GLenum PrepareISOTransShader (GLuint *p, GLint *mat) {
*p= CompileGLShader(
IsoTransparentShaders[SHADERNAME],
IsoTransparentShaders[SHADERVERTEX],
IsoTransparentShaders[SHADERFRAGMENT],
IsoTransparentShaders[SHADERTESSEVAL]
);
*mat=glGetUniformLocation(*p, "matrix");
if( *mat == -1 )
{
eprintf( "Unable to find matrix uniform in ISO shader\n" );
}
return glGetError();
}
//code not opengl es ready yet
#if defined(WIN32) || defined (CAVE)
bool SetupDepthPeeling(int renderWidth, int renderHeight, int zlayers, GLuint *textures /*[zlayers+2 (2 depth, zlayers colour)]*/,
GLuint *peelingFramebuffer)
{
//https://www.opengl.org/wiki/Common_Mistakes
//Until this is resolved in NVIDIA's drivers, it is advised to make sure that all textures have mipmap levels, and that all glTexParameteri​
//values are properly set up for the format of the texture. For example, integral textures are not complete if the mag and min filters have any LINEAR fields.
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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
if ((e = glGetError()) != GL_NO_ERROR)
eprintf("opengl error %d, SetupDepthPeeling a\n", e);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, renderWidth, renderHeight, 0,
GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);
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);
if ((e = glGetError()) != GL_NO_ERROR)
eprintf("opengl error %d, SetupDepthPeeling c\n", e);
}
for (int i = 0; i < zlayers; i++) {
glBindTexture(GL_TEXTURE_2D, textures[2+i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, renderWidth, renderHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
}
if ((e = glGetError()) != GL_NO_ERROR)
eprintf("opengl error %d, SetupDepthPeeling textures end\n", e);
//now create framebuffer
GLint dfb;
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &dfb);
glGenFramebuffers(1, peelingFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, *peelingFramebuffer);
if ((e = glGetError()) != GL_NO_ERROR)
eprintf("Gl error: %d, l %d\n", e, __LINE__);
glBindFramebuffer(GL_FRAMEBUFFER, dfb);
return (e == GL_NO_ERROR);
}
#endif
\ No newline at end of file
......@@ -23,6 +23,9 @@ GLenum PrepareGLiso (GLuint vao, GLuint vertbuffer, const std::vector<float> &ve
);
GLenum PrepareISOShader (GLuint *p, GLint *mat);
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);
#endif
#endif // __ISOSURFACESGL_H
......@@ -16,6 +16,9 @@
#ifndef GL_TESS_EVALUATION_SHADER
#define GL_TESS_EVALUATION_SHADER GL_TESS_EVALUATION_SHADER_EXT
#endif //GL_TESS_EVALUATION_SHADER
#ifndef GL_DEPTH_COMPONENT32
#define GL_DEPTH_COMPONENT32 GL_DEPTH_COMPONENT32_OES
#endif
#endif //WIN32
#endif //__MYGL_H
#define SHADERNAME 0
#define SHADERVERTEX 1
#define SHADERFRAGMENT 2
#define SHADERTESSEVAL 3
extern const char * const AtomShaders[];
extern const char * const AtomShadersNoTess [];
......@@ -314,7 +314,7 @@ const char * readAtomsJsonErrors[] = {
int readAtomsJsonURL (const char *const f, int **numatoms, int *timesteps, float ***pos, float abc[3][3],
std::vector<float>** clonedAtoms, const char *const token)
{
eprintf ("readAtomsJsonURL start");
//eprintf ("readAtomsJsonURL start");
try {
char host[2048], page[2048], url[2048], file[2048];
......@@ -399,7 +399,7 @@ fclose(out);
//system(cmd);
char file [2048];
sprintf (file, "%s%s", TMPDIR, "material");
eprintf ("readAtomsJsonURL before return");
//eprintf ("readAtomsJsonURL before return");
return readAtomsJson (file, numatoms, timesteps, pos, abc, clonedAtoms, token);
}
......@@ -444,7 +444,7 @@ void add (std::vector<float> *v, float x, float y, float z, float a)
int readAtomsJson (const char *const f, int **numatoms, int *timesteps, float ***pos, float abc[3][3],
std::vector<float>** clonedAtoms, const char *const token)
{
eprintf ("readAtomsJson start");
// eprintf ("readAtomsJson start");
char file[512];
int r;
sprintf (file, "%s_cells.json", f);
......@@ -540,7 +540,7 @@ int readAtomsJson (const char *const f, int **numatoms, int *timesteps, float **
}
TransformAtoms(*clonedAtoms, abc);
eprintf ("readAtomsJson, end");
//eprintf ("readAtomsJson, end");
return 0;
}
......
......@@ -197,6 +197,7 @@
<ClInclude Include="NOMADVRLib\CompileGLShader.h" />
<ClInclude Include="NOMADVRLib\ConfigFile.h" />
<ClInclude Include="NOMADVRLib\eprintf.h" />
<ClInclude Include="NOMADVRLib\IsoShaders.h" />
<ClInclude Include="NOMADVRLib\IsosurfacesGL.h" />
<ClInclude Include="NOMADVRLib\MyGL.h" />
<ClInclude Include="NOMADVRLib\polyhedron.h" />
......
......@@ -35,7 +35,7 @@
#include "NOMADVRLib/TessShaders.h"
#include "NOMADVRLib/UnitCellShaders.h"
#include "NOMADVRLib/IsoShaders.h"
#include "NOMADVRLib/CompileGLShader.h"
#include "NOMADVRLib/polyhedron.h"
......@@ -64,7 +64,7 @@
//shown on https://www.vbw-bayern.de/vbw/Aktionsfelder/Innovation-F-T/Forschung-und-Technologie/Zukunft-digital-Big-Data.jsp
#define GRID 1
#define GRIDSTR "1"
#define NUMLODS 1
......@@ -1089,61 +1089,9 @@ void CMainApplication::RenderFrame()
//-----------------------------------------------------------------------------
bool CMainApplication::CreateAllShaders()
{
m_unSceneProgramID = CompileGLShader(
"Scene",
//rgh FIXME, multiply normals by modelview!!
// Vertex Shader
"#version 410\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
"#version 410 core\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"
);
m_nSceneMatrixLocation = glGetUniformLocation( m_unSceneProgramID, "matrix" );
if( m_nSceneMatrixLocation == -1 )
if (GL_NO_ERROR!=PrepareISOTransShader (&m_unSceneProgramID, &m_nSceneMatrixLocation))
{
dprintf( "Unable to find matrix uniform in scene shader\n" );
dprintf( "Error Preparing Transparency shader\n" );
return false;
}
/*m_nBlendingIntLocation = glGetUniformLocation(m_unSceneProgramID, "blending");
......@@ -1328,59 +1276,12 @@ bool CMainApplication::SetupTexturemaps()
bool CMainApplication::SetupDepthPeeling()
{
//https://www.opengl.org/wiki/Common_Mistakes
//Until this is resolved in NVIDIA's drivers, it is advised to make sure that all textures have mipmap levels, and that all glTexParameteri​
//values are properly set up for the format of the texture. For example, integral textures are not complete if the mag and min filters have any LINEAR fields.
GLenum e;
if ((e = glGetError()) != GL_NO_ERROR)
dprintf("opengl error %d, start SetupDepthPeeling\n", e);
GLuint clearColor = 0;
for (int i = 0; i < 2; i++) {
glBindTexture(GL_TEXTURE_2D, m_iTexture[1 + i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
if ((e = glGetError()) != GL_NO_ERROR)
dprintf("opengl error %d, %s SetupDepthPeeling a\n", e, gluErrorString(e));
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, m_nRenderWidth, m_nRenderHeight, 0,
GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, 0);
if ((e = glGetError()) != GL_NO_ERROR)
dprintf("opengl error %d, %s SetupDepthPeeling b\n", e, gluErrorString(e));
glClearTexImage(m_iTexture[1], 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, &clearColor);
if ((e = glGetError()) != GL_NO_ERROR)
dprintf("opengl error %d, SetupDepthPeeling c\n", e);
}
for (int i = 0; i < ZLAYERS; i++) {
glBindTexture(GL_TEXTURE_2D, m_iTexture[3+i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_nRenderWidth, m_nRenderHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
}
bool e;
e=::SetupDepthPeeling(m_nRenderWidth, m_nRenderHeight, ZLAYERS, m_iTexture+1, &peelingFramebuffer);
if (!e)
dprintf("Errir setting up DepthPeeling\n");
if ((e = glGetError()) != GL_NO_ERROR)
dprintf("opengl error %d, %s SetupDepthPeeling textures end\n", e, gluErrorString(e));
//now create framebuffer
GLint dfb;
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &dfb);
glGenFramebuffers(1, &peelingFramebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, peelingFramebuffer);
if ((e = glGetError()) != GL_NO_ERROR)
dprintf("Gl error: %d, %s l %d\n", e, gluErrorString(e), __LINE__);
glBindFramebuffer(GL_FRAMEBUFFER, dfb);
return (e == GL_NO_ERROR);
return (e);
}
//-----------------------------------------------------------------------------
......@@ -2069,16 +1970,6 @@ void CMainApplication::RenderUnitCell(const vr::Hmd_Eye &nEye)
}
}
/*
Vector3 CMainApplication::GetDisplacement(int p[3])
{
Vector3 delta[3];
for (int ss=0;ss<3;ss++)
delta[ss]=static_cast<float>(p[ss])*Vector3(abc[ss][0], abc[ss][1], abc[ss][2]);
Vector3 f=delta[0]+delta[1]+delta[2];
return (f);
}*/
void CMainApplication::RenderAtomsUnitCell(const vr::Hmd_Eye &nEye, int p[3])
{
int e;
......
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