static Program make(void) { Program prog; VertexShader vs; vs.Source( "#version 330\n" "uniform mat4 ProjectionMatrix, CameraMatrix;" "layout (std140) uniform OffsetBlock {vec3 Offset[16*16*16];};" "in vec3 Position;" "void main(void)" "{" " gl_Position = " " ProjectionMatrix *" " CameraMatrix *" " vec4(Position+Offset[gl_InstanceID], 1.0);" "}" ).Compile(); FragmentShader fs; fs.Source( "#version 330\n" "out float fragValue;" "void main(void)" "{" " fragValue = 1.0/16.0;" "}" ).Compile(); prog.AttachShader(vs).AttachShader(fs).Link().Use(); return std::move(prog); }
static Program make(void) { VertexShader vs; vs.Source( "#version 330\n" "uniform mat4 CameraMatrix,ModelMatrix;" "in vec4 Position;" "void main(void)" "{" " gl_Position = CameraMatrix * ModelMatrix * Position;" "}" ).Compile(); FragmentShader fs; fs.Source( "#version 330\n" "void main(void)" "{" "}" ).Compile(); Program prog; prog << vs << fs; prog.Link().Use(); return std::move(prog); }
void ShaderManager<RS>::init() { //construct safe default shader VertexShader<RS>* dv = 0; FragmentShader<RS>* df = 0; dv = new VertexShader<RS>(); df = new FragmentShader<RS>(); Vertex def; def.addVector3("POSITION"); def.finalize(); dv->associateVertex(def); if(RS == RS_DX11) { dv->initFromDef(Defaults::shaderDefDX, Defaults::vertexFunc); df->initFromDef(Defaults::shaderDefDX, Defaults::fragmentFunc); } else { dv->initFromDef(Defaults::shaderDefGL, Defaults::vertexFunc); df->initFromDef(Defaults::shaderDefGL, Defaults::fragmentFunc); } vShaders[Defaults::mapName][Defaults::mapName] = dv; fShaders[Defaults::mapName][Defaults::mapName] = df; }
static Program make(void) { Program prog; VertexShader vs; vs.Source( "#version 330\n" "uniform vec2 ScreenSize;" "in vec4 Position;" "in vec2 TexCoord;" "out vec2 vertTexCoord;" "void main(void)" "{" " gl_Position = Position;" " vertTexCoord = TexCoord*ScreenSize;" "}" ).Compile(); FragmentShader fs; fs.Source( "#version 330\n" "uniform sampler1D Palette;" "uniform sampler2DRect Tex;" "in vec2 vertTexCoord;" "out vec4 fragColor;" "void main(void)" "{" " float Overdraw = texture(Tex, vertTexCoord).r;" " fragColor = texture(Palette, Overdraw);" "}" ).Compile(); prog.AttachShader(vs).AttachShader(fs).Link().Use(); return std::move(prog); }
static Program make_prog(void) { Program prog; VertexShader vs; vs.Source( "#version 330\n" "uniform mat4 ProjectionMatrix, CameraMatrix, ModelMatrix;" "mat4 Matrix = ProjectionMatrix*CameraMatrix*ModelMatrix;" "in vec4 Position;" "void main(void)" "{" " gl_Position = Matrix*Position;" "}" ); FragmentShader fs; fs.Source( "#version 330\n" "void main(void){ }" ); prog.AttachShader(vs).AttachShader(fs); prog.Link().Validate().Use(); return std::move(prog); }
// makes a fragment shader from the prologe, custom part and epilogue static FragmentShader make_fs(StrCRef color_fs) { FragmentShader shader; StrCRef source[3] = {fs_prologue(), color_fs, fs_epilogue()}; shader.Source(source); shader.Compile(); return shader; }
ShaderProgram::ShaderProgram( const string &filename, const VertexShader &vertexShader, const FragmentShader &fragmentShader) : _name(filename), _handle(0), _linked(0) { try { if (vertexShader.fail()) throw std::runtime_error("Invalid vertex shader"); if (fragmentShader.fail()) throw std::runtime_error("Invalid fragment shader"); if (GLEW_VERSION_2_0) { // Create program and attach vertex and fragment shaders _handle = glCreateProgram(); glAttachShader(_handle, vertexShader.handle()); glAttachShader(_handle, fragmentShader.handle()); // Perform link stage glLinkProgram(_handle); glGetProgramiv(_handle, GL_LINK_STATUS, &_linked); // Validate program glValidateProgram(_handle); printInfoLog(); // Check for success if (GL_FALSE == _linked) throw std::runtime_error("Link stage failed"); } else if (GLEW_VERSION_1_5) { // Create program and attach vertex and fragment shaders _handle = glCreateProgramObjectARB(); glAttachObjectARB(_handle, vertexShader.handle()); glAttachObjectARB(_handle, fragmentShader.handle()); // Perform link stage glLinkProgramARB(_handle); glGetObjectParameterivARB(_handle, GL_OBJECT_LINK_STATUS_ARB, &_linked); // Validate program glValidateProgramARB(_handle); printInfoLog(); // Check for success if (GL_FALSE == _linked) throw std::runtime_error("Link stage failed"); } cout << "* Shader \"" << _name << "\" successfully linked" << endl; } catch (std::runtime_error &err) { cerr << "Error: Faild to create shader " << _name << endl; cerr << "Reason: " << err.what() << endl; } }
static Program make_prog(void) { VertexShader vs; vs.Source( "#version 130\n" "uniform mat4 ProjectionMatrix, ModelMatrix, CameraMatrix;" "uniform vec4 ClipPlane;" "attribute vec4 Position;" "attribute vec2 TexCoord;" "varying vec2 vertTexCoord;" "void main(void)" "{" " vertTexCoord = TexCoord;" " gl_Position = " " ModelMatrix *" " Position;" " gl_ClipDistance[0] = dot(ClipPlane, gl_Position);" " gl_Position = " " ProjectionMatrix *" " CameraMatrix *" " gl_Position;" "}" ); vs.Compile(); FragmentShader fs; fs.Source( "#version 130\n" "varying vec2 vertTexCoord;" "void main(void)" "{" " float i = (" " int(vertTexCoord.x*36) % 2+" " int(vertTexCoord.y*24) % 2" " ) % 2;" " if(gl_FrontFacing)" " {" " gl_FragColor = vec4(1-i/2, 1-i/2, 1-i/2, 1.0);" " }" " else" " {" " gl_FragColor = vec4(0+i/2, 0+i/2, 0+i/2, 1.0);" " }" "}" ); fs.Compile(); Program prog; prog.AttachShader(vs); prog.AttachShader(fs); prog.Link(); prog.Use(); return prog; }
/** * @brief * Creates a fragment shader and sets the shader source code */ FragmentShader *ShaderLanguage::CreateFragmentShader(const String &sSourceCode, const String &sProfile, const String &sArguments, const String &sEntry) { // Create the fragment shader instance FragmentShader *pFragmentShader = CreateFragmentShader(); // Set the fragment shader source code if (pFragmentShader) pFragmentShader->SetSourceCode(sSourceCode, sProfile, sArguments, sEntry); // Return the created fragment shader instance return pFragmentShader; }
HRESULT DX9ShaderIncludeLoader::Open( D3DXINCLUDE_TYPE IncludeType, LPCSTR pFileName, LPCVOID pParentData, LPCVOID *ppData, UINT *pBytes ) { ResourcesManager& resMgr = ResourcesManager::getInstance(); FragmentShader* res = resMgr.create< FragmentShader >( FilePath( pFileName ) ); const std::string& script = res->getScript(); *ppData = (LPCVOID)script.c_str(); *pBytes = script.length(); return S_OK; }
ShaderProgram::ShaderProgram(const char *pFragmentFile, const char *pVertexFile) { Init(); FragmentShader *pFragmentShader = new FragmentShader(pFragmentFile); VertexShader *pVertexShader = new VertexShader(pVertexFile); SetFragmentShader(pFragmentShader); SetVertexShader(pVertexShader); pFragmentShader->Release(); pVertexShader->Release(); CreateProgram(); }
static oglplus::Program make_prog(void) { using namespace oglplus; Program prog; VertexShader vs; vs.Source( "#version 140\n" "uniform mat4 ProjectionMatrix, CameraMatrix, ModelMatrix;" "uniform vec3 LightPos;" "in vec4 Position;" "in vec3 Normal;" "in vec2 TexCoord;" "out vec3 vertNormal;" "out vec3 vertLight;" "out vec2 vertTexCoord;" "void main(void)" "{" " vertNormal = mat3(ModelMatrix)*Normal;" " gl_Position = ModelMatrix * Position;" " vertLight = LightPos - gl_Position.xyz;" " vertTexCoord = TexCoord;" " gl_Position = ProjectionMatrix * CameraMatrix * gl_Position;" "}" ).Compile(); FragmentShader fs; fs.Source( "#version 140\n" "uniform sampler2D Checker;" "in vec3 vertNormal;" "in vec3 vertLight;" "in vec2 vertTexCoord;" "out vec4 fragColor;" "void main(void)" "{" " float d = dot(vertNormal, normalize(vertLight));" " float i = 0.4 + 1.4*max(d, 0.0);" " vec4 t = texture(Checker, vertTexCoord);" " fragColor = vec4(t.rrr*i, 1.0);" "}" ).Compile(); prog.AttachShader(vs).AttachShader(fs).Link().Use(); return prog; }
UserInterface::UserInterface(float inRange) : mRange(inRange), mRatio(0.0f), mMouseX(0.0f), mMouseY(0.0f), mMouseOverWidget(NULL), mClickCandidate(NULL), mModelViewProjection(mMatrix) { if (mRange < 1.0f) mRange = 1.0f; VertexShader vs; const char* vertexShader = "#version 120\n" "uniform mat4 uMVPM;\n" "attribute vec2 iPosition;\n" "attribute vec2 iTexture;\n" "varying vec2 vTexture;\n" "void main(void)\n" "{\n" " vTexture = iTexture;\n" " vec4 p = vec4(iPosition, 0.0, 1.0);\n" " gl_Position = uMVPM * p;\n" "}\n" ; vs.loadFromBuffer(vertexShader); FragmentShader fs; const char* fragmentShader = "#version 120\n" "uniform sampler2D uTexture;\n" "varying vec2 vTexture;\n" "void main(void)\n" "{\n" " gl_FragColor = texture2D(uTexture, vTexture);\n" "}\n" ; fs.loadFromBuffer(fragmentShader); mProgram.attachShader(vs); mProgram.attachShader(fs); mProgram.bindAttribLocation(0, "iVertex"); mProgram.bindAttribLocation(1, "iTexture"); mProgram.link(); mUniMVPM = mProgram.getUniformLocation("uMVPM"); mUniTexture = mProgram.getUniformLocation("uTexture"); glUniform1i(mUniTexture, 0); }
static Program make_prog(void) { VertexShader vs; vs.Source( "#version 330\n" "in vec4 Position;" "in vec3 Normal;" "out vec3 vertColor;" "out vec3 vertNormal;" "out vec3 vertLight;" "uniform mat4 ProjectionMatrix, CameraMatrix, ModelMatrix;" "uniform vec3 LightPos;" "void main(void)" "{" " gl_Position = ModelMatrix * Position;" " vertColor = abs(normalize(Normal+vec3(1, 1, 1)));" " vertNormal = mat3(ModelMatrix)*Normal;" " vertLight = LightPos - gl_Position.xyz;" " gl_Position = ProjectionMatrix * CameraMatrix * gl_Position;" "}" ); vs.Compile(); FragmentShader fs; fs.Source( "#version 330\n" "in vec3 vertColor;" "in vec3 vertNormal;" "in vec3 vertLight;" "out vec4 fragColor;" "void main(void)" "{" " float l = dot(vertLight, vertLight);" " float d = l > 0.0 ? dot(vertNormal, normalize(vertLight)) / l : 0.0;" " float i = 0.2 + max(d*3.2, 0.0);" " fragColor = vec4(vertColor*i, 1.0);" "}" ); fs.Compile(); Program prog; prog.AttachShader(vs); prog.AttachShader(fs); prog.Link(); return prog; }
FragmentShader<RS>* ShaderManager<RS>::getFragmentShader(const String& file, const String& function) { FragmentShader<RS>* out = 0; auto f = fShaders.find(file); if(f != fShaders.end()) { auto fn = (*f).second.find(function); if(fn != (*f).second.end()) { out = (*fn).second; } else { out = new FragmentShader<RS>(); try { out->Shader::init(file, function); fShaders[file][function] = out; } catch(DeviceFailureException e) { LOG(e.what()); out->destroy(); out = fShaders[Defaults::mapName][Defaults::mapName]; } } } else { fShaders[file] = HashMap<String,FragmentShader<RS>*>(); out = new FragmentShader<RS>(); try { out->Shader::init(file, function); fShaders[file][function] = out; } catch(DeviceFailureException e) { LOG(e.what()); out->destroy(); out = fShaders[Defaults::mapName][Defaults::mapName]; } } return out; }
static Program make_face_prog(void) { FragmentShader fs; fs.Source( "#version 330\n" "in vec3 geomNormal;" "in vec3 geomLight;" "in float geomGlow;" "flat in int geomTop;" "uniform vec3 TopColor, SideColor;" "const vec3 LightColor = vec3(1.0, 1.0, 1.0);" "out vec4 fragColor;" "void main(void)" "{" " float d = max(dot(" " normalize(geomLight)," " normalize(geomNormal)" " ), 0.0);" " vec3 color;" " if(geomTop != 0)" " {" " color = TopColor * d +" " LightColor * pow(d, 8.0);" " }" " else" " {" " color = SideColor * geomGlow +" " LightColor *" " pow(d, 2.0) * 0.2;" " }" " fragColor = vec4(color, 1.0);" "}" ); fs.Compile(); Program prog; prog.AttachShader(fs); prog.MakeSeparable(); prog.Link(); ProgramUniform<Vec3f>(prog, "TopColor").Set(0.2f, 0.2f, 0.2f); ProgramUniform<Vec3f>(prog, "SideColor").Set(0.9f, 0.9f, 0.2f); return prog; }
//----------------------------------------------------------------------------------------------------------------------------------------------------- Ptr<GPUProgram> Context::CreateGPUProgram( LPCTSTR VertexShaderSource, LPCTSTR FragmentShaderSource ) { VertexShader* pVertexShader = 0; FragmentShader* pFragmentShader = 0; if (VertexShaderSource) { pVertexShader = new VertexShader(this); pVertexShader->Compile(VertexShaderSource); } if (FragmentShaderSource) { pFragmentShader = new FragmentShader(this); pFragmentShader->Compile(FragmentShaderSource); } return CreateGPUProgram( pVertexShader, pFragmentShader ); }
static Program make_frame_prog(void) { FragmentShader fs; fs.Source( "#version 330\n" "out vec4 fragColor;" "void main(void)" "{" " fragColor = vec4(0.2, 0.1, 0.0, 1.0);" "}" ); fs.Compile(); Program prog; prog.AttachShader(fs); prog.MakeSeparable(); prog.Link(); return prog; }
static Program make(void) { Program result; VertexShader vs; vs.Source( "#version 330\n" "#define side 128\n" "uniform mat4 ProjectionMatrix, CameraMatrix;" "uniform float Fade;" "uniform sampler2D Offsets;" "uniform sampler2D Heights;" "in vec4 Position;" "void main(void)" "{" " ivec2 Coord = ivec2(gl_InstanceID%side, gl_InstanceID/side);" " vec2 Offs = texelFetch(Offsets, Coord, 0).xy;" " float Height = 1.0-texelFetch(Heights, Coord, 0).r;" " gl_Position = Position;" " gl_Position.xz += Offs;" " gl_Position.y *= max(Height*Fade*side/2, 0.5);" " gl_Position = ProjectionMatrix * CameraMatrix * gl_Position;" "}" ).Compile(); FragmentShader fs; fs.Source( "#version 330\n" "void main(void) { }" ).Compile(); result.AttachShader(vs).AttachShader(fs); result.Link().Validate().Use(); return std::move(result); }
bool ShaderLoader::Load(ShaderResource** resource, Handle handle, const std::string& filename1, const std::string& filename2) { *resource = new ShaderResource(handle, filename1); Program* shader = new Program(); (*resource)->mRaw = shader; VertexShader vs; vs.Source(readFile(filename1)); vs.Compile(); FragmentShader fs; fs.Source(readFile(filename2)); fs.Compile(); shader->AttachShader(vs).AttachShader(fs); shader->BindAttribute(VertexAttributes::POSITION, "in_Position"); shader->BindAttribute(VertexAttributes::NORMAL, "in_Normal"); shader->BindAttribute(VertexAttributes::TANGENT, "in_Tangent"); shader->BindAttribute(VertexAttributes::TEXCOORD, "in_TexCoords"); shader->Link(); return true; }
Program * ProgramFromShaderMap(const map<string, string> &mapShdString, const string &root) { VertexShader vs; FragmentShader fs; Program *prog = new Program(); string defS("#version 420\n"); defS.append("#define MAX_BONES "); defS.append(ConvertIntString(G_MAX_BONES_UNIFORM)); defS.append("\n"); defS.append("#define MAX_BONES_INFL "); defS.append(ConvertIntString(G_MAX_BONES_INFLUENCING)); defS.append("\n"); string vsSrc(defS); vsSrc.append(mapShdString.at(string("vs").append(root))); string fsSrc(defS); fsSrc.append(mapShdString.at(string("fs").append(root))); vs.Source(vsSrc); fs.Source(fsSrc); vs.Compile(); fs.Compile(); prog->AttachShader(vs); prog->AttachShader(fs); prog->Link(); return prog; }
TorusExample(void) : make_torus(1.0, 0.5, 72, 48) , torus_instr(make_torus.Instructions()) , torus_indices(make_torus.Indices()) , projection_matrix(prog, "ProjectionMatrix") , camera_matrix(prog, "CameraMatrix") , model_matrix(prog, "ModelMatrix") { // Set the vertex shader source and compile it vs.Source( "#version 330\n" "uniform mat4 ProjectionMatrix, CameraMatrix, ModelMatrix;" "in vec4 Position;" "in vec3 Normal;" "out vec3 vertNormal;" "void main(void)" "{" " vertNormal = mat3(ModelMatrix)*Normal;" " gl_Position = " " ProjectionMatrix *" " CameraMatrix *" " ModelMatrix *" " Position;" "}" ).Compile(); // set the fragment shader source and compile it fs.Source( "#version 330\n" "in vec3 vertNormal;" "out vec4 fragColor;" "uniform vec3 LightPos;" "void main(void)" "{" " float intensity = 2.0 * max(" " dot(vertNormal, LightPos)/" " length(LightPos)," " 0.0" " );" " if(!gl_FrontFacing)" " fragColor = vec4(0.0, 0.0, 0.0, 1.0);" " else if(intensity > 0.9)" " fragColor = vec4(1.0, 0.9, 0.8, 1.0);" " else if(intensity > 0.1)" " fragColor = vec4(0.7, 0.6, 0.4, 1.0);" " else" " fragColor = vec4(0.3, 0.2, 0.1, 1.0);" "}" ).Compile(); // attach the shaders to the program prog << vs << fs; // link and use it prog.Link().Use(); // bind the VAO for the torus torus.Bind(); // bind the VBO for the torus vertices verts.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 (prog|"Position").Setup(n_per_vertex, DataType::Float).Enable(); } // bind the VBO for the torus normals normals.Bind(Buffer::Target::Array); { std::vector<GLfloat> data; GLuint n_per_vertex = make_torus.Normals(data); // upload the data Buffer::Data(Buffer::Target::Array, data); // setup the vertex attribs array for the vertices (prog|"Normal").Setup(n_per_vertex, DataType::Float).Enable(); } // // set the light position (prog/"LightPos").Set(Vec3f(4.0f, 4.0f, -8.0f)); gl.ClearColor(0.8f, 0.8f, 0.7f, 0.0f); gl.ClearDepth(1.0f); gl.Enable(Capability::DepthTest); gl.Enable(Capability::CullFace); gl.FrontFace(make_torus.FaceWinding()); gl.CullFace(Face::Back); glLineWidth(4.0f); }
HaloExample(void) : make_shape() , shape_indices(make_shape.Indices()) , shape_instr(make_shape.Instructions()) , vs_shape(ObjectDesc("Shape VS")) , vs_plane(ObjectDesc("Plane VS")) , fs_shape(ObjectDesc("Shape FS")) , fs_plane(ObjectDesc("Plane FS")) , vs_halo(ObjectDesc("Halo VS")) , gs_halo(ObjectDesc("Halo GS")) , fs_halo(ObjectDesc("Halo FS")) , shape_projection_matrix(shape_prog, "ProjectionMatrix") , shape_camera_matrix(shape_prog, "CameraMatrix") , shape_model_matrix(shape_prog, "ModelMatrix") , plane_projection_matrix(plane_prog, "ProjectionMatrix") , plane_camera_matrix(plane_prog, "CameraMatrix") , halo_projection_matrix(halo_prog, "ProjectionMatrix") , halo_camera_matrix(halo_prog, "CameraMatrix") , halo_model_matrix(halo_prog, "ModelMatrix") { vs_shape.Source( "#version 140\n" "in vec4 Position;" "in vec3 Normal;" "uniform mat4 ProjectionMatrix, CameraMatrix, ModelMatrix;" "uniform vec3 LightPos;" "out vec3 vertNormal;" "out vec3 vertViewNormal;" "out vec3 vertLight;" "void main(void)" "{" " gl_Position = ModelMatrix * Position;" " vertNormal = mat3(ModelMatrix)*Normal;" " vertViewNormal = mat3(CameraMatrix)*vertNormal;" " vertLight = LightPos - gl_Position.xyz;" " gl_Position = ProjectionMatrix * CameraMatrix * gl_Position;" "}" ); vs_shape.Compile(); fs_shape.Source( "#version 140\n" "in vec3 vertNormal;" "in vec3 vertViewNormal;" "in vec3 vertLight;" "uniform mat4 CameraMatrix;" "out vec4 fragColor;" "void main(void)" "{" " float ltlen = sqrt(length(vertLight));" " float ltexp = dot(" " normalize(vertNormal)," " normalize(vertLight)" " );" " float lview = dot(" " normalize(vertLight)," " normalize(vec3(" " CameraMatrix[0][2]," " CameraMatrix[1][2]," " CameraMatrix[2][2] " " ))" " );" " float depth = normalize(vertViewNormal).z;" " vec3 ftrefl = vec3(0.9, 0.8, 0.7);" " vec3 scatter = vec3(0.9, 0.6, 0.1);" " vec3 bklt = vec3(0.8, 0.6, 0.4);" " vec3 ambient = vec3(0.5, 0.4, 0.3);" " fragColor = vec4(" " pow(max(ltexp, 0.0), 8.0)*ftrefl+" " ( ltexp+1.0)/ltlen*pow(depth,2.0)*scatter+" " (-ltexp+1.0)/ltlen*(1.0-depth)*scatter+" " (-lview+1.0)*0.6*(1.0-abs(depth))*bklt+" " 0.2*ambient," " 1.0" " );" "}" ); fs_shape.Compile(); shape_prog.AttachShader(vs_shape); shape_prog.AttachShader(fs_shape); shape_prog.Link(); vs_plane.Source( "#version 140\n" "in vec4 Position;" "in vec3 Normal;" "uniform mat4 ProjectionMatrix, CameraMatrix;" "uniform vec3 LightPos;" "out vec3 vertNormal;" "out vec3 vertLight;" "void main(void)" "{" " gl_Position = " " ProjectionMatrix *" " CameraMatrix *" " Position;" " vertNormal = Normal;" " vertLight = LightPos-Position.xyz;" "}" ); vs_plane.Compile(); fs_plane.Source( "#version 140\n" "in vec3 vertNormal;" "in vec3 vertLight;" "out vec4 fragColor;" "void main(void)" "{" " float l = sqrt(length(vertLight));" " float e = dot(" " vertNormal," " normalize(vertLight)" " );" " float d = l > 0.0 ? e / l : 0.0;" " float i = 0.2 + 2.5 * d;" " fragColor = vec4(0.8*i, 0.7*i, 0.4*i, 1.0);" "}" ); fs_plane.Compile(); plane_prog.AttachShader(vs_plane); plane_prog.AttachShader(fs_plane); plane_prog.Link(); vs_halo.Source( "#version 150\n" "in vec4 Position;" "in vec3 Normal;" "uniform mat4 ModelMatrix, CameraMatrix;" "out vec3 vertNormal;" "out float vd;" "void main(void)" "{" " gl_Position = " " CameraMatrix *" " ModelMatrix *" " Position;" " vertNormal = (" " CameraMatrix *" " ModelMatrix *" " vec4(Normal, 0.0)" " ).xyz;" " vd = vertNormal.z;" "}" ); vs_halo.Compile(); gs_halo.Source( "#version 150\n" "layout(triangles) in;" "layout(triangle_strip, max_vertices = 12) out;" "in vec3 vertNormal[];" "in float vd[];" "uniform mat4 CameraMatrix, ProjectionMatrix;" "uniform vec3 LightPos;" "out float geomAlpha;" "void main(void)" "{" " for(int v=0; v!=3; ++v)" " {" " int a = v, b = (v+1)%3, c = (v+2)%3;" " vec4 pa = gl_in[a].gl_Position;" " vec4 pb = gl_in[b].gl_Position;" " vec4 pc = gl_in[c].gl_Position;" " vec4 px, py;" " vec3 na = vertNormal[a];" " vec3 nb = vertNormal[b];" " vec3 nc = vertNormal[c];" " vec3 nx, ny;" " if(vd[a] == 0.0 && vd[b] == 0.0)" " {" " px = pa;" " nx = na;" " py = pb;" " ny = nb;" " }" " else if(vd[a] > 0.0 && vd[b] < 0.0)" " {" " float x = vd[a]/(vd[a]-vd[b]);" " float y;" " px = mix(pa, pb, x);" " nx = mix(na, nb, x);" " if(vd[c] < 0.0)" " {" " y = vd[a]/(vd[a]-vd[c]);" " py = mix(pa, pc, y);" " ny = mix(na, nc, y);" " }" " else" " {" " y = vd[c]/(vd[c]-vd[b]);" " py = mix(pc, pb, y);" " ny = mix(nc, nb, y);" " }" " }" " else continue;" " vec4 gx1 = vec4(px.xyz, 1.0);" " vec4 gy1 = vec4(py.xyz, 1.0);" " vec4 gx2 = vec4(px.xyz + nx*0.3, 1.0);" " vec4 gy2 = vec4(py.xyz + ny*0.3, 1.0);" " gl_Position = ProjectionMatrix * gy1;" " geomAlpha = 1.0;" " EmitVertex();" " gl_Position = ProjectionMatrix * gx1;" " geomAlpha = 1.0;" " EmitVertex();" " gl_Position = ProjectionMatrix * gy2;" " geomAlpha = 0.0;" " EmitVertex();" " gl_Position = ProjectionMatrix * gx2;" " geomAlpha = 0.0;" " EmitVertex();" " EndPrimitive();" " break;" " }" "}" ); gs_halo.Compile(); fs_halo.Source( "#version 150\n" "in float geomAlpha;" "out vec4 fragColor;" "void main(void)" "{" " fragColor = vec4(" " 0.5, 0.4, 0.3," " pow(geomAlpha, 2.0)" " );" "}" ); fs_halo.Compile(); halo_prog.AttachShader(vs_halo); halo_prog.AttachShader(gs_halo); halo_prog.AttachShader(fs_halo); halo_prog.Link(); // bind the VAO for the shape shape.Bind(); // bind the VBO for the shape vertices shape_verts.Bind(Buffer::Target::Array); { std::vector<GLfloat> data; GLuint n_per_vertex = make_shape.Positions(data); Buffer::Data(Buffer::Target::Array, data); VertexAttribSlot location; if(VertexArrayAttrib::QueryCommonLocation( MakeGroup(shape_prog, halo_prog), "Position", location )) { VertexArrayAttrib attr(location); attr.Setup<GLfloat>(n_per_vertex); attr.Enable(); } else OGLPLUS_ABORT("Inconsistent 'Position' location"); } // bind the VBO for the shape normals shape_normals.Bind(Buffer::Target::Array); { std::vector<GLfloat> data; GLuint n_per_vertex = make_shape.Normals(data); Buffer::Data(Buffer::Target::Array, data); shape_prog.Use(); VertexArrayAttrib attr(shape_prog, "Normal"); attr.Setup<GLfloat>(n_per_vertex); attr.Enable(); } // bind the VAO for the plane plane.Bind(); // bind the VBO for the plane vertices plane_verts.Bind(Buffer::Target::Array); { GLfloat data[4*3] = { -9.0f, 0.0f, 9.0f, -9.0f, 0.0f, -9.0f, 9.0f, 0.0f, 9.0f, 9.0f, 0.0f, -9.0f }; Buffer::Data(Buffer::Target::Array, 4*3, data); plane_prog.Use(); VertexArrayAttrib attr(plane_prog, "Position"); attr.Setup<Vec3f>(); attr.Enable(); } // bind the VBO for the plane normals plane_normals.Bind(Buffer::Target::Array); { GLfloat data[4*3] = { -0.1f, 1.0f, 0.1f, -0.1f, 1.0f, -0.1f, 0.1f, 1.0f, 0.1f, 0.1f, 1.0f, -0.1f }; Buffer::Data(Buffer::Target::Array, 4*3, data); plane_prog.Use(); VertexArrayAttrib attr(plane_prog, "Normal"); attr.Setup<Vec3f>(); attr.Enable(); } Vec3f lightPos(2.0f, 2.5f, 9.0f); ProgramUniform<Vec3f>(shape_prog, "LightPos").Set(lightPos); ProgramUniform<Vec3f>(plane_prog, "LightPos").Set(lightPos); gl.ClearColor(0.2f, 0.2f, 0.2f, 0.0f); gl.ClearDepth(1.0f); gl.ClearStencil(0); gl.Enable(Capability::DepthTest); gl.BlendFunc(BlendFn::SrcAlpha, BlendFn::One); }
ReflectionExample(void) : torus_indices(make_torus.Indices()) , torus_instr(make_torus.Instructions()) , vs_norm(ObjectDesc("Vertex-Normal")) , vs_refl(ObjectDesc("Vertex-Reflection")) , gs_refl(ObjectDesc("Geometry-Reflection")) { namespace se = oglplus::smart_enums; // Set the normal object vertex shader source vs_norm.Source( "#version 330\n" "in vec4 Position;" "in vec3 Normal;" "out vec3 geomColor;" "out vec3 geomNormal;" "out vec3 geomLight;" "uniform mat4 ProjectionMatrix, CameraMatrix, ModelMatrix;" "uniform vec3 LightPos;" "void main(void)" "{" " gl_Position = ModelMatrix * Position;" " geomColor = Normal;" " geomNormal = mat3(ModelMatrix)*Normal;" " geomLight = LightPos-gl_Position.xyz;" " gl_Position = ProjectionMatrix * CameraMatrix * gl_Position;" "}" ); // compile it vs_norm.Compile(); // Set the reflected object vertex shader source // which just passes data to the geometry shader vs_refl.Source( "#version 330\n" "in vec4 Position;" "in vec3 Normal;" "out vec3 vertNormal;" "void main(void)" "{" " gl_Position = Position;" " vertNormal = Normal;" "}" ); // compile it vs_refl.Compile(); // Set the reflected object geometry shader source // This shader creates a reflection matrix that // relies on the fact that the reflection is going // to be done by the y-plane gs_refl.Source( "#version 330\n" "layout(triangles) in;" "layout(triangle_strip, max_vertices = 6) out;" "in vec3 vertNormal[];" "uniform mat4 ProjectionMatrix;" "uniform mat4 CameraMatrix;" "uniform mat4 ModelMatrix;" "out vec3 geomColor;" "out vec3 geomNormal;" "out vec3 geomLight;" "uniform vec3 LightPos;" "mat4 ReflectionMatrix = 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," " 0.0, 0.0, 0.0, 1.0 " ");" "void main(void)" "{" " for(int v=0; v!=gl_in.length(); ++v)" " {" " vec4 Position = gl_in[v].gl_Position;" " gl_Position = ModelMatrix * Position;" " geomColor = vertNormal[v];" " geomNormal = mat3(ModelMatrix)*vertNormal[v];" " geomLight = LightPos - gl_Position.xyz;" " gl_Position = " " ProjectionMatrix *" " CameraMatrix *" " ReflectionMatrix *" " gl_Position;" " EmitVertex();" " }" " EndPrimitive();" "}" ); // compile it gs_refl.Compile(); // set the fragment shader source fs.Source( "#version 330\n" "in vec3 geomColor;" "in vec3 geomNormal;" "in vec3 geomLight;" "out vec4 fragColor;" "void main(void)" "{" " float l = length(geomLight);" " float d = l > 0.0 ? dot(" " geomNormal, " " normalize(geomLight)" " ) / l : 0.0;" " float i = 0.2 + max(d, 0.0) * 2.0;" " fragColor = vec4(abs(geomNormal)*i, 1.0);" "}" ); // compile it fs.Compile(); // attach the shaders to the normal rendering program prog_norm.AttachShader(vs_norm); prog_norm.AttachShader(fs); // link it prog_norm.Link(); // attach the shaders to the reflection rendering program prog_refl.AttachShader(vs_refl); prog_refl.AttachShader(gs_refl); prog_refl.AttachShader(fs); // link it prog_refl.Link(); // bind the VAO for the torus torus.Bind(); // bind the VBO for the torus vertices torus_verts.Bind(se::Array()); { std::vector<GLfloat> data; GLuint n_per_vertex = make_torus.Positions(data); // upload the data Buffer::Data(se::Array(), data); // setup the vertex attribs array for the vertices typedef VertexAttribArray VAA; VertexAttribSlot loc_norm = VAA::GetLocation(prog_norm, "Position"), loc_refl = VAA::GetLocation(prog_refl, "Position"); assert(loc_norm == loc_refl); VertexAttribArray attr(loc_norm); attr.Setup<GLfloat>(n_per_vertex); attr.Enable(); } // bind the VBO for the torus normals torus_normals.Bind(se::Array()); { std::vector<GLfloat> data; GLuint n_per_vertex = make_torus.Normals(data); // upload the data Buffer::Data(se::Array(), data); // setup the vertex attribs array for the normals typedef VertexAttribArray VAA; VertexAttribSlot loc_norm = VAA::GetLocation(prog_norm, "Normal"), loc_refl = VAA::GetLocation(prog_refl, "Normal"); assert(loc_norm == loc_refl); VertexAttribArray attr(loc_norm); attr.Setup<GLfloat>(n_per_vertex); attr.Enable(); } // bind the VAO for the plane plane.Bind(); // bind the VBO for the plane vertices plane_verts.Bind(se::Array()); { GLfloat data[4*3] = { -2.0f, 0.0f, 2.0f, -2.0f, 0.0f, -2.0f, 2.0f, 0.0f, 2.0f, 2.0f, 0.0f, -2.0f }; // upload the data Buffer::Data(se::Array(), 4*3, data); // setup the vertex attribs array for the vertices prog_norm.Use(); VertexAttribArray attr(prog_norm, "Position"); attr.Setup<Vec3f>(); attr.Enable(); } // bind the VBO for the torus normals plane_normals.Bind(se::Array()); { GLfloat data[4*3] = { -0.1f, 1.0f, 0.1f, -0.1f, 1.0f, -0.1f, 0.1f, 1.0f, 0.1f, 0.1f, 1.0f, -0.1f }; // upload the data Buffer::Data(se::Array(), 4*3, data); // setup the vertex attribs array for the normals prog_norm.Use(); VertexAttribArray attr(prog_norm, "Normal"); attr.Setup<Vec3f>(); attr.Enable(); } VertexArray::Unbind(); Vec3f lightPos(2.0f, 2.0f, 3.0f); prog_norm.Use(); SetUniform(prog_norm, "LightPos", lightPos); prog_refl.Use(); SetUniform(prog_refl, "LightPos", lightPos); // gl.ClearColor(0.2f, 0.2f, 0.2f, 0.0f); gl.ClearDepth(1.0f); gl.ClearStencil(0); }
CubeExample(void) : cube_instr(make_cube.Instructions()) , cube_indices(make_cube.Indices()) { // Set the vertex shader source vs.Source( "#version 330\n" "uniform mat4 ProjectionMatrix, CameraMatrix;" "in vec4 Position;" "in vec2 TexCoord;" "out vec2 vertTexCoord;" "void main(void)" "{" " vertTexCoord = TexCoord;" " gl_Position = " " ProjectionMatrix *" " CameraMatrix *" " Position;" "}" ); // compile it vs.Compile(); // set the fragment shader source fs.Source( "#version 330\n" "in vec2 vertTexCoord;" "out vec4 fragColor;" "void main(void)" "{" " float i = (" " 1 +" " int(vertTexCoord.x*8) % 2+" " int(vertTexCoord.y*8) % 2" " ) % 2;" " fragColor = vec4(i, i, i, 1.0);" "}" ); // 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 cube cube.Bind(); // bind the VBO for the cube vertices verts.Bind(Buffer::Target::Array); { std::vector<GLfloat> data; GLuint n_per_vertex = make_cube.Positions(data); // upload the data Buffer::Data(Buffer::Target::Array, data); // setup the vertex attribs array for the vertices VertexAttribArray attr(prog, "Position"); attr.Setup(n_per_vertex, DataType::Float); attr.Enable(); } // bind the VBO for the cube texture-coordinates texcoords.Bind(Buffer::Target::Array); { std::vector<GLfloat> data; GLuint n_per_vertex = make_cube.TexCoordinates(data); // upload the data Buffer::Data(Buffer::Target::Array, data); // setup the vertex attribs array for the vertices VertexAttribArray attr(prog, "TexCoord"); attr.Setup(n_per_vertex, DataType::Float); attr.Enable(); } // gl.ClearColor(0.8f, 0.8f, 0.7f, 0.0f); gl.ClearDepth(1.0f); gl.Enable(Capability::DepthTest); }
LandscapeExample(void) : grid_side(8) , make_plane( Vec3f(0.0f, 0.0f, 0.0f), Vec3f(1.0f, 0.0f, 0.0f), Vec3f(0.0f, 0.0f,-1.0f), grid_side, grid_side ), plane_instr(make_plane.Instructions()) , plane_indices(make_plane.Indices()) , projection_matrix(prog, "ProjectionMatrix") , camera_matrix(prog, "CameraMatrix") , vc_int(prog, "vc_int") , gc_int(prog, "gc_int") , fc_int(prog, "fc_int") { VertexShader vs; vs.Source(StrLit( "#version 420\n" "uniform mat4 ProjectionMatrix, CameraMatrix;" "layout(binding = 0, offset = 0) uniform atomic_uint vc;" "const float mult = 1.0/128.0;" "uniform float vc_int;" "in vec4 Position;" "out vec3 vertColor;" "void main(void)" "{" " gl_Position = " " ProjectionMatrix *" " CameraMatrix *" " Position;" " vertColor = vec3(" " fract(atomicCounterIncrement(vc)*mult)," " 0.0," " 0.0 " " )*max(vc_int, 0.0);" "}" )); vs.Compile(); prog.AttachShader(vs); GeometryShader gs; gs.Source(StrLit( "#version 420\n" "layout (triangles) in;" "layout (triangle_strip, max_vertices = 3) out;" "layout(binding = 0, offset = 4) uniform atomic_uint gc;" "const float mult = 1.0/128.0;" "uniform float gc_int;" "in vec3 vertColor[3];" "out vec3 geomColor;" "void main(void)" "{" " vec3 Color = vec3(" " 0.0," " fract(atomicCounterIncrement(gc)*mult)," " 0.0 " " )*max(gc_int, 0.0);" " for(int v=0; v!=3; ++v)" " {" " gl_Position = gl_in[v].gl_Position;" " geomColor = vertColor[v] + Color;" " EmitVertex();" " }" " EndPrimitive();" "}" )); gs.Compile(); prog.AttachShader(gs); FragmentShader fs; fs.Source(StrLit( "#version 420\n" "layout(binding = 0, offset = 8) uniform atomic_uint fc;" "const float mult = 1.0/4096.0;" "uniform float fc_int;" "in vec3 geomColor;" "out vec3 fragColor;" "void main(void)" "{" " vec3 Color = vec3(" " 0.0," " 0.0," " sqrt(fract(atomicCounterIncrement(fc)*mult))" " )*max(fc_int, 0.0);" " fragColor = geomColor + Color;" "}" )); fs.Compile(); prog.AttachShader(fs); prog.Link(); prog.Use(); // bind the VAO for the plane plane.Bind(); // bind the VBO for the plane vertices positions.Bind(Buffer::Target::Array); { std::vector<GLfloat> data; GLuint n_per_vertex = make_plane.Positions(data); // upload the data Buffer::Data(Buffer::Target::Array, data); // setup the vertex attribs array for the vertices VertexAttribArray attr(prog, "Position"); attr.Setup<GLfloat>(n_per_vertex); attr.Enable(); } counters.Bind(Buffer::Target::AtomicCounter); { const GLuint tmp[3] = {0u, 0u, 0u}; Buffer::Data( Buffer::Target::AtomicCounter, 3, tmp, BufferUsage::DynamicDraw ); } counters.BindBase(Buffer::IndexedTarget::AtomicCounter, 0); gl.ClearColor(0.2f, 0.2f, 0.2f, 0.0f); gl.ClearDepth(1.0f); gl.Enable(Capability::DepthTest); plane.Bind(); }
SphereExample(void) : sphere_instr(make_sphere.Instructions()) , sphere_indices(make_sphere.Indices()) , hole_count(50) , hole_diameter(0.30f) { // This shader will be used in transform fedback mode // to transform the vertices used to "cut out the holes" // the same way the sphere is transformed vs_tfb.Source( "#version 330\n" "uniform mat4 CameraMatrix, ModelMatrix;" "uniform float Diameter;" "in vec3 Hole;" "out vec3 vertTransfHole;" "void main(void)" "{" " vertTransfHole = (" " CameraMatrix *" " ModelMatrix *" " vec4(Hole * (1.0 + 0.5 * Diameter), 0.0)" " ).xyz;" "}" ); // compile, setup transform feedback output variables // link and use the program vs_tfb.Compile(); prog_tfb.AttachShader(vs_tfb); const GLchar* var_name = "vertTransfHole"; prog_tfb.TransformFeedbackVaryings( 1, &var_name, TransformFeedbackMode::InterleavedAttribs ); prog_tfb.Link(); prog_tfb.Use(); Uniform<GLfloat> diameter(prog_tfb, "Diameter"); diameter.Set(hole_diameter); // bind the VAO for the holes holes.Bind(); // bind the VBO for the hole vertices hole_verts.Bind(Buffer::Target::Array); // and the VBO for the transformed hole vertices captured by tfb transf_hole_verts.Bind(Buffer::Target::TransformFeedback); { std::vector<GLfloat> data; make_hole_data(data, hole_count); Buffer::Data(Buffer::Target::TransformFeedback, data); Buffer::Data(Buffer::Target::Array, data); VertexAttribArray attr(prog_tfb, "Hole"); attr.Setup<Vec3f>(); attr.Enable(); } transf_hole_verts.BindBase( Buffer::IndexedTarget::TransformFeedback, 0 ); // Set the vertex shader source vs.Source( "#version 330\n" "uniform mat4 ProjectionMatrix, CameraMatrix, ModelMatrix;" "in vec4 Position;" "in vec3 Normal;" "out vec3 vertNormal;" "out vec3 vertLight;" "const vec3 LightPos = vec3(2.0, 3.0, 3.0);" "void main(void)" "{" " gl_Position = ModelMatrix * Position;" " vertNormal = mat3(ModelMatrix)*Normal;" " vertLight = LightPos-gl_Position.xyz;" " gl_Position = ProjectionMatrix * CameraMatrix * gl_Position;" "}" ); // compile it vs.Compile(); // set the fragment shader source fs.Source( "#version 330\n" "in vec3 vertNormal;" "in vec3 vertLight;" "out vec4 fragColor;" "const int HoleCount = 50;" "uniform vec3 TransfHole[50];" "uniform float Diameter;" "void main(void)" "{" " int imax = 0;" " float dmax = -1.0;" " for(int i=0; i!=HoleCount; ++i)" " {" " float d = dot(vertNormal, TransfHole[i]);" " if(dmax < d)" " {" " dmax = d;" " imax = i;" " }" " }" " float l = length(vertLight);" " vec3 FragDiff = TransfHole[imax] - vertNormal;" " vec3 FinalNormal = " " length(FragDiff) > Diameter?" " vertNormal:" " normalize(FragDiff+vertNormal*Diameter);" " float i = (l > 0.0) ? dot(" " FinalNormal, " " normalize(vertLight)" " ) / l : 0.0;" " i = 0.2+max(i*2.5, 0.0);" " fragColor = vec4(i, i, i, 1.0);" "}" ); // compile it fs.Compile(); // attach the shaders to the program prog.AttachShader(vs); prog.AttachShader(fs); // link and use it prog.Link(); prog.Use(); diameter.Set(hole_diameter); // bind the VAO for the sphere sphere.Bind(); // bind the VBO for the sphere vertices verts.Bind(Buffer::Target::Array); { std::vector<GLfloat> data; GLuint n_per_vertex = make_sphere.Positions(data); // upload the data Buffer::Data(Buffer::Target::Array, data); // setup the vertex attribs array for the vertices VertexAttribArray attr(prog, "Position"); attr.Setup<GLfloat>(n_per_vertex); attr.Enable(); } // bind the VBO for the sphere normals normals.Bind(Buffer::Target::Array); { std::vector<GLfloat> data; GLuint n_per_vertex = make_sphere.Normals(data); // upload the data Buffer::Data(Buffer::Target::Array, data); // setup the vertex attribs array for the vertices VertexAttribArray attr(prog, "Normal"); attr.Setup<GLfloat>(n_per_vertex); attr.Enable(); } gl.ClearColor(0.8f, 0.8f, 0.7f, 0.0f); gl.ClearDepth(1.0f); gl.Enable(Capability::DepthTest); }
TorusExample(void) : make_torus(1.0, 0.5, 72, 48) , torus_instr(make_torus.Instructions()) , torus_indices(make_torus.Indices()) , projection_matrix(prog, "ProjectionMatrix") , camera_matrix(prog, "CameraMatrix") , model_matrix(prog, "ModelMatrix") { // Set the vertex shader source and compile it vs.Source( "#version 330\n" "uniform mat4 ProjectionMatrix, CameraMatrix, ModelMatrix;" "in vec4 Position;" "in vec3 Normal;" "in vec2 TexCoord;" "out vec3 vertNormal;" "out vec3 vertLight;" "out vec2 vertTexCoord;" "uniform vec3 LightPos;" "void main(void)" "{" " gl_Position = ModelMatrix * Position;" " vertNormal = mat3(ModelMatrix)*Normal;" " vertLight = LightPos - gl_Position.xyz;" " vertTexCoord = TexCoord;" " gl_Position = ProjectionMatrix * CameraMatrix * gl_Position;" "}" ).Compile(); // set the fragment shader source and compile it fs.Source( "#version 330\n" "uniform sampler2D TexUnit;" "in vec3 vertNormal;" "in vec3 vertLight;" "in vec2 vertTexCoord;" "out vec4 fragColor;" "void main(void)" "{" " float l = sqrt(length(vertLight));" " float d = l > 0? dot(" " vertNormal, " " normalize(vertLight)" " ) / l : 0.0;" " float i = 0.2 + 3.2*max(d, 0.0);" " fragColor = texture(TexUnit, vertTexCoord)*i;" "}" ).Compile(); // attach the shaders to the program prog.AttachShader(vs).AttachShader(fs); // link and use it prog.Link().Use(); // bind the VAO for the torus torus.Bind(); // bind the VBO for the torus vertices verts.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(prog, "Position"); attr.Setup<GLfloat>(n_per_vertex); attr.Enable(); } // bind the VBO for the torus normals normals.Bind(Buffer::Target::Array); { std::vector<GLfloat> data; GLuint n_per_vertex = make_torus.Normals(data); // upload the data Buffer::Data(Buffer::Target::Array, data); // setup the vertex attribs array for the vertices VertexAttribArray attr(prog, "Normal"); attr.Setup<GLfloat>(n_per_vertex); attr.Enable(); } // bind the VBO for the torus texture coordinates 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(prog, "TexCoord"); attr.Setup<GLfloat>(n_per_vertex); attr.Enable(); } // setup the texture Texture::Target tex_tgt = Texture::Target::_2D; tex.Bind(tex_tgt); { GLuint s = 256; std::vector<GLubyte> tex_data(s*s); for(GLuint v=0;v!=s;++v) for(GLuint u=0;u!=s;++u) tex_data[v*s+u] = rand() % 0x100; Texture::Image2D( tex_tgt, 0, PixelDataInternalFormat::Red, s, s, 0, PixelDataFormat::Red, PixelDataType::UnsignedByte, tex_data.data() ); Texture::MinFilter(tex_tgt, TextureMinFilter::Linear); Texture::MagFilter(tex_tgt, TextureMagFilter::Linear); Texture::WrapS(tex_tgt, TextureWrap::Repeat); Texture::WrapT(tex_tgt, TextureWrap::Repeat); Texture::SwizzleG(tex_tgt, TextureSwizzle::Red); Texture::SwizzleB(tex_tgt, TextureSwizzle::Red); } // typechecked uniform with exact data type // on compilers supporting strongly typed enums // you can use: //Typechecked<Uniform<SLtoCpp<SLDataType::Sampler2D>>>(prog, "TexUnit").Set(0); // without strongly typed enums you need to do: typedef SLtoCpp<OGLPLUS_CONST_ENUM_VALUE(SLDataType::Sampler2D)> GLSLsampler2D; Typechecked<Uniform<GLSLsampler2D>>(prog, "TexUnit").Set(0); // Uniform<Vec3f>(prog, "LightPos").Set(4.0f, 4.0f, -8.0f); gl.ClearColor(0.8f, 0.8f, 0.7f, 0.0f); gl.ClearDepth(1.0f); gl.Enable(Capability::DepthTest); gl.Enable(Capability::CullFace); gl.FrontFace(make_torus.FaceWinding()); gl.CullFace(Face::Back); }
static Program make(void) { VertexShader vs; vs.Source( "#version 150\n" "uniform mat4 ModelMatrix;" "uniform vec3 LightPosition;" "uniform vec3 CameraPosition;" "in vec4 Position;" "out vec3 vertLightDir;" "out vec3 vertViewDir;" "void main(void)" "{" " gl_Position = ModelMatrix * Position;" " vertLightDir = LightPosition - gl_Position.xyz;" " vertViewDir = CameraPosition - gl_Position.xyz;" "}" ).Compile(); GeometryShader gs; gs.Source( "#version 150\n" "layout (triangles) in;" "layout (triangle_strip, max_vertices=3) out;" "uniform mat4 CameraMatrix;" "in vec3 vertLightDir[3];" "in vec3 vertViewDir[3];" "out vec3 geomLightDir;" "out vec3 geomViewDir;" "out vec3 geomNormal;" "void main(void)" "{" " geomNormal = normalize(" " cross(" " gl_in[1].gl_Position.xyz-" " gl_in[0].gl_Position.xyz," " gl_in[2].gl_Position.xyz-" " gl_in[0].gl_Position.xyz " " )" " );" " for(int v=0; v!=3; ++v)" " {" " gl_Position = CameraMatrix * gl_in[v].gl_Position;" " geomLightDir = vertLightDir[v];" " geomViewDir = vertViewDir[v];" " EmitVertex();" " }" " EndPrimitive();" "}" ).Compile(); FragmentShader fs; fs.Source( "#version 150\n" "in vec3 geomLightDir;" "in vec3 geomViewDir;" "in vec3 geomNormal;" "out vec4 fragColor;" "void main(void)" "{" " vec3 Normal = normalize(geomNormal);" " vec3 LightDir = normalize(geomLightDir);" " vec3 LightRefl = reflect(-LightDir, Normal);" " vec3 ViewDir = normalize(geomViewDir);" " fragColor = vec4(" " clamp(dot(Normal, ViewDir), 0, 1)," " clamp(dot(Normal, LightDir),0, 1)," " clamp(dot(ViewDir, LightRefl), 0, 1)," " gl_FragCoord.z" " );" "}" ).Compile(); Program prog(ObjectDesc("Data")); prog << vs << gs << fs; prog.Link().Use(); return std::move(prog); }
static Program make(void) { VertexShader vs; vs.Source( "#version 150\n" "in vec4 Position;" "void main(void)" "{" " gl_Position = Position;" "}" ).Compile(); FragmentShader fs; fs.Source( "#version 150\n" "uniform sampler2DRect DataMap;" "uniform float Slider;" "uniform vec2 SampleOffs[32];" "const int NSamples = 32;" "const float InvNSam = 1.0 / NSamples;" "in vec4 gl_FragCoord;" "out vec3 fragColor;" "void main(void)" "{" " vec4 FData = texture(DataMap, gl_FragCoord.xy);" " float Mask = ceil(FData.w);" " float Ambi = 1;" " float View = FData.x;" " float Diff = FData.y;" " float Spec = pow(FData.z, 8.0);" " float SampleSpread = mix(16, 8, FData.w);" " float SSAO = 0.0;" " for(int s=0; s!=NSamples; ++s)" " {" " vec2 SampleCoord = gl_FragCoord.xy+SampleSpread*SampleOffs[s];" " vec4 SData = texture(DataMap, SampleCoord);" " float x = (FData.w - SData.w)*16.0;" " float y = View*x*exp(1-abs(x))*min(exp(x), 1.0);" " SSAO += max(1.0-y*2.0, 0.0);" " }" " SSAO *= InvNSam;" " if(gl_FragCoord.x < Slider) SSAO = 1.0;" " Ambi *= (0.00+0.30*SSAO);" " Diff *= (0.05+0.40*SSAO);" " Spec *= (0.05+0.60*SSAO);" " float Sl = int(gl_FragCoord.x) == int(Slider)?1:0;" " vec3 Color = vec3(0.9, 0.8, 0.3)*(Ambi+Diff)+vec3(Spec);" " vec3 BgColor = vec3(0.2);" " vec3 SlColor = vec3(1.0);" " fragColor = mix(mix(BgColor, Color, Mask), SlColor, Sl);" "}" ).Compile(); Program prog(ObjectDesc("Draw")); prog << vs << fs; prog.Link().Use(); return std::move(prog); }