Browse Source

Moved examples to separate repository.

vectorfields
Vladimír Vondruš 15 years ago
parent
commit
b13fe66337
  1. 19
      CMakeLists.txt
  2. 2
      examples/CMakeLists.txt
  3. 2
      examples/texturedTriangle/CMakeLists.txt
  4. 64
      examples/texturedTriangle/TGATexture.cpp
  5. 49
      examples/texturedTriangle/TGATexture.h
  6. 38
      examples/texturedTriangle/TexturedIdentityShader.cpp
  7. 11
      examples/texturedTriangle/TexturedIdentityShader.frag
  8. 41
      examples/texturedTriangle/TexturedIdentityShader.h
  9. 12
      examples/texturedTriangle/TexturedIdentityShader.vert
  10. 51
      examples/texturedTriangle/TexturedTriangle.cpp
  11. 39
      examples/texturedTriangle/TexturedTriangle.h
  12. 63
      examples/texturedTriangle/main.cpp
  13. BIN
      examples/texturedTriangle/stone.tga
  14. 2
      examples/triangle/CMakeLists.txt
  15. 36
      examples/triangle/IdentityShader.cpp
  16. 9
      examples/triangle/IdentityShader.frag
  17. 34
      examples/triangle/IdentityShader.h
  18. 12
      examples/triangle/IdentityShader.vert
  19. 44
      examples/triangle/Triangle.cpp
  20. 37
      examples/triangle/Triangle.h
  21. 63
      examples/triangle/main.cpp
  22. 2
      src/CMakeLists.txt

19
CMakeLists.txt

@ -1,7 +1,6 @@
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)
@ -13,7 +12,6 @@ if(BUILD_TESTS)
else()
enable_testing()
endif()
endif()
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${Magnum_SOURCE_DIR}/modules/")
@ -21,28 +19,11 @@ include(MagnumMacros)
set_parent_scope(MAGNUM_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src")
set_parent_scope(MAGNUM_LIBRARY Magnum)
include(FindMagnum)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -std=c++0x")
add_subdirectory(modules)
add_subdirectory(src)
install(DIRECTORY src/ DESTINATION ${MAGNUM_INCLUDE_INSTALL_DIR}
FILES_MATCHING PATTERN "*.h"
PATTERN "*/Test" EXCLUDE)
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()

2
examples/CMakeLists.txt

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

2
examples/texturedTriangle/CMakeLists.txt

@ -1,2 +0,0 @@
add_executable(texturedTriangleExample main.cpp TexturedIdentityShader.cpp TexturedTriangle.cpp TGATexture.cpp)
target_link_libraries(texturedTriangleExample Magnum ${GLUT_LIBRARY})

64
examples/texturedTriangle/TGATexture.cpp

@ -1,64 +0,0 @@
/*
Copyright © 2010, 2011 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 "TGATexture.h"
#include <iostream>
#include <fstream>
using namespace std;
using namespace Magnum::Math;
namespace Magnum { namespace Examples {
TGATexture::TGATexture(const std::string& filename) {
ifstream image(filename.c_str(), ifstream::binary);
if(!image.good()) {
image.close();
return;
}
Header header;
image.read(reinterpret_cast<char*>(&header), sizeof(Header));
ColorFormat colorFormat;
int internalFormat;
switch(header.bpp) {
case 24:
colorFormat = BGR;
internalFormat = RGB;
break;
case 32:
colorFormat = BGRA;
internalFormat = RGBA;
break;
default:
cerr << (int) header.bpp << endl;
return;
}
size_t size = header.width*header.height*header.bpp/8;
GLubyte* buffer = new GLubyte[size];
image.read(reinterpret_cast<char*>(buffer), size);
image.close();
GLsizei dimensions[] = {header.width, header.height};
setData(0, internalFormat, dimensions, colorFormat, buffer);
delete[] buffer;
}
}}

49
examples/texturedTriangle/TGATexture.h

@ -1,49 +0,0 @@
#ifndef Magnum_Examples_TGATexture_h
#define Magnum_Examples_TGATexture_h
/*
Copyright © 2010, 2011 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 "Texture.h"
#include <string>
namespace Magnum { namespace Examples {
class TGATexture: public Texture2D {
public:
TGATexture(const std::string& filename);
private:
#pragma pack(1)
struct Header {
GLbyte identsize; /**< @brief Size of ID field that follows header (0) */
GLbyte colorMapType; /**< @brief 0 = None, 1 = paletted */
GLbyte imageType; /**< @brief 0 = none, 1 = indexed, 2 = rgb, 3 = grey, +8=rle */
unsigned short colorMapStart; /**< @brief First color map entry */
unsigned short colorMapLength; /**< @brief Number of colors */
unsigned char colorMapBpp; /**< @brief Bits per palette entry */
unsigned short beginX; /**< @brief Image x origin */
unsigned short beginY; /**< @brief Image y origin */
unsigned short width; /**< @brief Image width */
unsigned short height; /**< @brief Image height */
GLbyte bpp; /**< @brief Bits per pixel (8 16, 24, 32) */
GLbyte descriptor; /**< @brief Image descriptor */
};
#pragma pack(8)
};
}}
#endif

38
examples/texturedTriangle/TexturedIdentityShader.cpp

@ -1,38 +0,0 @@
/*
Copyright © 2010, 2011 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 "TexturedIdentityShader.h"
namespace Magnum { namespace Examples {
TexturedIdentityShader::TexturedIdentityShader() {
Shader* vertexShader = Shader::fromFile(Shader::Vertex, "TexturedIdentityShader.vert");
Shader* fragmentShader = Shader::fromFile(Shader::Fragment, "TexturedIdentityShader.frag");
attachShader(vertexShader);
attachShader(fragmentShader);
bindAttribute(Vertex, "vertex");
bindAttribute(TextureCoordinates, "textureCoordinates");
link();
textureUniform = uniformLocation("textureData");
delete vertexShader;
delete fragmentShader;
}
}}

11
examples/texturedTriangle/TexturedIdentityShader.frag

@ -1,11 +0,0 @@
#version 330
uniform sampler2D textureData;
in vec2 varyingTextureCoordinates;
out vec4 fragmentColor;
void main() {
fragmentColor = texture(textureData, varyingTextureCoordinates);
}

41
examples/texturedTriangle/TexturedIdentityShader.h

@ -1,41 +0,0 @@
#ifndef Magnum_Examples_TexturedIdentityShader_h
#define Magnum_Examples_TexturedIdentityShader_h
/*
Copyright © 2010, 2011 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 TexturedIdentityShader: public AbstractShaderProgram {
public:
enum Attributes {
Vertex = 1,
TextureCoordinates = 2
};
TexturedIdentityShader();
inline void setTextureUniform(GLuint level) {
setUniform(textureUniform, level);
}
private:
GLint textureUniform;
};
}}
#endif

12
examples/texturedTriangle/TexturedIdentityShader.vert

@ -1,12 +0,0 @@
#version 330
in vec4 vertex;
in vec2 textureCoordinates;
out vec2 varyingTextureCoordinates;
void main() {
varyingTextureCoordinates = textureCoordinates;
gl_Position = vertex;
}

51
examples/texturedTriangle/TexturedTriangle.cpp

@ -1,51 +0,0 @@
/*
Copyright © 2010, 2011 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 "TexturedTriangle.h"
#include "Buffer.h"
namespace Magnum { namespace Examples {
TexturedTriangle::TexturedTriangle(const std::string& textureFilename, Object* parent): Object(parent), mesh(Mesh::Triangles, 3), texture(textureFilename) {
/* Vertices and texture coordinates, interleaved */
GLfloat data[] = {
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, /* Lower left vertex */
0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, /* Lower right vertex */
0.0f, 0.5f, 0.0f, 1.0f, 0.5f, 1.0f, /* 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, TexturedIdentityShader::Vertex);
mesh.bindAttribute<Vector2>(buffer, TexturedIdentityShader::TextureCoordinates);
/* Texture */
texture.setMagnificationFilter(TGATexture::Linear);
texture.setMinificationFilter(TGATexture::Linear);
texture.setWrapping(Math::Vector2<TGATexture::Wrapping>(TGATexture::ClampToEdge, TGATexture::ClampToEdge));
}
void TexturedTriangle::draw(const Matrix4& transformationMatrix, const Matrix4& projectionMatrix) {
texture.bind();
shader.use();
shader.setTextureUniform(0);
mesh.draw();
}
}}

39
examples/texturedTriangle/TexturedTriangle.h

@ -1,39 +0,0 @@
#ifndef Magnum_Examples_TexturedTriangle_h
#define Magnum_Examples_TexturedTriangle_h
/*
Copyright © 2010, 2011 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 "Object.h"
#include "Mesh.h"
#include "TexturedIdentityShader.h"
#include "TGATexture.h"
namespace Magnum { namespace Examples {
class TexturedTriangle: public Object {
public:
TexturedTriangle(const std::string& textureFilename, Object* parent = 0);
virtual void draw(const Matrix4& transformationMatrix, const Matrix4& projectionMatrix);
private:
Mesh mesh;
TexturedIdentityShader shader;
TGATexture texture;
};
}}
#endif

63
examples/texturedTriangle/main.cpp

@ -1,63 +0,0 @@
/*
Copyright © 2010, 2011 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 "TexturedTriangle.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 TexturedTriangle 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::TexturedTriangle("stone.tga", &scene);
/* Main loop calls draw() periodically and setViewport() on window size change */
glutMainLoop();
return 0;
}

BIN
examples/texturedTriangle/stone.tga

Binary file not shown.

Before

Width:  |  Height:  |  Size: 192 KiB

2
examples/triangle/CMakeLists.txt

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

36
examples/triangle/IdentityShader.cpp

@ -1,36 +0,0 @@
/*
Copyright © 2010, 2011 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, "IdentityShader.vert");
Shader* fragmentShader = Shader::fromFile(Shader::Fragment, "IdentityShader.frag");
attachShader(vertexShader);
attachShader(fragmentShader);
bindAttribute(Vertex, "vertex");
bindAttribute(Color, "color");
link();
delete vertexShader;
delete fragmentShader;
}
}}

9
examples/triangle/IdentityShader.frag

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

34
examples/triangle/IdentityShader.h

@ -1,34 +0,0 @@
#ifndef Magnum_Examples_IdentityShader_h
#define Magnum_Examples_IdentityShader_h
/*
Copyright © 2010, 2011 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

12
examples/triangle/IdentityShader.vert

@ -1,12 +0,0 @@
#version 330
in vec4 vertex;
in vec4 color;
out vec4 varyingColor;
void main() {
varyingColor = color;
gl_Position = vertex;
}

44
examples/triangle/Triangle.cpp

@ -1,44 +0,0 @@
/*
Copyright © 2010, 2011 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(Object* parent): Object(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

@ -1,37 +0,0 @@
#ifndef Magnum_Examples_Triangle_h
#define Magnum_Examples_Triangle_h
/*
Copyright © 2010, 2011 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 "Object.h"
#include "Mesh.h"
#include "IdentityShader.h"
namespace Magnum { namespace Examples {
class Triangle: public Object {
public:
Triangle(Object* parent = 0);
virtual void draw(const Matrix4& transformationMatrix, const Matrix4& projectionMatrix);
private:
Mesh mesh;
IdentityShader shader;
};
}}
#endif

63
examples/triangle/main.cpp

@ -1,63 +0,0 @@
/*
Copyright © 2010, 2011 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;
}

2
src/CMakeLists.txt

@ -1,3 +1,5 @@
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -std=c++0x")
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
find_package(OpenGL REQUIRED)

Loading…
Cancel
Save