void GLTexture2D::Replace( const glm::ivec4& rect, GLenum format, GLenum type, const void* data ) { Bind(); glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x, rect.y, rect.z, rect.w, format, type, data); GenerateMipmap(); Unbind(); }
void GLTexture2D::Allocate( int width, int height, GLenum internalFormat, GLenum format, GLenum type, const void* data ) { this->width = width; this->height = height; this->internalFormat = internalFormat; Bind(); glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, type, data); GenerateMipmap(); UpdateTextureParams(); Unbind(); }
void ICubemap::GenerateMipmaps(ICubemapPtr map) { if (!(map->IsReadable()) || !(map->IsMipmapped())) return; const Face faces[] = {POSITIVE_X, NEGATIVE_X, POSITIVE_Y, NEGATIVE_Y, POSITIVE_Z, NEGATIVE_Z}; // For each mip level except the first for (int m = 1; m < map->MipmapCount(); ++m) { // for each side for (int f = 0; f < 6; ++f) { // for each pixel GenerateMipmap(map, faces[f], m); } } }
LiquidExample(const ExampleParams& params) : liquid_prog() , grid(liquid_prog, params.quality) , grid_repeat(1 + params.quality*2) { Texture::Active(0); { auto image = images::NewtonFractal( 256, 256, Vec3f(0.1f, 0.1f, 0.1f), Vec3f(1.0f, 1.0f, 1.0f), Vec2f(-1.0f, -1.0f), Vec2f( 1.0f, 1.0f), images::NewtonFractal::X4Minus1(), images::NewtonFractal::DefaultMixer() ); auto bound_tex = oglplus::Context::Bound( Texture::Target::CubeMap, env_map ); for(int i=0; i!=6; ++i) Texture::ImageCM(i, image); bound_tex.GenerateMipmap(); bound_tex.MinFilter(TextureMinFilter::LinearMipmapLinear); bound_tex.MagFilter(TextureMagFilter::Linear); bound_tex.WrapS(TextureWrap::ClampToEdge); bound_tex.WrapT(TextureWrap::ClampToEdge); bound_tex.WrapR(TextureWrap::ClampToEdge); } ProgramUniformSampler(liquid_prog, "EnvMap").Set(0); const Vec3f light_position(12.0, 1.0, 8.0); liquid_prog.light_position.Set(light_position); gl.ClearColor(0.7f, 0.65f, 0.55f, 0.0f); gl.ClearDepth(1.0f); gl.Enable(Capability::DepthTest); gl.Enable(Capability::CullFace); gl.FrontFace(FaceOrientation::CW); gl.CullFace(Face::Back); }
CubeExample(void) : cube( List("Position")("Normal")("TexCoord").Get(), shapes::Cube(), cube_prog ) { // setup the texture { GLuint tex_side = 512; auto image = images::NewtonFractal( tex_side, tex_side, Vec3f(0.2f, 0.1f, 0.4f), Vec3f(0.8f, 0.8f, 1.0f), Vec2f(-1.0f, -1.0f), Vec2f( 1.0f, 1.0f), images::NewtonFractal::X4Minus1(), images::NewtonFractal::DefaultMixer() ); auto bound_tex = Bind(cube_tex, Texture::Target::_2D); bound_tex.Image2D(image); bound_tex.GenerateMipmap(); bound_tex.BorderColor(Vec4f(0.8f, 0.8f, 1.0f, 1.0f)); bound_tex.MinFilter(TextureMinFilter::LinearMipmapLinear); bound_tex.MagFilter(TextureMagFilter::Linear); bound_tex.WrapS(TextureWrap::Repeat); bound_tex.WrapT(TextureWrap::Repeat); } cube_prog.cube_tex = 0; cube_prog.light_position.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.CullFace(Face::Back); }
CubeExample(void) : cube_instr(make_cube.Instructions()) , cube_indices(make_cube.Indices()) , projection_matrix(prog, "ProjectionMatrix") , camera_matrix(prog, "CameraMatrix") , model_matrix(prog, "ModelMatrix") { namespace se = oglplus::smart_enums; VertexShader vs; vs.Source( "#version 120\n" "uniform mat4 ProjectionMatrix, CameraMatrix, ModelMatrix;" "attribute vec4 Position;" "attribute vec3 Normal;" "attribute vec2 TexCoord;" "varying vec3 vertNormal;" "varying vec3 vertLight;" "varying vec2 vertTexCoord;" "uniform vec3 LightPos;" "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 120\n" "uniform sampler2D TexUnit;" "varying vec3 vertNormal;" "varying vec3 vertLight;" "varying vec2 vertTexCoord;" "void main(void)" "{" " float l = length(vertLight);" " float d = l > 0 ? dot(vertNormal, normalize(vertLight)) / l : 0.0;" " float i = 0.3 + 2.0*max(d, 0.0);" " vec4 t = texture2D(TexUnit, vertTexCoord);" " gl_FragColor = vec4(t.rgb*i, 1.0);" "}" ).Compile(); prog.AttachShader(vs).AttachShader(fs).Link().Use(); // bind the VAO for the cube cube.Bind(); verts.Bind(se::Array()); { std::vector<GLfloat> data; GLuint n_per_vertex = make_cube.Positions(data); Buffer::Data(se::Array(), data); (prog|"Position").Setup(n_per_vertex, se::Float()).Enable(); } normals.Bind(se::Array()); { std::vector<GLfloat> data; GLuint n_per_vertex = make_cube.Normals(data); Buffer::Data(se::Array(), data); (prog|"Normal").Setup(n_per_vertex, se::Float()).Enable(); } texcoords.Bind(se::Array()); { std::vector<GLfloat> data; GLuint n_per_vertex = make_cube.TexCoordinates(data); Buffer::Data(se::Array(), data); (prog|"TexCoord").Setup(n_per_vertex, se::Float()).Enable(); } // setup the texture { auto bound_tex = Bind(tex, se::_2D()); auto image = images::NewtonFractal( 512, 512, Vec3f(1.0f, 1.4f, 1.3f), Vec3f(0.2f, 0.3f, 0.1f), Vec2f(-1.0f, -1.0f), Vec2f( 1.0f, 1.0f), images::NewtonFractal::X4Minus1(), [](double x) -> double { return pow(SineWave(pow(x,0.5)), 4.0); } ); bound_tex.Image2D(image); bound_tex.GenerateMipmap(); bound_tex.MinFilter(se::LinearMipmapLinear()); bound_tex.MagFilter(se::Linear()); bound_tex.WrapS(se::Repeat()); bound_tex.WrapT(se::Repeat()); } // set the uniform values (prog/"TexUnit") = 0; (prog/"LightPos") = Vec3f(1.0f, 2.0f, 3.0f); // gl.ClearColor(0.1f, 0.1f, 0.1f, 0.0f); gl.ClearDepth(1.0f); gl.Enable(se::DepthTest()); gl.Enable(se::CullFace()); gl.FrontFace(make_cube.FaceWinding()); }
CloudExample(void) : projection_matrix(prog, "ProjectionMatrix") , camera_matrix(prog, "CameraMatrix") { // Set the vertex shader source vs.Source( "#version 330\n" "in vec4 Position;" "uniform mat4 CameraMatrix;" "void main(void)" "{" " gl_Position = " " CameraMatrix *" " Position;" "}" ); // compile it vs.Compile(); // Set the geometry shader source gs.Source( "#version 330\n" "layout(points) in;" "layout(triangle_strip, max_vertices = 100) out;" "const int p = 25;" "const float hp = (p-1)*0.5;" "uniform vec3 LightPos;" "uniform mat4 CameraMatrix, ProjectionMatrix;" "out vec3 geomTexCoord;" "out vec3 geomLightDir;" "void main(void)" "{" " float s = 0.6;" " float yo[2] = float[2](-1.0, 1.0);" " float xo[2] = float[2](-1.0, 1.0);" " vec3 cx = vec3(" " CameraMatrix[0][0]," " CameraMatrix[1][0]," " CameraMatrix[2][0] " " );" " vec3 cy = vec3(" " CameraMatrix[0][1]," " CameraMatrix[1][1]," " CameraMatrix[2][1] " " );" " vec3 cz = vec3(" " CameraMatrix[0][2]," " CameraMatrix[1][2]," " CameraMatrix[2][2] " " );" " for(int k=0;k!=p;++k)" " {" " for(int j=0;j!=2;++j)" " for(int i=0;i!=2;++i)" " {" " float zo = ((k - hp) / hp);" " float xoffs = xo[i]*s;" " float yoffs = yo[j]*s;" " float zoffs = zo *s;" " vec4 v = vec4(" " gl_in[0].gl_Position.x+xoffs," " gl_in[0].gl_Position.y+yoffs," " gl_in[0].gl_Position.z+zoffs," " 1.0" " );" " gl_Position = ProjectionMatrix * v;" " geomLightDir = LightPos - v.xyz;" " geomTexCoord = " " vec3(0.5, 0.5, 0.5)+" " cx*(xo[i])*0.707+" " cy*(yo[j])*0.707+" " cz*(zo )*0.707;" " EmitVertex();" " }" " EndPrimitive();" " }" "}" ); // compile it gs.Compile(); // set the fragment shader source fs.Source( "#version 330\n" "uniform sampler3D cloudTex;" "in vec3 geomTexCoord;" "in vec3 geomLightDir;" "out vec4 fragColor;" "void main(void)" "{" " float d = texture(cloudTex, geomTexCoord).r;" " float o = 1.0;" " float s = 2.0/128.0;" " float r = s * 8.0;" " vec3 sampleOffs = normalize(geomLightDir) * s;" " vec3 samplePos = geomTexCoord;" " if(d > 0.01) while(o > 0.0)" " {" " if(samplePos.x<0.0 || samplePos.x>1.0)" " break;" " if(samplePos.y<0.0 || samplePos.y>1.0)" " break;" " if(samplePos.z<0.0 || samplePos.z>1.0)" " break;" " o -= texture(cloudTex, samplePos).r*r;" " samplePos += sampleOffs;" " }" " float a = 0.2 * d;" " float i = mix(0.4, 1.0, o);" " fragColor = vec4(i, i, i, a);" "}" ); // compile it fs.Compile(); // attach the shaders to the program prog.AttachShader(vs); prog.AttachShader(gs); prog.AttachShader(fs); // link and use it prog.Link(); prog.Use(); // bind the VAO for the clouds clouds.Bind(); // bind the VBO for the cloud positions buffer.Bind(Buffer::Target::Array); { GLfloat positions[3] = {0.5f, 0.1f, 0.2f}; Buffer::Data(Buffer::Target::Array, 3, positions); VertexAttribArray attr(prog, "Position"); attr.Setup(3, DataType::Float); attr.Enable(); } { Texture::Active(0); UniformSampler(prog, "cloudTex").Set(0); auto bound_tex = Bind(cloud_tex, Texture::Target::_3D); bound_tex.Image3D(images::Cloud(128, 128, 128)); bound_tex.GenerateMipmap(); bound_tex.MinFilter(TextureMinFilter::LinearMipmapLinear); bound_tex.MagFilter(TextureMagFilter::Linear); bound_tex.BorderColor(Vec4f(0.0f, 0.0f, 0.0f, 0.0f)); bound_tex.WrapS(TextureWrap::ClampToBorder); bound_tex.WrapT(TextureWrap::ClampToBorder); bound_tex.WrapR(TextureWrap::ClampToBorder); } Uniform<Vec3f>(prog, "LightPos").Set(Vec3f(10.0f, 1.0f, 5.0f)); gl.ClearColor(0.2f, 0.3f, 0.4f, 0.0f); gl.ClearDepth(1.0f); gl.Enable(Capability::DepthTest); gl.Enable(Capability::Blend); gl.BlendFunc(BlendFn::SrcAlpha, BlendFn::OneMinusSrcAlpha); }
Test01(void) { // Set the vertex shader source vs.Source( "#version 330\n" "uniform mat4 projectionMatrix, cameraMatrix, modelMatrix;" "in vec4 vertex;" "in vec3 normal;" "in vec3 tangent;" "in vec2 texcoord;" "out vec3 fragLight;" "out vec2 fragTex;" "out mat3 normalMatrix;" "uniform vec3 lightPos;" "void main(void)" "{" " vec3 fragNormal = (" " modelMatrix *" " vec4(normal, 0.0)" " ).xyz;" " vec3 fragTangent = (" " modelMatrix *" " vec4(tangent, 0.0)" " ).xyz;" " normalMatrix[0] = fragTangent;" " normalMatrix[1] = cross(fragNormal, fragTangent);" " normalMatrix[2] = fragNormal;" " fragLight = (" " vec4(lightPos, 0.0)-" " modelMatrix*vertex" " ).xyz;" " fragTex = texcoord;" " gl_Position = " " projectionMatrix *" " cameraMatrix *" " modelMatrix *" " vertex;" "}" ); // compile it vs.Compile(); // set the fragment shader source fs.Source( "#version 330\n" "uniform sampler2D colorTex, normalTex;" "in vec3 fragLight;" "in vec2 fragTex;" "in mat3 normalMatrix;" "out vec4 fragColor;" "void main(void)" "{" " float s = 5.0;" " float l = length(fragLight);" " vec3 n = texture2D(normalTex, fragTex*s).xyz;" " vec3 finalNormal = normalMatrix * n;" " float d = (l != 0.0)?" " dot(fragLight, finalNormal)/l:" " 0.0;" " float i = 0.1 + 2.5*clamp(d, 0.0, 1.0);" " vec4 t = texture2D(colorTex, fragTex*s);" " fragColor = vec4(t.rgb*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 shape vao.Bind(); { std::vector<GLfloat> data; GLuint n_per_vertex = shape.Positions(data); Bind(verts, Buffer::Target::Array).Data(data); // setup the vertex attribs array for the vertices VertexAttribArray attr(prog, "vertex"); attr.Setup(n_per_vertex, DataType::Float); attr.Enable(); } { std::vector<GLfloat> data; GLuint n_per_vertex = shape.Normals(data); Bind(normals, Buffer::Target::Array).Data(data); // setup the vertex attribs array for the normals VertexAttribArray attr(prog, "normal"); attr.Setup(n_per_vertex, DataType::Float); attr.Enable(); } { std::vector<GLfloat> data; GLuint n_per_vertex = shape.Tangents(data); Bind(tangents, Buffer::Target::Array).Data(data); VertexAttribArray attr(prog, "tangent"); attr.Setup(n_per_vertex, DataType::Float); attr.Enable(); } // bind the VBO for the shape tex-coords { std::vector<GLfloat> data; GLuint n_per_vertex = shape.TexCoordinates(data); Bind(texcoords, Buffer::Target::Array).Data(data); // VertexAttribArray attr(prog, "texcoord"); attr.Setup(n_per_vertex, DataType::Float); attr.Enable(); } // setup the textures { Texture::Active(0); UniformSampler(prog, "colorTex").Set(0); auto bound_tex = Bind(color_tex, Texture::Target::_2D); bound_tex.Image2D(images::LoadTexture("stones")); bound_tex.GenerateMipmap(); bound_tex.MinFilter(TextureMinFilter::LinearMipmapLinear); bound_tex.MagFilter(TextureMagFilter::Linear); bound_tex.WrapS(TextureWrap::Repeat); bound_tex.WrapT(TextureWrap::Repeat); } { Texture::Active(1); UniformSampler(prog, "normalTex").Set(1); auto bound_tex = Bind(normal_tex, Texture::Target::_2D); bound_tex.Image2D( images::NormalMap(images::LoadTexture("stones-hmap")) ); bound_tex.GenerateMipmap(); bound_tex.MinFilter(TextureMinFilter::LinearMipmapLinear); bound_tex.MagFilter(TextureMagFilter::Linear); bound_tex.WrapS(TextureWrap::Repeat); bound_tex.WrapT(TextureWrap::Repeat); } Uniform<Mat4f>(prog, "projectionMatrix").Set( CamMatrixf::Perspective(Degrees(24), 1.25, 1, 100) ); // VertexArray::Unbind(); gl.ClearColor(0.3f, 0.3f, 0.3f, 0.0f); gl.ClearDepth(1.0f); gl.Enable(Capability::DepthTest); // gl.FrontFace(shape.FaceWinding()); gl.CullFace(Face::Back); gl.Enable(Capability::CullFace); }
SketchExample(void) : transf_prog() , sketch_prog() , plane( transf_prog, shapes::Plane( Vec3f(0, 0, 0), Vec3f(9, 0, 0), Vec3f(0, 0,-9), 9, 9 ) ), torus(transf_prog, shapes::WickerTorus()) , sketch_tex_layers(8) , shadow_tex_side(1024) { NoProgram().Use(); shadow_pp.Bind(); shadow_pp.UseStages(transf_prog).Vertex(); shadow_pp.UseStages(shadow_prog).Fragment(); sketch_pp.Bind(); sketch_pp.UseStages(transf_prog).Vertex(); sketch_pp.UseStages(sketch_prog).Fragment(); line_pp.Bind(); line_pp.UseStages(transf_prog).Vertex(); line_pp.UseStages(line_prog).Geometry().Fragment(); Texture::Active(0); sketch_prog.sketch_tex.Set(0); { auto bound_tex = gl.Bound(Texture::Target::_3D, sketch_texture); for(GLuint i=0; i<sketch_tex_layers; ++i) { auto image = images::BrushedMetalUByte( 512, 512, 64 + i*128, -(2+i*4), +(2+i*4), 64, 256-i*4 ); if(i == 0) { bound_tex.Image3D( 0, PixelDataInternalFormat::RGB, image.Width(), image.Height(), sketch_tex_layers, 0, image.Format(), image.Type(), nullptr ); } bound_tex.SubImage3D( 0, 0, 0, i, image.Width(), image.Height(), 1, image.Format(), image.Type(), image.RawData() ); } bound_tex.GenerateMipmap(); bound_tex.MinFilter(TextureMinFilter::LinearMipmapLinear); bound_tex.MagFilter(TextureMagFilter::Linear); bound_tex.WrapS(TextureWrap::Repeat); bound_tex.WrapT(TextureWrap::Repeat); bound_tex.WrapR(TextureWrap::ClampToEdge); } Texture::Active(1); sketch_prog.shadow_tex.Set(1); gl.Bound(Texture::Target::_2D, shadow_tex) .MinFilter(TextureMinFilter::Linear) .MagFilter(TextureMagFilter::Linear) .WrapS(TextureWrap::ClampToEdge) .WrapT(TextureWrap::ClampToEdge) .CompareMode(TextureCompareMode::CompareRefToTexture) .Image2D( 0, PixelDataInternalFormat::DepthComponent32, shadow_tex_side, shadow_tex_side, 0, PixelDataFormat::DepthComponent, PixelDataType::Float, nullptr ); gl.Bound(Framebuffer::Target::Draw, frame_shadow_fbo) .AttachTexture( FramebufferAttachment::Depth, shadow_tex, 0 ); gl.ClearDepth(1.0f); gl.Enable(Capability::DepthTest); gl.Enable(Capability::CullFace); gl.DepthFunc(CompareFn::LEqual); gl.BlendFunc(BlendFn::SrcAlpha, BlendFn::OneMinusSrcAlpha); gl.PolygonOffset(1.0, 1.0); gl.LineWidth(1.5); }
GL_APICALL void GL_APIENTRY glGenerateMipmap (GLenum target) { CONTEXT_EXEC(GenerateMipmap(target)); }
CubeExample(void) : cube_instr(make_cube.Instructions()) , cube_indices(make_cube.Indices()) , projection_matrix(prog, "ProjectionMatrix") , camera_matrix(prog, "CameraMatrix") , model_matrix(prog, "ModelMatrix") { // Set the vertex shader source 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)" "{" " vertNormal = mat3(ModelMatrix)*Normal;" " gl_Position = ModelMatrix * Position;" " vertLight = LightPos - gl_Position.xyz;" " vertTexCoord = TexCoord * 6.0;" " gl_Position = ProjectionMatrix * CameraMatrix * gl_Position;" "}" ); // compile it vs.Compile(); // set the fragment shader source 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 = dot(vertLight, vertLight);" " float d = l != 0.0 ? dot(" " vertNormal, " " normalize(vertLight)" " ) / l : 0.0;" " vec3 c = vec3(0.9, 0.8, 0.2);" " vec4 t = texture(TexUnit, vertTexCoord);" " float a = 1.0 - sqrt(abs(d)), e;" " if(gl_FrontFacing)" " {" " e = d >= 0.0 ?" " d * mix(0.5, 1.0, t.a):" " (-0.9*d) * (1.0 - t.a);" " }" " else" " {" " e = d >= 0.0 ?" " (0.6*d) * (1.0 - t.a):" " (-0.7*d) * mix(0.5, 1.0, t.a);" " }" " float i = 0.1 + 9.0*e;" " fragColor = vec4(" " t.r*c.r*i, " " t.g*c.g*i, " " t.b*c.b*i, " " clamp(pow(t.a,2) + a*0.4, 0.0, 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(); verts.Bind(Buffer::Target::Array); { std::vector<GLfloat> data; GLuint n_per_vertex = make_cube.Positions(data); Buffer::Data(Buffer::Target::Array, data); VertexAttribArray attr(prog, "Position"); attr.Setup(n_per_vertex, DataType::Float); attr.Enable(); } normals.Bind(Buffer::Target::Array); { std::vector<GLfloat> data; GLuint n_per_vertex = make_cube.Normals(data); Buffer::Data(Buffer::Target::Array, data); VertexAttribArray attr(prog, "Normal"); attr.Setup(n_per_vertex, DataType::Float); attr.Enable(); } texcoords.Bind(Buffer::Target::Array); { std::vector<GLfloat> data; GLuint n_per_vertex = make_cube.TexCoordinates(data); Buffer::Data(Buffer::Target::Array, data); VertexAttribArray attr(prog, "TexCoord"); attr.Setup(n_per_vertex, DataType::Float); attr.Enable(); } // setup the texture { auto bound_tex = Bind(tex, Texture::Target::_2D); bound_tex.Image2D(images::LoadTexture("honeycomb")); bound_tex.GenerateMipmap(); bound_tex.MinFilter(TextureMinFilter::LinearMipmapLinear); bound_tex.MagFilter(TextureMagFilter::Linear); bound_tex.WrapS(TextureWrap::MirroredRepeat); bound_tex.WrapT(TextureWrap::MirroredRepeat); } // UniformSampler(prog, "TexUnit").Set(0); Uniform<Vec3f>(prog, "LightPos").Set(Vec3f(1.0f, 2.0f, 3.0f)); // gl.ClearColor(0.1f, 0.1f, 0.1f, 0.0f); gl.ClearDepth(1.0f); gl.Enable(Capability::DepthTest); gl.Enable(Capability::Blend); gl.BlendFunc( BlendFn::SrcAlpha, BlendFn::OneMinusSrcAlpha ); gl.Enable(Capability::CullFace); gl.FrontFace(make_cube.FaceWinding()); }
CloudExample(const ExampleParams& params) : sphere_instr(make_sphere.Instructions()) , sphere_indices(make_sphere.Indices()) , samples(25 + params.quality*params.quality*100) , positions(make_positions()) , sizes(make_sizes()) , cloud_tex(positions.size()) , light_path(make_light_path_cps()) { assert(positions.size() == sizes.size()); std::srand(123456); light_vs.Source( "#version 330\n" "in vec3 Position;" "uniform vec3 LightPos;" "uniform mat4 CameraMatrix, ProjectionMatrix;" "void main(void)" "{" " float s = 0.1;" " gl_Position = " " ProjectionMatrix*" " CameraMatrix*" " vec4(Position*s + LightPos, 1.0);" "}" ).Compile(); light_fs.Source( "#version 330\n" "out vec4 fragLight;" "void main(void)" "{" " fragLight = vec4(1.0, 1.0, 1.0, 1.0);" "}" ).Compile(); light_prog << light_vs << light_fs; light_prog.Link().Use(); light.Bind(); buffer.Bind(Buffer::Target::Array); { std::vector<GLfloat> data; GLuint n_per_vertex = make_sphere.Positions(data); Buffer::Data(Buffer::Target::Array, data); (light_prog|"Position").Setup(n_per_vertex, DataType::Float).Enable(); } cloud_vs.Source( "#version 330\n" "in vec4 Position;" "in float Size;" "uniform int SampleCount;" "uniform mat4 CameraMatrix;" "uniform vec4 ViewZ;" "out float vertZOffs;" "out float vertSize;" "void main(void)" "{" " float hp = (SampleCount-1) * 0.5;" " vertZOffs = (gl_InstanceID - hp)/hp;" " vertSize = Size;" " gl_Position = vec4(" " Position.xyz +" " ViewZ.xyz*vertZOffs*Size*0.5," " 1.0" " );" "}" ).Compile(); cloud_gs.Source( "#version 330\n" "layout(points) in;" "layout(triangle_strip, max_vertices = 4) out;" "in float vertZOffs[];" "in float vertSize[];" "uniform vec3 LightPos;" "uniform mat4 CameraMatrix, ProjectionMatrix;" "uniform vec4 ViewX, ViewY, ViewZ;" "out vec3 geomTexCoord;" "out vec3 geomLightDir;" "void main(void)" "{" " float zo = vertZOffs[0];" " float s = vertSize[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.xyz * xo[i] * s * 0.5+" " ViewY.xyz * yo[j] * s * 0.5," " 1.0" " );" " gl_Position = ProjectionMatrix * CameraMatrix * v;" " geomLightDir = LightPos - v.xyz;" " geomTexCoord = " " vec3(0.5, 0.5, 0.5)+" " ViewX.xyz*(xo[i])*0.707+" " ViewY.xyz*(yo[j])*0.707+" " ViewZ.xyz*(zo )*0.707;" " EmitVertex();" " }" " EndPrimitive();" "}" ).Compile(); cloud_fs.Source( "#version 330\n" "uniform sampler3D CloudTex;" "in vec3 geomTexCoord;" "in vec3 geomLightDir;" "out vec4 fragColor;" "void main(void)" "{" " float d = texture(CloudTex, geomTexCoord).r;" " float o = 1.0;" " float s = 2.0/128.0;" " float r = s * 8.0;" " vec3 sampleOffs = normalize(geomLightDir) * s;" " vec3 samplePos = geomTexCoord;" " if(d > 0.01) while(o > 0.0)" " {" " if(samplePos.x<0.0 || samplePos.x>1.0)" " break;" " if(samplePos.y<0.0 || samplePos.y>1.0)" " break;" " if(samplePos.z<0.0 || samplePos.z>1.0)" " break;" " o -= texture(CloudTex, samplePos).r*r;" " samplePos += sampleOffs;" " }" " float a = 0.4 * d;" " float i = mix(0.2, 1.0, o);" " fragColor = vec4(i, i, i, a);" "}" ).Compile(); cloud_prog << cloud_vs << cloud_gs << cloud_fs; cloud_prog.Link().Use(); // bind the VAO for the clouds clouds.Bind(); // bind the VBO for the cloud positions pos_buffer.Bind(Buffer::Target::Array); { Buffer::Data(Buffer::Target::Array, positions); (cloud_prog|"Position").Setup(3, DataType::Float).Enable(); } // bind the VBO for the cloud sizes size_buffer.Bind(Buffer::Target::Array); { Buffer::Data(Buffer::Target::Array, sizes); (cloud_prog|"Size").Setup(1, DataType::Float).Enable(); } // set the number of samples cloud_prog/"SampleCount" = GLint(samples); Texture::Active(0); cloud_prog/"CloudTex" = 0; for(std::size_t i=0, n=positions.size(); i!=n; ++i) { auto bound_tex = Bind(cloud_tex[i], Texture::Target::_3D); bound_tex.Image3D( images::Cloud( 128, 128, 128, Vec3f(0.1f, -0.5f, 0.3f), 0.5f ) ); bound_tex.GenerateMipmap(); bound_tex.MinFilter(TextureMinFilter::LinearMipmapLinear); bound_tex.MagFilter(TextureMagFilter::Linear); bound_tex.BorderColor(Vec4f(0.0f, 0.0f, 0.0f, 0.0f)); bound_tex.WrapS(TextureWrap::ClampToBorder); bound_tex.WrapT(TextureWrap::ClampToBorder); bound_tex.WrapR(TextureWrap::ClampToBorder); } gl.ClearColor(0.0f, 0.1f, 0.2f, 0.0f); gl.ClearDepth(1.0f); gl.Disable(Capability::DepthTest); gl.Enable(Capability::Blend); gl.BlendFunc(BlendFn::SrcAlpha, BlendFn::OneMinusSrcAlpha); }
WavesExample(void) : make_plane( Vec3f( 0.0f, 0.0f, 0.0f), Vec3f(100.0f, 0.0f, 0.0f), Vec3f( 0.0f, 0.0f,-100.0f), 50, 50 ), plane_instr(make_plane.PatchInstructions()) , plane_indices(make_plane.PatchIndices()) , camera_matrix(prog, "CameraMatrix") , camera_position(prog, "CameraPosition") , anim_time(prog, "Time") , prev_period(-1) { VertexShader vs(ObjectDesc("Vertex")); vs.Source(StrLit( "#version 410\n" "uniform vec3 CameraPosition;" "in vec3 Position;" "out vec3 vertPosition;" "out float vertDistance;" "void main(void)" "{" " vertPosition = Position;" " vertDistance = distance(CameraPosition, Position);" "}" )); vs.Compile(); prog.AttachShader(vs); TessControlShader cs(ObjectDesc("TessControl")); cs.Source( "#version 410\n" "layout(vertices = 3) out;" "in vec3 vertPosition[];" "in float vertDistance[];" "out vec3 tecoPosition[];" "int tessLevel(float dist)" "{" " return clamp(int(150.0 / (dist+0.1)), 1, 10);" "}" "void main(void)" "{" " tecoPosition[gl_InvocationID] =" " vertPosition[gl_InvocationID];" " if(gl_InvocationID == 0)" " {" " gl_TessLevelInner[0] = tessLevel((" " vertDistance[0]+" " vertDistance[1]+" " vertDistance[2] " " )*0.333);" " gl_TessLevelOuter[0] = tessLevel((" " vertDistance[1]+" " vertDistance[2] " " )*0.5);" " gl_TessLevelOuter[1] = tessLevel((" " vertDistance[2]+" " vertDistance[0] " " )*0.5);" " gl_TessLevelOuter[2] = tessLevel((" " vertDistance[0]+" " vertDistance[1] " " )*0.5);" " }" "}" ); cs.Compile(); prog.AttachShader(cs); TessEvaluationShader es(ObjectDesc("TessEvaluation")); es.Source( "#version 410\n" "#define MaxWaves 5\n" "layout(triangles, equal_spacing, ccw) in;" "uniform mat4 ProjectionMatrix, CameraMatrix;" "uniform vec3 LightPosition;" "uniform vec3 CameraPosition;" "uniform float Time;" "uniform int WaveCount;" "uniform vec3 WaveDirections[MaxWaves];" "uniform vec3 WaveDimensions[MaxWaves];" "in vec3 tecoPosition[];" "out vec3 teevNormal;" "out vec3 teevLightDir;" "out vec3 teevViewDir;" "out float teevDistance;" "void main(void)" "{" " const vec3 Up = vec3(0.0, 1.0, 0.0);" " vec3 Position =" " gl_TessCoord.x * tecoPosition[0]+" " gl_TessCoord.y * tecoPosition[1]+" " gl_TessCoord.z * tecoPosition[2];" " vec3 Pos = Position;" " vec3 Nml = Up;" " for(int w=0; w!=WaveCount; ++w)" " {" " vec3 Dir = WaveDirections[w];" " vec3 Dim = WaveDimensions[w];" " float Dist = dot(Position, Dir);" " float u = Dim.y*sin(Dist/Dim.x + Time*Dim.z);" " Pos += Up * u;" " float w = (Dim.y/Dim.x)*cos(Dist/Dim.x + Time*Dim.z);" " Nml -= Dir * w;" " float d = -0.125*Dim.x*sin(2.0*Dist/Dim.x + Time*Dim.z);" " Pos += Dir * d;" " }" " gl_Position = " " ProjectionMatrix *" " CameraMatrix *" " vec4(Pos, 1.0);" " teevNormal = normalize(Nml);" " teevLightDir = normalize(LightPosition - Pos);" " teevViewDir = normalize(CameraPosition - Pos);" " teevDistance = distance(CameraPosition, Pos);" "}" ); es.Compile(); prog.AttachShader(es); FragmentShader fs(ObjectDesc("Fragment")); fs.Source(StrLit( "#version 410\n" "uniform samplerCube EnvMap;" "in vec3 teevNormal;" "in vec3 teevLightDir;" "in vec3 teevViewDir;" "in float teevDistance;" "out vec3 fragColor;" "void main(void)" "{" " float Dim = clamp(30.0/teevDistance, 0.0, 1.0);" " float LightRefl = dot(reflect(-teevLightDir, teevNormal), teevViewDir);" " float LightHit = dot(teevNormal, teevLightDir);" " float Diffuse = clamp(LightHit+0.1, 0.0, 1.0);" " float Specular = pow(clamp(LightRefl, 0.0, 0.91), 32);" " vec3 Environ=texture(EnvMap,reflect(-teevViewDir, teevNormal)).rgb;" " vec3 WaterColor = vec3(0.4, 0.5, 0.5);" " vec3 LightColor = vec3(1.0, 1.0, 1.0);" " vec3 FogColor = vec3(0.9, 0.9, 0.9);" " vec3 WaveColor =" " LightColor*Specular+" " WaterColor*Diffuse+" " Environ*0.02;" " fragColor = mix(WaveColor, FogColor, 1.0-Dim);" "}" )); fs.Compile(); prog.AttachShader(fs); prog.Link(); prog.Use(); plane.Bind(); verts.Bind(Buffer::Target::Array); { std::vector<GLfloat> data; GLuint n_per_vertex = make_plane.Positions(data); Buffer::Data(Buffer::Target::Array, data); VertexAttribArray attr(prog, "Position"); attr.Setup(n_per_vertex, DataType::Float); attr.Enable(); } Uniform<Vec3f>(prog, "LightPosition").Set(-100.0, 100.0, 20.0); Uniform<Vec3f> wave_directions(prog, "WaveDirections"); Uniform<Vec3f> wave_dimensions(prog, "WaveDimensions"); Uniform<GLint>(prog, "WaveCount").Set(5); wave_directions[0] = Normalized(Vec3f(1.0f, 0.0f, 1.0f)); wave_dimensions[0] = Vec3f(5.0f, 1.5f, 1.2f); wave_directions[1] = Normalized(Vec3f(1.0f, 0.0f, 0.5f)); wave_dimensions[1] = Vec3f(4.0f, 0.8f, 1.2f); wave_directions[2] = Normalized(Vec3f(1.0f, 0.0f, 0.1f)); wave_dimensions[2] = Vec3f(2.0f, 0.5f, 2.4f); wave_directions[3] = Normalized(Vec3f(1.0f, 0.0f,-0.1f)); wave_dimensions[3] = Vec3f(1.5f, 0.2f, 3.7f); wave_directions[4] = Normalized(Vec3f(1.0f, 0.0f, 0.4f)); wave_dimensions[4] = Vec3f(1.1f, 0.2f, 4.7f); Texture::Active(0); { auto image = images::Squares(512, 512, 0.9f, 16, 16); auto bound_tex = Bind(env_map, Texture::Target::CubeMap); for(int i=0; i!=6; ++i) Texture::Image2D(Texture::CubeMapFace(i), image); bound_tex.GenerateMipmap(); bound_tex.MinFilter(TextureMinFilter::LinearMipmapLinear); bound_tex.MagFilter(TextureMagFilter::Linear); bound_tex.WrapS(TextureWrap::ClampToEdge); bound_tex.WrapT(TextureWrap::ClampToEdge); bound_tex.WrapR(TextureWrap::ClampToEdge); bound_tex.SwizzleG(TextureSwizzle::Red); bound_tex.SwizzleB(TextureSwizzle::Red); } UniformSampler(prog, "EnvMap").Set(0); gl.ClearColor(0.9f, 0.9f, 0.9f, 0.0f); gl.ClearDepth(1.0f); gl.Enable(Capability::DepthTest); }
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" "uniform sampler2D SmokeTex;" "in vec2 geomTexCoord;" "in float geomAge;" "in float geomLightVal;" "in float geomLightBias;" "out vec4 fragColor;" "void main(void)" "{" " vec3 c = texture(SmokeTex, geomTexCoord).rgb;" " float depth = c.g - c.r;" " if(depth == 0.0) discard;" " float density = min(depth * c.b * 2.0, 1.0);" " float intensity = min(" " max(" " geomLightVal*0.5+" " geomLightBias," " 0.0" " )+max(" " -geomLightVal*" " (1.0 - density)*" " geomLightBias * 5.0," " 0.0" " )," " 1.0" " ) + 0.1;" " fragColor = vec4(" " intensity," " intensity," " intensity," " (1.0 - geomAge)*density" " );" "}" ); // compile it fs.Compile(); // attach the shaders to the program prog.AttachShader(vs); prog.AttachShader(gs); prog.AttachShader(fs); // link and use it prog.Link(); prog.Use(); // bind the VAO for the particles particles.Bind(); // bind the VBO for the particle positions pos_buf.Bind(Buffer::Target::Array); { Buffer::Data(Buffer::Target::Array, positions); VertexAttribArray attr(prog, "Position"); attr.Setup<Vec3f>(); attr.Enable(); } // bind the VBO for the particle ages age_buf.Bind(Buffer::Target::Array); { Buffer::Data(Buffer::Target::Array, ages); VertexAttribArray attr(prog, "Age"); attr.Setup<GLfloat>(); attr.Enable(); } // bind the VBO for the particle identifiers id_buf.Bind(Buffer::Target::Array); { Buffer::Data(Buffer::Target::Array, ids); VertexAttribArray attr(prog, "Id"); attr.Setup<GLint>(); attr.Enable(); } Texture::Active(0); UniformSampler(prog, "SmokeTex").Set(0); { auto bound_tex = Bind(smoke_tex, Texture::Target::_2D); bound_tex.Image2D( images::Cloud2D( images::Cloud( 128, 128, 128, Vec3f(0.1f, -0.5f, 0.3f), 0.5f ) ) ); bound_tex.GenerateMipmap(); bound_tex.MinFilter(TextureMinFilter::LinearMipmapLinear); bound_tex.MagFilter(TextureMagFilter::Linear); bound_tex.BorderColor(Vec4f(0.0f, 0.0f, 0.0f, 0.0f)); bound_tex.WrapS(TextureWrap::ClampToBorder); bound_tex.WrapT(TextureWrap::ClampToBorder); } // gl.ClearColor(0.0f, 0.1f, 0.2f, 0.0f); gl.ClearDepth(1.0f); gl.Enable(Capability::DepthTest); gl.Enable(Capability::Blend); gl.BlendFunc(BlendFn::SrcAlpha, BlendFn::OneMinusSrcAlpha); }
CubeExample(void) : cube_instr(make_cube.Instructions()) , cube_indices(make_cube.Indices()) , projection_matrix(prog, "ProjectionMatrix") , camera_matrix(prog, "CameraMatrix") , model_matrix(prog, "ModelMatrix") , light_pos(prog, "LightPos") { namespace se = oglplus::smart_enums; // Set the vertex shader source vs.Source( "#version 330\n" "uniform mat4 ProjectionMatrix, CameraMatrix, ModelMatrix;" "in vec4 Position;" "in vec3 Normal;" "in vec3 Tangent;" "in vec2 TexCoord;" "out vec3 vertLight;" "out vec2 vertTexCoord;" "out mat3 NormalMatrix;" "uniform vec3 LightPos;" "void main(void)" "{" " vec3 fragNormal = mat3(ModelMatrix) * Normal;" " vec3 fragTangent = mat3(ModelMatrix) * Tangent;" " NormalMatrix[0] = fragTangent;" " NormalMatrix[1] = cross(fragNormal, fragTangent);" " NormalMatrix[2] = fragNormal;" " gl_Position = ModelMatrix * Position;" " vertLight = LightPos - gl_Position.xyz;" " vertTexCoord = TexCoord;" " gl_Position = ProjectionMatrix * CameraMatrix * gl_Position;" "}" ); // compile it vs.Compile(); // set the fragment shader source fs.Source( "#version 330\n" "uniform sampler2D ColorTex, NormalTex;" "in mat3 NormalMatrix;" "in vec3 vertLight;" "in vec2 vertTexCoord;" "out vec4 fragColor;" "void main(void)" "{" " float l = dot(vertLight, vertLight);" " vec3 n = texture(NormalTex, vertTexCoord).xyz;" " vec3 finalNormal = NormalMatrix * n;" " float d = (l > 0.0) ? dot(" " normalize(vertLight), " " finalNormal" " ) / l : 0.0;" " float i = 0.2 + 4.5*max(d, 0.0);" " vec4 t = texture(ColorTex, vertTexCoord);" " fragColor = vec4(t.rgb*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(); verts.Bind(Buffer::Target::Array); { std::vector<GLfloat> data; GLuint n_per_vertex = make_cube.Positions(data); Buffer::Data(se::Array(), data); VertexAttribArray attr(prog, "Position"); attr.Setup<GLfloat>(n_per_vertex); attr.Enable(); } normals.Bind(Buffer::Target::Array); { std::vector<GLfloat> data; GLuint n_per_vertex = make_cube.Normals(data); Buffer::Data(se::Array(), data); VertexAttribArray attr(prog, "Normal"); attr.Setup<GLfloat>(n_per_vertex); attr.Enable(); } tangents.Bind(Buffer::Target::Array); { std::vector<GLfloat> data; GLuint n_per_vertex = make_cube.Tangents(data); Buffer::Data(se::Array(), data); VertexAttribArray attr(prog, "Tangent"); attr.Setup<GLfloat>(n_per_vertex); attr.Enable(); } texcoords.Bind(Buffer::Target::Array); { std::vector<GLfloat> data; GLuint n_per_vertex = make_cube.TexCoordinates(data); Buffer::Data(se::Array(), data); VertexAttribArray attr(prog, "TexCoord"); attr.Setup<GLfloat>(n_per_vertex); attr.Enable(); } // setup the textures { Texture::Active(0); UniformSampler(prog, "ColorTex").Set(0); auto bound_tex = Bind(colorTex, se::_2D()); bound_tex.Image2D(images::LoadTexture("wooden_crate")); bound_tex.GenerateMipmap(); bound_tex.MinFilter(se::LinearMipmapLinear()); bound_tex.MagFilter(se::Linear()); bound_tex.WrapS(se::Repeat()); bound_tex.WrapT(se::Repeat()); } { Texture::Active(1); UniformSampler(prog, "NormalTex").Set(1); auto bound_tex = Bind(normalTex, se::_2D()); bound_tex.Image2D( images::NormalMap( images::LoadTexture("wooden_crate-hmap"), images::NormalMap::FromRed() ) ); bound_tex.GenerateMipmap(); bound_tex.MinFilter(se::LinearMipmapLinear()); bound_tex.MagFilter(se::Linear()); bound_tex.WrapS(se::Repeat()); bound_tex.WrapT(se::Repeat()); } // gl.ClearColor(0.1f, 0.1f, 0.1f, 0.0f); gl.ClearDepth(1.0f); gl.Enable(se::DepthTest()); gl.Enable(se::CullFace()); gl.FrontFace(make_cube.FaceWinding()); }
bool Texture::LoadCubeVerticalCross(const std::string & path, const TextureInfo & info, std::ostream & error) { SDL_Surface * surface = IMG_Load(path.c_str()); if (!surface) { error << "Error loading texture file: " + path << std::endl; error << IMG_GetError() << std::endl; return false; } unsigned id = 0; glGenTextures(1, &id); CheckForOpenGLErrors("Cubemap ID generation", error); glBindTexture(GL_TEXTURE_CUBE_MAP, id); glEnable(GL_TEXTURE_CUBE_MAP); // set sampler glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); if (info.mipmap) { glTexParameteri(GL_TEXTURE_CUBE_MAP,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); if (!glGenerateMipmap) glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_GENERATE_MIPMAP, GL_TRUE); } m_id = id; m_w = surface->w / 3; m_h = surface->h / 4; // upload texture unsigned bytespp = surface->format->BytesPerPixel; std::vector<unsigned char> cubeface(m_w * m_h * bytespp); for (int i = 0; i < 6; ++i) { // detect channels int format = GL_RGB; switch (bytespp) { case 1: format = GL_LUMINANCE; break; case 2: format = GL_LUMINANCE_ALPHA; break; case 3: format = GL_RGB; break; case 4: format = GL_RGBA; break; default: error << "Texture has unknown format: " + path << std::endl; return false; break; } int offsetx = 0; int offsety = 0; GLenum targetparam; if (i == 0) { targetparam = GL_TEXTURE_CUBE_MAP_NEGATIVE_X; offsetx = 0; offsety = m_h; } else if (i == 1) { targetparam = GL_TEXTURE_CUBE_MAP_POSITIVE_X; offsetx = m_w*2; offsety = m_h; } else if (i == 2) { targetparam = GL_TEXTURE_CUBE_MAP_NEGATIVE_Y; offsetx = m_w; offsety = m_h*2; } else if (i == 3) { targetparam = GL_TEXTURE_CUBE_MAP_POSITIVE_Y; offsetx = m_w; offsety = 0; } else if (i == 4) { targetparam = GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; offsetx = m_w; offsety = m_h*3; } else if (i == 5) { targetparam = GL_TEXTURE_CUBE_MAP_POSITIVE_Z; offsetx = m_w; offsety = m_h; } else { error << "Texture has unknown format: " + path << std::endl; return false; } if (i == 4) //special case for negative z { for (unsigned yi = 0; yi < m_h; yi++) { for (unsigned xi = 0; xi < m_w; xi++) { for (unsigned ci = 0; ci < bytespp; ci++) { int idx1 = ((m_h - yi - 1) + offsety) * surface->w * bytespp + (m_w - xi - 1 + offsetx) * bytespp + ci; int idx2 = yi * m_w * bytespp + xi * bytespp + ci; cubeface[idx2] = ((unsigned char *)(surface->pixels))[idx1]; } } } } else { for (unsigned yi = 0; yi < m_h; yi++) { for (unsigned xi = 0; xi < m_w; xi++) { for (unsigned ci = 0; ci < bytespp; ci++) { int idx1 = (yi + offsety) * surface->w * bytespp + (xi + offsetx) * bytespp + ci; int idx2 = yi * m_w * bytespp + xi * bytespp + ci; cubeface[idx2] = ((unsigned char *)(surface->pixels))[idx1]; } } } } glTexImage2D(targetparam, 0, format, m_w, m_h, 0, format, GL_UNSIGNED_BYTE, &cubeface[0]); } if (info.mipmap) GenerateMipmap(GL_TEXTURE_CUBE_MAP); glDisable(GL_TEXTURE_CUBE_MAP); CheckForOpenGLErrors("Cubemap creation", error); SDL_FreeSurface(surface); return true; }
bool Texture::LoadDDS(const std::string & path, const TextureInfo & info, std::ostream & error) { std::ifstream file(path.c_str(), std::ifstream::in | std::ifstream::binary); if (!file) return false; // test for dds magic value char magic[4]; file.read(magic, 4); if (!IsDDS(magic, 4)) return false; // get length of file: file.seekg (0, file.end); const unsigned long length = file.tellg(); file.seekg (0, file.beg); // read file into memory std::vector<char> data(length); file.read(&data[0], length); // load dds const char * texdata(0); unsigned long texlen(0); unsigned format(0), width(0), height(0), levels(0); if (!ReadDDS( (void*)&data[0], length, (const void*&)texdata, texlen, format, width, height, levels)) { return false; } // set properties m_w = width; m_h = height; m_scale = 1.0f; m_alpha = (format != GL_BGR); m_cube = false; // gl3 renderer expects srgb unsigned iformat = format; if (info.srgb) { if (format == GL_BGR) iformat = GL_SRGB8; else if (format == GL_BGRA) iformat = GL_SRGB8_ALPHA8; else if (format == GL_COMPRESSED_RGBA_S3TC_DXT1_EXT) iformat = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT; else if (format == GL_COMPRESSED_RGBA_S3TC_DXT3_EXT) iformat = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT; else if (format == GL_COMPRESSED_RGBA_S3TC_DXT5_EXT) iformat = GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT; } // load texture assert(!m_id); glGenTextures(1, &m_id); CheckForOpenGLErrors("Texture ID generation", error); glBindTexture(GL_TEXTURE_2D, m_id); SetSampler(info, levels > 1); const char * idata = texdata; unsigned blocklen = 16 * texlen / (width * height); unsigned ilen = texlen; unsigned iw = width; unsigned ih = height; for (unsigned i = 0; i < levels; ++i) { if (format == GL_BGR || format == GL_BGRA) { // fixme: support compression here? ilen = iw * ih * blocklen / 16; glTexImage2D(GL_TEXTURE_2D, i, iformat, iw, ih, 0, format, GL_UNSIGNED_BYTE, idata); } else { ilen = std::max(1u, iw / 4) * std::max(1u, ih / 4) * blocklen; glCompressedTexImage2D(GL_TEXTURE_2D, i, iformat, iw, ih, 0, ilen, idata); } CheckForOpenGLErrors("Texture creation", error); idata += ilen; iw = std::max(1u, iw / 2); ih = std::max(1u, ih / 2); } // force mipmaps for GL3 if (levels == 1) GenerateMipmap(GL_TEXTURE_2D); return true; }
TriangleExample(void) : projection_matrix(prog, "ProjectionMatrix") , camera_matrix(prog, "CameraMatrix") , camera_position(prog, "CameraPosition") , light_position(prog, "LightPosition") , shape( List("Position")("TexCoord").Get(), shapes::Plane( Vec3f(), Vec3f(1.0f, 0.0f, 0.0f), Vec3f(0.0f, 0.0f,-1.0f), 32, 32 ) ) { VertexShader vs; vs.Source(StrLit( "#version 330\n" "in vec3 Position;" "in vec2 TexCoord;" "out vec2 vertTexCoord;" "void main(void)" "{" " gl_Position = vec4(Position, 1.0);" " vertTexCoord = TexCoord;" "}" )).Compile(); prog.AttachShader(vs); GeometryShader gs; gs.Source(StrLit( "#version 330\n" "#extension GL_ARB_gpu_shader5 : enable\n" "layout(triangles, invocations = 7) in;" "layout(triangle_strip, max_vertices = 21) out;" "uniform mat4 ProjectionMatrix, CameraMatrix;" "mat4 Matrix = ProjectionMatrix * CameraMatrix;" "uniform vec3 CameraPosition;" "in vec2 vertTexCoord[3];" "out gl_PerVertex {" " vec4 gl_Position;" " float gl_ClipDistance[3];" "};" "flat out mat3 geomPositionFront;" "flat out mat3 geomTexCoordFront;" "flat out vec3 geomWFront;" "noperspective out vec3 geomBarycentric;" "out vec3 geomPosition;" "out vec3 geomTexCoord;" "void main(void)" "{" " vec4 world_pos[8*3];" " vec3 tex_coord[8*3];" " vec4 view_pos[8*3];" " vec3 screen_pos[8*3];" " bool front_facing[8];" " int ft = gl_InvocationID+1;" " for(int pass=0; pass!=2; ++pass)" " {" " bool first = pass == 0;" " if(((ft == 0) && first) || (((ft != 0) && !first)))" " {" " for(int v=0; v!=3; ++v)" " {" " int w = 2-v;" " world_pos[0+v] = gl_in[w].gl_Position;" " tex_coord[0+v] = vec3(vertTexCoord[w], 0.0);" " }" " }" " vec4 n = vec4(-0.15 * 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 " " )), 0.0);" " if(((ft == 1) && first) || (((ft != 1) && !first)))" " {" " for(int v=0; v!=3; ++v)" " {" " world_pos[3+v] = gl_in[v].gl_Position + n;" " tex_coord[3+v] = vec3(vertTexCoord[v], 1.0);" " }" " }" " for(int v=0; v!=3; ++v)" " {" " int w = (v+1)%3;" " int k = 2+2*v;" " if(((ft == k) && first) || (((ft != k) && !first)))" " {" " world_pos[6+0+v*6] = gl_in[v].gl_Position;" " tex_coord[6+0+v*6] = vec3(vertTexCoord[v], 0.0);" " world_pos[6+1+v*6] = gl_in[w].gl_Position;" " tex_coord[6+1+v*6] = vec3(vertTexCoord[w], 0.0);" " world_pos[6+2+v*6] = gl_in[v].gl_Position + n;" " tex_coord[6+2+v*6] = vec3(vertTexCoord[v], 1.0);" " }" " k = 3+2*v;" " if(((ft == k) && first) || (((ft != k) && !first)))" " {" " world_pos[6+3+v*6] = gl_in[w].gl_Position;" " tex_coord[6+3+v*6] = vec3(vertTexCoord[w], 0.0);" " world_pos[6+4+v*6] = gl_in[w].gl_Position + n;" " tex_coord[6+4+v*6] = vec3(vertTexCoord[w], 1.0);" " world_pos[6+5+v*6] = gl_in[v].gl_Position + n;" " tex_coord[6+5+v*6] = vec3(vertTexCoord[v], 1.0);" " }" " }" " for(int t=first?ft:0; t!=8; ++t)" " {" " if(!first && (t == ft)) continue;" " int o = t*3;" " for(int v=0; v!=3; ++v)" " {" " int w = o+v;" " view_pos[w] = Matrix * world_pos[w];" " screen_pos[w] = view_pos[w].xyz/view_pos[w].w;" " }" " front_facing[t] = cross(" " screen_pos[o+1]-screen_pos[o+0]," " screen_pos[o+2]-screen_pos[o+0] " " ).z < 0.0;" " if(first) break;" " }" " if(first && !front_facing[ft]) return;" " }" " int o = ft*3;" " vec4 clip_plane[3];" " for(int v=0; v!=3; ++v)" " {" " int w = (v+1)%3;" " vec3 p0 = world_pos[o+v].xyz;" " vec3 p1 = world_pos[o+w].xyz;" " vec3 p2 = CameraPosition;" " vec3 pv = normalize(cross(p1-p0, p2-p0));" " clip_plane[v] = vec4(pv, -dot(pv, p0));" " }" " vec3 lo = CameraPosition;" " vec3 p0 = world_pos[o+0].xyz;" " vec3 pu = world_pos[o+1].xyz-p0;" " vec3 pv = world_pos[o+2].xyz-p0;" " vec3 lp = lo-p0;" " float w0 = view_pos[o+0].w;" " float wu = view_pos[o+1].w-w0;" " float wv = view_pos[o+2].w-w0;" " vec3 t0 = tex_coord[o+0];" " vec3 tu = tex_coord[o+1]-t0;" " vec3 tv = tex_coord[o+2]-t0;" " for(int bt=0; bt!=8; ++bt)" " {" " int k = bt*3;" " if((ft != bt) && !front_facing[bt])" " {" " for(int v=0; v!=3; ++v)" " {" " vec3 lt = world_pos[k+v].xyz;" " mat3 im = mat3(lo-lt, pu, pv);" " vec3 ic = inverse(im)*lp;" " float s = ic.y;" " float t = ic.z;" " geomPositionFront[v] = p0+pu*s+pv*t;" " geomTexCoordFront[v] = t0+tu*s+tv*t;" " geomWFront[v] = w0+wu*s+wv*t;" " }" " for(int v=0; v!=3; ++v)" " {" " int w = k+v;" " gl_Position = view_pos[w];" " for(int c=0; c!=3; ++c)" " {" " gl_ClipDistance[c] = dot(" " clip_plane[c]," " world_pos[w]" " );" " }" " geomPosition = world_pos[w].xyz;" " geomTexCoord = tex_coord[w];" " geomBarycentric = vec3(0.0);" " geomBarycentric[v] = 1.0;" " EmitVertex();" " }" " EndPrimitive();" " }" " }" "}" )).Compile(); prog.AttachShader(gs); FragmentShader fs; fs.Source(StrLit( "#version 330\n" "uniform float Time;" "uniform sampler2D ColorMap;" "uniform sampler2D BumpMap;" "uniform vec3 LightPosition;" "flat in mat3 geomPositionFront;" "flat in mat3 geomTexCoordFront;" "flat in vec3 geomWFront;" "noperspective in vec3 geomBarycentric;" "in vec3 geomPosition;" "in vec3 geomTexCoord;" "out vec3 fragColor;" "vec3 vcdiv(vec3 a, vec3 b)" "{" " return vec3(a.x/b.x, a.y/b.y, a.z/b.z);" "}" "void main(void)" "{" " const vec3 one = vec3(1.0, 1.0, 1.0);" " vec3 bzfv = vcdiv(geomBarycentric,geomWFront);" " vec3 p0 = geomPosition;" " vec3 p1 = (geomPositionFront*bzfv)/dot(one,bzfv);" " vec3 tc0 = geomTexCoord;" " vec3 tc1 = (geomTexCoordFront*bzfv)/dot(one,bzfv);" " ivec2 ts = textureSize(BumpMap, 0);" " int mts = max(ts.x, ts.y);" " vec2 dtc = tc1.xy - tc0.xy;" " float mdtc = max(abs(dtc.x), abs(dtc.y));" " int nsam = max(min(int(mdtc*mts), mts/2), 1);" " float step = 1.0 / nsam;" " for(int s=0; s<=nsam; ++s)" " {" " vec3 tc = mix(tc1, tc0, s*step);" " vec4 bm = texture(BumpMap, tc.xy);" " if(tc.z <= bm.w)" " {" " vec3 p = mix(p1, p0, s*step);" " vec3 ldir = normalize(LightPosition - p);" " float l = max(dot(ldir, bm.xzy), 0.0)*1.3;" " fragColor = texture(ColorMap, tc.xy).rgb*l;" " return;" " }" " }" " discard;" "}" )).Compile(); prog.AttachShader(fs); prog.Link(); prog.Use(); shape.UseInProgram(prog); auto tex_image = images::LoadTexture("stones_color_hmap"); Texture::Active(0); try { UniformSampler(prog, "ColorMap").Set(0); auto bound_tex = Bind(color_tex, Texture::Target::_2D); bound_tex.Image2D(tex_image); bound_tex.GenerateMipmap(); bound_tex.MinFilter(TextureMinFilter::LinearMipmapLinear); bound_tex.MagFilter(TextureMagFilter::Linear); bound_tex.WrapS(TextureWrap::Repeat); bound_tex.WrapT(TextureWrap::Repeat); } catch(Error&){ } Texture::Active(1); try { UniformSampler(prog, "BumpMap").Set(1); auto bound_tex = Bind(bump_tex, Texture::Target::_2D); bound_tex.Image2D( images::NormalMap( tex_image, images::NormalMap::FromAlpha() ) ); bound_tex.GenerateMipmap(); bound_tex.MinFilter(TextureMinFilter::LinearMipmapLinear); bound_tex.MagFilter(TextureMagFilter::Linear); bound_tex.WrapS(TextureWrap::Repeat); bound_tex.WrapT(TextureWrap::Repeat); } catch(Error&){ } gl.ClearColor(0.1f, 0.1f, 0.1f, 0.0f); gl.ClearDepth(1.0f); gl.Enable(Capability::DepthTest); gl.Disable(Capability::CullFace); gl.Enable(Functionality::ClipDistance, 0); gl.Enable(Functionality::ClipDistance, 1); gl.Enable(Functionality::ClipDistance, 2); }