Skip to content
Overv edited this page Oct 3, 2012 · 3 revisions

Assets

Model and texture

Sample

#include <GL/OOGL.hpp>

int main()
{
	GL::Window window( 800, 600, "OpenGL Window", GL::WindowStyle::Close );
	GL::Context& gl = window.GetContext();
	gl.Enable( GL::Capability::DepthTest );

	// Shaders
	GL::Shader vert( GL::ShaderType::Vertex, GLSL(
		in vec3 position;
		in vec2 coords;
		out vec2 Coords;
		uniform mat4 trans;
		void main() {
			Coords = coords;
			gl_Position = trans * vec4( position, 1.0 );
		}
	) );
	GL::Shader frag( GL::ShaderType::Fragment, GLSL(
		in vec2 Coords;
		out vec4 outColor;
		uniform sampler2D tex;
		void main()
		{
			outColor = texture2D( tex, Coords );
		}
	) );
	GL::Program program( vert, frag );

	// Load model
	GL::Mesh object( "tank_normals.obj" );
	GL::VertexBuffer vbo( object, GL::BufferUsage::StaticDraw, [] ( const GL::Vertex& v, GL::VertexDataBuffer& data )
	{
		data.Vec3( v.Pos );
		data.Vec2( GL::Vec2( v.Tex.X, 1.0f - v.Tex.Y ) );
	} );

	// Load texture
	GL::Image tank( "tank.jpg" );
	GL::Texture tex( tank );
	gl.BindTexture( tex, 0 );

	// VAO
	GL::VertexArray vao;
	vao.BindAttribute( program.GetAttribute( "position" ), vbo, GL::Type::Float, 3, 5 * sizeof( float ), 0 );
	vao.BindAttribute( program.GetAttribute( "coords" ), vbo, GL::Type::Float, 2, 5 * sizeof( float ), 3 * sizeof( float ) );

	// Main loop
	GL::Event ev;
	while ( window.IsOpen() )
	{
		while ( window.GetEvent( ev ) );

		GL::Mat4 model;
		model.RotateZ( gl.Time() );
		GL::Mat4 view = GL::Mat4::LookAt( GL::Vec3( 150, 150, 120 ), GL::Vec3( 0, 0, 50 ), GL::Vec3( 0, 0, 1 ) );
		GL::Mat4 proj = GL::Mat4::Perspective( GL::Rad( 60 ), 800.0f / 600.0f, 0.1f, 1000.0f );

		program.SetUniform( program.GetUniform( "trans" ), proj * view * model );

		gl.Clear();
		gl.DrawArrays( vao, GL::Primitive::Triangles, 0, object.VertexCount() );
		window.Present();
	}

	return 0;
}
![lol](http://i.imgur.com/hvPdw.png)
Clone this wiki locally