aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-12-27 19:21:12 +0000
committerYann Herklotz <ymherklotz@gmail.com>2017-12-27 19:21:12 +0000
commit354d7df4d2779ed7391701d1ef4344e959b64582 (patch)
tree81ecdc8d323cae78a86fb9c99524f57c705eeabc /examples
parentf949692714e72a0e2d45ebb6a5d698424ab71dee (diff)
downloadYAGE-354d7df4d2779ed7391701d1ef4344e959b64582.tar.gz
YAGE-354d7df4d2779ed7391701d1ef4344e959b64582.zip
[Broken] Texture is black.
Diffstat (limited to 'examples')
-rw-r--r--examples/CMakeLists.txt2
-rw-r--r--examples/resources/colourshader.frag10
-rw-r--r--examples/resources/colourshader.vert15
-rw-r--r--examples/resources/textureshader.frag8
-rw-r--r--examples/resources/textureshader.vert7
-rw-r--r--examples/shooter/main.cpp46
-rw-r--r--examples/simplegame/main.cpp25
7 files changed, 82 insertions, 31 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index a75f6528..156b6a34 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -4,7 +4,7 @@ function(make_example name)
add_executable(${name} ${SOURCES})
target_link_libraries(${name} yage)
-endfunction(make_test)
+endfunction(make_example)
make_example(simplegame)
make_example(shooter)
diff --git a/examples/resources/colourshader.frag b/examples/resources/colourshader.frag
new file mode 100644
index 00000000..e49d1ebb
--- /dev/null
+++ b/examples/resources/colourshader.frag
@@ -0,0 +1,10 @@
+#version 450
+
+layout(location = 0) in vec4 fragment_colour;
+
+layout(location = 0) out vec4 colour;
+
+void main()
+{
+ colour = fragment_colour;
+}
diff --git a/examples/resources/colourshader.vert b/examples/resources/colourshader.vert
new file mode 100644
index 00000000..e5c10d77
--- /dev/null
+++ b/examples/resources/colourshader.vert
@@ -0,0 +1,15 @@
+#version 450
+
+layout(location = 0) in vec2 vertex_position;
+layout(location = 1) in vec4 vertex_colour;
+
+layout(location = 0) out vec4 fragment_colour;
+
+uniform mat4 P;
+
+void main()
+{
+ gl_Position = vec4(vertex_position, 0.f, 1.f);
+
+ fragment_colour = vertex_colour;
+}
diff --git a/examples/resources/textureshader.frag b/examples/resources/textureshader.frag
index ef728b04..8d916be6 100644
--- a/examples/resources/textureshader.frag
+++ b/examples/resources/textureshader.frag
@@ -4,13 +4,15 @@ layout(location = 0) in vec2 fragment_position;
layout(location = 1) in vec4 fragment_colour;
layout(location = 2) in vec2 fragment_uv;
-out vec4 colour;
+layout(location = 0) out vec4 colour;
+layout(location = 1) out vec4 colour2;
uniform sampler2D texture_sampler;
void main()
{
- vec4 texture_color = texture(texture_sampler, fragment_uv);
+ vec4 texture_colour = texture(texture_sampler, fragment_uv);
- colour = texture_color * fragment_colour;
+ colour2 = vec4(1.f, 0.f, 0.f, 1.f);
+ colour = texture_colour;
}
diff --git a/examples/resources/textureshader.vert b/examples/resources/textureshader.vert
index 3277d8b0..b0c11a6c 100644
--- a/examples/resources/textureshader.vert
+++ b/examples/resources/textureshader.vert
@@ -12,12 +12,9 @@ uniform mat4 P;
void main()
{
- gl_Position.xy = (P*vec4(vertex_position, 0.0, 1.0)).xy;
- gl_Position.z = 0.0;
- gl_Position.w = 1.0;
+ gl_Position = vec4((P*vec4(vertex_position, 0.f, 1.f)).xy, 0.f, 1.f);
fragment_position = vertex_position;
fragment_colour = vertex_colour;
- fragment_uv = vec2(vertex_uv.x, 1-vertex_uv.y);
-
+ fragment_uv = vec2(vertex_uv.x, 1-vertex_uv.y);
}
diff --git a/examples/shooter/main.cpp b/examples/shooter/main.cpp
index d28af83e..fe98f300 100644
--- a/examples/shooter/main.cpp
+++ b/examples/shooter/main.cpp
@@ -1,22 +1,60 @@
#include <yage/yage.h>
+#include "glad/glad.h"
+
#include <iostream>
using std::cout;
-int main(int argc, char** argv)
+int main(int argc, char **argv)
{
cout << "Starting Shooter example...\n";
yage::Window window;
- window.create("Shooter example", 1920, 1080);
+ window.create("Shooter example", 800, 600);
+
+ yage::Shader shader("examples/resources/colourshader.vert",
+ "examples/resources/colourshader.frag");
+
+ GLfloat vertices[] = {
+ 0.0f, 0.5f, 1.f, 0.f, 0.f, // Vertex 1 (X, Y, R, G, B)
+ 0.5f, -0.5f, 0.f, 1.f, 0.f, // Vertex 2 (X, Y, R, G, B)
+ -0.5f, -0.5f, 0.f, 0.f, 1.f, // Vertex 3 (X, Y, R, G, B)
+ };
+
+ // create vertex array
+ GLuint rect_vao, rect_vbo;
+
+ // bind vertex array object
+ glGenVertexArrays(1, &rect_vao);
+ glBindVertexArray(rect_vao);
+
+ // bind vertex buffer object
+ glGenBuffers(1, &rect_vbo);
+ glBindBuffer(GL_ARRAY_BUFFER, rect_vbo);
- while(!window.shouldClose()) {
+ glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
+ shader.use();
+
+ // enable vertex attribute arrays
+ glEnableVertexAttribArray(0);
+ glEnableVertexAttribArray(1);
+
+ // set the vertex attribute pointers
+ glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), 0);
+ glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat),
+ (void *)(2 * sizeof(GLfloat)));
+
+ while (!window.shouldClose()) {
window.pollEvents();
window.clearBuffer();
-
+ glDrawArrays(GL_TRIANGLES, 0, 3);
window.swapBuffer();
}
+
+ glDeleteBuffers(1, &rect_vbo);
+
+ glDeleteVertexArrays(1, &rect_vao);
}
diff --git a/examples/simplegame/main.cpp b/examples/simplegame/main.cpp
index 9fba20eb..f5547e53 100644
--- a/examples/simplegame/main.cpp
+++ b/examples/simplegame/main.cpp
@@ -17,15 +17,11 @@ using namespace yage;
int main()
{
Window window;
- GlslProgram textureProgram;
-
window.create("Simple Game", 800, 640);
- SpriteBatch sp;
- textureProgram.compileShadersFromFile(
- "examples/resources/textureshader.vert",
- "examples/resources/textureshader.frag");
- textureProgram.linkShaders();
+ Shader textureProgram("examples/resources/textureshader.vert",
+ "examples/resources/textureshader.frag");
+ SpriteBatch sp;
Texture fountain = ResourceManager::getTexture(
"examples/resources/dngn_blood_fountain.png");
@@ -36,6 +32,9 @@ int main()
Camera camera(800, 640);
+ textureProgram.use();
+ textureProgram.setUniform("texture_sampler", 0);
+
while (!window.shouldClose()) {
window.clearBuffer();
Texture texture = fountain;
@@ -48,22 +47,12 @@ int main()
texture = breast_plate;
}
- textureProgram.use();
camera.update(textureProgram);
- glActiveTexture(GL_TEXTURE0);
-
- GLint texture_location =
- textureProgram.getUniformLocation("texture_sampler");
- glUniform1i(texture_location, 0);
-
- sp.draw({0.f, 0.f, 64.f, 64.f}, {0, 0, 1, 1}, texture.id,
+ sp.draw({0.f, 0.f, 64.f, 64.f}, {0, 0, 1, 1}, fountain.id,
Colour(255, 0, 255, 255), 0);
sp.render();
- glBindTexture(GL_TEXTURE_2D, 0);
- textureProgram.unuse();
-
window.swapBuffer();
}
}