mirror of https://github.com/mosra/magnum.git
10 changed files with 253 additions and 1 deletions
@ -0,0 +1,2 @@ |
|||||||
|
add_executable(triangleExample main.cpp IdentityShader.cpp Triangle.cpp) |
||||||
|
target_link_libraries(triangleExample Magnum ${GLUT_LIBRARY}) |
||||||
@ -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; |
||||||
|
} |
||||||
|
|
||||||
|
}} |
||||||
@ -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 |
||||||
@ -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(); |
||||||
|
} |
||||||
|
|
||||||
|
}} |
||||||
@ -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 |
||||||
@ -0,0 +1,9 @@ |
|||||||
|
#version 330 |
||||||
|
|
||||||
|
in vec4 varyingColor; |
||||||
|
|
||||||
|
out vec4 fragmentColor; |
||||||
|
|
||||||
|
void main() { |
||||||
|
fragmentColor = varyingColor; |
||||||
|
} |
||||||
@ -0,0 +1,12 @@ |
|||||||
|
#version 330 |
||||||
|
|
||||||
|
in vec4 vertex; |
||||||
|
in vec4 color; |
||||||
|
|
||||||
|
out vec4 varyingColor; |
||||||
|
|
||||||
|
void main() { |
||||||
|
varyingColor = color; |
||||||
|
|
||||||
|
gl_Position = vertex; |
||||||
|
} |
||||||
@ -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…
Reference in new issue