Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Demos / OpenGL / RenderTexture.h
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans  http://continuousphysics.com/Bullet/
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose, 
8 including commercial applications, and to alter it and redistribute it freely, 
9 subject to the following restrictions:
10
11 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15
16 #ifndef RENDER_TEXTURE_H
17 #define RENDER_TEXTURE_H
18
19 #include "LinearMath/btVector3.h"
20 #include "GLDebugFont.h"
21
22 ///
23 ///renderTexture provides a software-render context (setpixel/printf)
24 ///
25 class renderTexture
26 {
27         int m_height;
28         int m_width;
29         unsigned char*  m_buffer;
30
31 public:
32
33         renderTexture(int width,int height);
34         ~renderTexture();
35
36         ///rgba input is in range [0..1] for each component
37         inline void     setPixel(int x,int y,const btVector4& rgba)
38         {
39                 unsigned char* pixel = &m_buffer[ (x+y*m_width) * 4];
40
41                 pixel[0] = (unsigned char)(255.*rgba.getX());
42                 pixel[1] = (unsigned char)(255.*rgba.getY());
43                 pixel[2] = (unsigned char)(255.*rgba.getZ());
44                 pixel[3] = (unsigned char)(255.*rgba.getW());
45         }
46
47         inline void     addPixel(int x,int y,const btVector4& rgba)
48         {
49                 unsigned char* pixel = &m_buffer[ (x+y*m_width) * 4];
50                 pixel[0] = (unsigned char)btMin(btScalar(255.f),((btScalar)pixel[0] + btScalar(255.f)*rgba.getX()));
51                 pixel[1] = (unsigned char)btMin(btScalar(255.f),((btScalar)pixel[1] + btScalar(255.f)*rgba.getY()));
52                 pixel[2] = (unsigned char)btMin(btScalar(255.f),((btScalar)pixel[2] + btScalar(255.f)*rgba.getZ()));
53 //              pixel[3] = (unsigned char)btMin(btScalar(255.f),((btScalar)pixel[3] + btScalar(255.f)*rgba.getW()));
54         }
55
56         inline btVector4 getPixel(int x,int y)
57         {
58                 unsigned char* pixel = &m_buffer[ (x+y*m_width) * 4];
59                 return btVector4(pixel[0]*1.f/255.f,
60                         pixel[1]*1.f/255.f,
61                         pixel[2]*1.f/255.f,
62                         pixel[3]*1.f/255.f);
63         }
64
65         const unsigned char*    getBuffer() const { return m_buffer;}
66         int     getWidth() const { return m_width;}
67         int     getHeight() const { return m_height;}
68         void grapicalPrintf(char* str,  void* fontData, int startx = 0,int starty=0);
69
70 };
71
72 #endif //RENDER_TEXTURE_H
73