CubeExample(void)
	 : cube_instr(make_cube.Instructions())
	 , cube_indices(make_cube.Indices())
	 , prog(make_prog())
	 , projection_matrix(prog, "ProjectionMatrix")
	 , camera_matrix(prog, "CameraMatrix")
	 , model_matrix(prog, "ModelMatrix")
	 , light_pos(prog, "LightPos")
	{
		gl.Bind(cube);

		gl.Bind(Buffer::Target::Array, verts);
		{
			std::vector<GLfloat> data;
			GLuint n_per_vertex = make_cube.Positions(data);
			gl.Current(Buffer::Target::Array).Data(data);
			VertexArrayAttrib attr(prog, "Position");
			attr.Setup<GLfloat>(n_per_vertex);
			attr.Enable();
		}

		gl.Bind(Buffer::Target::Array, normals);
		{
			std::vector<GLfloat> data;
			GLuint n_per_vertex = make_cube.Normals(data);
			gl.Current(Buffer::Target::Array).Data(data);
			VertexArrayAttrib attr(prog, "Normal");
			attr.Setup<GLfloat>(n_per_vertex);
			attr.Enable();
		}

		gl.Bind(Buffer::Target::Array, tangents);
		{
			std::vector<GLfloat> data;
			GLuint n_per_vertex = make_cube.Tangents(data);
			gl.Current(Buffer::Target::Array).Data(data);
			VertexArrayAttrib attr(prog, "Tangent");
			attr.Setup<GLfloat>(n_per_vertex);
			attr.Enable();
		}

		gl.Bind(Buffer::Target::Array, texcoords);
		{
			std::vector<GLfloat> data;
			GLuint n_per_vertex = make_cube.TexCoordinates(data);
			gl.Current(Buffer::Target::Array).Data(data);
			VertexArrayAttrib attr(prog, "TexCoord");
			attr.Setup<GLfloat>(n_per_vertex);
			attr.Enable();
		}

		// setup the textures
		{
			Texture::Active(0);
			UniformSampler(prog, "ColorTex").Set(0);

			gl.Bind(Texture::Target::_2D, colorTex);
			gl.Current(Texture::Target::_2D)
				.MinFilter(TextureMinFilter::LinearMipmapLinear)
				.MagFilter(TextureMagFilter::Linear)
				.WrapS(TextureWrap::Repeat)
				.WrapT(TextureWrap::Repeat)
				.Image2D(images::LoadTexture("wooden_crate"))
				.GenerateMipmap();
		}
		{
			Texture::Active(1);
			UniformSampler(prog, "NormalTex").Set(1);

			gl.Bind(Texture::Target::_2D, normalTex);
			gl.Current(Texture::Target::_2D)
				.MinFilter(TextureMinFilter::LinearMipmapLinear)
				.MagFilter(TextureMagFilter::Linear)
				.WrapS(TextureWrap::Repeat)
				.WrapT(TextureWrap::Repeat)
				.Image2D(
					images::NormalMap(
						images::LoadTexture("wooden_crate-hmap")
					)
				).GenerateMipmap();
		}
		//
		gl.ClearColor(0.1f, 0.1f, 0.1f, 0.0f);
		gl.ClearDepth(1.0f);
		gl.Enable(Capability::DepthTest);

		gl.Enable(Capability::CullFace);
		gl.FrontFace(make_cube.FaceWinding());
	}
Beispiel #2
0
	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());
	}
    CubeExample()
      : cube_instr(make_cube.Instructions())
      , cube_indices(make_cube.Indices())
      , prog(make())
      , projection_matrix(prog, "ProjectionMatrix")
      , camera_matrix(prog, "CameraMatrix")
      , model_matrix(prog, "ModelMatrix")
      , light_pos(prog, "LightPos") {
        // bind the VAO for the cube
        gl.Bind(cube);

        gl.Bind(Buffer::Target::Array, verts);
        {
            std::vector<GLfloat> data;
            GLuint n_per_vertex = make_cube.Positions(data);
            Buffer::Data(Buffer::Target::Array, data);
            (prog | "Position").Setup<GLfloat>(n_per_vertex).Enable();
        }

        gl.Bind(Buffer::Target::Array, normals);
        {
            std::vector<GLfloat> data;
            GLuint n_per_vertex = make_cube.Normals(data);
            Buffer::Data(Buffer::Target::Array, data);
            (prog | "Normal").Setup<GLfloat>(n_per_vertex).Enable();
        }

        gl.Bind(Buffer::Target::Array, tangents);
        {
            std::vector<GLfloat> data;
            GLuint n_per_vertex = make_cube.Tangents(data);
            Buffer::Data(Buffer::Target::Array, data);
            (prog | "Tangent").Setup<GLfloat>(n_per_vertex).Enable();
        }

        gl.Bind(Buffer::Target::Array, texcoords);
        {
            std::vector<GLfloat> data;
            GLuint n_per_vertex = make_cube.TexCoordinates(data);
            Buffer::Data(Buffer::Target::Array, data);
            (prog | "TexCoord").Setup<GLfloat>(n_per_vertex).Enable();
        }

        {
            auto img = images::SphereBumpMap(512, 512, 2, 2);
            Uniform<GLsizei>(prog, "BumpTexWidth").Set(img.Width());
            Uniform<GLsizei>(prog, "BumpTexHeight").Set(img.Height());
            UniformSampler(prog, "BumpTex").Set(0);
            Texture::Active(0);

            gl.Bound(Texture::Target::_2D, bumpTex)
              .MinFilter(TextureMinFilter::LinearMipmapLinear)
              .MagFilter(TextureMagFilter::Linear)
              .WrapS(TextureWrap::Repeat)
              .WrapT(TextureWrap::Repeat)
              .Image2D(img)
              .GenerateMipmap();
        }
        //
        gl.ClearColor(0.1f, 0.1f, 0.1f, 0.0f);
        gl.ClearDepth(1.0f);
        gl.Enable(Capability::DepthTest);

        gl.Enable(Capability::CullFace);
        gl.FrontFace(make_cube.FaceWinding());
    }