Browse Source

Added Triangle example.

vectorfields
Vladimír Vondruš 16 years ago
parent
commit
2d0d5ff005
  1. 16
      CMakeLists.txt
  2. 1
      examples/CMakeLists.txt
  3. 2
      examples/triangle/CMakeLists.txt
  4. 36
      examples/triangle/IdentityShader.cpp
  5. 34
      examples/triangle/IdentityShader.h
  6. 44
      examples/triangle/Triangle.cpp
  7. 37
      examples/triangle/Triangle.h
  8. 9
      examples/triangle/identity.frag
  9. 12
      examples/triangle/identity.vert
  10. 63
      examples/triangle/main.cpp

16
CMakeLists.txt

@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 2.6) cmake_minimum_required(VERSION 2.6)
project(Magnum) project(Magnum)
option(BUILD_EXAMPLES "Build examples (requires GLUT)." OFF)
option(BUILD_TESTS "Build unit tests (requires Qt4)." OFF) option(BUILD_TESTS "Build unit tests (requires Qt4)." OFF)
if(BUILD_TESTS) if(BUILD_TESTS)
@ -16,7 +17,20 @@ if(BUILD_TESTS)
endif() endif()
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${Magnum_SOURCE_DIR}/modules/") set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${Magnum_SOURCE_DIR}/modules/")
include(MagnumMacros) include(MagnumMacros)
add_subdirectory(src) 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()

1
examples/CMakeLists.txt

@ -0,0 +1 @@
add_subdirectory(triangle)

2
examples/triangle/CMakeLists.txt

@ -0,0 +1,2 @@
add_executable(triangleExample main.cpp IdentityShader.cpp Triangle.cpp)
target_link_libraries(triangleExample Magnum ${GLUT_LIBRARY})

36
examples/triangle/IdentityShader.cpp

@ -0,0 +1,36 @@
/*
Copyright © 2010 Vladimír Vondruš <mosra@centrum.cz>
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;
}
}}

34
examples/triangle/IdentityShader.h

@ -0,0 +1,34 @@
#ifndef Magnum_Examples_IdentityShader_h
#define Magnum_Examples_IdentityShader_h
/*
Copyright © 2010 Vladimír Vondruš <mosra@centrum.cz>
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

44
examples/triangle/Triangle.cpp

@ -0,0 +1,44 @@
/*
Copyright © 2010 Vladimír Vondruš <mosra@centrum.cz>
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<Vector4>(buffer, IdentityShader::Vertex);
mesh.bindAttribute<Vector4>(buffer, IdentityShader::Color);
}
void Triangle::draw(const Matrix4& transformationMatrix, const Matrix4& projectionMatrix) {
shader.use();
mesh.draw();
}
}}

37
examples/triangle/Triangle.h

@ -0,0 +1,37 @@
#ifndef Magnum_Examples_Triangle_h
#define Magnum_Examples_Triangle_h
/*
Copyright © 2010 Vladimír Vondruš <mosra@centrum.cz>
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

9
examples/triangle/identity.frag

@ -0,0 +1,9 @@
#version 330
in vec4 varyingColor;
out vec4 fragmentColor;
void main() {
fragmentColor = varyingColor;
}

12
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;
}

63
examples/triangle/main.cpp

@ -0,0 +1,63 @@
/*
Copyright © 2010 Vladimír Vondruš <mosra@centrum.cz>
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 <iostream>
#include <GL/glew.h>
#include <GL/freeglut.h>
#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;
}
Loading…
Cancel
Save