2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using System.Runtime.InteropServices;
21 using Tizen.NUI.UIComponents;
22 using Tizen.NUI.BaseComponents;
23 using Tizen.NUI.Constants;
26 namespace GeometryTest
28 class Example : NUIApplication
30 public Example():base()
34 protected override void OnCreate()
40 [StructLayout(LayoutKind.Sequential)]
45 public Vec2(float xIn, float yIn)
52 public struct TexturedQuadVertex
55 // public Vec2 textureCoordinates;
58 public static byte[] Struct2Bytes(TexturedQuadVertex[] obj)
60 int size = Marshal.SizeOf(obj);
61 byte[] bytes = new byte[size];
62 IntPtr ptr = Marshal.AllocHGlobal(size);
63 Marshal.StructureToPtr(obj, ptr, false);
64 Marshal.Copy(ptr, bytes, 0, size);
65 Marshal.FreeHGlobal(ptr);
69 private void Initialize()
71 // Connect the signal callback for window touched signal
72 Window window = Window.Instance;
73 window.BackgroundColor = Color.White;
76 const string VERTEX_SHADER = "" +
77 "attribute mediump vec2 aPosition;\n" +
78 "uniform mediump mat4 uMvpMatrix;\n" +
79 "uniform mediump vec3 uSize;\n" +
83 "mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n" +
84 "vertexPosition.xyz *= uSize;\n"+
85 "gl_Position = uMvpMatrix * vertexPosition;\n" +
89 const string FRAGMENT_SHADER = "" +
90 "uniform mediump vec4 uColor;\n" +
94 "gl_FragColor = uColor;\n" +
97 TexturedQuadVertex vertex1 = new TexturedQuadVertex();
98 vertex1.position = new Vec2(-1.0f, -1.0f);
99 TexturedQuadVertex vertex2 = new TexturedQuadVertex();
100 vertex2.position = new Vec2(1.0f, 1.0f);
102 TexturedQuadVertex[] texturedQuadVertexData;
103 texturedQuadVertexData = new TexturedQuadVertex[2] { vertex1, vertex2 };
105 int lenght = Marshal.SizeOf(vertex1);
106 IntPtr pA = Marshal.AllocHGlobal(lenght * 2);
108 for (int i = 0; i < 2; i++)
110 Marshal.StructureToPtr(texturedQuadVertexData[i], pA + i * lenght, true);
114 Shader shader = new Shader(VERTEX_SHADER, FRAGMENT_SHADER);
116 /* Create Property buffer */
117 PropertyMap vertexFormat = new PropertyMap();
118 vertexFormat.Add("aPosition", new PropertyValue((int)PropertyType.Vector2));
120 PropertyBuffer vertexBuffer = new PropertyBuffer( vertexFormat );
121 vertexBuffer.SetData(pA, 4);
123 /* Create geometry */
124 Geometry geometry = new Geometry();
125 geometry.AddVertexBuffer(vertexBuffer);
126 geometry.SetType(Geometry.Type.LINES);
128 /* Create renderer */
129 Renderer renderer = new Renderer(geometry, shader);
132 View view = new View()
134 Size2D = new Size2D(300, 300),
135 ParentOrigin = ParentOrigin.Center,
136 PivotPoint = PivotPoint.Center,
137 PositionUsesPivotPoint = true,
138 BackgroundColor = Color.Black,
140 view.AddRenderer(renderer);
146 /// The main entry point for the application.
149 static void _Main(string[] args)
151 Example example = new Example();