1. Christian Fischer
  2. wiesel-apidemos

Commits

baldur  committed 7097e49

added shader effects demo

  • Participants
  • Parent commits 19424c0
  • Branches default

Comments (0)

Files changed (8)

File resources/common/images/SAM_2694.png

  • Ignore whitespace
Added
New image

File resources/common/shaders/default.glsl.frag

View file
  • Ignore whitespace
+#ifdef GL_ES
+precision mediump float;
+#endif
+
+uniform	sampler2D	texture0;
+varying	vec2		my_texcoord0;
+
+void main(void) {
+	gl_FragColor = texture2D(texture0, my_texcoord0);
+}

File resources/common/shaders/default.glsl.vert

View file
  • Ignore whitespace
+uniform		mat4		mProjection;
+uniform		mat4		mModelview;
+uniform		sampler2D	texture0;
+attribute	vec4		vPosition;
+attribute	vec2		vTexCoord0;
+varying		vec2		my_texcoord0;
+
+void main() {
+	gl_Position  = vPosition * mModelview * mProjection;
+	my_texcoord0 = vTexCoord0;
+}

File resources/common/shaders/grayscale.glsl.frag

View file
  • Ignore whitespace
+#ifdef GL_ES
+precision mediump float;
+#endif
+
+uniform	sampler2D	texture0;
+varying	vec2		my_texcoord0;
+
+
+void main(void) {
+	vec4 texel = texture2D(texture0, my_texcoord0);
+	float gray = dot(texel.rgb, vec3(0.299, 0.587, 0.114));
+	
+	gl_FragColor = vec4(vec3(gray), 1.0);
+}

File resources/common/shaders/sepia.glsl.frag

View file
  • Ignore whitespace
+#ifdef GL_ES
+precision mediump float;
+#endif
+
+uniform	sampler2D	texture0;
+varying	vec2		my_texcoord0;
+
+
+void main(void) {
+	vec4 texel  = texture2D(texture0, my_texcoord0);
+	float gray  = dot(texel.rgb, vec3(0.299, 0.587, 0.114));
+	vec4 sepia1 = vec4(0.15, 0.05, 0.00, 1.0);
+	vec4 sepia2 = vec4(1.00, 0.90, 0.60, 1.0);
+
+	gl_FragColor = mix(
+					mix(sepia1, sepia2, gray),
+					texel,
+					0.2
+	);
+}

File src/graphics2d/graphicsmenu_scene.cpp

View file
  • Ignore whitespace
 
 #include "spritedemo_scene.h"
 #include "multispritedemo_scene.h"
+#include "shaderdemo_scene.h"
 
 
 Graphics2DMenuScene::Graphics2DMenuScene(ApiDemosApp* app, SceneBase *previous) : MenuScene(app, previous) {
 	this->addMenuEntry("1.024 single sprites",		&createScene<SpriteDemoScene>);
 	this->addMenuEntry("1.024 batch frames",		&createScene<MultiSpriteDemoScene>);
+	this->addMenuEntry("shader effects",			&createScene<ShaderDemoScene>);
 
 	return;
 }

File src/graphics2d/shaderdemo_scene.cpp

View file
  • Ignore whitespace
+/**
+ * Copyright (C) 2013
+ * Christian Fischer
+ *
+ * https://bitbucket.org/baldur/wiesel/
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library 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 for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+#include "shaderdemo_scene.h"
+#include "wiesel/io/filesystem.h"
+#include "wiesel/video/shaders.h"
+
+#include <wiesel/video/texture.h>
+
+#include <sstream>
+
+
+using namespace wiesel;
+using namespace wiesel::video;
+
+
+ShaderDemoScene::ShaderDemoScene(ApiDemosApp *app, SceneBase *previous)
+	:	SceneBase(app, previous)
+{
+	File*		sprite_file		= Engine::getInstance()->getAssetFileSystem()->findFile("/images/SAM_2694.png");
+	Texture*	sprite_texture	= Texture::fromFile(sprite_file);
+	sprite_texture->loadContentFrom(app->getScreen());
+
+	sprite = new SpriteNode(sprite_texture);
+	sprite->setPivot(0.5f, 0.5f);
+	this->addChild(sprite);
+
+	this->setScaleMode(
+			Viewport::FitToParent,
+			sprite->getContentSize()
+	);
+
+	// make default entry
+	DataSource *default_glsl_vert_source
+			= Engine::getInstance()->getAssetFileSystem()->findFile("/shaders/default.glsl.vert")
+			->asDataSource()
+	;
+
+	std::vector<std::string> shader_names;
+	shader_names.push_back("default");
+	shader_names.push_back("grayscale");
+	shader_names.push_back("sepia");
+
+	for(std::vector<std::string>::iterator it=shader_names.begin(); it!=shader_names.end(); it++) {
+		std::string glsl_frag_name = "/shaders/" + (*it) + ".glsl.frag";
+		File *glsl_frag_file = Engine::getInstance()->getAssetFileSystem()->findFile(glsl_frag_name);
+
+		if (glsl_frag_file == NULL) {
+			continue;
+		}
+
+		std::stringstream attr_tex;
+		attr_tex << Shaders::UNIFORM_TEXTURE;
+		attr_tex << 0;
+
+		std::stringstream attr_tex_coord;
+		attr_tex_coord << Shaders::ATTRIBUTE_VERTEX_TEXTURE_COORDINATE;
+		attr_tex_coord << 0;
+
+		Shader *shader = new Shader();
+		shader->setSource(Shader::GLSL_VERTEX_SHADER,	default_glsl_vert_source);
+		shader->setSource(Shader::GLSL_FRAGMENT_SHADER,	glsl_frag_file->asDataSource());
+
+		shader->setAttributeName(Shader::VertexPosition,			0, Shaders::ATTRIBUTE_VERTEX_POSITION);
+		shader->setAttributeName(Shader::Texture,					0, attr_tex.str());
+		shader->setAttributeName(Shader::VertexTextureCoordinate,	0, attr_tex_coord.str());
+
+		shader->addConstantBuffer(
+								Shaders::CONSTANTBUFFER_MODELVIEW_MATRIX,
+								Shader::Context_VertexShader,
+								Shaders::instance()->getModelviewMatrixBufferTemplate()
+		);
+
+		shader->addConstantBuffer(
+								Shaders::CONSTANTBUFFER_PROJECTION_MATRIX,
+								Shader::Context_VertexShader,
+								Shaders::instance()->getProjectionMatrixBufferTemplate()
+		);
+
+		ShaderEntry entry;
+		entry.name		= *it;
+		entry.shader	= shader;
+		shaders.push_back(entry);
+	}
+
+	hud_node = new Node2D();
+	this->addChild(hud_node);
+
+	shader_label = new LabelNode();
+	shader_label->setFont(app->getDefaultFont());
+	shader_label->setPivot(0.5f, 0.5f);
+	shader_label->setScale(2.0f);
+	hud_node->addChild(shader_label);
+
+	Button *bt_prev = new Button(
+			getApplication()->getHudSpritesheet()->get("button_left"),
+			getApplication()->getHudSpritesheet()->get("button_left_selected"),
+			this, (Button::OnButtonClicked)&ShaderDemoScene::onButtonPrevious
+	);
+	bt_prev->setPosition(-100, 0);
+	hud_node->addChild(bt_prev);
+
+	Button *bt_next = new Button(
+			getApplication()->getHudSpritesheet()->get("button_right"),
+			getApplication()->getHudSpritesheet()->get("button_right_selected"),
+			this, (Button::OnButtonClicked)&ShaderDemoScene::onButtonNext
+	);
+	bt_next->setPosition(+100, 0);
+	hud_node->addChild(bt_next);
+
+	current_shader = 0;
+	setNextShader(0);
+
+	return;
+}
+
+
+ShaderDemoScene::~ShaderDemoScene() {
+}
+
+
+void ShaderDemoScene::update(float dt) {
+	SceneBase::update(dt);
+
+	return;
+}
+
+
+void ShaderDemoScene::onViewportChanged(const rectangle& old_viewport, const rectangle& new_viewport) {
+	SceneBase::onViewportChanged(old_viewport, new_viewport);
+	float scale = 1.0f / this->getLocalTransform().m11;
+
+	hud_node->setScale(scale);
+	hud_node->setPosition(
+						new_viewport.size.width  / 2,
+						new_viewport.size.height / 10
+	);
+
+	sprite->setPosition(
+						new_viewport.size.width  / 2,
+						new_viewport.size.height / 2
+	);
+
+	return;
+}
+
+
+void ShaderDemoScene::onButtonPrevious(Button* button) {
+	if ((--current_shader) < 0) {
+		current_shader = shaders.size() - 1;
+	}
+
+	setNextShader(current_shader);
+
+	return;
+}
+
+
+void ShaderDemoScene::onButtonNext(Button* button) {
+	if ((++current_shader) >= shaders.size()) {
+		current_shader = 0;
+	}
+
+	setNextShader(current_shader);
+
+	return;
+}
+
+
+void ShaderDemoScene::setNextShader(int index) {
+	sprite->setShader(shaders[index].shader);
+	shader_label->setText(shaders[index].name);
+}
+

File src/graphics2d/shaderdemo_scene.h

View file
  • Ignore whitespace
+/**
+ * Copyright (C) 2013
+ * Christian Fischer
+ *
+ * https://bitbucket.org/baldur/wiesel/
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library 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 for more details.
+ *
+ * You should have received a copy of the GNU Lesser General
+ * Public License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA
+ */
+#ifndef __GRAPHICS2D_SHADERDEMO_SCENE_H__
+#define __GRAPHICS2D_SHADERDEMO_SCENE_H__
+
+#include "../scene_base.h"
+
+#include <wiesel/graph/2d/sprite_node.h>
+#include <wiesel/ui/label_node.h>
+#include <wiesel/video/shader.h>
+#include <string>
+#include <vector>
+
+
+class ShaderDemoScene : public SceneBase
+{
+public:
+	ShaderDemoScene(ApiDemosApp *app, SceneBase *previous);
+	virtual ~ShaderDemoScene();
+
+public:
+	virtual void update(float dt);
+
+protected:
+	virtual void onViewportChanged(const wiesel::rectangle& old_viewport, const wiesel::rectangle& new_viewport);
+
+private:
+	void onButtonPrevious(Button *button);
+	void onButtonNext(Button *button);
+
+	void setNextShader(int index);
+
+private:
+	struct ShaderEntry {
+		std::string								name;
+		wiesel::ref<wiesel::video::Shader>		shader;
+	};
+
+	wiesel::ref<wiesel::SpriteNode>		sprite;
+	wiesel::ref<wiesel::Node2D>			hud_node;
+	wiesel::ref<wiesel::LabelNode>		shader_label;
+	std::vector<ShaderEntry>			shaders;
+	int									current_shader;
+};
+
+#endif // __GRAPHICS2D_SHADERDEMO_SCENE_H__