diff --git a/CMakeLists.txt b/CMakeLists.txt index aa89fb572..c78506e94 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,7 @@ cmake_minimum_required(VERSION 2.6) project(Magnum) +option(BUILD_EXAMPLES "Build examples (requires GLUT)." OFF) option(BUILD_TESTS "Build unit tests (requires Qt4)." OFF) if(BUILD_TESTS) @@ -16,7 +17,20 @@ if(BUILD_TESTS) endif() set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${Magnum_SOURCE_DIR}/modules/") - include(MagnumMacros) add_subdirectory(src) + +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src) + +if(BUILD_EXAMPLES) + find_package(GLUT) + + if(NOT GLUT_FOUND) + message(WARNING "GLUT is required for building examples. No examples will be build.") + set(BUILD_EXAMPLES OFF) + else() + add_subdirectory(examples) + endif() + +endif() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt new file mode 100644 index 000000000..fd9ac19fc --- /dev/null +++ b/examples/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(triangle) diff --git a/examples/triangle/CMakeLists.txt b/examples/triangle/CMakeLists.txt new file mode 100644 index 000000000..6cf10b04c --- /dev/null +++ b/examples/triangle/CMakeLists.txt @@ -0,0 +1,2 @@ +add_executable(triangleExample main.cpp IdentityShader.cpp Triangle.cpp) +target_link_libraries(triangleExample Magnum ${GLUT_LIBRARY}) diff --git a/examples/triangle/IdentityShader.cpp b/examples/triangle/IdentityShader.cpp new file mode 100644 index 000000000..fd96449bd --- /dev/null +++ b/examples/triangle/IdentityShader.cpp @@ -0,0 +1,36 @@ +/* + Copyright © 2010 Vladimír Vondruš + + This file is part of Magnum. + + Magnum is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 3 + only, as published by the Free Software Foundation. + + Magnum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License version 3 for more details. +*/ + +#include "IdentityShader.h" + +namespace Magnum { namespace Examples { + +IdentityShader::IdentityShader() { + Shader* vertexShader = Shader::fromFile(Shader::Vertex, "identity.vert"); + Shader* fragmentShader = Shader::fromFile(Shader::Fragment, "identity.frag"); + + loadShader(vertexShader); + loadShader(fragmentShader); + + bindAttribute(Vertex, "vertex"); + bindAttribute(Color, "color"); + + link(); + + delete vertexShader; + delete fragmentShader; +} + +}} diff --git a/examples/triangle/IdentityShader.h b/examples/triangle/IdentityShader.h new file mode 100644 index 000000000..0d34cf714 --- /dev/null +++ b/examples/triangle/IdentityShader.h @@ -0,0 +1,34 @@ +#ifndef Magnum_Examples_IdentityShader_h +#define Magnum_Examples_IdentityShader_h +/* + Copyright © 2010 Vladimír Vondruš + + This file is part of Magnum. + + Magnum is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 3 + only, as published by the Free Software Foundation. + + Magnum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License version 3 for more details. +*/ + +#include "AbstractShaderProgram.h" + +namespace Magnum { namespace Examples { + +class IdentityShader: public AbstractShaderProgram { + public: + enum Attributes { + Vertex = 1, + Color = 2 + }; + + IdentityShader(); +}; + +}} + +#endif diff --git a/examples/triangle/Triangle.cpp b/examples/triangle/Triangle.cpp new file mode 100644 index 000000000..3713f1ecf --- /dev/null +++ b/examples/triangle/Triangle.cpp @@ -0,0 +1,44 @@ +/* + Copyright © 2010 Vladimír Vondruš + + This file is part of Magnum. + + Magnum is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 3 + only, as published by the Free Software Foundation. + + Magnum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License version 3 for more details. +*/ + +#include "Triangle.h" + +#include "Buffer.h" + +namespace Magnum { namespace Examples { + +Triangle::Triangle(AbstractObject* parent): AbstractObject(parent), mesh(Mesh::Triangles, 3) { + /* Vertices and colors, interleaved */ + Vector4 data[] = { + Vector4(-0.5f, -0.5f, 0.0f), Vector4(1.0f, 0.0f, 0.0f), /* Red lower left vertex */ + Vector4(0.5f, -0.5f, 0.0f), Vector4(0.0f, 1.0f, 0.0f), /* Green lower right vertex */ + Vector4(0.0f, 0.5f, 0.0f), Vector4(0.0f, 0.0f, 1.0f) /* Blue top vertex */ + }; + + /* Fill the mesh with data */ + Buffer* buffer = mesh.addBuffer(true); + buffer->setData(sizeof(data), data, Buffer::DrawStatic); + + /* Bind attributes (first vertex data, then color data) */ + mesh.bindAttribute(buffer, IdentityShader::Vertex); + mesh.bindAttribute(buffer, IdentityShader::Color); +} + +void Triangle::draw(const Matrix4& transformationMatrix, const Matrix4& projectionMatrix) { + shader.use(); + mesh.draw(); +} + +}} diff --git a/examples/triangle/Triangle.h b/examples/triangle/Triangle.h new file mode 100644 index 000000000..80f43e0de --- /dev/null +++ b/examples/triangle/Triangle.h @@ -0,0 +1,37 @@ +#ifndef Magnum_Examples_Triangle_h +#define Magnum_Examples_Triangle_h +/* + Copyright © 2010 Vladimír Vondruš + + This file is part of Magnum. + + Magnum is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 3 + only, as published by the Free Software Foundation. + + Magnum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License version 3 for more details. +*/ + +#include "AbstractObject.h" +#include "Mesh.h" +#include "IdentityShader.h" + +namespace Magnum { namespace Examples { + +class Triangle: public AbstractObject { + public: + Triangle(AbstractObject* parent = 0); + + virtual void draw(const Matrix4& transformationMatrix, const Matrix4& projectionMatrix); + + private: + Mesh mesh; + IdentityShader shader; +}; + +}} + +#endif diff --git a/examples/triangle/identity.frag b/examples/triangle/identity.frag new file mode 100644 index 000000000..cf382b2a1 --- /dev/null +++ b/examples/triangle/identity.frag @@ -0,0 +1,9 @@ +#version 330 + +in vec4 varyingColor; + +out vec4 fragmentColor; + +void main() { + fragmentColor = varyingColor; +} diff --git a/examples/triangle/identity.vert b/examples/triangle/identity.vert new file mode 100644 index 000000000..e103ffb8e --- /dev/null +++ b/examples/triangle/identity.vert @@ -0,0 +1,12 @@ +#version 330 + +in vec4 vertex; +in vec4 color; + +out vec4 varyingColor; + +void main() { + varyingColor = color; + + gl_Position = vertex; +} diff --git a/examples/triangle/main.cpp b/examples/triangle/main.cpp new file mode 100644 index 000000000..83b2447c1 --- /dev/null +++ b/examples/triangle/main.cpp @@ -0,0 +1,63 @@ +/* + Copyright © 2010 Vladimír Vondruš + + This file is part of Magnum. + + Magnum is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 3 + only, as published by the Free Software Foundation. + + Magnum is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License version 3 for more details. +*/ + +#include +#include +#include + +#include "Scene.h" +#include "Triangle.h" + +Magnum::Scene* s; + +/* Wrapper functions so GLUT can handle that */ +void setViewport(int w, int h) { + s->setViewport(w, h); +} +void draw() { + s->draw(); + glutSwapBuffers(); +} + +int main(int argc, char** argv) { + /* Init GLUT */ + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH|GLUT_STENCIL); + glutInitWindowSize(800, 600); + glutCreateWindow("Shaded Triangle example"); + glutReshapeFunc(setViewport); + glutDisplayFunc(draw); + + /* Init GLEW */ + GLenum err = glewInit(); + if(err != GLEW_OK) { + std::cerr << "GLEW error:" << glewGetErrorString(err) << std::endl; + return 1; + } + + /* Initialize scene */ + Magnum::Scene scene; + s = &scene; + + /* Every scene needs a camera */ + scene.setCamera(new Magnum::Camera(&scene)); + + /* Add triangle to the scene */ + new Magnum::Examples::Triangle(&scene); + + /* Main loop calls draw() periodically and setViewport() on window size change */ + glutMainLoop(); + return 0; +}