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