Browse Source

Textured triangle example.

vectorfields
Vladimír Vondruš 16 years ago
parent
commit
2dd404bc23
  1. 1
      examples/CMakeLists.txt
  2. 2
      examples/texturedTriangle/CMakeLists.txt
  3. 64
      examples/texturedTriangle/TGATexture.cpp
  4. 49
      examples/texturedTriangle/TGATexture.h
  5. 38
      examples/texturedTriangle/TexturedIdentityShader.cpp
  6. 11
      examples/texturedTriangle/TexturedIdentityShader.frag
  7. 41
      examples/texturedTriangle/TexturedIdentityShader.h
  8. 12
      examples/texturedTriangle/TexturedIdentityShader.vert
  9. 51
      examples/texturedTriangle/TexturedTriangle.cpp
  10. 39
      examples/texturedTriangle/TexturedTriangle.h
  11. 63
      examples/texturedTriangle/main.cpp
  12. BIN
      examples/texturedTriangle/stone.tga

1
examples/CMakeLists.txt

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

2
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})

64
examples/texturedTriangle/TGATexture.cpp

@ -0,0 +1,64 @@
/*
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 "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

@ -0,0 +1,49 @@
#ifndef Magnum_Examples_TGATexture_h
#define Magnum_Examples_TGATexture_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 "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

@ -0,0 +1,38 @@
/*
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 "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

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

41
examples/texturedTriangle/TexturedIdentityShader.h

@ -0,0 +1,41 @@
#ifndef Magnum_Examples_TexturedIdentityShader_h
#define Magnum_Examples_TexturedIdentityShader_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 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

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

51
examples/texturedTriangle/TexturedTriangle.cpp

@ -0,0 +1,51 @@
/*
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 "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

@ -0,0 +1,39 @@
#ifndef Magnum_Examples_TexturedTriangle_h
#define Magnum_Examples_TexturedTriangle_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 "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

@ -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 "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.

After

Width:  |  Height:  |  Size: 192 KiB

Loading…
Cancel
Save