本文整理汇总了C++中Shader::Compile方法的典型用法代码示例。如果您正苦于以下问题:C++ Shader::Compile方法的具体用法?C++ Shader::Compile怎么用?C++ Shader::Compile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Shader
的用法示例。
在下文中一共展示了Shader::Compile方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: LoadShadersFromDisk
void ShaderRegistry::LoadShadersFromDisk(const std::string& rShadersDirectory)
{
std::vector<std::string> shaderFiles = Directory::ListFiles(rShadersDirectory);
for (unsigned int i = 0; i < shaderFiles.size(); i++)
{
std::string shaderName;
ShaderType shaderType;
if (!ExtractShaderInfo(shaderFiles[i], shaderName, shaderType))
{
continue;
}
Shader* pShader;
std::map<std::string, Shader*>::iterator it = mShadersByName.find(shaderName);
if (it == mShadersByName.end())
{
pShader = new Shader(shaderName);
mShadersByName.insert(std::make_pair(shaderName, pShader));
}
else
{
pShader = it->second;
}
pShader->Compile(rShadersDirectory + "/" + shaderFiles[i], shaderType);
}
std::map<std::string, Shader*>::iterator it = mShadersByName.begin();
while (it != mShadersByName.end())
{
it->second->Link();
it++;
}
}
示例2: Load
ShaderPtr ShaderLoader::Load(const Path & vertex_file_name, const Path & fragment_file_name, bool replaceCached)
{
Path resourceName = vertex_file_name.filename().generic_string() + fragment_file_name.filename().generic_string();
Resource<Shader> existingResource = this->GetResource(resourceName);
if (existingResource.resource && !replaceCached)
{
GetContext().GetLogger()->log(LOG_LOG, "Shader returned from cache.");
return existingResource.resource;
}
FilePtr vertexFile = GetContext().GetFileSystem()->OpenRead(vertex_file_name);
FilePtr fragmentFile = GetContext().GetFileSystem()->OpenRead(fragment_file_name);
if (!vertexFile->IsOpen() || !fragmentFile->IsOpen())
{
return ShaderPtr();
}
GetContext().GetLogger()->log(LOG_LOG, "Shader resource name: %s", resourceName.generic_string().c_str());
ByteBufferPtr vertexBuffer = vertexFile->ReadText();
ByteBufferPtr fragmentBuffer = fragmentFile->ReadText();
Shader * shader = new Shader(resourceName.generic_string(), (char*)vertexBuffer->data(), (char*)fragmentBuffer->data(), "");
shader->Compile();
if (shader->IsCompiledAndLinked())
{
if (existingResource.resource)
{
RemoveResource(existingResource.path);
GetContext().GetLogger()->log(LOG_LOG, "Removed cached shader: '%s'.", resourceName.c_str());
}
GetContext().GetLogger()->log(LOG_LOG, "Shader loaded: '%s'.", resourceName.c_str());
Resource<Shader> res(ShaderPtr(shader), resourceName);
this->AddResource(res);
return res.resource;
}
else
{
delete shader;
if (existingResource.resource)
GetContext().GetLogger()->log(LOG_ERROR, "Shader failed to load: '%s', using cached version.", resourceName.generic_string().c_str());
else
GetContext().GetLogger()->log(LOG_ERROR, "Shader failed to load: '%s'.", resourceName.generic_string().c_str());
return existingResource.resource;
}
}
示例3: loadShaderFromFile
Shader ResourceManager::loadShaderFromFile(const GLchar* vShaderFile, const GLchar* fShaderFile, const GLchar* gShaderFile)
{
// Define variables
std::string vertexCode;
std::string fragmentCode;
std::string geometryCode;
try
{
// Vertex
std::ifstream vertexShaderFile(vShaderFile);
std::stringstream vShaderStream;
vShaderStream << vertexShaderFile.rdbuf();
vertexShaderFile.close();
vertexCode = vShaderStream.str();
// Fragment
std::ifstream fragmentShaderFile(fShaderFile);
std::stringstream fShaderStream;
fShaderStream << fragmentShaderFile.rdbuf();
fragmentShaderFile.close();
fragmentCode = fShaderStream.str();
// Geometry
if (gShaderFile != nullptr)
{
std::ifstream geometryShaderFile(gShaderFile);
std::stringstream gShaderStream;
gShaderStream << geometryShaderFile.rdbuf();
geometryShaderFile.close();
geometryCode = gShaderStream.str();
}
}
catch (std::ifstream::failure e)
{
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ" << std::endl;
}
Shader shader;
shader.Compile(vertexCode.c_str(), fragmentCode.c_str(), gShaderFile != nullptr ? geometryCode.c_str() : nullptr);
return shader;
}
示例4: loadShaderFromFile
Shader resourceManagerClass::loadShaderFromFile(const GLchar *vShaderFile, const GLchar *fShaderFile, const GLchar *gShaderFile)
{
// 1. Retrieve the vertex/fragment source code from filePath
std::string vertexCode;
std::string fragmentCode;
std::string geometryCode;
try
{
// Open files
std::ifstream vertexShaderFile(vShaderFile);
std::ifstream fragmentShaderFile(fShaderFile);
std::stringstream vShaderStream, fShaderStream;
// Read file's buffer contents into streams
vShaderStream << vertexShaderFile.rdbuf();
fShaderStream << fragmentShaderFile.rdbuf();
// close file handlers
vertexShaderFile.close();
fragmentShaderFile.close();
// Convert stream into string
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
// If geometry shader path is present, also load a geometry shader
if (gShaderFile != nullptr)
{
std::ifstream geometryShaderFile(gShaderFile);
std::stringstream gShaderStream;
gShaderStream << geometryShaderFile.rdbuf();
geometryShaderFile.close();
geometryCode = gShaderStream.str();
}
}
catch (std::exception e)
{
std::cout << "ERROR::SHADER: Failed to read shader files" << std::endl;
}
const GLchar *vShaderCode = vertexCode.c_str();
const GLchar *fShaderCode = fragmentCode.c_str();
const GLchar *gShaderCode = geometryCode.c_str();
// 2. Now create shader object from source code
Shader shader;
shader.Compile(vShaderCode, fShaderCode, gShaderFile != nullptr ? gShaderCode : nullptr);
return shader;
}
示例5: vs_source
RectangleExample(void)
: vs(ShaderType::Vertex)
, fs(ShaderType::Fragment)
{
// this could be any istream
std::stringstream vs_source(
"#version 120\n"
"attribute vec2 Position;"
"attribute vec3 Color;"
"varying vec3 vertColor;"
"void main(void)"
"{"
" vertColor = Color;"
" gl_Position = vec4(Position, 0.0, 1.0);"
"}"
);
// set the vertex shader source
vs.Source(GLSLSource::FromStream(vs_source));
// compile it
vs.Compile();
std::stringstream fs_source(
"#version 120\n"
"varying vec3 vertColor;"
"void main(void)"
"{"
" gl_FragColor = vec4(vertColor, 1.0);"
"}"
);
// set the fragment shader source
fs.Source(GLSLSource::FromStream(fs_source));
// compile it
fs.Compile();
// attach the shaders to the program
prog.AttachShader(vs);
prog.AttachShader(fs);
// link it
prog.Link();
// and use it
gl.Program.Bind(prog);
// bind the VAO for the rectangle
gl.VertexArray.Bind(rectangle);
// bind the VBO for the rectangle vertices
if(auto x = gl.Buffer.Array.Push(verts))
{
GLfloat rectangle_verts[8] = {
-1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, -1.0f,
1.0f, 1.0f
};
// upload the data
gl.Buffer.Array.Data(8, rectangle_verts);
// setup the vertex attribs array for the vertices
VertexArrayAttrib vert_attr(prog, "Position");
vert_attr.Setup<Vec2f>().Enable();
}
// bind the VBO for the rectangle colors
if(auto x = gl.Buffer.Array.Push(colors))
{
GLfloat rectangle_colors[12] = {
1.0f, 1.0f, 1.0f,
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
};
// upload the data
gl.Buffer.Array.Data(12, rectangle_colors);
// setup the vertex attribs array for the vertices
VertexArrayAttrib color_attr(prog, "Color");
color_attr.Setup<Vec3f>().Enable();
}
gl.Caps.DepthTest.Disable();
}
示例6: attr
TorusExample(void)
: make_torus(1.0, 0.5, 36, 24)
, torus_instr(make_torus.Instructions())
, torus_indices(make_torus.Indices())
, make_plane(make_plane_builders())
, plane_instr(make_plane[0].Instructions())
, plane_indices(make_plane[0].Indices())
, torus_vs(ShaderType::Vertex, ObjectDesc("Torus vertex"))
, plane_vs(ShaderType::Vertex, ObjectDesc("Plane vertex"))
, torus_fs(ShaderType::Fragment, ObjectDesc("Torus fragment"))
, plane_fs(ShaderType::Fragment, ObjectDesc("Plane fragment"))
, torus_prog(ObjectDesc("Torus"))
, plane_prog(ObjectDesc("Plane"))
, plane_normal(plane_prog, "Normal")
, plane_camera_matrix(plane_prog, "CameraMatrix")
, torus_camera_matrix(torus_prog, "CameraMatrix")
, torus_model_matrix(torus_prog, "ModelMatrix")
, plane(make_plane.size())
, plane_positions(make_plane.size())
{
std::stringstream plane_count_def;
plane_count_def <<"#define PlaneCount "<<plane.size()<<'\n';
const GLchar* torus_vs_source[3] = {
"#version 330\n",
plane_count_def.str().c_str(),
"uniform mat4 ProjectionMatrix, ModelMatrix, CameraMatrix;"
"uniform float ClipSign[PlaneCount];"
"uniform vec4 ClipPlane[PlaneCount];"
"in vec4 Position;"
"in vec2 TexCoord;"
"out vec2 vertTexCoord;"
"void main(void)"
"{"
" vertTexCoord = TexCoord;"
" gl_Position = "
" ModelMatrix *"
" Position;"
" for(int p=0; p!=PlaneCount; ++p)"
" {"
" gl_ClipDistance[p] = "
" ClipSign[p]* "
" dot(ClipPlane[p], gl_Position);"
" }"
" gl_Position = "
" ProjectionMatrix *"
" CameraMatrix *"
" gl_Position;"
"}"
};
torus_vs.Source(torus_vs_source, 3);
torus_vs.Compile();
torus_fs.Source(
"#version 330\n"
"in vec2 vertTexCoord;"
"out vec4 fragColor;"
"void main(void)"
"{"
" float i = ("
" int(vertTexCoord.x*36) % 2+"
" int(vertTexCoord.y*24) % 2"
" ) % 2;"
" fragColor = vec4(1-i/2, 1-i/2, 1-i/2, 1.0);"
"}"
);
torus_fs.Compile();
torus_prog.AttachShader(torus_vs);
torus_prog.AttachShader(torus_fs);
torus_prog.Link();
torus_prog.Use();
// bind the VAO for the torus
torus.Bind();
// bind the VBO for the torus vertices
torus_positions.Bind(Buffer::Target::Array);
{
std::vector<GLfloat> data;
GLuint n_per_vertex = make_torus.Positions(data);
// upload the data
Buffer::Data(Buffer::Target::Array, data);
// setup the vertex attribs array for the vertices
VertexAttribArray attr(torus_prog, "Position");
attr.Setup(n_per_vertex, DataType::Float);
attr.Enable();
}
// bind the VBO for the torus UV-coordinates
torus_texcoords.Bind(Buffer::Target::Array);
{
std::vector<GLfloat> data;
GLuint n_per_vertex = make_torus.TexCoordinates(data);
// upload the data
Buffer::Data(Buffer::Target::Array, data);
// setup the vertex attribs array for the vertices
VertexAttribArray attr(torus_prog, "TexCoord");
attr.Setup(n_per_vertex, DataType::Float);
attr.Enable();
//.........这里部分代码省略.........
示例7: draw_attr
PickingExample(void)
: cube_instr(make_cube.Instructions())
, cube_indices(make_cube.Indices())
, vs(ShaderType::Vertex, ObjectDesc("Vertex"))
, gs(ShaderType::Geometry, ObjectDesc("Geometry"))
, fs(ShaderType::Fragment, ObjectDesc("Fragment"))
{
// Set the vertex shader source
vs.Source(
"#version 330\n"
"uniform mat4 ProjectionMatrix, CameraMatrix;"
"in vec4 Position;"
"out vec3 vertColor;"
"flat out int vertInstanceID;"
"void main(void)"
"{"
" float x = gl_InstanceID % 6 - 2.5;"
" float y = gl_InstanceID / 6 - 2.5;"
" mat4 ModelMatrix = mat4("
" 1.0, 0.0, 0.0, 0.0,"
" 0.0, 1.0, 0.0, 0.0,"
" 0.0, 0.0, 1.0, 0.0,"
" 2*x, 2*y, 0.0, 1.0 "
" );"
" gl_Position = "
" ProjectionMatrix *"
" CameraMatrix *"
" ModelMatrix *"
" Position;"
" vertColor = vec3("
" abs(normalize((ModelMatrix*Position).xy)),"
" 0.1"
" );"
" vertInstanceID = gl_InstanceID;"
"}"
);
vs.Compile();
// Set the geometry shader source
gs.Source(
"#version 330\n"
"layout(triangles) in;"
"layout(points, max_vertices = 1) out;"
"flat in int vertInstanceID[];"
"uniform vec2 MousePos;"
"out int geomInstanceID;"
"out float geomDepth;"
"vec2 barycentric_coords(vec4 a, vec4 b, vec4 c)"
"{"
// we'll need normalized device coordinates
// of the triangle vertices
" vec2 ad = vec2(a.x/a.w, a.y/a.w);"
" vec2 bd = vec2(b.x/b.w, b.y/b.w);"
" vec2 cd = vec2(c.x/c.w, c.y/c.w);"
" vec2 u = cd - ad;"
" vec2 v = bd - ad;"
" vec2 r = MousePos - ad;"
" float d00 = dot(u, u);"
" float d01 = dot(u, v);"
" float d02 = dot(u, r);"
" float d11 = dot(v, v);"
" float d12 = dot(v, r);"
" float id = 1.0 / (d00 * d11 - d01 * d01);"
" float ut = (d11 * d02 - d01 * d12) * id;"
" float vt = (d00 * d12 - d01 * d02) * id;"
" return vec2(ut, vt);"
"}"
"vec3 intersection(vec3 a, vec3 b, vec3 c, vec2 bc)"
"{"
" return (c - a)*bc.x + (b - a)*bc.y;"
"}"
"bool inside_triangle(vec2 b)"
"{"
" return ("
" (b.x >= 0.0) &&"
" (b.y >= 0.0) &&"
" (b.x + b.y <= 1.0)"
" );"
"}"
"void main(void)"
"{"
" vec2 bc = barycentric_coords("
" gl_in[0].gl_Position,"
" gl_in[1].gl_Position,"
" gl_in[2].gl_Position "
" );"
" if(inside_triangle(bc))"
" {"
" gl_Position = vec4(intersection("
" gl_in[0].gl_Position.xyz,"
" gl_in[1].gl_Position.xyz,"
" gl_in[2].gl_Position.xyz,"
" bc"
" ), 1.0);"
//.........这里部分代码省略.........
示例8: attr
SmokeExample(void)
: emitters(
{
{
{
{-20.0f, -10.0f, 10.0f},
{ 20.0f, 0.0f, -20.0f},
{ 20.0f, 10.0f, 20.0f},
{-20.0f, 0.0f, -10.0f}
}, 15.0, 200.0
},
{
{
{ 30.0f, 0.0f, 5.0f},
{-30.0f, 0.0f, -5.0f},
{-20.0f, 20.0f, 5.0f},
{ 20.0f, -10.0f, -5.0f}
}, 17.0, 200.0
},
}
), vs(ShaderType::Vertex, "Vertex")
, gs(ShaderType::Geometry, "Geometry")
, fs(ShaderType::Fragment, "Fragment")
, projection_matrix(prog, "ProjectionMatrix")
, camera_matrix(prog, "CameraMatrix")
, light_cam_pos(prog, "LightCamPos")
{
// Set the vertex shader source
vs.Source(
"#version 330\n"
"uniform mat4 CameraMatrix;"
"in vec4 Position;"
"in float Age;"
"in int Id;"
"out float vertAge;"
"out int vertId;"
"void main(void)"
"{"
" gl_Position = CameraMatrix * Position;"
" vertAge = Age;"
" vertId = Id;"
"}"
);
// compile it
vs.Compile();
// Set the geometry shader source
gs.Source(
"#version 330\n"
"layout(points) in;"
"layout(triangle_strip, max_vertices = 4) out;"
"uniform vec3 LightCamPos;"
"uniform mat4 ProjectionMatrix;"
"in float vertAge[];"
"in int vertId[];"
"out vec2 geomTexCoord;"
"out float geomAge;"
"out float geomLightVal;"
"out float geomLightBias;"
"void main(void)"
"{"
" if(vertAge[0] > 1.0) return;"
" vec3 pos = gl_in[0].gl_Position.xyz;"
" vec3 lightDir = normalize(LightCamPos - pos);"
" float s = 0.8, g = 3.0;"
" float yo[2] = float[2](-1.0, 1.0);"
" float xo[2] = float[2](-1.0, 1.0);"
" float angle = vertId[0];"
" float cx = cos(angle), sx = sin(angle);"
" mat2 rot = mat2(cx, sx, -sx, cx);"
" for(int j=0;j!=2;++j)"
" for(int i=0;i!=2;++i)"
" {"
" float xoffs = xo[i]*(1.0+vertAge[0]*g)*s;"
" float yoffs = yo[j]*(1.0+vertAge[0]*g)*s;"
" vec2 offs = rot*vec2(xoffs, yoffs);"
" gl_Position = ProjectionMatrix * vec4("
" pos.x-offs.x,"
" pos.y-offs.y,"
" pos.z,"
" 1.0"
" );"
" geomTexCoord = vec2(float(i), float(j));"
" geomAge = vertAge[0];"
" geomLightVal = lightDir.z;"
" geomLightBias = -dot("
" normalize(vec3(offs, 0.0)),"
" lightDir"
" );"
" EmitVertex();"
" }"
" EndPrimitive();"
"}"
);
// compile it
gs.Compile();
// set the fragment shader source
fs.Source(
"#version 330\n"
//.........这里部分代码省略.........
示例9: shape_attr
ShadowVolExample(const ExampleParams& params)
: shape_instr(make_shape.Instructions())
, shape_indices(make_shape.Indices())
, shape_vs(ShaderType::Vertex, ObjectDesc("Shape vertex"))
, depth_vs(ShaderType::Vertex, ObjectDesc("Depth vertex"))
, light_vs(ShaderType::Vertex, ObjectDesc("Light vertex"))
, depth_gs(ShaderType::Geometry, ObjectDesc("Depth geometry"))
, light_gs(ShaderType::Geometry, ObjectDesc("Light geometry"))
, shape_fs(ShaderType::Fragment, ObjectDesc("Shape fragment"))
, depth_fs(ShaderType::Fragment, ObjectDesc("Depthfragment"))
, light_fs(ShaderType::Fragment, ObjectDesc("Light fragment"))
, shape_prog(ObjectDesc("Shape"))
, depth_prog(ObjectDesc("Depth"))
, light_prog(ObjectDesc("Light"))
, tex_side(128)
, sample_count(params.HighQuality()?1024:128)
{
shape_vs.Source(
"#version 330\n"
"uniform mat4 ProjectionMatrix, CameraMatrix, ModelMatrix;"
"in vec4 Position;"
"in vec3 Normal;"
"in vec2 TexCoord;"
"out vec3 vertNormal;"
"out vec3 vertLightDir;"
"out vec3 vertLightRefl;"
"out vec3 vertViewDir;"
"out vec3 vertViewRefl;"
"uniform vec3 LightPos;"
"void main(void)"
"{"
" gl_Position = ModelMatrix * Position;"
" vertNormal = mat3(ModelMatrix)*Normal;"
" vertLightDir = LightPos - gl_Position.xyz;"
" vertLightRefl = reflect("
" -normalize(vertLightDir),"
" normalize(vertNormal)"
" );"
" vertViewDir = (vec4(0.0, 0.0, 1.0, 1.0)* CameraMatrix).xyz;"
" vertViewRefl = reflect("
" normalize(vertViewDir),"
" normalize(vertNormal)"
" );"
" gl_Position = ProjectionMatrix * CameraMatrix * gl_Position;"
"}"
);
shape_vs.Compile();
shape_fs.Source(
"#version 330\n"
"in vec3 vertNormal;"
"in vec3 vertLightDir;"
"in vec3 vertLightRefl;"
"in vec3 vertViewDir;"
"in vec3 vertViewRefl;"
"out vec4 fragColor;"
"void main(void)"
"{"
" float l = length(vertLightDir);"
" float d = dot("
" normalize(vertNormal), "
" normalize(vertLightDir)"
" ) / l;"
" float s = dot("
" normalize(vertLightRefl),"
" normalize(vertViewDir)"
" );"
" vec3 ambi = vec3(0.6, 0.3, 0.5);"
" vec3 diff = vec3(0.9, 0.7, 0.8);"
" vec3 spec = vec3(1.0, 0.9, 0.95);"
" fragColor = vec4("
" ambi * 0.3 + "
" diff * 0.7 * max(d, 0.0) + "
" spec * pow(max(s, 0.0), 64), "
" 1.0"
" );"
"}"
);
shape_fs.Compile();
shape_prog.AttachShader(shape_vs);
shape_prog.AttachShader(shape_fs);
shape_prog.Link();
depth_vs.Source(
"#version 330\n"
"uniform mat4 ModelMatrix;"
"uniform vec3 LightPos;"
"in vec4 Position;"
"void main(void)"
"{"
" gl_Position = "
" mat4("
" 1.0, 0.0, 0.0, -LightPos.x,"
" 0.0, 1.0, 0.0, -LightPos.y,"
" 0.0, 0.0, 1.0, -LightPos.z,"
" 0.0, 0.0, 0.0, 1.0"
" )*"
" ModelMatrix *"
" mat4("
//.........这里部分代码省略.........
示例10: attr
VolLightExample(void)
: volume_vs(ShaderType::Vertex, ObjectDesc("Volume vertex"))
, plane_vs(ShaderType::Vertex, ObjectDesc("Plane vertex"))
, volume_gs(ShaderType::Geometry, ObjectDesc("Volume geometry"))
, volume_fs(ShaderType::Fragment, ObjectDesc("Volume fragment"))
, plane_fs(ShaderType::Fragment, ObjectDesc("Plane fragment"))
, volume_prog(ObjectDesc("Volume"))
, plane_prog(ObjectDesc("Plane"))
, samples(150)
{
volume_vs.Source(
"#version 330\n"
"in vec4 Position;"
"out float vertZOffs;"
"uniform int SampleCount;"
"uniform mat4 CameraMatrix;"
"uniform vec3 ViewZ;"
"uniform float Size;"
"void main(void)"
"{"
" float hp = (SampleCount-1) * 0.5;"
" vertZOffs = (gl_InstanceID - hp)/hp;"
" gl_Position = vec4("
" Position.xyz +"
" ViewZ*vertZOffs*Size*0.5,"
" 1.0"
" );"
"}"
);
volume_vs.Compile();
volume_gs.Source(
"#version 330\n"
"layout(points) in;"
"layout(triangle_strip, max_vertices = 4) out;"
"uniform mat4 CameraMatrix, ProjectionMatrix;"
"uniform mat4 TexProjectionMatrix;"
"uniform vec3 ViewX, ViewY;"
"uniform float Size;"
"in float vertZOffs[];"
"out vec4 geomTexCoord;"
"void main(void)"
"{"
" float zo = vertZOffs[0];"
" float yo[2] = float[2](-1.0, 1.0);"
" float xo[2] = float[2](-1.0, 1.0);"
" for(int j=0;j!=2;++j)"
" for(int i=0;i!=2;++i)"
" {"
" vec4 v = vec4("
" gl_in[0].gl_Position.xyz+"
" ViewX * xo[i] * Size+"
" ViewY * yo[j] * Size,"
" 1.0"
" );"
" geomTexCoord = "
" TexProjectionMatrix *"
" v;"
" gl_Position = "
" ProjectionMatrix *"
" CameraMatrix * v;"
" EmitVertex();"
" }"
" EndPrimitive();"
"}"
);
volume_gs.Compile();
volume_fs.Source(
"#version 330\n"
"uniform sampler2D LightTex;"
"uniform int SampleCount;"
"in vec4 geomTexCoord;"
"out vec4 fragColor;"
"void main(void)"
"{"
" vec2 coord = geomTexCoord.st/geomTexCoord.q;"
" float depth = geomTexCoord.z;"
" if(depth < 0.0) discard;"
" vec4 t = texture(LightTex, coord*0.5 + 0.5);"
" if(t.a == 0.0) discard;"
" float alpha = 10.0*(1.0-t.a)/SampleCount;"
" alpha *= (t.r+t.g+t.b)*0.3333;"
" alpha /= sqrt(depth);"
" fragColor = vec4(t.rgb, alpha);"
"}"
);
volume_fs.Compile();
volume_prog.AttachShader(volume_vs);
volume_prog.AttachShader(volume_gs);
volume_prog.AttachShader(volume_fs);
volume_prog.Link();
volume_prog.Use();
// bind the VAO for the volumes
volume.Bind();
// bind the VBO for the volume positions
volume_pos.Bind(Buffer::Target::Array);
//.........这里部分代码省略.........
示例11: GetShader
IShader* ShaderManager::GetShader( std::string shaderName, u32 shaderStages, std::vector<std::string> shaderFlags ) {
u32 shaderHash = HashStringCRC32( shaderName + TextFileToString( shaderFlags ) );
shaderHash ^= shaderStages;
std::unordered_map<u32, IShader*>::const_iterator it = m_CachedShaders.find( shaderHash );
if( it != m_CachedShaders.end() ) {
return it->second;
}
Shader* shader = new Shader();
#if defined( FLAG_DEBUG )
shader_watch_t watch = {
shaderName,
shaderStages,
shaderFlags,
shader
};
m_WatchedShaders[shaderName].push_back( watch );
#endif
if( shaderStages & SHADER_STAGE_COMPUTE ) {
shader->AddStage( uberShaderFolder + shaderName +
stageComputeExtension, GL_COMPUTE_SHADER,
&shaderFlags );
} else {
if( shaderStages & SHADER_STAGE_VERTEX ) {
shader->AddStage( uberShaderFolder + shaderName +
stageVertexExtension, GL_VERTEX_SHADER,
&shaderFlags );
}
if( shaderStages & SHADER_STAGE_TESS_CONTROL ) {
shader->AddStage( uberShaderFolder + shaderName +
stageTessControlExtension, GL_TESS_CONTROL_SHADER,
&shaderFlags );
}
if( shaderStages & SHADER_STAGE_TESS_EVAL ) {
shader->AddStage( uberShaderFolder + shaderName +
stageTessEvalExtension, GL_TESS_EVALUATION_SHADER,
&shaderFlags );
}
if( shaderStages & SHADER_STAGE_GEOMETRY ) {
shader->AddStage( uberShaderFolder + shaderName +
stageGeometryExtension, GL_GEOMETRY_SHADER,
&shaderFlags );
}
if( shaderStages & SHADER_STAGE_FRAGMENT ) {
shader->AddStage( uberShaderFolder + shaderName +
stageFragmentExtension, GL_FRAGMENT_SHADER,
&shaderFlags );
}
}
if( !shader->Compile() ) {
CONSOLE_PRINT_ERROR( "%s => Failed to compile shader %s\n",
__FUNCTION__,
shaderName.c_str() );
SAFE_DELETE( shader )
return m_DefaultShader;
}
示例12: main
int main(){
try{
ILogger::Init();
Settings::Call().Parse();
ResourceManager::Call().AddPath("Data/shaders", "Shader");
ResourceManager::Call().AddPath("Data/textures", "Image");
{
Window myWindow;
Image Crate;
Texture CrateTexture;
Text FPSText, MousePosText;
Clock FrameClock, FpsClock;
Input myInput;
myInput.Init(myWindow);
Renderer& myRenderer = Renderer::Call();
myRenderer.Init(myWindow);
Crate.LoadFromFile("crate.jpg");
CrateTexture.LoadFromImage(Crate);
Light l;
l.SetPosition(Vector3F(1,3,1.5));
l.SetDiffuse(Color(1.f,1.f,1.f));
l.SetRange(8);
Shader ColorShader;
ColorShader.Compile("shaderColor.vs", "shaderColor.fs");
ColorShader.Bind();
ColorShader.SendColor("ambientColor", myRenderer.GetSpecifications().mAmbientColor);
ColorShader.SendFloat("constantAtt", l.GetAttenuationConstant());
ColorShader.SendFloat("linearAtt", l.GetAttenuationLinear());
ColorShader.SendFloat("quadraticAtt", l.GetAttenuationQuadratic());
ColorShader.SendFloat("range", l.GetRange());
ColorShader.SendVector3("lightPosition", l.GetPosition());
ColorShader.SendColor("lightColor", l.GetDiffuse());
ColorShader.UnBind();
Object obj1;
obj1.MakeCube("cube", ColorShader);
obj1.GetMaterial().mAmbient = Color(0.f, 0.08f, 0.08f);
obj1.GetMaterial().mDiffuse = Color(0.f, 0.8f, 0.8f);
obj1.GetMaterial().mSpecular = Color(0.0f, 0.5f, 0.5f);
obj1.GetMaterial().mShininess = 50.f;
Camera cam;
cam.LookAt(Vector3F(0.5f,0,1), Vector3F(-2.5f,2,4));
FPSText.SetSize(12);
FPSText.SetPosition(10,10);
MousePosText.SetSize(12);
MousePosText.SetPosition(10,22);
while(myWindow.IsOpened()){
ElapsedTime = FrameClock.GetElapsedTime();
FrameClock.Reset();
if(FpsClock.GetElapsedTime() > 1.f){
FPSText.SetText(String(1.f/ElapsedTime));
FpsClock.Reset();
}
while(myInput.GetEvent()){
if(myInput.GetEventType() == sf::Event::Closed)
myWindow.Close();
if(myInput.IsKeyHit(Space))
if(!paused){
paused = true;
FrameClock.Pause();
}else{
paused = false;
FrameClock.Resume();
}
}
MousePosText.SetText(String("X : ")+myInput.GetMouseX()+" Y : "+myInput.GetMouseY());
MousePosText.Draw();
FPSText.Draw();
obj1.Draw();
myRenderer.BeginScene(myRenderer.GetSpecifications().mAmbientColor);
myRenderer.Render();
myRenderer.EndScene();
}
}
}catch(Exception e){
std::cout << e.what() << std::endl;
system("PAUSE");
}
Renderer::Kill();
ResourceManager::Kill();
Settings::Kill();
ILogger::Kill();
//.........这里部分代码省略.........
示例13: vs_source
RectangleExample(void)
: vs(ShaderType::Vertex)
, fs(ShaderType::Fragment)
{
// this could be any istream
std::stringstream vs_source(
"#version 330\n"
"in vec2 Position;"
"in vec3 Color;"
"out vec3 vertColor;"
"void main(void)"
"{"
" vertColor = Color;"
" gl_Position = vec4(Position, 0.0, 1.0);"
"}"
);
// set the vertex shader source
vs.Source(GLSLSource::FromStream(vs_source));
// compile it
vs.Compile();
std::stringstream fs_source(
"#version 330\n"
"in vec3 vertColor;"
"out vec4 fragColor;"
"void main(void)"
"{"
" fragColor = vec4(vertColor, 1.0);"
"}"
);
// set the fragment shader source
fs.Source(GLSLSource::FromStream(fs_source));
// compile it
fs.Compile();
// attach the shaders to the program
prog.AttachShader(vs);
prog.AttachShader(fs);
// link and use it
prog.Link();
prog.Use();
// bind the VAO for the rectangle
rectangle.Bind();
GLfloat rectangle_verts[8] = {
-1.0f, -1.0f,
-1.0f, 1.0f,
1.0f, -1.0f,
1.0f, 1.0f
};
// bind the VBO for the rectangle vertices
verts.Bind(Buffer::Target::Array);
// upload the data
Buffer::Data(Buffer::Target::Array, 8, rectangle_verts);
// setup the vertex attribs array for the vertices
VertexAttribArray vert_attr(prog, "Position");
vert_attr.Setup<Vec2f>().Enable();
GLfloat rectangle_colors[12] = {
1.0f, 1.0f, 1.0f,
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
};
// bind the VBO for the rectangle colors
colors.Bind(Buffer::Target::Array);
// upload the data
Buffer::Data(Buffer::Target::Array, 12, rectangle_colors);
// setup the vertex attribs array for the vertices
VertexAttribArray color_attr(prog, "Color");
color_attr.Setup<Vec3f>().Enable();
//
gl.Disable(Capability::DepthTest);
}