Shows the usage of OGLplus and OALplusCopyright 2008-2014 Matus Chochlik. Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include "glut_glew_example.hpp"
struct OGLOALExampleSource
{
{
vs.Source(
"#version 330\n"
"uniform mat4 ProjectionMatrix, CameraMatrix;"
"uniform vec3 SourcePosition;"
"in vec4 Position;"
"void main(void)"
"{"
" vec4 TmpPos = Position;"
" TmpPos.xyz += SourcePosition;"
" gl_Position = ProjectionMatrix * CameraMatrix * TmpPos;"
"}"
).Compile();
fs.Source(
"#version 330\n"
"out vec3 fragColor;"
"void main(void)"
"{"
" fragColor = vec3(1, 1, 1);"
"}"
).Compile();
prog.AttachShader(vs).AttachShader(fs).Link().Use();
return prog;
}
oglplus::Uniform<oglplus::Mat4f> projection_matrix;
oglplus::Uniform<oglplus::Mat4f> camera_matrix;
oglplus::Uniform<oglplus::Vec3f> light_position;
{
);
}
OGLOALExampleSource(void)
: prog(make_prog())
, projection_matrix(prog, "ProjectionMatrix")
, camera_matrix(prog, "CameraMatrix")
, light_position(prog, "SourcePosition")
, shape(create_source())
, vao(shape.VAOForProgram(prog))
, alut(false)
, sound_buffer(alut.CreateBufferHelloWorld())
{
sound.Buffer(sound_buffer);
sound.Looping(true);
sound.ReferenceDistance(20);
sound.Play();
}
};
struct OGLOALExampleObjects
{
{
vs.Source(
"#version 330\n"
"uniform mat4 ProjectionMatrix, CameraMatrix;"
"uniform vec3 LightPosition;"
"in vec4 Position;"
"in vec3 Normal;"
"in vec2 TexCoord;"
"out vec3 vertNormal;"
"out vec3 vertLightDir;"
"out vec2 vertTexCoord;"
"void main(void)"
"{"
" gl_Position = ProjectionMatrix * CameraMatrix * Position;"
" vertNormal = Normal;"
" vertLightDir = LightPosition - Position.xyz;"
" vertTexCoord = TexCoord;"
"}"
).Compile();
fs.Source(
"#version 330\n"
"uniform sampler2D RandTex;"
"in vec3 vertNormal;"
"in vec3 vertLightDir;"
"in vec2 vertTexCoord;"
"out vec3 fragColor;"
"void main(void)"
"{"
" vec3 Normal = normalize(vertNormal);"
" vec3 LightDir = normalize(vertLightDir);"
" float Ambient = 0.4;"
" float Diffuse = max(dot(Normal, LightDir), 0.0);"
" float x = texture(RandTex, vertTexCoord).r*"
" (0.6+texture(RandTex, vertTexCoord/9.0).r*0.4)*"
" (0.4+texture(RandTex, vertTexCoord/37.0).r*0.6);"
" vec3 Color = mix(vec3(0.4, 0.3, 0.2), vec3(1.0, 0.9, 0.8), x);"
" fragColor = Color*(Ambient+Diffuse);"
"}"
).Compile();
prog.AttachShader(vs).AttachShader(fs).Link().Use();
return prog;
}
oglplus::Uniform<oglplus::Mat4f> projection_matrix;
oglplus::Uniform<oglplus::Mat4f> camera_matrix;
oglplus::Uniform<oglplus::Vec3f> light_position;
{
std::ifstream input_stream;
oglplus::OpenResourceFile(input_stream, "models", "monoliths", ".obj");
if(!input_stream.good())
throw std::runtime_error("Error opening file for reading");
input_stream,
oglplus::shapes::ObjMesh::LoadingOptions()
)
);
}
OGLOALExampleObjects(void)
: prog(make_prog())
, projection_matrix(prog, "ProjectionMatrix")
, camera_matrix(prog, "CameraMatrix")
, light_position(prog, "LightPosition")
, shape(load_objects())
, vao(shape.VAOForProgram(prog))
{
using namespace oglplus;
prog.Use();
Uniform<GLint>(prog, "RandTex").Set(0);
Texture::Active(0);
rand_tex<< TextureTarget::_2D
<< TextureMinFilter::LinearMipmapLinear
<< TextureMagFilter::Linear
<< TextureWrap::Repeat
;
}
};
class OGLOALExample
: public oglplus::StandaloneExample
{
private:
OGLOALExampleSource source;
OGLOALExampleObjects objects;
bool first_run;
public:
OGLOALExample(int, const char**)
: al(al_device)
, first_run(true)
{
gl.ClearColor(0.9f, 0.8f, 0.7f, 0.0f);
gl.ClearDepth(1.0f);
}
void Reshape(void)
{
oglplus::Degrees(110),
Aspect(),
1, 200
);
objects.prog.Use();
objects.projection_matrix.Set(projection);
source.prog.Use();
source.projection_matrix.Set(projection);
}
void Render(void)
{
gl.Clear().ColorBuffer().DepthBuffer();
double time = FrameTime();
double crad = 3.0, trad = 10.0, lrad = 15.0;
double ctime = time / 17.0;
double ttime = time / 23.0;
double ltime = time / 19.0;
);
);
);
camera_position,
target_position
);
listener.Position(camera_position);
listener.Velocity(camera_velocity);
listener.Orientation(
target_position-
camera_position,
);
objects.prog.Use();
objects.camera_matrix.Set(camera);
objects.light_position.Set(light_position);
objects.vao.Bind();
objects.shape.Draw();
oglplus::Query::Activator qa(
query,
oglplus::Query::Target::SamplesPassed
);
source.prog.Use();
source.camera_matrix.Set(camera);
source.light_position.Set(light_position);
source.sound.Position(light_position);
source.vao.Bind();
source.shape.Draw();
qa.Finish();
GLuint sp = 0;
query.WaitForResult(sp);
double sound_gain = double(sp)/300.0;
sound_gain = 0.2 + 0.8*sound_gain;
if(sound_gain > 1.0) sound_gain = 1.0;
source.sound.Gain(sound_gain);
if(first_run)
{
first_run = false;
}
else
{
camera_velocity = camera_position - old_camera_position;
old_camera_position = camera_position;
}
}
};
int main(int argc, char* argv[])
{
return oglplus::GlutGlewMain<OGLOALExample>(
"The usage of OGLplus with OALplus",
argc, argv
);
}