Skip to content

Commit

Permalink
Added text shader modifications.
Browse files Browse the repository at this point in the history
Added offset and size settings to image binding in order to draw sprites from
spritesheets
  • Loading branch information
jsmtux committed May 26, 2014
1 parent 5efb7e6 commit ca64064
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 31 deletions.
7 changes: 6 additions & 1 deletion files/shaders/normal.sfx
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@
uniform sampler2D gSampler;
uniform unsigned int gObjectIndex;
uniform unsigned int gMeshIndex;
uniform vec2 offset = vec2(0.0, 0.0);
uniform vec2 size = vec2(1.0, 1.0);

void main()
{
FragColor = texture2D(gSampler, TexCoord0.xy);
vec2 texture_coordinate = vec2(TexCoord0);
texture_coordinate += offset;
texture_coordinate *= size;
FragColor = texture2D(gSampler, texture_coordinate.xy);
}
</fragment>
</shader>
32 changes: 32 additions & 0 deletions files/shaders/text.sfx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE scene SYSTEM "shaders.dtd">
<shader>
<version>
330
</version>
<vertex>
layout (location = 0) in vec3 Position;
layout (location = 1) in vec2 TexCoord;

uniform mat4 gWVP;
uniform mat4 gObjMat;

out vec2 TexCoord0;

void main(){
gl_Position = gWVP * gObjMat * vec4(Position, 1.0);
TexCoord0 = TexCoord;
}
</vertex>
<fragment>
in vec2 TexCoord0;
in vec3 color;
out vec4 FragColor;

uniform sampler2D gSampler;

void main()
{
FragColor = vec4(1, 1, 1, texture2D(gSampler, TexCoord0).a);
}
</fragment>
</shader>
40 changes: 26 additions & 14 deletions src/Engine/Graphics/Shading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,6 @@ void Shading::update(){
#endif
}

GLint Shading::getId(string name){
GLint Location= glGetUniformLocation(program, name.c_str());
if (Location == 0xFFFFFFFF)
{
//cerr << "Warning! Unable to get the location of uniform " << name << endl;
}

return Location;
}

void Shading::deleteShading(){
for (vector<GLuint>::iterator it = shaderlist.begin() ; it != shaderlist.end() ; ++it)
{
Expand Down Expand Up @@ -131,6 +121,29 @@ void Shading::compileShader(){
initVars();
}

GLint Shading::getId(string name, int index)
{
if(index != -1){
stringstream str;
str << name << "[" << index <<"]";
name = str.str();
}
return glGetUniformLocation(program, name.c_str());
}

GLint Shading::getVarLocation(string name, int index)
{
map<string,GLint>::iterator tmp;
if((tmp=shaderLocs.find(name))==shaderLocs.end()){
cout << name << "not found";
GLint ret = getId(name, index);
shaderLocs.insert(pair<string, GLint>(name,ret));
cout << "Its location is " << ret << endl;
return ret;
}
return tmp->second;
}

void Shading::initVars(){
#ifdef ANDROID
posId = glGetAttribLocation(gProgram, "Position");
Expand All @@ -157,12 +170,11 @@ void Shading::initVars(){
colorId=getId("color");

animatedId=getId("animated");

for(int i=0;i<MAX_BONES;i++){
stringstream str;
str << "gBones[" << i <<"]";
boneLocation[i]= getId(str.str());
boneLocation[i] = getId("gBones", i);
}

}

void Shading::setBoneTransform(uint ind, const Matrix& transform){
Expand Down
12 changes: 8 additions & 4 deletions src/Engine/Graphics/Shading.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class Shading{
GLuint getTexLocation();
GLuint getNormLocation();
GLuint getSamplerLocation();
GLint getId(string name);
void deleteShading();
static Shading* getActive();
void setWVP(Matrix w);
Expand All @@ -79,17 +78,22 @@ class Shading{
void setObjInd(unsigned int ind);
void setMeshInd(unsigned int ind);
void setAnimated(bool animated);
GLint getVarLocation(string name, int index=-1);
static void push();
static void pop();
private:
void addShader(string shader, GLenum shadertype);
GLint getId(string name, int index=-1);
virtual void initVars();

static Shading* active;
static vector<GLuint> shaderlist;
static stack<Shading*> shaderStack;
map<string, GLint> shaderLocs;

void addShader(string shader, GLenum shadertype);
virtual void initVars();
GLuint wvpId, worldMatId, objMatId, animatedId;
static Matrix WVP, worldMat, objMat;

GLuint wvpId, worldMatId, objMatId, animatedId;
GLuint objId, meshId;
GLint program;
GLuint colorId;
Expand Down
27 changes: 18 additions & 9 deletions src/Engine/ObjectTypes/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,18 @@ void Image::del(){
list.erase(name);
}

void Image::Bind(){
void Image::Bind(Vector2 offset, Vector2 size){
#ifndef NODRAW
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, imageId);
Shading* current = Shading::getActive();
GLuint pos;
if((pos=current->getVarLocation("offset"))!=-1){
glUniform2f(pos, offset.x, offset.y);
}
if((pos=current->getVarLocation("size"))!=-1){
glUniform2f(pos, size.x, size.y);
}
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, imageId);
#endif
}

Expand All @@ -71,21 +79,22 @@ Resource::ResourceType Image::getType(){
void Image::Draw(){
Bind();
#ifndef NODRAW
Shading* current = Shading::getActive();
glDisable(GL_CULL_FACE);
glEnableVertexAttribArray(Shading::getActive()->getPosLocation());
glEnableVertexAttribArray(Shading::getActive()->getTexLocation());
glEnableVertexAttribArray(current->getPosLocation());
glEnableVertexAttribArray(current->getTexLocation());
// glEnableVertexAttribArray(Shading::getActive()->getNormLocation());

glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(Shading::getActive()->getPosLocation(), 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(Shading::getActive()->getTexLocation(), 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)12);
glVertexAttribPointer(current->getPosLocation(), 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glVertexAttribPointer(current->getTexLocation(), 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)12);
// glVertexAttribPointer(getNormLocation(), 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid*)20);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);

glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, (GLvoid*)0);

glDisableVertexAttribArray(Shading::getActive()->getPosLocation());
glDisableVertexAttribArray(Shading::getActive()->getTexLocation());
glDisableVertexAttribArray(current->getPosLocation());
glDisableVertexAttribArray(current->getTexLocation());
// glDisableVertexAttribArray(Shading::getActive()->getNormLocation());
glEnable(GL_CULL_FACE);
#endif
Expand Down
3 changes: 2 additions & 1 deletion src/Engine/ObjectTypes/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <map>
using namespace std;
#include "Resource.h"
#include "../Math/Vector2.h"

class Image: public Resource
{
Expand All @@ -36,7 +37,7 @@ class Image: public Resource
* @return name of an image
*/
ResourceType getType();
void Bind();
void Bind(Vector2 offset=Vector2(0,0), Vector2 size=Vector2(1,1));
void del();
static void init();
static void unBind();
Expand Down
4 changes: 2 additions & 2 deletions src/Engine/ObjectTypes/Model3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include "../Math/Vector3.h"
#include "../Math/Matrix.h"
#include "Resource.h"
#include "Animation.h"
//#include "Animation.h"

class Image;

Expand Down Expand Up @@ -79,7 +79,7 @@ struct VertexBoneData{
void AddBoneData(uint BoneID, float Weight);
};

class Model3d: public Resource, Animation
class Model3d: public Resource//, Animation
{
public:
void Draw();
Expand Down

0 comments on commit ca64064

Please sign in to comment.