diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index fd9ac19fc..c1594aeb9 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1 +1,2 @@ +add_subdirectory(texturedTriangle) add_subdirectory(triangle) diff --git a/examples/texturedTriangle/CMakeLists.txt b/examples/texturedTriangle/CMakeLists.txt new file mode 100644 index 000000000..79fc3e8b2 --- /dev/null +++ b/examples/texturedTriangle/CMakeLists.txt @@ -0,0 +1,2 @@ +add_executable(texturedTriangleExample main.cpp TexturedIdentityShader.cpp TexturedTriangle.cpp TGATexture.cpp) +target_link_libraries(texturedTriangleExample Magnum ${GLUT_LIBRARY}) diff --git a/examples/texturedTriangle/TGATexture.cpp b/examples/texturedTriangle/TGATexture.cpp new file mode 100644 index 000000000..083cd82b2 --- /dev/null +++ b/examples/texturedTriangle/TGATexture.cpp @@ -0,0 +1,64 @@ +/* + 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 "TGATexture.h" + +#include +#include + +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(&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(buffer), size); + image.close(); + + GLsizei dimensions[] = {header.width, header.height}; + + setData(0, internalFormat, dimensions, colorFormat, buffer); + delete[] buffer; +} + +}} diff --git a/examples/texturedTriangle/TGATexture.h b/examples/texturedTriangle/TGATexture.h new file mode 100644 index 000000000..5da5621f8 --- /dev/null +++ b/examples/texturedTriangle/TGATexture.h @@ -0,0 +1,49 @@ +#ifndef Magnum_Examples_TGATexture_h +#define Magnum_Examples_TGATexture_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 "Texture.h" + +#include + +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 diff --git a/examples/texturedTriangle/TexturedIdentityShader.cpp b/examples/texturedTriangle/TexturedIdentityShader.cpp new file mode 100644 index 000000000..edd5d24df --- /dev/null +++ b/examples/texturedTriangle/TexturedIdentityShader.cpp @@ -0,0 +1,38 @@ +/* + 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 "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; +} + +}} diff --git a/examples/texturedTriangle/TexturedIdentityShader.frag b/examples/texturedTriangle/TexturedIdentityShader.frag new file mode 100644 index 000000000..6bae9c534 --- /dev/null +++ b/examples/texturedTriangle/TexturedIdentityShader.frag @@ -0,0 +1,11 @@ +#version 330 + +uniform sampler2D textureData; + +in vec2 varyingTextureCoordinates; + +out vec4 fragmentColor; + +void main() { + fragmentColor = texture(textureData, varyingTextureCoordinates); +} diff --git a/examples/texturedTriangle/TexturedIdentityShader.h b/examples/texturedTriangle/TexturedIdentityShader.h new file mode 100644 index 000000000..0a261c76e --- /dev/null +++ b/examples/texturedTriangle/TexturedIdentityShader.h @@ -0,0 +1,41 @@ +#ifndef Magnum_Examples_TexturedIdentityShader_h +#define Magnum_Examples_TexturedIdentityShader_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 TexturedIdentityShader: public AbstractShaderProgram { + public: + enum Attributes { + Vertex = 1, + TextureCoordinates = 2 + }; + + TexturedIdentityShader(); + + inline void setTextureUniform(GLuint level) { + setUniform(textureUniform, level); + } + + private: + GLint textureUniform; +}; + +}} + +#endif diff --git a/examples/texturedTriangle/TexturedIdentityShader.vert b/examples/texturedTriangle/TexturedIdentityShader.vert new file mode 100644 index 000000000..c1f665baa --- /dev/null +++ b/examples/texturedTriangle/TexturedIdentityShader.vert @@ -0,0 +1,12 @@ +#version 330 + +in vec4 vertex; +in vec2 textureCoordinates; + +out vec2 varyingTextureCoordinates; + +void main() { + varyingTextureCoordinates = textureCoordinates; + + gl_Position = vertex; +} diff --git a/examples/texturedTriangle/TexturedTriangle.cpp b/examples/texturedTriangle/TexturedTriangle.cpp new file mode 100644 index 000000000..1cd26156f --- /dev/null +++ b/examples/texturedTriangle/TexturedTriangle.cpp @@ -0,0 +1,51 @@ +/* + 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 "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(buffer, TexturedIdentityShader::Vertex); + mesh.bindAttribute(buffer, TexturedIdentityShader::TextureCoordinates); + + /* Texture */ + texture.setMagnificationFilter(TGATexture::Linear); + texture.setMinificationFilter(TGATexture::Linear); + texture.setWrapping(Math::Vector2(TGATexture::ClampToEdge, TGATexture::ClampToEdge)); +} + +void TexturedTriangle::draw(const Matrix4& transformationMatrix, const Matrix4& projectionMatrix) { + texture.bind(); + shader.use(); + shader.setTextureUniform(0); + mesh.draw(); +} + +}} diff --git a/examples/texturedTriangle/TexturedTriangle.h b/examples/texturedTriangle/TexturedTriangle.h new file mode 100644 index 000000000..464ea552f --- /dev/null +++ b/examples/texturedTriangle/TexturedTriangle.h @@ -0,0 +1,39 @@ +#ifndef Magnum_Examples_TexturedTriangle_h +#define Magnum_Examples_TexturedTriangle_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 "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 diff --git a/examples/texturedTriangle/main.cpp b/examples/texturedTriangle/main.cpp new file mode 100644 index 000000000..fe8872f21 --- /dev/null +++ b/examples/texturedTriangle/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 "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; +} diff --git a/examples/texturedTriangle/stone.tga b/examples/texturedTriangle/stone.tga new file mode 100644 index 000000000..4b30cd578 Binary files /dev/null and b/examples/texturedTriangle/stone.tga differ