Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string>
- #include <math.h>
- #include <vector>
- #include <list>
- #include <IwGx.h>
- #include "IwGL.h"
- #include <Iw2D.h>
- #include <IwMaterial.h>
- #include "s3e.h"
- #include "IwNUI.h"
- namespace Marmalade
- {
- namespace Testing
- {
- namespace RenderingGL
- {
- class Vector2
- {
- public:
- Vector2() : m_X(0.0f), m_Y(0.0f)
- {
- }
- Vector2(float x, float y) : m_X(x), m_Y(y)
- {
- }
- public:
- friend Vector2& operator += (Vector2& left, const Vector2& right);
- public:
- union
- {
- float m_Vec[2];
- struct
- {
- float m_X;
- float m_Y;
- };
- };
- };
- Vector2& operator += (Vector2& left, const Vector2& right)
- {
- left.m_X += right.m_X;
- left.m_Y += right.m_Y;
- return left;
- }
- class Color
- {
- public:
- Color() : m_Red(1.0f), m_Green(1.0f), m_Blue(1.0f), m_Alpha(1.0f)
- {
- }
- Color(float r, float g, float b, float a) : m_Red(r), m_Green(g), m_Blue(b), m_Alpha(a)
- {
- }
- public:
- float m_Red;
- float m_Green;
- float m_Blue;
- float m_Alpha;
- };
- class VertexShader
- {
- public:
- GLuint GetHandle() const
- {
- return m_Shader;
- }
- public:
- static VertexShader Create(const std::string& source)
- {
- VertexShader shader;
- shader.m_Shader = glCreateShader(GL_VERTEX_SHADER);
- const char* sourceCode = source.c_str();
- glShaderSource(shader.m_Shader, 1, &sourceCode, 0);
- glCompileShader(shader.m_Shader);
- GLint compiled;
- glGetShaderiv ( shader.m_Shader, GL_COMPILE_STATUS, &compiled );
- if ( !compiled )
- {
- GLint infoLen = 0;
- glGetShaderiv ( shader.m_Shader, GL_INFO_LOG_LENGTH, &infoLen );
- if ( infoLen > 1 )
- {
- std::vector<char> buffer;
- buffer.resize(infoLen);
- glGetShaderInfoLog ( shader.m_Shader, infoLen, NULL, &buffer[0] );
- }
- }
- return shader;
- }
- private:
- GLuint m_Shader;
- };
- class FragmentShader
- {
- public:
- GLuint GetHandle() const
- {
- return m_Shader;
- }
- public:
- static FragmentShader Create(const std::string& source)
- {
- FragmentShader shader;
- shader.m_Shader = glCreateShader(GL_FRAGMENT_SHADER);
- const char* sourceCode = source.c_str();
- glShaderSource(shader.m_Shader, 1, &sourceCode, 0);
- glCompileShader(shader.m_Shader);
- GLint compiled;
- glGetShaderiv ( shader.m_Shader, GL_COMPILE_STATUS, &compiled );
- if ( !compiled )
- {
- GLint infoLen = 0;
- glGetShaderiv ( shader.m_Shader, GL_INFO_LOG_LENGTH, &infoLen );
- if ( infoLen > 1 )
- {
- std::vector<char> buffer;
- buffer.resize(infoLen);
- glGetShaderInfoLog ( shader.m_Shader, infoLen, NULL, &buffer[0] );
- }
- }
- return shader;
- }
- private:
- GLuint m_Shader;
- };
- class ShaderProgram
- {
- public:
- ShaderProgram(FragmentShader* fragmentShader, VertexShader* vertexShader)
- : m_Handle(glCreateProgram()), m_FragmentShader(fragmentShader), m_VertexShader(vertexShader)
- {
- glAttachShader(m_Handle, vertexShader->GetHandle());
- glAttachShader(m_Handle, fragmentShader->GetHandle());
- glLinkProgram(m_Handle);
- GLint linked;
- glGetProgramiv ( m_Handle, GL_LINK_STATUS, &linked );
- if ( !linked )
- {
- GLint infoLen = 0;
- glGetProgramiv ( m_Handle, GL_INFO_LOG_LENGTH, &infoLen );
- if ( infoLen > 1 )
- {
- std::vector<char> buffer;
- buffer.resize(infoLen);
- glGetProgramInfoLog ( m_Handle, infoLen, NULL, &buffer[0] );
- }
- }
- }
- void UseIt()
- {
- ::glUseProgram(m_Handle);
- }
- GLuint GetHandle()
- {
- return m_Handle;
- }
- std::string GetProjectionUniformName()
- {
- return "u_projection";
- }
- std::string GetOriginTranslationUniformName()
- {
- return "u_origin_translation";
- }
- std::string GetTranslationUniformName()
- {
- return "u_translation";
- }
- std::string GetColorUniformName()
- {
- return "u_color";
- }
- std::string GetColorAttributeName()
- {
- return "a_color";
- }
- std::string GetPositionAttributeName()
- {
- return "a_position";
- }
- std::string GetTexCoordAttributeName()
- {
- return "a_texCoord";
- }
- std::string GetSamplerTextureName()
- {
- return "s_texture";
- }
- private:
- GLuint m_Handle;
- FragmentShader* m_FragmentShader;
- VertexShader* m_VertexShader;
- };
- class Context
- {
- public:
- Context() : m_ShaderProgram(0), m_Width(0.0f), m_Height(0.0f)
- {
- }
- void Init()
- {
- IwGLInit();
- m_Width = ::IwGLGetInt(IW_GL_WIDTH);
- m_Height = ::IwGLGetInt(IW_GL_HEIGHT);
- }
- void SetViewport()
- {
- glViewport(0, 0, m_Width, m_Height);
- }
- void SetOrtho()
- {
- ApplyOrtho(-(float)m_Width/2.0f, (float)m_Width/2.0f, -(float)m_Height/2.0f, (float)m_Height/2.0f, -1.0f, 1.0f);
- }
- void SetShaderProgram(ShaderProgram* shaderProgram)
- {
- m_ShaderProgram = shaderProgram;
- }
- ShaderProgram* GetShaderProgram() const
- {
- return m_ShaderProgram;
- }
- uint32_t GetWidth() const
- {
- return m_Width;
- }
- uint32_t GetHeight() const
- {
- return m_Height;
- }
- Vector2 GetOriginTranslation() const
- {
- return Vector2(-(float)m_Width / 2.0f, (float)m_Height / 2.0f);
- }
- private:
- void ApplyOrtho(float left, float right,float bottom, float top,float near, float far)
- {
- float a = 2.0f / (right - left);
- float b = 2.0f / (top - bottom);
- float c = -2.0f / (far - near);
- float tx = - (right + left)/(right - left);
- float ty = - (top + bottom)/(top - bottom);
- float tz = - (far + near)/(far - near);
- float ortho[16] = {
- a, 0, 0, tx,
- 0, b, 0, ty,
- 0, 0, c, tz,
- 0, 0, 0, 1
- };
- GLint projectionUniformLocation = glGetUniformLocation(m_ShaderProgram->GetHandle(), m_ShaderProgram->GetProjectionUniformName().c_str());
- glUniformMatrix4fv(projectionUniformLocation, 1, 0, &ortho[0]);
- }
- private:
- ShaderProgram* m_ShaderProgram;
- uint32_t m_Width;
- uint32_t m_Height;
- };
- class Texture
- {
- public:
- uint8_t* GetTexels();
- public:
- void Init(Context* context)
- {
- m_TexCoordLocation = glGetAttribLocation(context->GetShaderProgram()->GetHandle(),
- context->GetShaderProgram()->GetTexCoordAttributeName().c_str());
- m_SamplerLocation = glGetUniformLocation(context->GetShaderProgram()->GetHandle(),
- context->GetShaderProgram()->GetSamplerTextureName().c_str());
- // Use tightly packed data
- glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 );
- // Generate a texture object
- glGenTextures ( 1, &m_TextureId );
- // Bind the texture object
- glBindTexture ( GL_TEXTURE_2D, m_TextureId );
- // Load the texture
- glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGB, m_Width, m_Height, 0, GL_RGB, GL_UNSIGNED_BYTE, &m_Texels[0] );
- // Set the filtering mode
- glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
- glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
- }
- void Apply()
- {
- GLfloat texCoord[] = {
- 0.0f, 1.0f, // TexCoord 0
- 0.0f, 0.0f, // TexCoord 1
- 1.0f, 0.0f, // TexCoord 2
- 1.0f, 1.0f, // TexCoord 3
- };
- // Load the texture coordinate
- glVertexAttribPointer ( m_TexCoordLocation, 2, GL_FLOAT,
- GL_FALSE, 2 * sizeof(float), &texCoord[0] );
- GLint lastError =::eglGetError();
- glEnableVertexAttribArray ( m_TexCoordLocation );
- lastError =::eglGetError();
- glActiveTexture ( GL_TEXTURE0 );
- glBindTexture ( GL_TEXTURE_2D, m_TextureId );
- lastError =::eglGetError();
- glUniform1i ( m_SamplerLocation, 0 );
- lastError =::eglGetError();
- }
- public:
- static IwNUI::CSharedPtr<Texture> LoadFromFile(const std::string& name)
- {
- IwNUI::CSharedPtr<Texture> resultTexture(new Texture);
- CIwImage image;
- image.LoadFromFile(name.c_str());
- resultTexture->m_Width = image.GetWidth();
- resultTexture->m_Height = image.GetHeight();
- resultTexture->m_Texels.resize(image.GetTexelsMemSize());
- memcpy(&resultTexture->m_Texels[0], image.GetTexels(), image.GetTexelsMemSize());
- return resultTexture;
- }
- private:
- std::vector<uint8_t> m_Texels;
- uint32_t m_Width;
- uint32_t m_Height;
- GLuint m_TextureId;
- GLint m_TexCoordLocation;
- GLint m_SamplerLocation;
- };
- class Material
- {
- public:
- Material(Texture* texture) : m_Color(), m_Texture(texture)
- {
- }
- Material(Color color, Texture* texture) : m_Color(color), m_Texture(texture)
- {
- }
- Texture* GetTexture() const
- {
- return m_Texture;
- }
- void Apply(Context* context)
- {
- if (m_Texture)
- {
- m_Texture->Apply();
- }
- GLint colorUniformLocation = ::glGetUniformLocation(context->GetShaderProgram()->GetHandle(),
- context->GetShaderProgram()->GetColorUniformName().c_str());
- ::glUniform4f(colorUniformLocation, m_Color.m_Red, m_Color.m_Green, m_Color.m_Blue, m_Color.m_Alpha);
- }
- public:
- Color m_Color;
- Texture* m_Texture;
- };
- class Quad
- {
- public:
- Quad(float width, float height)
- {
- // 0----*
- // | |
- // *----*
- m_Vertices.push_back(0.0f);
- m_Vertices.push_back(0.0f);
- m_Vertices.push_back(0.0f);
- //*----*
- //| |
- //1----*
- m_Vertices.push_back(0.0f);
- m_Vertices.push_back(-height);
- m_Vertices.push_back(0.0f);
- //*----*
- //| |
- //*----2
- m_Vertices.push_back(width);
- m_Vertices.push_back(-height);
- m_Vertices.push_back(0.0f);
- //*----3
- //| |
- //*----*
- m_Vertices.push_back(width);
- m_Vertices.push_back(0.0f);
- m_Vertices.push_back(0.0f);
- m_Indices.push_back(0);
- m_Indices.push_back(1);
- m_Indices.push_back(2);
- m_Indices.push_back(0);
- m_Indices.push_back(2);
- m_Indices.push_back(3);
- }
- GLfloat* GetVertices()
- {
- return &m_Vertices[0];
- }
- GLushort* GetIndices()
- {
- return &m_Indices[0];
- }
- uint32_t GetVertexCount() const
- {
- return m_Vertices.size();
- }
- uint32_t GetIndicesCount() const
- {
- return m_Indices.size();
- }
- private:
- std::vector<GLfloat> m_Vertices;
- std::vector<GLushort> m_Indices;
- };
- class Translation
- {
- public:
- Translation() : m_Vector()
- {
- }
- Translation(float x, float y) : m_Vector(x, y)
- {
- }
- void ApplyTranslation(Context* context)
- {
- GLint translationUniformLocation = ::glGetUniformLocation(context->GetShaderProgram()->GetHandle(),
- context->GetShaderProgram()->GetTranslationUniformName().c_str());
- ::glUniform4f(translationUniformLocation, m_Vector.m_X, m_Vector.m_Y, 0.0f, 0.0f);
- }
- void ApplyOriginTranslation(Context* context)
- {
- GLint originTranslationUniformLocation = ::glGetUniformLocation(context->GetShaderProgram()->GetHandle(),
- context->GetShaderProgram()->GetOriginTranslationUniformName().c_str());
- Vector2 originTranslationVec2 = context->GetOriginTranslation();
- ::glUniform4f(originTranslationUniformLocation, originTranslationVec2.m_X, originTranslationVec2.m_Y, 0.0f, 0.0f);
- }
- void Move(const Vector2& direction)
- {
- m_Vector += direction;
- }
- private:
- Vector2 m_Vector;
- };
- class GameObject
- {
- public:
- GameObject(Translation* translation, Quad* quad, Material* material)
- : m_Translation(translation), m_Quad(quad), m_Material(material)
- {
- }
- Translation* GetTranslation()
- {
- return m_Translation;
- }
- Quad* GetQuad()
- {
- return m_Quad;
- }
- Material* GetMaterial()
- {
- return m_Material;
- }
- private:
- Quad* m_Quad;
- Translation* m_Translation;
- Material* m_Material;
- };
- class Sprite
- {
- public:
- Sprite(Quad* quad) : m_Quad(quad)
- {
- }
- void Render(Context* context)
- {
- GLint positionAttribLocation = ::glGetAttribLocation(context->GetShaderProgram()->GetHandle(),
- context->GetShaderProgram()->GetPositionAttributeName().c_str());
- ::glVertexAttribPointer(positionAttribLocation, 3, GL_FLOAT, false, 3 * sizeof(float), m_Quad->GetVertices());
- ::glEnableVertexAttribArray(positionAttribLocation);
- GLint colorAttribLocation = ::glGetAttribLocation(context->GetShaderProgram()->GetHandle(),
- context->GetShaderProgram()->GetColorAttributeName().c_str());
- float colors [] =
- {
- 1.0f, 0.0f, 0.0f, 1.0f,
- 0.0f, 0.0f, 1.0f, 1.0f,
- 0.0f, 1.0f, 0.0f, 1.0f,
- 1.0f, 1.0f, 0.0f, 1.0f,
- };
- ::glVertexAttribPointer(colorAttribLocation, 4, GL_FLOAT, false, 4 * sizeof(float), colors);
- ::glEnableVertexAttribArray(colorAttribLocation);
- ::glDrawElements(GL_TRIANGLES, m_Quad->GetIndicesCount(), GL_UNSIGNED_SHORT, m_Quad->GetIndices());
- }
- private:
- Quad* m_Quad;
- GLint m_PositionAttrLocation;
- Material* m_Material;
- };
- class Renderer
- {
- public:
- Renderer(Context* context) : m_Context(context)
- {
- }
- void Push(GameObject* gameObject)
- {
- m_GameObjectList.push_back(gameObject);
- }
- void Render()
- {
- GLint lastError =::eglGetError();
- Clear();
- lastError =::eglGetError();
- m_Context->SetViewport();
- lastError =::eglGetError();
- m_Context->SetOrtho();
- lastError =::eglGetError();
- m_Context->GetShaderProgram()->UseIt();
- lastError =::eglGetError();
- for (std::list<GameObject*>::iterator iter = m_GameObjectList.begin(); iter != m_GameObjectList.end(); ++iter)
- {
- GameObject* gameObject = *iter;
- Translation* translation = gameObject->GetTranslation();
- translation->ApplyOriginTranslation(m_Context);
- translation->ApplyTranslation(m_Context);
- gameObject->GetMaterial()->Apply(m_Context);
- Sprite sprite(gameObject->GetQuad());
- sprite.Render(m_Context);
- }
- lastError =::eglGetError();
- ShowBackBuffer();
- lastError =::eglGetError();
- }
- void Clear()
- {
- ::glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- ::glClear(GL_COLOR_BUFFER_BIT);
- }
- void ShowBackBuffer()
- {
- ::IwGLSwapBuffers();
- }
- private:
- Context* m_Context;
- std::list<GameObject*> m_GameObjectList;
- };
- }
- }
- }
- int main()
- {
- try
- {
- Marmalade::Testing::RenderingGL::Context context;
- context.Init();
- std::string vertexShaderSource = "attribute vec4 a_position; uniform mat4 u_projection; uniform vec4 u_origin_translation; uniform vec4 u_translation; attribute vec2 a_texCoord; varying vec2 v_texCoord; uniform vec4 u_color; varying vec4 v_color; attribute vec4 a_color; void main() { vec4 pos = a_position + u_origin_translation + u_translation; gl_Position = u_projection * pos; v_texCoord = a_texCoord; v_color = a_color * u_color; }";
- Marmalade::Testing::RenderingGL::VertexShader vertexShader = Marmalade::Testing::RenderingGL::VertexShader::Create(vertexShaderSource);
- std::string fragmentShaderSource = "precision mediump float; varying vec2 v_texCoord; uniform sampler2D s_texture; varying vec4 v_color; void main() { vec4 texColor = texture2D(s_texture, v_texCoord); gl_FragColor = texColor * v_color; }";
- Marmalade::Testing::RenderingGL::FragmentShader fragmentShader = Marmalade::Testing::RenderingGL::FragmentShader::Create(fragmentShaderSource);
- Marmalade::Testing::RenderingGL::ShaderProgram shaderProgram(&fragmentShader, &vertexShader);
- context.SetShaderProgram(&shaderProgram);
- IwNUI::CSharedPtr<Marmalade::Testing::RenderingGL::Texture> texture =
- Marmalade::Testing::RenderingGL::Texture::LoadFromFile("index.png");
- texture->Init(&context);
- Marmalade::Testing::RenderingGL::Renderer renderer(&context);
- Marmalade::Testing::RenderingGL::Quad quad(context.GetWidth()/2, context.GetHeight()/2);
- Marmalade::Testing::RenderingGL::Translation translation(100, -100);
- Marmalade::Testing::RenderingGL::Material material(Marmalade::Testing::RenderingGL::Color(1.0f, 1.0f, 1.0f, 1.0f), texture.get());
- Marmalade::Testing::RenderingGL::GameObject gameObject(&translation, &quad, &material);
- renderer.Push(&gameObject);
- while (!::s3eDeviceCheckQuitRequest())
- {
- renderer.Render();
- }
- }
- catch(...)
- {
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement