Advertisement
Guest User

Marmalade with OpenGL ES 2.0 little wrapper

a guest
Nov 11th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 18.08 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string>
  4. #include <math.h>
  5. #include <vector>
  6. #include <list>
  7. #include <IwGx.h>
  8. #include "IwGL.h"
  9. #include <Iw2D.h>
  10. #include <IwMaterial.h>
  11. #include "s3e.h"
  12. #include "IwNUI.h"
  13.  
  14.  
  15. namespace Marmalade
  16. {
  17. namespace Testing
  18. {
  19. namespace RenderingGL
  20. {
  21. class Vector2
  22. {
  23. public:
  24.  
  25.     Vector2() : m_X(0.0f), m_Y(0.0f)
  26.     {
  27.     }
  28.  
  29.     Vector2(float x, float y) : m_X(x), m_Y(y)
  30.     {
  31.     }
  32.  
  33.  
  34. public:
  35.  
  36.     friend Vector2& operator += (Vector2& left, const Vector2& right);
  37.  
  38.  
  39. public:
  40.  
  41.     union
  42.     {
  43.         float m_Vec[2];
  44.  
  45.         struct
  46.         {
  47.             float m_X;
  48.             float m_Y;
  49.         };
  50.     };
  51. };
  52.  
  53.  
  54. Vector2& operator += (Vector2& left, const Vector2& right)
  55. {
  56.     left.m_X += right.m_X;
  57.     left.m_Y += right.m_Y;
  58.  
  59.     return left;
  60. }
  61.  
  62.  
  63. class Color
  64. {
  65. public:
  66.  
  67.     Color() : m_Red(1.0f), m_Green(1.0f), m_Blue(1.0f), m_Alpha(1.0f)
  68.     {
  69.     }
  70.  
  71.     Color(float r, float g, float b, float a) : m_Red(r), m_Green(g), m_Blue(b), m_Alpha(a)
  72.     {
  73.     }
  74.  
  75.  
  76. public:
  77.  
  78.     float m_Red;
  79.     float m_Green;
  80.     float m_Blue;
  81.     float m_Alpha;
  82. };
  83.  
  84.  
  85. class VertexShader
  86. {
  87. public:
  88.  
  89.     GLuint GetHandle() const
  90.     {
  91.         return m_Shader;
  92.     }
  93.  
  94.  
  95. public:
  96.  
  97.     static VertexShader Create(const std::string& source)
  98.     {
  99.         VertexShader shader;
  100.  
  101.         shader.m_Shader = glCreateShader(GL_VERTEX_SHADER);
  102.  
  103.         const char* sourceCode = source.c_str();
  104.  
  105.         glShaderSource(shader.m_Shader, 1, &sourceCode, 0);
  106.    
  107.         glCompileShader(shader.m_Shader);
  108.  
  109.  
  110.         GLint compiled;
  111.         glGetShaderiv ( shader.m_Shader, GL_COMPILE_STATUS, &compiled );
  112.  
  113.        if ( !compiled )
  114.        {
  115.            GLint infoLen = 0;
  116.  
  117.           glGetShaderiv ( shader.m_Shader, GL_INFO_LOG_LENGTH, &infoLen );
  118.      
  119.           if ( infoLen > 1 )
  120.           {
  121.               std::vector<char> buffer;
  122.  
  123.              buffer.resize(infoLen);
  124.  
  125.              glGetShaderInfoLog ( shader.m_Shader, infoLen, NULL, &buffer[0] );
  126.           }
  127.        }
  128.  
  129.  
  130.         return shader;
  131.     }
  132.  
  133.  
  134. private:
  135.  
  136.     GLuint m_Shader;
  137. };
  138.  
  139.  
  140. class FragmentShader
  141. {
  142. public:
  143.  
  144.     GLuint GetHandle() const
  145.     {
  146.         return m_Shader;
  147.     }
  148.  
  149.  
  150. public:
  151.  
  152.     static FragmentShader Create(const std::string& source)
  153.     {
  154.         FragmentShader shader;
  155.  
  156.         shader.m_Shader = glCreateShader(GL_FRAGMENT_SHADER);
  157.    
  158.         const char* sourceCode = source.c_str();
  159.  
  160.         glShaderSource(shader.m_Shader, 1, &sourceCode, 0);
  161.    
  162.         glCompileShader(shader.m_Shader);
  163.  
  164.  
  165.         GLint compiled;
  166.         glGetShaderiv ( shader.m_Shader, GL_COMPILE_STATUS, &compiled );
  167.  
  168.        if ( !compiled )
  169.        {
  170.            GLint infoLen = 0;
  171.  
  172.           glGetShaderiv ( shader.m_Shader, GL_INFO_LOG_LENGTH, &infoLen );
  173.      
  174.           if ( infoLen > 1 )
  175.           {
  176.               std::vector<char> buffer;
  177.  
  178.              buffer.resize(infoLen);
  179.  
  180.              glGetShaderInfoLog ( shader.m_Shader, infoLen, NULL, &buffer[0] );
  181.           }
  182.        }
  183.  
  184.  
  185.         return shader;
  186.     }
  187.  
  188.  
  189. private:
  190.  
  191.     GLuint m_Shader;
  192. };
  193.  
  194.  
  195. class ShaderProgram
  196. {
  197. public:
  198.  
  199.     ShaderProgram(FragmentShader* fragmentShader, VertexShader* vertexShader)
  200.         : m_Handle(glCreateProgram()), m_FragmentShader(fragmentShader), m_VertexShader(vertexShader)
  201.     {
  202.         glAttachShader(m_Handle, vertexShader->GetHandle());
  203.         glAttachShader(m_Handle, fragmentShader->GetHandle());
  204.  
  205.         glLinkProgram(m_Handle);
  206.  
  207.  
  208.         GLint linked;
  209.         glGetProgramiv ( m_Handle, GL_LINK_STATUS, &linked );
  210.  
  211.    if ( !linked )
  212.    {
  213.        GLint infoLen = 0;
  214.  
  215.       glGetProgramiv ( m_Handle, GL_INFO_LOG_LENGTH, &infoLen );
  216.      
  217.       if ( infoLen > 1 )
  218.       {
  219.           std::vector<char> buffer;
  220.  
  221.           buffer.resize(infoLen);
  222.  
  223.           glGetProgramInfoLog ( m_Handle, infoLen, NULL, &buffer[0] );
  224.       }
  225.    }
  226.  
  227.  
  228.        
  229.     }
  230.  
  231.     void UseIt()
  232.     {
  233.         ::glUseProgram(m_Handle);
  234.     }
  235.  
  236.     GLuint GetHandle()
  237.     {
  238.         return m_Handle;
  239.     }
  240.  
  241.  
  242.     std::string GetProjectionUniformName()
  243.     {
  244.         return "u_projection";
  245.     }
  246.  
  247.  
  248.     std::string GetOriginTranslationUniformName()
  249.     {
  250.         return "u_origin_translation";
  251.     }
  252.  
  253.  
  254.     std::string GetTranslationUniformName()
  255.     {
  256.         return "u_translation";
  257.     }
  258.  
  259.  
  260.     std::string GetColorUniformName()
  261.     {
  262.         return "u_color";
  263.     }
  264.  
  265.  
  266.     std::string GetColorAttributeName()
  267.     {
  268.         return "a_color";
  269.     }
  270.  
  271.  
  272.     std::string GetPositionAttributeName()
  273.     {
  274.         return "a_position";
  275.     }
  276.  
  277.  
  278.     std::string GetTexCoordAttributeName()
  279.     {
  280.         return "a_texCoord";
  281.     }
  282.  
  283.  
  284.     std::string GetSamplerTextureName()
  285.     {
  286.         return "s_texture";
  287.     }
  288.  
  289.  
  290. private:
  291.  
  292.     GLuint m_Handle;
  293.  
  294.     FragmentShader* m_FragmentShader;
  295.     VertexShader* m_VertexShader;
  296. };
  297.  
  298.  
  299. class Context
  300. {
  301. public:
  302.  
  303.     Context() : m_ShaderProgram(0), m_Width(0.0f), m_Height(0.0f)
  304.     {
  305.     }
  306.  
  307.     void Init()
  308.     {
  309.         IwGLInit();
  310.  
  311.         m_Width = ::IwGLGetInt(IW_GL_WIDTH);
  312.         m_Height = ::IwGLGetInt(IW_GL_HEIGHT);
  313.     }
  314.  
  315.     void SetViewport()
  316.     {
  317.         glViewport(0, 0, m_Width, m_Height);
  318.     }
  319.  
  320.     void SetOrtho()
  321.     {
  322.         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);
  323.     }
  324.  
  325.     void SetShaderProgram(ShaderProgram* shaderProgram)
  326.     {
  327.         m_ShaderProgram = shaderProgram;
  328.     }
  329.  
  330.     ShaderProgram* GetShaderProgram() const
  331.     {
  332.         return m_ShaderProgram;
  333.     }
  334.  
  335.     uint32_t GetWidth() const
  336.     {
  337.         return m_Width;
  338.     }
  339.  
  340.     uint32_t GetHeight() const
  341.     {
  342.         return m_Height;
  343.     }
  344.  
  345.     Vector2 GetOriginTranslation() const
  346.     {
  347.         return Vector2(-(float)m_Width / 2.0f, (float)m_Height / 2.0f);
  348.     }
  349.  
  350.  
  351. private:
  352.  
  353.     void ApplyOrtho(float left, float right,float bottom, float top,float near, float far)
  354.     {
  355.         float a = 2.0f / (right - left);
  356.         float b = 2.0f / (top - bottom);
  357.         float c = -2.0f / (far - near);
  358.  
  359.         float tx = - (right + left)/(right - left);
  360.         float ty = - (top + bottom)/(top - bottom);
  361.         float tz = - (far + near)/(far - near);
  362.  
  363.         float ortho[16] = {
  364.             a, 0, 0, tx,
  365.             0, b, 0, ty,
  366.             0, 0, c, tz,
  367.             0, 0, 0, 1
  368.         };
  369.  
  370.  
  371.         GLint projectionUniformLocation = glGetUniformLocation(m_ShaderProgram->GetHandle(), m_ShaderProgram->GetProjectionUniformName().c_str());
  372.         glUniformMatrix4fv(projectionUniformLocation, 1, 0, &ortho[0]);
  373.     }
  374.  
  375.  
  376. private:
  377.  
  378.     ShaderProgram* m_ShaderProgram;
  379.  
  380.     uint32_t m_Width;
  381.     uint32_t m_Height;
  382. };
  383.  
  384.  
  385. class Texture
  386. {
  387. public:
  388.  
  389.     uint8_t* GetTexels();
  390.  
  391.  
  392. public:
  393.  
  394.     void Init(Context* context)
  395.     {
  396.         m_TexCoordLocation = glGetAttribLocation(context->GetShaderProgram()->GetHandle(),
  397.             context->GetShaderProgram()->GetTexCoordAttributeName().c_str());
  398.    
  399.         m_SamplerLocation = glGetUniformLocation(context->GetShaderProgram()->GetHandle(),
  400.             context->GetShaderProgram()->GetSamplerTextureName().c_str());
  401.  
  402.  
  403.         // Use tightly packed data
  404.         glPixelStorei ( GL_UNPACK_ALIGNMENT, 1 );
  405.  
  406.         // Generate a texture object
  407.         glGenTextures ( 1, &m_TextureId );
  408.  
  409.         // Bind the texture object
  410.         glBindTexture ( GL_TEXTURE_2D, m_TextureId );
  411.  
  412.         // Load the texture
  413.         glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGB, m_Width, m_Height, 0, GL_RGB, GL_UNSIGNED_BYTE, &m_Texels[0] );
  414.  
  415.         // Set the filtering mode
  416.         glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  417.         glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
  418.     }
  419.  
  420.  
  421.     void Apply()
  422.     {
  423.         GLfloat texCoord[] = {
  424.                             0.0f,  1.0f,        // TexCoord 0
  425.                             0.0f,  0.0f,        // TexCoord 1
  426.                             1.0f,  0.0f,        // TexCoord 2
  427.                             1.0f,  1.0f,        // TexCoord 3
  428.                          };
  429.            // Load the texture coordinate
  430.         glVertexAttribPointer ( m_TexCoordLocation, 2, GL_FLOAT,
  431.             GL_FALSE, 2 * sizeof(float), &texCoord[0] );
  432.  
  433.         GLint lastError =::eglGetError();
  434.  
  435.         glEnableVertexAttribArray ( m_TexCoordLocation );
  436.  
  437.         lastError =::eglGetError();
  438.  
  439.  
  440.         glActiveTexture ( GL_TEXTURE0 );
  441.    
  442.         glBindTexture ( GL_TEXTURE_2D, m_TextureId );
  443.  
  444.         lastError =::eglGetError();
  445.  
  446.         glUniform1i ( m_SamplerLocation, 0 );
  447.  
  448.         lastError =::eglGetError();
  449.     }
  450.  
  451.  
  452. public:
  453.  
  454.     static IwNUI::CSharedPtr<Texture> LoadFromFile(const std::string& name)
  455.     {
  456.         IwNUI::CSharedPtr<Texture> resultTexture(new Texture);
  457.  
  458.  
  459.         CIwImage image;
  460.  
  461.         image.LoadFromFile(name.c_str());
  462.  
  463.         resultTexture->m_Width = image.GetWidth();
  464.         resultTexture->m_Height = image.GetHeight();
  465.  
  466.         resultTexture->m_Texels.resize(image.GetTexelsMemSize());
  467.  
  468.         memcpy(&resultTexture->m_Texels[0], image.GetTexels(), image.GetTexelsMemSize());
  469.  
  470.  
  471.         return resultTexture;
  472.     }
  473.  
  474.  
  475. private:
  476.  
  477.     std::vector<uint8_t> m_Texels;
  478.  
  479.     uint32_t m_Width;
  480.     uint32_t m_Height;
  481.  
  482.     GLuint m_TextureId;
  483.     GLint m_TexCoordLocation;
  484.     GLint m_SamplerLocation;
  485. };
  486.  
  487.  
  488. class Material
  489. {
  490. public:
  491.  
  492.     Material(Texture* texture) : m_Color(), m_Texture(texture)
  493.     {
  494.     }
  495.  
  496.     Material(Color color, Texture* texture) : m_Color(color), m_Texture(texture)
  497.     {
  498.     }
  499.  
  500.    
  501.     Texture* GetTexture() const
  502.     {
  503.         return m_Texture;
  504.     }
  505.  
  506.  
  507.     void Apply(Context* context)
  508.     {
  509.         if (m_Texture)
  510.         {
  511.             m_Texture->Apply();
  512.         }
  513.  
  514.  
  515.         GLint colorUniformLocation = ::glGetUniformLocation(context->GetShaderProgram()->GetHandle(),
  516.             context->GetShaderProgram()->GetColorUniformName().c_str());
  517.        
  518.         ::glUniform4f(colorUniformLocation, m_Color.m_Red, m_Color.m_Green, m_Color.m_Blue, m_Color.m_Alpha);
  519.     }
  520.  
  521.  
  522. public:
  523.  
  524.     Color m_Color;
  525.     Texture* m_Texture;
  526. };
  527.  
  528.  
  529. class Quad
  530. {
  531. public:
  532.  
  533.     Quad(float width, float height)
  534.     {
  535.         // 0----*
  536.         // |    |
  537.         // *----*
  538.         m_Vertices.push_back(0.0f);
  539.         m_Vertices.push_back(0.0f);
  540.         m_Vertices.push_back(0.0f);
  541.  
  542.         //*----*
  543.         //|    |
  544.         //1----*
  545.         m_Vertices.push_back(0.0f);
  546.         m_Vertices.push_back(-height);
  547.         m_Vertices.push_back(0.0f);
  548.  
  549.         //*----*
  550.         //|    |
  551.         //*----2
  552.         m_Vertices.push_back(width);
  553.         m_Vertices.push_back(-height);
  554.         m_Vertices.push_back(0.0f);
  555.  
  556.         //*----3
  557.         //|    |
  558.         //*----*
  559.         m_Vertices.push_back(width);
  560.         m_Vertices.push_back(0.0f);
  561.         m_Vertices.push_back(0.0f);
  562.        
  563.  
  564.        
  565.         m_Indices.push_back(0);
  566.         m_Indices.push_back(1);
  567.         m_Indices.push_back(2);
  568.         m_Indices.push_back(0);
  569.         m_Indices.push_back(2);
  570.         m_Indices.push_back(3);
  571.     }
  572.  
  573.     GLfloat* GetVertices()
  574.     {
  575.         return &m_Vertices[0];
  576.     }
  577.  
  578.     GLushort* GetIndices()
  579.     {
  580.         return &m_Indices[0];
  581.     }
  582.  
  583.     uint32_t GetVertexCount() const
  584.     {
  585.         return m_Vertices.size();
  586.     }
  587.  
  588.     uint32_t GetIndicesCount() const
  589.     {
  590.         return m_Indices.size();
  591.     }
  592.  
  593.  
  594. private:
  595.  
  596.     std::vector<GLfloat> m_Vertices;
  597.     std::vector<GLushort> m_Indices;
  598. };
  599.  
  600.  
  601. class Translation
  602. {
  603. public:
  604.  
  605.     Translation() : m_Vector()
  606.     {
  607.     }
  608.  
  609.     Translation(float x, float y) : m_Vector(x, y)
  610.     {
  611.     }
  612.  
  613.     void ApplyTranslation(Context* context)
  614.     {
  615.         GLint translationUniformLocation = ::glGetUniformLocation(context->GetShaderProgram()->GetHandle(),
  616.             context->GetShaderProgram()->GetTranslationUniformName().c_str());
  617.        
  618.         ::glUniform4f(translationUniformLocation, m_Vector.m_X, m_Vector.m_Y, 0.0f, 0.0f);
  619.     }
  620.  
  621.     void ApplyOriginTranslation(Context* context)
  622.     {
  623.         GLint originTranslationUniformLocation = ::glGetUniformLocation(context->GetShaderProgram()->GetHandle(),
  624.             context->GetShaderProgram()->GetOriginTranslationUniformName().c_str());
  625.        
  626.         Vector2 originTranslationVec2 = context->GetOriginTranslation();
  627.        
  628.         ::glUniform4f(originTranslationUniformLocation, originTranslationVec2.m_X, originTranslationVec2.m_Y, 0.0f, 0.0f);
  629.     }
  630.  
  631.     void Move(const Vector2& direction)
  632.     {
  633.         m_Vector += direction;
  634.     }
  635.  
  636.  
  637. private:
  638.  
  639.     Vector2 m_Vector;
  640. };
  641.  
  642.  
  643. class GameObject
  644. {
  645. public:
  646.  
  647.     GameObject(Translation* translation, Quad* quad, Material* material)
  648.        
  649.         : m_Translation(translation), m_Quad(quad), m_Material(material)
  650.     {
  651.     }
  652.  
  653.     Translation* GetTranslation()
  654.     {
  655.         return m_Translation;
  656.     }
  657.  
  658.     Quad* GetQuad()
  659.     {
  660.         return m_Quad;
  661.     }
  662.  
  663.     Material* GetMaterial()
  664.     {
  665.         return m_Material;
  666.     }
  667.  
  668.  
  669. private:
  670.  
  671.     Quad* m_Quad;
  672.     Translation* m_Translation;
  673.     Material* m_Material;
  674. };
  675.  
  676.  
  677. class Sprite
  678. {
  679. public:
  680.  
  681.     Sprite(Quad* quad) : m_Quad(quad)
  682.     {
  683.     }
  684.  
  685.  
  686.     void Render(Context* context)
  687.     {
  688.         GLint positionAttribLocation = ::glGetAttribLocation(context->GetShaderProgram()->GetHandle(),
  689.             context->GetShaderProgram()->GetPositionAttributeName().c_str());
  690.  
  691.         ::glVertexAttribPointer(positionAttribLocation, 3, GL_FLOAT, false, 3 * sizeof(float), m_Quad->GetVertices());
  692.  
  693.        
  694.         ::glEnableVertexAttribArray(positionAttribLocation);
  695.  
  696.  
  697.  
  698.         GLint colorAttribLocation = ::glGetAttribLocation(context->GetShaderProgram()->GetHandle(),
  699.             context->GetShaderProgram()->GetColorAttributeName().c_str());
  700.  
  701.  
  702.         float colors [] =
  703.         {
  704.             1.0f, 0.0f, 0.0f, 1.0f,
  705.             0.0f, 0.0f, 1.0f, 1.0f,
  706.             0.0f, 1.0f, 0.0f, 1.0f,
  707.             1.0f, 1.0f, 0.0f, 1.0f,
  708.         };
  709.  
  710.         ::glVertexAttribPointer(colorAttribLocation, 4, GL_FLOAT, false, 4 * sizeof(float), colors);
  711.  
  712.        
  713.         ::glEnableVertexAttribArray(colorAttribLocation);
  714.  
  715.  
  716.         ::glDrawElements(GL_TRIANGLES, m_Quad->GetIndicesCount(), GL_UNSIGNED_SHORT, m_Quad->GetIndices());
  717.  
  718.     }
  719.  
  720.  
  721. private:
  722.  
  723.     Quad* m_Quad;
  724.  
  725.     GLint m_PositionAttrLocation;
  726.  
  727.     Material* m_Material;
  728. };
  729.  
  730.  
  731. class Renderer
  732. {
  733. public:
  734.  
  735.     Renderer(Context* context) : m_Context(context)
  736.     {
  737.     }
  738.  
  739.     void Push(GameObject* gameObject)
  740.     {
  741.         m_GameObjectList.push_back(gameObject);
  742.     }
  743.  
  744.     void Render()
  745.     {
  746.         GLint lastError =::eglGetError();
  747.  
  748.  
  749.         Clear();
  750.  
  751.        
  752.         lastError =::eglGetError();
  753.  
  754.  
  755.         m_Context->SetViewport();
  756.  
  757.  
  758.         lastError =::eglGetError();
  759.  
  760.  
  761.         m_Context->SetOrtho();
  762.  
  763.  
  764.         lastError =::eglGetError();
  765.  
  766.        
  767.         m_Context->GetShaderProgram()->UseIt();
  768.  
  769.  
  770.         lastError =::eglGetError();
  771.  
  772.  
  773.         for (std::list<GameObject*>::iterator iter = m_GameObjectList.begin(); iter != m_GameObjectList.end(); ++iter)
  774.         {
  775.             GameObject* gameObject = *iter;
  776.  
  777.             Translation* translation = gameObject->GetTranslation();
  778.             translation->ApplyOriginTranslation(m_Context);
  779.             translation->ApplyTranslation(m_Context);
  780.  
  781.             gameObject->GetMaterial()->Apply(m_Context);
  782.  
  783.             Sprite sprite(gameObject->GetQuad());
  784.  
  785.             sprite.Render(m_Context);
  786.         }
  787.  
  788.  
  789.         lastError =::eglGetError();
  790.  
  791.  
  792.         ShowBackBuffer();
  793.  
  794.  
  795.         lastError =::eglGetError();
  796.     }
  797.  
  798.  
  799.     void Clear()
  800.     {
  801.         ::glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  802.  
  803.         ::glClear(GL_COLOR_BUFFER_BIT);
  804.     }
  805.  
  806.     void ShowBackBuffer()
  807.     {
  808.         ::IwGLSwapBuffers();
  809.     }
  810.  
  811.  
  812. private:
  813.  
  814.     Context* m_Context;
  815.  
  816.     std::list<GameObject*> m_GameObjectList;
  817. };
  818. }
  819. }
  820. }
  821.  
  822.  
  823. int main()
  824. {
  825.     try
  826.     {
  827.         Marmalade::Testing::RenderingGL::Context context;
  828.  
  829.         context.Init();
  830.  
  831.  
  832.         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; }";
  833.  
  834.  
  835.         Marmalade::Testing::RenderingGL::VertexShader vertexShader = Marmalade::Testing::RenderingGL::VertexShader::Create(vertexShaderSource);
  836.  
  837.        
  838.         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; }";
  839.  
  840.  
  841.         Marmalade::Testing::RenderingGL::FragmentShader fragmentShader = Marmalade::Testing::RenderingGL::FragmentShader::Create(fragmentShaderSource);
  842.  
  843.         Marmalade::Testing::RenderingGL::ShaderProgram shaderProgram(&fragmentShader, &vertexShader);
  844.    
  845.  
  846.        
  847.  
  848.         context.SetShaderProgram(&shaderProgram);
  849.  
  850.  
  851.         IwNUI::CSharedPtr<Marmalade::Testing::RenderingGL::Texture> texture =
  852.             Marmalade::Testing::RenderingGL::Texture::LoadFromFile("index.png");
  853.  
  854.         texture->Init(&context);
  855.  
  856.  
  857.         Marmalade::Testing::RenderingGL::Renderer renderer(&context);
  858.  
  859.         Marmalade::Testing::RenderingGL::Quad quad(context.GetWidth()/2, context.GetHeight()/2);
  860.         Marmalade::Testing::RenderingGL::Translation translation(100, -100);
  861.         Marmalade::Testing::RenderingGL::Material material(Marmalade::Testing::RenderingGL::Color(1.0f, 1.0f, 1.0f, 1.0f), texture.get());
  862.         Marmalade::Testing::RenderingGL::GameObject gameObject(&translation, &quad, &material);
  863.         renderer.Push(&gameObject);
  864.  
  865.  
  866.         while (!::s3eDeviceCheckQuitRequest())
  867.         {
  868.             renderer.Render();
  869.         }
  870.  
  871.     }
  872.     catch(...)
  873.     {
  874.     }
  875.  
  876.  
  877.     return 0;
  878. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement