Emscripten workarounds and llvm syntax fixes
[platform/core/uifw/dali-core.git] / dali / internal / render / dynamics / scene-graph-dynamics-debug-renderer.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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
7 //
8 //     http://floralicense.org/license/
9 //
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.
15 //
16
17 // CLASS HEADER
18 #include "scene-graph-dynamics-debug-renderer.h"
19
20 // INTERNAL HEADERS
21 #include <dali/integration-api/debug.h>
22 #include <dali/internal/render/gl-resources/context.h>
23 #include <dali/internal/render/shaders/program.h>
24 #include <dali/internal/render/shaders/shader.h>
25
26 namespace Dali
27 {
28
29 namespace Internal
30 {
31
32 namespace SceneGraph
33 {
34
35 DynamicsDebugRenderer::DynamicsDebugRenderer(const Shader& debugShader)
36 : mShader(const_cast<Shader&>(debugShader)),
37   mContext(NULL),
38   mBufferIndex(0),
39   mViewMatrix(Matrix::IDENTITY),
40   mProjectionMatrix(Matrix::IDENTITY),
41   mNumberOfPoints(0)
42 {
43   DALI_LOG_INFO(Debug::Filter::gDynamics, Debug::Verbose, "%s\n", __PRETTY_FUNCTION__);
44 }
45
46 DynamicsDebugRenderer::~DynamicsDebugRenderer()
47 {
48 }
49
50 void DynamicsDebugRenderer::Initialize(Context& context)
51 {
52   DALI_LOG_INFO(Debug::Filter::gDynamics, Debug::Verbose, "%s\n", __PRETTY_FUNCTION__);
53
54   mContext = &context;
55 }
56
57 void DynamicsDebugRenderer::UpdateMatrices( BufferIndex bufferIndex, const Matrix& projectionMatrix, const Matrix& viewMatrix )
58 {
59   mProjectionMatrix = projectionMatrix;
60   mViewMatrix = viewMatrix;
61   mBufferIndex = bufferIndex;
62 }
63
64 void DynamicsDebugRenderer::UpdateBuffer( const Integration::DynamicsDebugVertexContainer& vertices )
65 {
66   if( NULL != mContext )
67   {
68     if( !mBuffer )
69     {
70       mBuffer = new GpuBuffer(*mContext, GL_ARRAY_BUFFER, GL_STATIC_DRAW);
71     }
72
73     if( vertices.size() > 1 )
74     {
75       mBuffer->UpdateDataBuffer( vertices.size() * sizeof(vertices[0]), vertices.data() );
76     }
77     mNumberOfPoints = vertices.size();
78   }
79 }
80
81 void DynamicsDebugRenderer::Render()
82 {
83   if( mBuffer && mNumberOfPoints )
84   {
85     Integration::DynamicsDebugVertex* vertex = 0;
86
87     Program& program = mShader.Apply(*mContext, mBufferIndex, GEOMETRY_TYPE_IMAGE, Matrix::IDENTITY, Matrix::IDENTITY, mViewMatrix, mProjectionMatrix, Color::WHITE, SHADER_DEFAULT);
88     const GLint positionLoc = program.GetAttribLocation(Program::ATTRIB_POSITION);
89     const GLint colorLoc = program.GetAttribLocation(Program::ATTRIB_COLOR);
90     mBuffer->Bind();
91     mContext->VertexAttribPointer(positionLoc, 3, GL_FLOAT, false, sizeof(Integration::DynamicsDebugVertex), (void*)&vertex->position);
92     mContext->VertexAttribPointer(colorLoc, 4, GL_FLOAT, false, sizeof(Integration::DynamicsDebugVertex), (void*)&vertex->color);
93     mContext->EnableVertexAttributeArray(positionLoc);
94     mContext->EnableVertexAttributeArray(colorLoc);
95     mContext->DrawArrays( GL_LINES, 0, mNumberOfPoints );
96     mContext->DisableVertexAttributeArray(positionLoc);
97     mContext->DisableVertexAttributeArray(colorLoc);
98     mContext->BindArrayBuffer( 0 );
99   }
100 }
101
102
103 } // namespace SceneGraph
104
105 } // namespace Internal
106
107 } // namespace Dali