1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the demonstration applications of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #ifndef GLBUFFERS_H
|
---|
43 | #define GLBUFFERS_H
|
---|
44 |
|
---|
45 | //#include <GL/glew.h>
|
---|
46 | #include "glextensions.h"
|
---|
47 |
|
---|
48 | #include <QtGui>
|
---|
49 | #include <QtOpenGL>
|
---|
50 |
|
---|
51 | #include "vector.h"
|
---|
52 |
|
---|
53 | #define BUFFER_OFFSET(i) ((char*)0 + (i))
|
---|
54 | #define SIZE_OF_MEMBER(cls, member) sizeof(static_cast<cls *>(0)->member)
|
---|
55 |
|
---|
56 | #define GLBUFFERS_ASSERT_OPENGL(prefix, assertion, returnStatement) \
|
---|
57 | if (m_failed || !(assertion)) { \
|
---|
58 | if (!m_failed) qCritical(prefix ": The necessary OpenGL functions are not available."); \
|
---|
59 | m_failed = true; \
|
---|
60 | returnStatement; \
|
---|
61 | }
|
---|
62 |
|
---|
63 | class GLTexture
|
---|
64 | {
|
---|
65 | public:
|
---|
66 | GLTexture();
|
---|
67 | virtual ~GLTexture();
|
---|
68 | virtual void bind() = 0;
|
---|
69 | virtual void unbind() = 0;
|
---|
70 | virtual bool failed() const {return m_failed;}
|
---|
71 | protected:
|
---|
72 | GLuint m_texture;
|
---|
73 | bool m_failed;
|
---|
74 | };
|
---|
75 |
|
---|
76 | class GLFrameBufferObject
|
---|
77 | {
|
---|
78 | public:
|
---|
79 | friend class GLRenderTargetCube;
|
---|
80 | // friend class GLRenderTarget2D;
|
---|
81 |
|
---|
82 | GLFrameBufferObject(int width, int height);
|
---|
83 | virtual ~GLFrameBufferObject();
|
---|
84 | bool isComplete();
|
---|
85 | virtual bool failed() const {return m_failed;}
|
---|
86 | protected:
|
---|
87 | void setAsRenderTarget(bool state = true);
|
---|
88 | GLuint m_fbo;
|
---|
89 | GLuint m_depthBuffer;
|
---|
90 | int m_width, m_height;
|
---|
91 | bool m_failed;
|
---|
92 | };
|
---|
93 |
|
---|
94 | class GLTexture2D : public GLTexture
|
---|
95 | {
|
---|
96 | public:
|
---|
97 | GLTexture2D(int width, int height);
|
---|
98 | GLTexture2D(const QString& fileName, int width = 0, int height = 0);
|
---|
99 | void load(int width, int height, QRgb *data);
|
---|
100 | virtual void bind();
|
---|
101 | virtual void unbind();
|
---|
102 | };
|
---|
103 |
|
---|
104 | class GLTexture3D : public GLTexture
|
---|
105 | {
|
---|
106 | public:
|
---|
107 | GLTexture3D(int width, int height, int depth);
|
---|
108 | // TODO: Implement function below
|
---|
109 | //GLTexture3D(const QString& fileName, int width = 0, int height = 0);
|
---|
110 | void load(int width, int height, int depth, QRgb *data);
|
---|
111 | virtual void bind();
|
---|
112 | virtual void unbind();
|
---|
113 | };
|
---|
114 |
|
---|
115 | class GLTextureCube : public GLTexture
|
---|
116 | {
|
---|
117 | public:
|
---|
118 | GLTextureCube(int size);
|
---|
119 | GLTextureCube(const QStringList& fileNames, int size = 0);
|
---|
120 | void load(int size, int face, QRgb *data);
|
---|
121 | virtual void bind();
|
---|
122 | virtual void unbind();
|
---|
123 | };
|
---|
124 |
|
---|
125 | // TODO: Define and implement class below
|
---|
126 | //class GLRenderTarget2D : public GLTexture2D
|
---|
127 |
|
---|
128 | class GLRenderTargetCube : public GLTextureCube
|
---|
129 | {
|
---|
130 | public:
|
---|
131 | GLRenderTargetCube(int size);
|
---|
132 | // begin rendering to one of the cube's faces. 0 <= face < 6
|
---|
133 | void begin(int face);
|
---|
134 | // end rendering
|
---|
135 | void end();
|
---|
136 | virtual bool failed() {return m_failed || m_fbo.failed();}
|
---|
137 |
|
---|
138 | static void getViewMatrix(gfx::Matrix4x4f& mat, int face);
|
---|
139 | static void getProjectionMatrix(gfx::Matrix4x4f& mat, float nearZ, float farZ);
|
---|
140 | private:
|
---|
141 | GLFrameBufferObject m_fbo;
|
---|
142 | };
|
---|
143 |
|
---|
144 | struct VertexDescription
|
---|
145 | {
|
---|
146 | enum
|
---|
147 | {
|
---|
148 | Null = 0, // Terminates a VertexDescription array
|
---|
149 | Position,
|
---|
150 | TexCoord,
|
---|
151 | Normal,
|
---|
152 | Color,
|
---|
153 | };
|
---|
154 | int field; // Position, TexCoord, Normal, Color
|
---|
155 | int type; // GL_FLOAT, GL_UNSIGNED_BYTE
|
---|
156 | int count; // number of elements
|
---|
157 | int offset; // field's offset into vertex struct
|
---|
158 | int index; // 0 (unused at the moment)
|
---|
159 | };
|
---|
160 |
|
---|
161 | // Implementation of interleaved buffers.
|
---|
162 | // 'T' is a struct which must include a null-terminated static array
|
---|
163 | // 'VertexDescription* description'.
|
---|
164 | // Example:
|
---|
165 | /*
|
---|
166 | struct Vertex
|
---|
167 | {
|
---|
168 | GLfloat position[3];
|
---|
169 | GLfloat texCoord[2];
|
---|
170 | GLfloat normal[3];
|
---|
171 | GLbyte color[4];
|
---|
172 | static VertexDescription description[];
|
---|
173 | };
|
---|
174 |
|
---|
175 | VertexDescription Vertex::description[] = {
|
---|
176 | {VertexDescription::Position, GL_FLOAT, SIZE_OF_MEMBER(Vertex, position) / sizeof(GLfloat), offsetof(Vertex, position), 0},
|
---|
177 | {VertexDescription::TexCoord, GL_FLOAT, SIZE_OF_MEMBER(Vertex, texCoord) / sizeof(GLfloat), offsetof(Vertex, texCoord), 0},
|
---|
178 | {VertexDescription::Normal, GL_FLOAT, SIZE_OF_MEMBER(Vertex, normal) / sizeof(GLfloat), offsetof(Vertex, normal), 0},
|
---|
179 | {VertexDescription::Color, GL_BYTE, SIZE_OF_MEMBER(Vertex, color) / sizeof(GLbyte), offsetof(Vertex, color), 0},
|
---|
180 | {VertexDescription::Null, 0, 0, 0, 0},
|
---|
181 | };
|
---|
182 | */
|
---|
183 | template<class T>
|
---|
184 | class GLVertexBuffer
|
---|
185 | {
|
---|
186 | public:
|
---|
187 | GLVertexBuffer(int length, const T *data = 0, int mode = GL_STATIC_DRAW)
|
---|
188 | : m_length(0)
|
---|
189 | , m_mode(mode)
|
---|
190 | , m_buffer(0)
|
---|
191 | , m_failed(false)
|
---|
192 | {
|
---|
193 | GLBUFFERS_ASSERT_OPENGL("GLVertexBuffer::GLVertexBuffer", glGenBuffers && glBindBuffer && glBufferData, return)
|
---|
194 |
|
---|
195 | glGenBuffers(1, &m_buffer);
|
---|
196 | glBindBuffer(GL_ARRAY_BUFFER, m_buffer);
|
---|
197 | glBufferData(GL_ARRAY_BUFFER, (m_length = length) * sizeof(T), data, mode);
|
---|
198 | }
|
---|
199 |
|
---|
200 | ~GLVertexBuffer()
|
---|
|
---|