1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <3DChartObjects.hxx>
11 #include <vcl/virdev.hxx>
12 #include <vcl/svapp.hxx>
14 #include <vcl/opengl/GLMHelper.hxx>
15 #include <vcl/opengl/OpenGLHelper.hxx>
16 #include <vcl/bitmapaccess.hxx>
22 Renderable3DObject::Renderable3DObject(OpenGL3DRenderer
* pRenderer
, sal_uInt32 nId
):
23 mpRenderer(pRenderer
),
28 void Renderable3DObject::render()
33 Bar::Bar(OpenGL3DRenderer
* pRenderer
, const glm::mat4
& rPosition
, Color aColor
, sal_uInt32 nId
)
34 : Renderable3DObject(pRenderer
, nId
)
38 SAL_INFO("chart2.3dopengl", rPosition
);
43 mpRenderer
->AddShape3DExtrudeObject(true/*RoundedCorners*/, maColor
, 0xFFFFFF, maPos
, mnUniqueId
);
44 mpRenderer
->EndAddShape3DExtrudeObject();
47 Line::Line(OpenGL3DRenderer
* pRenderer
, sal_uInt32 nId
):
48 Renderable3DObject(pRenderer
, nId
)
54 mpRenderer
->AddShapePolygon3DObject(Color(0), true, maLineColor
, 0, 0, mnUniqueId
);
55 mpRenderer
->AddPolygon3DObjectPoint(maPosBegin
.x
, maPosBegin
.y
, maPosBegin
.z
);
56 mpRenderer
->AddPolygon3DObjectPoint(maPosEnd
.x
, maPosEnd
.y
, maPosEnd
.z
);
57 mpRenderer
->EndAddPolygon3DObjectPoint();
58 mpRenderer
->EndAddShapePolygon3DObject();
61 void Line::setPosition(const glm::vec3
& rBegin
, const glm::vec3
& rEnd
)
67 void Line::setLineColor(const Color
& rColor
)
72 const TextCacheItem
& TextCache::getText(OUString
const & rText
, bool bIs3dText
)
74 TextCacheType::const_iterator
const itr
= m_TextCache
.find(rText
);
75 if (itr
!= m_TextCache
.end())
78 ScopedVclPtrInstance
< VirtualDevice
> pDevice(*Application::GetDefaultDevice(),
79 DeviceFormat::DEFAULT
, DeviceFormat::DEFAULT
);
82 aFont
= vcl::Font("Brillante St",Size(0,0));
84 aFont
= pDevice
->GetFont();
85 aFont
.SetFontSize(Size(0, 96));
86 aFont
.SetColor(COL_GREEN
);
87 pDevice
->SetFont(aFont
);
90 pDevice
->SetOutputSize(Size(pDevice
->GetTextWidth(rText
), pDevice
->GetTextHeight()));
91 pDevice
->SetBackground(Wallpaper(COL_TRANSPARENT
));
92 pDevice
->DrawText(Point(0,0), rText
);
94 BitmapEx
aText(pDevice
->GetBitmapEx(Point(0,0), pDevice
->GetOutputSize()));
95 Bitmap
aBitmap (aText
.GetBitmap());
96 BitmapReadAccess
*pAcc
= aBitmap
.AcquireReadAccess();
97 sal_uInt8
*buf
= reinterpret_cast<sal_uInt8
*>(pAcc
->GetBuffer());
98 long nBmpWidth
= aText
.GetSizePixel().Width();
99 long nBmpHeight
= aText
.GetSizePixel().Height();
100 sal_uInt8
* pBitmapBuf(new sal_uInt8
[3* nBmpWidth
* nBmpHeight
]);
101 memcpy(pBitmapBuf
, buf
, 3* nBmpWidth
* nBmpHeight
);
102 m_TextCache
.insert(std::make_pair(rText
, TextCacheItem(pBitmapBuf
, aText
.GetSizePixel())));
103 Bitmap::ReleaseAccess(pAcc
);
104 return m_TextCache
.find(rText
)->second
;
107 Text::Text(OpenGL3DRenderer
* pRenderer
, TextCache
& rTextCache
, const OUString
& rStr
, sal_uInt32 nId
):
108 Renderable3DObject(pRenderer
, nId
),
109 maText(rTextCache
.getText(rStr
))
115 glm::vec3 dir2
= maTopRight
- maTopLeft
;
116 glm::vec3 bottomLeft
= maBottomRight
- dir2
;
117 mpRenderer
->CreateTextTexture(maText
.maPixels
, maText
.maSize
,
118 maTopLeft
, maTopRight
, maBottomRight
, bottomLeft
,
122 void Text::setPosition(const glm::vec3
& rTopLeft
, const glm::vec3
& rTopRight
, const glm::vec3
& rBottomRight
)
124 maTopLeft
= rTopLeft
;
125 maTopRight
= rTopRight
;
126 maBottomRight
= rBottomRight
;
129 ScreenText::ScreenText(OpenGL3DRenderer
* pRenderer
, TextCache
& rTextCache
,
130 const OUString
& rStr
, const glm::vec4
& rColor
, sal_uInt32 nId
, bool bIs3dText
):
131 Renderable3DObject(pRenderer
, nId
),
132 maText(rTextCache
.getText(rStr
,bIs3dText
)),
137 void ScreenText::setPosition(const glm::vec2
& rTopLeft
, const glm::vec2
& rBottomRight
,
138 const glm::vec3
& r3DPos
)
140 maTopLeft
= rTopLeft
;
141 maBottomRight
= rBottomRight
;
145 void ScreenText::render()
147 mpRenderer
->CreateScreenTextTexture(maText
.maPixels
, maText
.maSize
,
148 maTopLeft
, maBottomRight
, ma3DPos
, maColor
,
152 Rectangle::Rectangle(OpenGL3DRenderer
* pRenderer
, sal_uInt32 nId
):
153 Renderable3DObject(pRenderer
, nId
)
157 void Rectangle::render()
159 glm::vec3 dir1
= maBottomRight
- maTopLeft
;
160 glm::vec3 dir2
= maTopRight
- maTopLeft
;
161 glm::vec3 normal
= glm::normalize(glm::cross(dir1
, dir2
));
162 mpRenderer
->AddShapePolygon3DObject(maColor
, false, Color(0), 1, 0xFFFFFF, mnUniqueId
);
163 glm::vec3 bottomLeft
= maBottomRight
- dir2
;
164 //set polygon points and normals
165 mpRenderer
->AddPolygon3DObjectPoint(maBottomRight
.x
, maBottomRight
.y
, maBottomRight
.z
);
166 mpRenderer
->AddPolygon3DObjectNormalPoint(normal
.x
, normal
.y
, normal
.z
);
167 mpRenderer
->AddPolygon3DObjectPoint(maTopRight
.x
, maTopRight
.y
, maTopRight
.z
);
168 mpRenderer
->AddPolygon3DObjectNormalPoint(normal
.x
, normal
.y
, normal
.z
);
169 mpRenderer
->AddPolygon3DObjectPoint(maTopLeft
.x
, maTopLeft
.y
, maTopLeft
.z
);
170 mpRenderer
->AddPolygon3DObjectNormalPoint(normal
.x
, normal
.y
, normal
.z
);
171 mpRenderer
->AddPolygon3DObjectPoint(bottomLeft
.x
, bottomLeft
.y
, bottomLeft
.z
);
172 mpRenderer
->AddPolygon3DObjectNormalPoint(normal
.x
, normal
.y
, normal
.z
);
173 mpRenderer
->EndAddPolygon3DObjectPoint();
174 mpRenderer
->EndAddPolygon3DObjectNormalPoint();
175 mpRenderer
->EndAddShapePolygon3DObject();
176 //we should render the edge if the edge color is different from the fill color
177 if (maColor
!= maLineColor
)
179 mpRenderer
->AddShapePolygon3DObject(Color(0), true, maLineColor
, 0, 0xFFFFFF, mnUniqueId
);
180 mpRenderer
->AddPolygon3DObjectPoint(maBottomRight
.x
, maBottomRight
.y
, maBottomRight
.z
);
181 mpRenderer
->AddPolygon3DObjectPoint(maTopRight
.x
, maTopRight
.y
, maTopRight
.z
);
182 mpRenderer
->AddPolygon3DObjectPoint(maTopLeft
.x
, maTopLeft
.y
, maTopLeft
.z
);
183 mpRenderer
->AddPolygon3DObjectPoint(bottomLeft
.x
, bottomLeft
.y
, bottomLeft
.z
);
184 mpRenderer
->EndAddPolygon3DObjectPoint();
185 mpRenderer
->EndAddShapePolygon3DObject();
189 void Rectangle::setPosition(const glm::vec3
& rTopLeft
, const glm::vec3
& rTopRight
, const glm::vec3
& rBottomRight
)
191 maTopLeft
= rTopLeft
;
192 maTopRight
= rTopRight
;
193 maBottomRight
= rBottomRight
;
196 void Rectangle::setFillColor(const Color
& rColor
)
201 void Rectangle::setLineColor(const Color
& rColor
)
203 maLineColor
= rColor
;
206 Camera::Camera(OpenGL3DRenderer
* pRenderer
):
207 Renderable3DObject(pRenderer
, 0),
210 maDirection(glm::vec3(0,0,0))
214 void Camera::render()
216 mpRenderer
->SetCameraInfo(maPos
, maDirection
, maUp
);
219 void Camera::setPosition(const glm::vec3
& rPos
)
224 void Camera::setDirection(const glm::vec3
& rDir
)
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */