summaryrefslogtreecommitdiffstats
path: root/demos/boxes
diff options
context:
space:
mode:
Diffstat (limited to 'demos/boxes')
-rw-r--r--demos/boxes/boxes.pro5
-rw-r--r--demos/boxes/glbuffers.cpp26
-rw-r--r--demos/boxes/glbuffers.h8
-rw-r--r--demos/boxes/glextensions.cpp37
-rw-r--r--demos/boxes/glextensions.h85
-rw-r--r--demos/boxes/glshaders.cpp266
-rw-r--r--demos/boxes/glshaders.h108
-rw-r--r--demos/boxes/qtbox.cpp33
-rw-r--r--demos/boxes/qtbox.h8
-rw-r--r--demos/boxes/roundedbox.cpp21
-rw-r--r--demos/boxes/roundedbox.h9
-rw-r--r--demos/boxes/scene.cpp152
-rw-r--r--demos/boxes/scene.h17
-rw-r--r--demos/boxes/trackball.cpp50
-rw-r--r--demos/boxes/trackball.h17
-rw-r--r--demos/boxes/vector.h602
16 files changed, 177 insertions, 1267 deletions
diff --git a/demos/boxes/boxes.pro b/demos/boxes/boxes.pro
index 6c1a33156c..33f1f1428e 100644
--- a/demos/boxes/boxes.pro
+++ b/demos/boxes/boxes.pro
@@ -11,17 +11,14 @@ INCLUDEPATH += .
HEADERS += 3rdparty/fbm.h \
glbuffers.h \
glextensions.h \
- glshaders.h \
gltrianglemesh.h \
qtbox.h \
roundedbox.h \
scene.h \
- trackball.h \
- vector.h
+ trackball.h
SOURCES += 3rdparty/fbm.c \
glbuffers.cpp \
glextensions.cpp \
- glshaders.cpp \
main.cpp \
qtbox.cpp \
roundedbox.cpp \
diff --git a/demos/boxes/glbuffers.cpp b/demos/boxes/glbuffers.cpp
index b2a594e97e..a25a425096 100644
--- a/demos/boxes/glbuffers.cpp
+++ b/demos/boxes/glbuffers.cpp
@@ -40,6 +40,7 @@
****************************************************************************/
#include "glbuffers.h"
+#include <QtGui/qmatrix4x4.h>
//============================================================================//
// GLTexture //
@@ -346,7 +347,7 @@ void GLRenderTargetCube::end()
m_fbo.setAsRenderTarget(false);
}
-void GLRenderTargetCube::getViewMatrix(gfx::Matrix4x4f& mat, int face)
+void GLRenderTargetCube::getViewMatrix(QMatrix4x4& mat, int face)
{
if (face < 0 || face >= 6) {
qWarning("GLRenderTargetCube::getViewMatrix: 'face' must be in the range [0, 6). (face == %d)", face);
@@ -371,20 +372,21 @@ void GLRenderTargetCube::getViewMatrix(gfx::Matrix4x4f& mat, int face)
{-1.0f, -1.0f, +1.0f},
};
- memset(mat.bits(), 0, sizeof(float) * 16);
+ memset(mat.data(), 0, sizeof(float) * 16);
for (int i = 0; i < 3; ++i)
- mat(perm[face][i], i) = signs[face][i];
+ mat(i, perm[face][i]) = signs[face][i];
mat(3, 3) = 1.0f;
}
-void GLRenderTargetCube::getProjectionMatrix(gfx::Matrix4x4f& mat, float nearZ, float farZ)
+void GLRenderTargetCube::getProjectionMatrix(QMatrix4x4& mat, float nearZ, float farZ)
{
- float proj[] = {
- 1.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 1.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, (nearZ+farZ)/(nearZ-farZ), -1.0f,
- 0.0f, 0.0f, 2.0f*nearZ*farZ/(nearZ-farZ), 0.0f,
- };
-
- memcpy(mat.bits(), proj, sizeof(float) * 16);
+ static const QMatrix4x4 reference(
+ 1.0f, 0.0f, 0.0f, 0.0f,
+ 0.0f, 1.0f, 0.0f, 0.0f,
+ 0.0f, 0.0f, 0.0f, 0.0f,
+ 0.0f, 0.0f, -1.0f, 0.0f);
+
+ mat = reference;
+ mat(2, 2) = (nearZ+farZ)/(nearZ-farZ);
+ mat(2, 3) = 2.0f*nearZ*farZ/(nearZ-farZ);
}
diff --git a/demos/boxes/glbuffers.h b/demos/boxes/glbuffers.h
index 88de4e82fa..392924e8bb 100644
--- a/demos/boxes/glbuffers.h
+++ b/demos/boxes/glbuffers.h
@@ -48,8 +48,6 @@
#include <QtGui>
#include <QtOpenGL>
-#include "vector.h"
-
#define BUFFER_OFFSET(i) ((char*)0 + (i))
#define SIZE_OF_MEMBER(cls, member) sizeof(static_cast<cls *>(0)->member)
@@ -60,6 +58,8 @@ if (m_failed || !(assertion)) {
returnStatement; \
}
+class QMatrix4x4;
+
class GLTexture
{
public:
@@ -135,8 +135,8 @@ public:
void end();
virtual bool failed() {return m_failed || m_fbo.failed();}
- static void getViewMatrix(gfx::Matrix4x4f& mat, int face);
- static void getProjectionMatrix(gfx::Matrix4x4f& mat, float nearZ, float farZ);
+ static void getViewMatrix(QMatrix4x4& mat, int face);
+ static void getProjectionMatrix(QMatrix4x4& mat, float nearZ, float farZ);
private:
GLFrameBufferObject m_fbo;
};
diff --git a/demos/boxes/glextensions.cpp b/demos/boxes/glextensions.cpp
index 59256a8494..5f168b676c 100644
--- a/demos/boxes/glextensions.cpp
+++ b/demos/boxes/glextensions.cpp
@@ -47,23 +47,6 @@ bool GLExtensionFunctions::resolve(const QGLContext *context)
{
bool ok = true;
- RESOLVE_GL_FUNC(CreateShaderObjectARB)
- RESOLVE_GL_FUNC(ShaderSourceARB)
- RESOLVE_GL_FUNC(CompileShaderARB)
- RESOLVE_GL_FUNC(GetObjectParameterivARB)
- RESOLVE_GL_FUNC(DeleteObjectARB)
- RESOLVE_GL_FUNC(GetInfoLogARB)
- RESOLVE_GL_FUNC(CreateProgramObjectARB)
- RESOLVE_GL_FUNC(AttachObjectARB)
- RESOLVE_GL_FUNC(DetachObjectARB)
- RESOLVE_GL_FUNC(LinkProgramARB)
- RESOLVE_GL_FUNC(UseProgramObjectARB)
- RESOLVE_GL_FUNC(GetUniformLocationARB)
- RESOLVE_GL_FUNC(Uniform1iARB)
- RESOLVE_GL_FUNC(Uniform1fARB)
- RESOLVE_GL_FUNC(Uniform4fARB)
- RESOLVE_GL_FUNC(UniformMatrix4fvARB)
-
RESOLVE_GL_FUNC(GenFramebuffersEXT)
RESOLVE_GL_FUNC(GenRenderbuffersEXT)
RESOLVE_GL_FUNC(BindRenderbufferEXT)
@@ -88,26 +71,6 @@ bool GLExtensionFunctions::resolve(const QGLContext *context)
return ok;
}
-bool GLExtensionFunctions::glslSupported() {
- return CreateShaderObjectARB
- && CreateShaderObjectARB
- && ShaderSourceARB
- && CompileShaderARB
- && GetObjectParameterivARB
- && DeleteObjectARB
- && GetInfoLogARB
- && CreateProgramObjectARB
- && AttachObjectARB
- && DetachObjectARB
- && LinkProgramARB
- && UseProgramObjectARB
- && GetUniformLocationARB
- && Uniform1iARB
- && Uniform1fARB
- && Uniform4fARB
- && UniformMatrix4fvARB;
-}
-
bool GLExtensionFunctions::fboSupported() {
return GenFramebuffersEXT
&& GenRenderbuffersEXT
diff --git a/demos/boxes/glextensions.h b/demos/boxes/glextensions.h
index 7ba3b32212..af36e2926f 100644
--- a/demos/boxes/glextensions.h
+++ b/demos/boxes/glextensions.h
@@ -47,23 +47,6 @@
/*
Functions resolved:
-glCreateShaderObjectARB
-glShaderSourceARB
-glCompileShaderARB
-glGetObjectParameterivARB
-glDeleteObjectARB
-glGetInfoLogARB
-glCreateProgramObjectARB
-glAttachObjectARB
-glDetachObjectARB
-glLinkProgramARB
-glUseProgramObjectARB
-glGetUniformLocationARB
-glUniform1iARB
-glUniform1fARB
-glUniform4fARB
-glUniformMatrix4fvARB
-
glGenFramebuffersEXT
glGenRenderbuffersEXT
glBindRenderbufferEXT
@@ -139,39 +122,6 @@ typedef ptrdiff_t GLsizeiptrARB;
#define GL_DEPTH_ATTACHMENT_EXT 0x8D00
#endif
-#ifndef GL_ARB_vertex_shader
-#define GL_VERTEX_SHADER_ARB 0x8B31
-#endif
-
-#ifndef GL_ARB_fragment_shader
-#define GL_FRAGMENT_SHADER_ARB 0x8B30
-#endif
-
-#ifndef GL_ARB_shader_objects
-typedef char GLcharARB;
-typedef unsigned int GLhandleARB;
-#define GL_OBJECT_COMPILE_STATUS_ARB 0x8B81
-#define GL_OBJECT_LINK_STATUS_ARB 0x8B82
-#define GL_OBJECT_INFO_LOG_LENGTH_ARB 0x8B84
-#endif
-
-typedef GLhandleARB (APIENTRY *_glCreateShaderObjectARB) (GLenum);
-typedef void (APIENTRY *_glShaderSourceARB) (GLhandleARB, GLuint, const GLcharARB**, GLint *);
-typedef void (APIENTRY *_glCompileShaderARB) (GLhandleARB);
-typedef void (APIENTRY *_glGetObjectParameterivARB) (GLhandleARB, GLenum, int *);
-typedef void (APIENTRY *_glDeleteObjectARB) (GLhandleARB);
-typedef void (APIENTRY *_glGetInfoLogARB) (GLhandleARB, GLsizei, GLsizei *, GLcharARB *);
-typedef GLhandleARB (APIENTRY *_glCreateProgramObjectARB) ();
-typedef void (APIENTRY *_glAttachObjectARB) (GLhandleARB, GLhandleARB);
-typedef void (APIENTRY *_glDetachObjectARB) (GLhandleARB, GLhandleARB);
-typedef void (APIENTRY *_glLinkProgramARB) (GLhandleARB);
-typedef void (APIENTRY *_glUseProgramObjectARB) (GLhandleARB);
-typedef GLint (APIENTRY *_glGetUniformLocationARB) (GLhandleARB, const GLcharARB *);
-typedef void (APIENTRY *_glUniform1iARB) (GLint, GLint);
-typedef void (APIENTRY *_glUniform1fARB) (GLint, GLfloat);
-typedef void (APIENTRY *_glUniform4fARB) (GLint, GLfloat, GLfloat, GLfloat, GLfloat);
-typedef void (APIENTRY *_glUniformMatrix4fvARB) (GLint, GLuint, GLboolean, const GLfloat *);
-
typedef void (APIENTRY *_glGenFramebuffersEXT) (GLsizei, GLuint *);
typedef void (APIENTRY *_glGenRenderbuffersEXT) (GLsizei, GLuint *);
typedef void (APIENTRY *_glBindRenderbufferEXT) (GLenum, GLuint);
@@ -197,27 +147,9 @@ struct GLExtensionFunctions
{
bool resolve(const QGLContext *context);
- bool glslSupported();
bool fboSupported();
bool openGL15Supported(); // the rest: multi-texture, 3D-texture, vertex buffer objects
- _glCreateShaderObjectARB CreateShaderObjectARB;
- _glShaderSourceARB ShaderSourceARB;
- _glCompileShaderARB CompileShaderARB;
- _glGetObjectParameterivARB GetObjectParameterivARB;
- _glDeleteObjectARB DeleteObjectARB;
- _glGetInfoLogARB GetInfoLogARB;
- _glCreateProgramObjectARB CreateProgramObjectARB;
- _glAttachObjectARB AttachObjectARB;
- _glDetachObjectARB DetachObjectARB;
- _glLinkProgramARB LinkProgramARB;
- _glUseProgramObjectARB UseProgramObjectARB;
- _glGetUniformLocationARB GetUniformLocationARB;
- _glUniform1iARB Uniform1iARB;
- _glUniform1fARB Uniform1fARB;
- _glUniform4fARB Uniform4fARB;
- _glUniformMatrix4fvARB UniformMatrix4fvARB;
-
_glGenFramebuffersEXT GenFramebuffersEXT;
_glGenRenderbuffersEXT GenRenderbuffersEXT;
_glBindRenderbufferEXT BindRenderbufferEXT;
@@ -246,23 +178,6 @@ inline GLExtensionFunctions &getGLExtensionFunctions()
return funcs;
}
-#define glCreateShaderObjectARB getGLExtensionFunctions().CreateShaderObjectARB
-#define glShaderSourceARB getGLExtensionFunctions().ShaderSourceARB
-#define glCompileShaderARB getGLExtensionFunctions().CompileShaderARB
-#define glGetObjectParameterivARB getGLExtensionFunctions().GetObjectParameterivARB
-#define glDeleteObjectARB getGLExtensionFunctions().DeleteObjectARB
-#define glGetInfoLogARB getGLExtensionFunctions().GetInfoLogARB
-#define glCreateProgramObjectARB getGLExtensionFunctions().CreateProgramObjectARB
-#define glAttachObjectARB getGLExtensionFunctions().AttachObjectARB
-#define glDetachObjectARB getGLExtensionFunctions().DetachObjectARB
-#define glLinkProgramARB getGLExtensionFunctions().LinkProgramARB
-#define glUseProgramObjectARB getGLExtensionFunctions().UseProgramObjectARB
-#define glGetUniformLocationARB getGLExtensionFunctions().GetUniformLocationARB
-#define glUniform1iARB getGLExtensionFunctions().Uniform1iARB
-#define glUniform1fARB getGLExtensionFunctions().Uniform1fARB
-#define glUniform4fARB getGLExtensionFunctions().Uniform4fARB
-#define glUniformMatrix4fvARB getGLExtensionFunctions().UniformMatrix4fvARB
-
#define glGenFramebuffersEXT getGLExtensionFunctions().GenFramebuffersEXT
#define glGenRenderbuffersEXT getGLExtensionFunctions().GenRenderbuffersEXT
#define glBindRenderbufferEXT getGLExtensionFunctions().BindRenderbufferEXT
diff --git a/demos/boxes/glshaders.cpp b/demos/boxes/glshaders.cpp
deleted file mode 100644
index b6999a8e0f..0000000000
--- a/demos/boxes/glshaders.cpp
+++ /dev/null
@@ -1,266 +0,0 @@
-/****************************************************************************
-**
-** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
-** Contact: Qt Software Information (qt-info@nokia.com)
-**
-** This file is part of the demonstration applications of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** No Commercial Usage
-** This file contains pre-release code and may not be distributed.
-** You may use this file in accordance with the terms and conditions
-** contained in the either Technology Preview License Agreement or the
-** Beta Release License Agreement.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 2.1 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 2.1 requirements
-** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
-**
-** In addition, as a special exception, Nokia gives you certain
-** additional rights. These rights are described in the Nokia Qt LGPL
-** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
-** package.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3.0 as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU General Public License version 3.0 requirements will be
-** met: http://www.gnu.org/copyleft/gpl.html.
-**
-** If you are unsure which license is appropriate for your use, please
-** contact the sales department at qt-sales@nokia.com.
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-#include "glshaders.h"
-
-#define GLSHADERS_ASSERT_OPENGL(prefix, assertion, returnStatement) \
-if (m_failed || !(assertion)) { \
- if (!m_failed) qCritical(prefix ": The necessary OpenGL functions are not available."); \
- m_failed = true; \
- returnStatement; \
-}
-
-
-GLShader::GLShader(const char *data, int size, GLenum shaderType)
-: m_compileError(false), m_failed(false)
-{
- GLSHADERS_ASSERT_OPENGL("GLShader::GLShader",
- glCreateShaderObjectARB && glShaderSourceARB && glCompileShaderARB && glGetObjectParameterivARB, return)
-
- m_shader = glCreateShaderObjectARB(shaderType);
-
- GLint glSize = size;
- glShaderSourceARB(m_shader, 1, &data, &glSize);
- glCompileShaderARB(m_shader);
- int status;
- glGetObjectParameterivARB(m_shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
- m_compileError = (status != 1);
-}
-
-GLShader::GLShader(const QString& fileName, GLenum shaderType)
- : m_compileError(false), m_failed(false)
-{
- GLSHADERS_ASSERT_OPENGL("GLShader::GLShader",
- glCreateShaderObjectARB && glShaderSourceARB && glCompileShaderARB && glGetObjectParameterivARB, return)
-
- m_shader = glCreateShaderObjectARB(shaderType);
-
- QFile file(fileName);
- if (file.open(QIODevice::ReadOnly)) {
- QByteArray bytes = file.readAll();
- GLint size = file.size();
- const char *p = bytes.data();
- file.close();
- glShaderSourceARB(m_shader, 1, &p, &size);
- glCompileShaderARB(m_shader);
- int status;
- glGetObjectParameterivARB(m_shader, GL_OBJECT_COMPILE_STATUS_ARB, &status);
- m_compileError = (status != 1);
- } else {
- m_compileError = true;
- }
-}
-
-GLShader::~GLShader()
-{
- GLSHADERS_ASSERT_OPENGL("GLShader::~GLShader", glDeleteObjectARB, return)
-
- glDeleteObjectARB(m_shader);
-}
-
-QString GLShader::log()
-{
- GLSHADERS_ASSERT_OPENGL("GLShader::log", glGetObjectParameterivARB
- && glGetInfoLogARB, return QLatin1String("GLSL not supported."))
-
- int length;
- glGetObjectParameterivARB(m_shader, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
- char *log = new char[length + 1];
- GLsizei glLength = length;
- glGetInfoLogARB(m_shader, glLength, &glLength, log);
- log[glLength] = '\0';
- QString result(log);
- delete log;
- return result;
-}
-
-GLVertexShader::GLVertexShader(const char *data, int size) : GLShader(data, size, GL_VERTEX_SHADER_ARB)
-{
-}
-
-GLVertexShader::GLVertexShader(const QString& fileName) : GLShader(fileName, GL_VERTEX_SHADER_ARB)
-{
-}
-
-GLFragmentShader::GLFragmentShader(const char *data, int size) : GLShader(data, size, GL_FRAGMENT_SHADER_ARB)
-{
-}
-
-GLFragmentShader::GLFragmentShader(const QString& fileName) : GLShader(fileName, GL_FRAGMENT_SHADER_ARB)
-{
-}
-
-GLProgram::GLProgram() : m_linked(false), m_linkError(false), m_failed(false)
-{
- GLSHADERS_ASSERT_OPENGL("GLProgram::GLProgram", glCreateProgramObjectARB, return)
-
- m_program = glCreateProgramObjectARB();
-}
-
-GLProgram::~GLProgram()
-{
- GLSHADERS_ASSERT_OPENGL("GLProgram::~GLProgram", glDeleteObjectARB, return)
-
- glDeleteObjectARB(m_program);
-}
-
-void GLProgram::attach(const GLShader &shader)
-{
- GLSHADERS_ASSERT_OPENGL("GLProgram::attach", glAttachObjectARB, return)
-
- glAttachObjectARB(m_program, shader.m_shader);
- m_linked = m_linkError = false;
-}
-
-void GLProgram::detach(const GLShader &shader)
-{
- GLSHADERS_ASSERT_OPENGL("GLProgram::detach", glDetachObjectARB, return)
-
- glDetachObjectARB(m_program, shader.m_shader);
- m_linked = m_linkError = false;
-}
-
-bool GLProgram::failed()
-{
- if (m_failed || m_linkError)
- return true;
-
- if (m_linked)
- return false;
-
- GLSHADERS_ASSERT_OPENGL("GLProgram::failed", glLinkProgramARB && glGetObjectParameterivARB, return true)
-
- glLinkProgramARB(m_program);
- int status;
- glGetObjectParameterivARB(m_program, GL_OBJECT_LINK_STATUS_ARB, &status);
- m_linkError = !(m_linked = (status == 1));
- return m_linkError;
-}
-
-QString GLProgram::log()
-{
- GLSHADERS_ASSERT_OPENGL("GLProgram::log", glGetObjectParameterivARB && glGetInfoLogARB,
- return QLatin1String("Failed."))
-
- int length;
- glGetObjectParameterivARB(m_program, GL_OBJECT_INFO_LOG_LENGTH_ARB, &length);
- char *log = new char[length + 1];
- GLsizei glLength = length;
- glGetInfoLogARB(m_program, glLength, &glLength, log);
- log[glLength] = '\0';
- QString result(log);
- delete log;
- return result;
-}
-
-void GLProgram::bind()
-{
- GLSHADERS_ASSERT_OPENGL("GLProgram::bind", glUseProgramObjectARB, return)
-
- if (!failed())
- glUseProgramObjectARB(m_program);
-}
-
-void GLProgram::unbind()
-{
- GLSHADERS_ASSERT_OPENGL("GLProgram::bind", glUseProgramObjectARB, return)
-
- glUseProgramObjectARB(0);
-}
-
-bool GLProgram::hasParameter(const QString& name)
-{
- GLSHADERS_ASSERT_OPENGL("GLProgram::hasParameter", glGetUniformLocationARB, return false)
-
- if (!failed()) {
- QByteArray asciiName = name.toAscii();
- return -1 != glGetUniformLocationARB(m_program, asciiName.data());
- }
- return false;
-}
-
-void GLProgram::setInt(const QString& name, int value)
-{
- GLSHADERS_ASSERT_OPENGL("GLProgram::setInt", glGetUniformLocationARB && glUniform1iARB, return)
-
- if (!failed()) {
- QByteArray asciiName = name.toAscii();
- int loc = glGetUniformLocationARB(m_program, asciiName.data());
- glUniform1iARB(loc, value);
- }
-}
-
-void GLProgram::setFloat(const QString& name, float value)
-{
- GLSHADERS_ASSERT_OPENGL("GLProgram::setFloat", glGetUniformLocationARB && glUniform1fARB, return)
-
- if (!failed()) {
- QByteArray asciiName = name.toAscii();
- int loc = glGetUniformLocationARB(m_program, asciiName.data());
- glUniform1fARB(loc, value);
- }
-}
-
-void GLProgram::setColor(const QString& name, QRgb value)
-{
- GLSHADERS_ASSERT_OPENGL("GLProgram::setColor", glGetUniformLocationARB && glUniform4fARB, return)
-
- //qDebug() << "Setting color" << name;
- if (!failed()) {
- QByteArray asciiName = name.toAscii();
- int loc = glGetUniformLocationARB(m_program, asciiName.data());
- //qDebug() << "Location of" << name << "is" << loc;
- QColor color(value);
- glUniform4fARB(loc, color.redF(), color.greenF(), color.blueF(), color.alphaF());
- }
-}
-
-void GLProgram::setMatrix(const QString& name, const gfx::Matrix4x4f &mat)
-{
- GLSHADERS_ASSERT_OPENGL("GLProgram::setMatrix", glGetUniformLocationARB && glUniformMatrix4fvARB, return)
-
- if (!failed()) {
- QByteArray asciiName = name.toAscii();
- int loc = glGetUniformLocationARB(m_program, asciiName.data());
- //qDebug() << "Location of" << name << "is" << loc;
- glUniformMatrix4fvARB(loc, 1, GL_FALSE, mat.bits());
- }
-} \ No newline at end of file
diff --git a/demos/boxes/glshaders.h b/demos/boxes/glshaders.h
deleted file mode 100644
index 2b6209a5ef..0000000000
--- a/demos/boxes/glshaders.h
+++ /dev/null
@@ -1,108 +0,0 @@
-/****************************************************************************
-**
-** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
-** Contact: Qt Software Information (qt-info@nokia.com)
-**
-** This file is part of the demonstration applications of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** No Commercial Usage
-** This file contains pre-release code and may not be distributed.
-** You may use this file in accordance with the terms and conditions
-** contained in the either Technology Preview License Agreement or the
-** Beta Release License Agreement.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 2.1 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 2.1 requirements
-** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
-**
-** In addition, as a special exception, Nokia gives you certain
-** additional rights. These rights are described in the Nokia Qt LGPL
-** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
-** package.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3.0 as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU General Public License version 3.0 requirements will be
-** met: http://www.gnu.org/copyleft/gpl.html.
-**
-** If you are unsure which license is appropriate for your use, please
-** contact the sales department at qt-sales@nokia.com.
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-#ifndef GLSHADERS_H
-#define GLSHADERS_H
-
-//#include <GL/glew.h>
-#include "glextensions.h"
-
-#include <QtGui>
-#include <QtOpenGL>
-
-#include "vector.h"
-
-class GLShader
-{
-public:
- friend class GLProgram;
- virtual ~GLShader();
- bool failed() const {return m_failed;}
- QString log();
-protected:
- GLShader(const char *data, int size, GLenum shaderType);
- GLShader(const QString& fileName, GLenum shaderType);
-
- GLhandleARB m_shader;
- bool m_compileError;
- bool m_failed;
-};
-
-class GLVertexShader : public GLShader
-{
-public:
- GLVertexShader(const char *data, int size);
- GLVertexShader(const QString& fileName);
-};
-
-class GLFragmentShader : public GLShader
-{
-public:
- GLFragmentShader(const char *data, int size);
- GLFragmentShader(const QString& fileName);
-};
-
-class GLProgram
-{
-public:
- GLProgram();
- ~GLProgram();
- void attach(const GLShader &shader);
- void detach(const GLShader &shader);
- void bind();
- void unbind();
- bool failed();
- QString log();
- bool hasParameter(const QString& name);
- // use program before setting values
- void setInt(const QString& name, int value);
- void setFloat(const QString& name, float value);
- void setColor(const QString& name, QRgb value);
- void setMatrix(const QString& name, const gfx::Matrix4x4f &mat);
- // TODO: add a bunch of set-functions for different types.
-private:
- GLhandleARB m_program;
- bool m_linked;
- bool m_linkError;
- bool m_failed;
-};
-
-#endif
diff --git a/demos/boxes/qtbox.cpp b/demos/boxes/qtbox.cpp
index 060769801f..ba9f95d00a 100644
--- a/demos/boxes/qtbox.cpp
+++ b/demos/boxes/qtbox.cpp
@@ -269,19 +269,20 @@ bool ItemBase::isInResizeArea(const QPointF &pos)
QtBox::QtBox(int size, int x, int y) : ItemBase(size, x, y), m_texture(0)
{
for (int i = 0; i < 8; ++i) {
- m_vertices[i][0] = (i & 1 ? 0.5f : -0.5f);
- m_vertices[i][1] = (i & 2 ? 0.5f : -0.5f);
- m_vertices[i][2] = (i & 4 ? 0.5f : -0.5f);
+ m_vertices[i].setX(i & 1 ? 0.5f : -0.5f);
+ m_vertices[i].setY(i & 2 ? 0.5f : -0.5f);
+ m_vertices[i].setZ(i & 4 ? 0.5f : -0.5f);
}
for (int i = 0; i < 4; ++i) {
- m_texCoords[i][0] = (i & 1 ? 1.0f : 0.0f);
- m_texCoords[i][1] = (i & 2 ? 1.0f : 0.0f);
- }
- memset(m_normals, 0, sizeof(m_normals));
- for (int i = 0; i < 3; ++i) {
- m_normals[2 * i + 0][i] = -1.0f;
- m_normals[2 * i + 1][i] = 1.0f;
+ m_texCoords[i].setX(i & 1 ? 1.0f : 0.0f);
+ m_texCoords[i].setY(i & 2 ? 1.0f : 0.0f);
}
+ m_normals[0] = QVector3D(-1.0f, 0.0f, 0.0f);
+ m_normals[1] = QVector3D(1.0f, 0.0f, 0.0f);
+ m_normals[2] = QVector3D(0.0f, -1.0f, 0.0f);
+ m_normals[3] = QVector3D(0.0f, 1.0f, 0.0f);
+ m_normals[4] = QVector3D(0.0f, 0.0f, -1.0f);
+ m_normals[5] = QVector3D(0.0f, 0.0f, 1.0f);
}
QtBox::~QtBox()
@@ -351,21 +352,21 @@ void QtBox::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWi
glColor4f(1.0f, 1.0f, 1.0f, 1.0);
glBegin(GL_TRIANGLE_STRIP);
- glNormal3fv(m_normals[2 * dir + 0].bits());
+ glNormal3fv(reinterpret_cast<float *>(&m_normals[2 * dir + 0]));
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
- glTexCoord2fv(m_texCoords[(j << 1) | i].bits());
- glVertex3fv(m_vertices[(i << ((dir + 2) % 3)) | (j << ((dir + 1) % 3))].bits());
+ glTexCoord2fv(reinterpret_cast<float *>(&m_texCoords[(j << 1) | i]));
+ glVertex3fv(reinterpret_cast<float *>(&m_vertices[(i << ((dir + 2) % 3)) | (j << ((dir + 1) % 3))]));
}
}
glEnd();
glBegin(GL_TRIANGLE_STRIP);
- glNormal3fv(m_normals[2 * dir + 1].bits());
+ glNormal3fv(reinterpret_cast<float *>(&m_normals[2 * dir + 1]));
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
- glTexCoord2fv(m_texCoords[(j << 1) | i].bits());
- glVertex3fv(m_vertices[(1 << dir) | (i << ((dir + 1) % 3)) | (j << ((dir + 2) % 3))].bits());
+ glTexCoord2fv(reinterpret_cast<float *>(&m_texCoords[(j << 1) | i]));
+ glVertex3fv(reinterpret_cast<float *>(&m_vertices[(1 << dir) | (i << ((dir + 1) % 3)) | (j << ((dir + 2) % 3))]));
}
}
glEnd();
diff --git a/demos/boxes/qtbox.h b/demos/boxes/qtbox.h
index aae8256811..5042077313 100644
--- a/demos/boxes/qtbox.h
+++ b/demos/boxes/qtbox.h
@@ -44,7 +44,7 @@
#include <QtGui>
-#include "vector.h"
+#include <QtGui/qvector3d.h>
#include "glbuffers.h"
class ItemBase : public QObject, public QGraphicsItem
@@ -85,9 +85,9 @@ public:
protected:
virtual ItemBase *createNew(int size, int x, int y);
private:
- gfx::Vector3f m_vertices[8];
- gfx::Vector2f m_texCoords[4];
- gfx::Vector3f m_normals[6];
+ QVector3D m_vertices[8];
+ QVector3D m_texCoords[4];
+ QVector3D m_normals[6];
GLTexture *m_texture;
};
diff --git a/demos/boxes/roundedbox.cpp b/demos/boxes/roundedbox.cpp
index f238da249f..93ebce79df 100644
--- a/demos/boxes/roundedbox.cpp
+++ b/demos/boxes/roundedbox.cpp
@@ -46,9 +46,10 @@
//============================================================================//
VertexDescription P3T2N3Vertex::description[] = {
- {VertexDescription::Position, GL_FLOAT, SIZE_OF_MEMBER(P3T2N3Vertex, position) / sizeof(float), offsetof(P3T2N3Vertex, position), 0},
- {VertexDescription::TexCoord, GL_FLOAT, SIZE_OF_MEMBER(P3T2N3Vertex, texCoord) / sizeof(float), offsetof(P3T2N3Vertex, texCoord), 0},
- {VertexDescription::Normal, GL_FLOAT, SIZE_OF_MEMBER(P3T2N3Vertex, normal) / sizeof(float), offsetof(P3T2N3Vertex, normal), 0},
+ {VertexDescription::Position, GL_FLOAT, SIZE_OF_MEMBER(P3T2N3Vertex, position) / sizeof(float), 0, 0},
+ {VertexDescription::TexCoord, GL_FLOAT, SIZE_OF_MEMBER(P3T2N3Vertex, texCoord) / sizeof(float), sizeof(QVector3D), 0},
+ {VertexDescription::Normal, GL_FLOAT, SIZE_OF_MEMBER(P3T2N3Vertex, normal) / sizeof(float), sizeof(QVector3D) + sizeof(QVector2D), 0},
+
{VertexDescription::Null, 0, 0, 0, 0},
};
@@ -78,10 +79,9 @@ GLRoundedBox::GLRoundedBox(float r, float scale, int n)
}
for (int corner = 0; corner < 8; ++corner) {
- gfx::Vector3f centre;
- centre[0] = (corner & 1 ? 1.0f : -1.0f);
- centre[1] = (corner & 2 ? 1.0f : -1.0f);
- centre[2] = (corner & 4 ? 1.0f : -1.0f);
+ QVector3D centre(corner & 1 ? 1.0f : -1.0f,
+ corner & 2 ? 1.0f : -1.0f,
+ corner & 4 ? 1.0f : -1.0f);
int winding = (corner & 1) ^ ((corner >> 1) & 1) ^ (corner >> 2);
int offsX = ((corner ^ 1) - corner) * vertexCountPerCorner;
int offsY = ((corner ^ 2) - corner) * vertexCountPerCorner;
@@ -129,12 +129,13 @@ GLRoundedBox::GLRoundedBox(float r, float scale, int n)
}
for (int j = 0; j <= i; ++j) {
- gfx::Vector3f normal = gfx::Vector3f::vector(i - j, j, n + 1 - i).normalized();
- gfx::Vector3f pos = centre * (0.5f - r + r * normal);
+ QVector3D normal = QVector3D(i - j, j, n + 1 - i).normalized();
+ QVector3D offset(0.5f - r, 0.5f - r, 0.5f - r);
+ QVector3D pos = centre * (offset + r * normal);
vp[vidx].position = scale * pos;
vp[vidx].normal = centre * normal;
- vp[vidx].texCoord = gfx::Vector2f::vector(pos[0], pos[1]) + 0.5f;
+ vp[vidx].texCoord = QVector2D(pos.x() + 0.5f, pos.y() + 0.5f);
// Corner polygons
if (i < n + 1) {
diff --git a/demos/boxes/roundedbox.h b/demos/boxes/roundedbox.h
index b934ade2e2..65b545ef8d 100644
--- a/demos/boxes/roundedbox.h
+++ b/demos/boxes/roundedbox.h
@@ -49,14 +49,15 @@
#include <QtOpenGL>
#include "gltrianglemesh.h"
-#include "vector.h"
+#include <QtGui/qvector3d.h>
+#include <QtGui/qvector2d.h>
#include "glbuffers.h"
struct P3T2N3Vertex
{
- gfx::Vector3f position;
- gfx::Vector2f texCoord;
- gfx::Vector3f normal;
+ QVector3D position;
+ QVector2D texCoord;
+ QVector3D normal;
static VertexDescription description[];
};
diff --git a/demos/boxes/scene.cpp b/demos/boxes/scene.cpp
index 1040e17675..e2aa16b511 100644
--- a/demos/boxes/scene.cpp
+++ b/demos/boxes/scene.cpp
@@ -39,7 +39,10 @@
**
****************************************************************************/
+#include <QDebug>
#include "scene.h"
+#include <QtGui/qmatrix4x4.h>
+#include <QtGui/qvector3d.h>
#include "3rdparty/fbm.h"
@@ -484,9 +487,9 @@ Scene::Scene(int width, int height, int maxTextureSize)
{
setSceneRect(0, 0, width, height);
- m_trackBalls[0] = TrackBall(0.0005f, gfx::Vector3f::vector(0, 1, 0), TrackBall::Sphere);
- m_trackBalls[1] = TrackBall(0.0001f, gfx::Vector3f::vector(0, 0, 1), TrackBall::Sphere);
- m_trackBalls[2] = TrackBall(0.0f, gfx::Vector3f::vector(0, 1, 0), TrackBall::Plane);
+ m_trackBalls[0] = TrackBall(0.05f, QVector3D(0, 1, 0), TrackBall::Sphere);
+ m_trackBalls[1] = TrackBall(0.005f, QVector3D(0, 0, 1), TrackBall::Sphere);
+ m_trackBalls[2] = TrackBall(0.0f, QVector3D(0, 1, 0), TrackBall::Plane);
m_renderOptions = new RenderOptionsDialog;
m_renderOptions->move(20, 120);
@@ -531,11 +534,11 @@ Scene::~Scene()
if (texture) delete texture;
if (m_mainCubemap)
delete m_mainCubemap;
- foreach (GLProgram *program, m_programs)
+ foreach (QGLShaderProgram *program, m_programs)
if (program) delete program;
if (m_vertexShader)
delete m_vertexShader;
- foreach (GLFragmentShader *shader, m_fragmentShaders)
+ foreach (QGLShader *shader, m_fragmentShaders)
if (shader) delete shader;
foreach (GLRenderTargetCube *rt, m_cubemaps)
if (rt) delete rt;
@@ -549,16 +552,18 @@ void Scene::initGL()
{
m_box = new GLRoundedBox(0.25f, 1.0f, 10);
- m_vertexShader = new GLVertexShader(":/res/boxes/basic.vsh");
+ m_vertexShader = new QGLShader(":/res/boxes/basic.vsh", QGLShader::VertexShader);
QStringList list;
list << ":/res/boxes/cubemap_posx.jpg" << ":/res/boxes/cubemap_negx.jpg" << ":/res/boxes/cubemap_posy.jpg"
<< ":/res/boxes/cubemap_negy.jpg" << ":/res/boxes/cubemap_posz.jpg" << ":/res/boxes/cubemap_negz.jpg";
m_environment = new GLTextureCube(list, qMin(1024, m_maxTextureSize));
- m_environmentShader = new GLFragmentShader(environmentShaderText, strlen(environmentShaderText));
- m_environmentProgram = new GLProgram;
- m_environmentProgram->attach(*m_vertexShader);
- m_environmentProgram->attach(*m_environmentShader);
+ m_environmentShader = new QGLShader(QGLShader::FragmentShader);
+ m_environmentShader->setSourceCode(environmentShaderText);
+ m_environmentProgram = new QGLShaderProgram;
+ m_environmentProgram->addShader(m_vertexShader);
+ m_environmentProgram->addShader(m_environmentShader);
+ m_environmentProgram->link();
const int NOISE_SIZE = 128; // for a different size, B and BM in fbm.c must also be changed
m_noise = new GLTexture3D(NOISE_SIZE, NOISE_SIZE, NOISE_SIZE);
@@ -610,12 +615,12 @@ void Scene::initGL()
filter = QStringList("*.fsh");
files = QDir(":/res/boxes/").entryInfoList(filter, QDir::Files | QDir::Readable);
foreach (QFileInfo file, files) {
- GLProgram *program = new GLProgram;
- GLFragmentShader* shader = new GLFragmentShader(file.absoluteFilePath());
+ QGLShaderProgram *program = new QGLShaderProgram;
+ QGLShader* shader = new QGLShader(file.absoluteFilePath(), QGLShader::FragmentShader);
// The program does not take ownership over the shaders, so store them in a vector so they can be deleted afterwards.
- program->attach(*m_vertexShader);
- program->attach(*shader);
- if (program->failed()) {
+ program->addShader(m_vertexShader);
+ program->addShader(shader);
+ if (!program->link()) {
qWarning("Failed to compile and link shader program");
qWarning("Vertex shader log:");
qWarning() << m_vertexShader->log();
@@ -633,22 +638,22 @@ void Scene::initGL()
m_programs << program;
m_renderOptions->addShader(file.baseName());
- program->bind();
- m_cubemaps << (program->hasParameter("env") ? new GLRenderTargetCube(qMin(256, m_maxTextureSize)) : 0);
- program->unbind();
+ program->enable();
+ m_cubemaps << ((program->uniformLocation("env") != -1) ? new GLRenderTargetCube(qMin(256, m_maxTextureSize)) : 0);
+ program->disable();
}
if (m_programs.size() == 0)
- m_programs << new GLProgram;
+ m_programs << new QGLShaderProgram;
m_renderOptions->emitParameterChanged();
}
// If one of the boxes should not be rendered, set excludeBox to its index.
// If the main box should not be rendered, set excludeBox to -1.
-void Scene::renderBoxes(const gfx::Matrix4x4f &view, int excludeBox)
+void Scene::renderBoxes(const QMatrix4x4 &view, int excludeBox)
{
- gfx::Matrix4x4f invView = view.inverse();
+ QMatrix4x4 invView = view.inverted();
// If multi-texturing is supported, use three saplers.
if (glActiveTexture) {
@@ -664,26 +669,26 @@ void Scene::renderBoxes(const gfx::Matrix4x4f &view, int excludeBox)
glDisable(GL_LIGHTING);
glDisable(GL_CULL_FACE);
- gfx::Matrix4x4f viewRotation(view);
+ QMatrix4x4 viewRotation(view);
viewRotation(3, 0) = viewRotation(3, 1) = viewRotation(3, 2) = 0.0f;
viewRotation(0, 3) = viewRotation(1, 3) = viewRotation(2, 3) = 0.0f;
viewRotation(3, 3) = 1.0f;
- glLoadMatrixf(viewRotation.bits());
+ glLoadMatrixf(viewRotation.data());
glScalef(20.0f, 20.0f, 20.0f);
// Don't render the environment if the environment texture can't be set for the correct sampler.
if (glActiveTexture) {
m_environment->bind();
- m_environmentProgram->bind();
- m_environmentProgram->setInt("tex", 0);
- m_environmentProgram->setInt("env", 1);
- m_environmentProgram->setInt("noise", 2);
+ m_environmentProgram->enable();
+ m_environmentProgram->setUniformValue("tex", GLint(0));
+ m_environmentProgram->setUniformValue("env", GLint(1));
+ m_environmentProgram->setUniformValue("noise", GLint(2));
m_box->draw();
- m_environmentProgram->unbind();
+ m_environmentProgram->disable();
m_environment->unbind();
}
- glLoadMatrixf(view.bits());
+ glLoadMatrixf(view.data());
glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
@@ -693,9 +698,11 @@ void Scene::renderBoxes(const gfx::Matrix4x4f &view, int excludeBox)
continue;
glPushMatrix();
- gfx::Matrix4x4f m;
- m_trackBalls[1].rotation().matrix(m);
- glMultMatrixf(m.bits());
+ QMatrix4x4 m;
+ m.rotate(m_trackBalls[1].rotation());
+ m = m.transposed();
+
+ glMultMatrixf(m.data());
glRotatef(360.0f * i / m_programs.size(), 0.0f, 0.0f, 1.0f);
glTranslatef(2.0f, 0.0f, 0.0f);
@@ -707,14 +714,14 @@ void Scene::renderBoxes(const gfx::Matrix4x4f &view, int excludeBox)
else
m_environment->bind();
}
- m_programs[i]->bind();
- m_programs[i]->setInt("tex", 0);
- m_programs[i]->setInt("env", 1);
- m_programs[i]->setInt("noise", 2);
- m_programs[i]->setMatrix("view", view);
- m_programs[i]->setMatrix("invView", invView);
+ m_programs[i]->enable();
+ m_programs[i]->setUniformValue("tex", GLint(0));
+ m_programs[i]->setUniformValue("env", GLint(1));
+ m_programs[i]->setUniformValue("noise", GLint(2));
+ m_programs[i]->setUniformValue("view", view);
+ m_programs[i]->setUniformValue("invView", invView);
m_box->draw();
- m_programs[i]->unbind();
+ m_programs[i]->disable();
if (glActiveTexture) {
if (m_dynamicCubemap && m_cubemaps[i])
@@ -726,9 +733,10 @@ void Scene::renderBoxes(const gfx::Matrix4x4f &view, int excludeBox)
}
if (-1 != excludeBox) {
- gfx::Matrix4x4f m;
- m_trackBalls[0].rotation().matrix(m);
- glMultMatrixf(m.bits());
+ QMatrix4x4 m;
+ m.rotate(m_trackBalls[0].rotation());
+ m = m.transposed();
+ glMultMatrixf(m.data());
if (glActiveTexture) {
if (m_dynamicCubemap)
@@ -737,14 +745,14 @@ void Scene::renderBoxes(const gfx::Matrix4x4f &view, int excludeBox)
m_environment->bind();
}
- m_programs[m_currentShader]->bind();
- m_programs[m_currentShader]->setInt("tex", 0);
- m_programs[m_currentShader]->setInt("env", 1);
- m_programs[m_currentShader]->setInt("noise", 2);
- m_programs[m_currentShader]->setMatrix("view", view);
- m_programs[m_currentShader]->setMatrix("invView", invView);
+ m_programs[m_currentShader]->enable();
+ m_programs[m_currentShader]->setUniformValue("tex", GLint(0));
+ m_programs[m_currentShader]->setUniformValue("env", GLint(1));
+ m_programs[m_currentShader]->setUniformValue("noise", GLint(2));
+ m_programs[m_currentShader]->setUniformValue("view", view);
+ m_programs[m_currentShader]->setUniformValue("invView", invView);
m_box->draw();
- m_programs[m_currentShader]->unbind();
+ m_programs[m_currentShader]->disable();
if (glActiveTexture) {
if (m_dynamicCubemap)
@@ -829,31 +837,32 @@ void Scene::renderCubemaps()
// To speed things up, only update the cubemaps for the small cubes every N frames.
const int N = (m_updateAllCubemaps ? 1 : 3);
- gfx::Matrix4x4f mat;
+ QMatrix4x4 mat;
GLRenderTargetCube::getProjectionMatrix(mat, 0.1f, 100.0f);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
- glLoadMatrixf(mat.bits());
+ glLoadMatrixf(mat.data());
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
- gfx::Vector3f center;
+ QVector3D center;
for (int i = m_frame % N; i < m_cubemaps.size(); i += N) {
if (0 == m_cubemaps[i])
continue;
float angle = 2.0f * PI * i / m_cubemaps.size();
- center = m_trackBalls[1].rotation().transform(gfx::Vector3f::vector(cos(angle), sin(angle), 0));
+
+ center = m_trackBalls[1].rotation().rotateVector(QVector3D(cos(angle), sin(angle), 0.0f));
for (int face = 0; face < 6; ++face) {
m_cubemaps[i]->begin(face);
GLRenderTargetCube::getViewMatrix(mat, face);
- gfx::Vector4f v = gfx::Vector4f::vector(-center[0], -center[1], -center[2], 1.0);
- mat[3] = v * mat;
+ QVector4D v = QVector4D(-center.x(), -center.y(), -center.z(), 1.0);
+ mat.setColumn(3, v * mat);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
renderBoxes(mat, i);
@@ -897,12 +906,9 @@ void Scene::drawBackground(QPainter *painter, const QRectF &)
glMatrixMode(GL_MODELVIEW);
- //gfx::Matrix4x4f view = gfx::Matrix4x4f::identity();
- //view(3, 2) -= 2.0f * exp(m_distExp / 1200.0f);
-
- gfx::Matrix4x4f view;
- m_trackBalls[2].rotation().matrix(view);
- view(3, 2) -= 2.0f * exp(m_distExp / 1200.0f);
+ QMatrix4x4 view;
+ view.rotate(m_trackBalls[2].rotation());
+ view(2, 3) -= 2.0f * exp(m_distExp / 1200.0f);
renderBoxes(view);
defaultStates();
@@ -936,10 +942,10 @@ void Scene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
}
if (event->buttons() & Qt::MidButton) {
- m_trackBalls[2].move(pixelPosToViewPos(event->scenePos()), gfx::Quaternionf::identity());
+ m_trackBalls[2].move(pixelPosToViewPos(event->scenePos()), QQuaternion());
event->accept();
} else {
- m_trackBalls[2].release(pixelPosToViewPos(event->scenePos()), gfx::Quaternionf::identity());
+ m_trackBalls[2].release(pixelPosToViewPos(event->scenePos()), QQuaternion());
}
}
@@ -960,7 +966,7 @@ void Scene::mousePressEvent(QGraphicsSceneMouseEvent *event)
}
if (event->buttons() & Qt::MidButton) {
- m_trackBalls[2].push(pixelPosToViewPos(event->scenePos()), gfx::Quaternionf::identity());
+ m_trackBalls[2].push(pixelPosToViewPos(event->scenePos()), QQuaternion());
event->accept();
}
}
@@ -982,7 +988,7 @@ void Scene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
}
if (event->button() == Qt::MidButton) {
- m_trackBalls[2].release(pixelPosToViewPos(event->scenePos()), gfx::Quaternionf::identity());
+ m_trackBalls[2].release(pixelPosToViewPos(event->scenePos()), QQuaternion());
event->accept();
}
}
@@ -1021,20 +1027,20 @@ void Scene::toggleDynamicCubemap(int state)
void Scene::setColorParameter(const QString &name, QRgb color)
{
// set the color in all programs
- foreach (GLProgram *program, m_programs) {
- program->bind();
- program->setColor(name, color);
- program->unbind();
+ foreach (QGLShaderProgram *program, m_programs) {
+ program->enable();
+ program->setUniformValue(program->uniformLocation(name), QColor(color));
+ program->disable();
}
}
void Scene::setFloatParameter(const QString &name, float value)
{
// set the color in all programs
- foreach (GLProgram *program, m_programs) {
- program->bind();
- program->setFloat(name, value);
- program->unbind();
+ foreach (QGLShaderProgram *program, m_programs) {
+ program->enable();
+ program->setUniformValue(program->uniformLocation(name), value);
+ program->disable();
}
}
diff --git a/demos/boxes/scene.h b/demos/boxes/scene.h
index 2db9317042..48ecaba52c 100644
--- a/demos/boxes/scene.h
+++ b/demos/boxes/scene.h
@@ -50,14 +50,13 @@
#include "roundedbox.h"
#include "gltrianglemesh.h"
-#include "vector.h"
#include "trackball.h"
#include "glbuffers.h"
-#include "glshaders.h"
#include "qtbox.h"
#define PI 3.14159265358979
+class QMatrix4x4;
class ParameterEdit : public QWidget
{
public:
@@ -195,7 +194,7 @@ public slots:
void setFloatParameter(const QString &name, float value);
void newItem(ItemDialog::ItemType type);
protected:
- void renderBoxes(const gfx::Matrix4x4f &view, int excludeBox = -2);
+ void renderBoxes(const QMatrix4x4 &view, int excludeBox = -2);
void setStates();
void setLights();
void defaultStates();
@@ -231,13 +230,11 @@ private:
GLTexture3D *m_noise;
GLRenderTargetCube *m_mainCubemap;
QVector<GLRenderTargetCube *> m_cubemaps;
- QVector<GLProgram *> m_programs;
- GLVertexShader *m_vertexShader;
- QVector<GLFragmentShader *> m_fragmentShaders;
- GLFragmentShader *m_environmentShader;
- GLProgram *m_environmentProgram;
+ QVector<QGLShaderProgram *> m_programs;
+ QGLShader *m_vertexShader;
+ QVector<QGLShader *> m_fragmentShaders;
+ QGLShader *m_environmentShader;
+ QGLShaderProgram *m_environmentProgram;
};
-
-
#endif
diff --git a/demos/boxes/trackball.cpp b/demos/boxes/trackball.cpp
index 980f6ed6d1..a8bfe44271 100644
--- a/demos/boxes/trackball.cpp
+++ b/demos/boxes/trackball.cpp
@@ -40,6 +40,7 @@
****************************************************************************/
#include "trackball.h"
+#include "scene.h"
//============================================================================//
// TrackBall //
@@ -51,23 +52,23 @@ TrackBall::TrackBall(TrackMode mode)
, m_pressed(false)
, m_mode(mode)
{
- m_axis = gfx::Vector3f::vector(0, 1, 0);
- m_rotation = gfx::Quaternionf::quaternion(1.0f, 0.0f, 0.0f, 0.0f);
+ m_axis = QVector3D(0, 1, 0);
+ m_rotation = QQuaternion();
m_lastTime = QTime::currentTime();
}
-TrackBall::TrackBall(float angularVelocity, const gfx::Vector3f& axis, TrackMode mode)
+TrackBall::TrackBall(float angularVelocity, const QVector3D& axis, TrackMode mode)
: m_axis(axis)
, m_angularVelocity(angularVelocity)
, m_paused(false)
, m_pressed(false)
, m_mode(mode)
{
- m_rotation = gfx::Quaternionf::quaternion(1.0f, 0.0f, 0.0f, 0.0f);
+ m_rotation = QQuaternion();
m_lastTime = QTime::currentTime();
}
-void TrackBall::push(const QPointF& p, const gfx::Quaternionf &)
+void TrackBall::push(const QPointF& p, const QQuaternion &)
{
m_rotation = rotation();
m_pressed = true;
@@ -76,7 +77,7 @@ void TrackBall::push(const QPointF& p, const gfx::Quaternionf &)
m_angularVelocity = 0.0f;
}
-void TrackBall::move(const QPointF& p, const gfx::Quaternionf &transformation)
+void TrackBall::move(const QPointF& p, const QQuaternion &transformation)
{
if (!m_pressed)
return;
@@ -90,44 +91,45 @@ void TrackBall::move(const QPointF& p, const gfx::Quaternionf &transformation)
case Plane:
{
QLineF delta(m_lastPos, p);
- m_angularVelocity = delta.length() / msecs;
- m_axis = gfx::Vector3f::vector(delta.dy(), -delta.dx(), 0.0f).normalized();
- m_axis = transformation.transform(m_axis);
- m_rotation *= gfx::Quaternionf::rotation(delta.length(), m_axis);
+ m_angularVelocity = 180*delta.length() / (PI*msecs);
+ m_axis = QVector3D(delta.dy(), -delta.dx(), 0.0f).normalized();
+ m_axis = transformation.rotateVector(m_axis);
+ m_rotation *= QQuaternion::fromAxisAndAngle(m_axis, delta.length());
}
break;
case Sphere:
{
- gfx::Vector3f lastPos3D = gfx::Vector3f::vector(m_lastPos.x(), m_lastPos.y(), 0);
- float sqrZ = 1 - lastPos3D.sqrNorm();
+ QVector3D lastPos3D = QVector3D(m_lastPos.x(), m_lastPos.y(), 0.0f);
+ float sqrZ = 1 - QVector3D::dotProduct(lastPos3D, lastPos3D);
if (sqrZ > 0)
- lastPos3D[2] = sqrt(sqrZ);
+ lastPos3D.setZ(sqrt(sqrZ));
else
lastPos3D.normalize();
- gfx::Vector3f currentPos3D = gfx::Vector3f::vector(p.x(), p.y(), 0);
- sqrZ = 1 - currentPos3D.sqrNorm();
+ QVector3D currentPos3D = QVector3D(p.x(), p.y(), 0.0f);
+ sqrZ = 1 - QVector3D::dotProduct(currentPos3D, currentPos3D);
if (sqrZ > 0)
- currentPos3D[2] = sqrt(sqrZ);
+ currentPos3D.setZ(sqrt(sqrZ));
else
currentPos3D.normalize();
- m_axis = gfx::Vector3f::cross(currentPos3D, lastPos3D);
- float angle = asin(sqrt(m_axis.sqrNorm()));
+ m_axis = QVector3D::crossProduct(currentPos3D, lastPos3D);
+ float angle = asin(sqrt(QVector3D::dotProduct(m_axis, m_axis)));
- m_angularVelocity = angle / msecs;
+ m_angularVelocity = 180*angle / (PI*msecs);
m_axis.normalize();
- m_axis = transformation.transform(m_axis);
- m_rotation *= gfx::Quaternionf::rotation(angle, m_axis);
+ m_axis = transformation.rotateVector(m_axis);
+ m_rotation *= QQuaternion::fromAxisAndAngle(m_axis, angle);
}
break;
}
+
m_lastPos = p;
m_lastTime = currentTime;
}
-void TrackBall::release(const QPointF& p, const gfx::Quaternionf &transformation)
+void TrackBall::release(const QPointF& p, const QQuaternion &transformation)
{
// Calling move() caused the rotation to stop if the framerate was too low.
move(p, transformation);
@@ -146,13 +148,13 @@ void TrackBall::stop()
m_paused = true;
}
-gfx::Quaternionf TrackBall::rotation() const
+QQuaternion TrackBall::rotation() const
{
if (m_paused || m_pressed)
return m_rotation;
QTime currentTime = QTime::currentTime();
float angle = m_angularVelocity * m_lastTime.msecsTo(currentTime);
- return m_rotation * gfx::Quaternionf::rotation(angle, m_axis);
+ return m_rotation * QQuaternion::fromAxisAndAngle(m_axis, angle);
}
diff --git a/demos/boxes/trackball.h b/demos/boxes/trackball.h
index 5e3f40cb49..66e9b68257 100644
--- a/demos/boxes/trackball.h
+++ b/demos/boxes/trackball.h
@@ -44,7 +44,8 @@
#include <QtGui>
-#include "vector.h"
+#include <QtGui/qvector3d.h>
+#include <QtGui/qquaternion.h>
class TrackBall
{
@@ -55,17 +56,17 @@ public:
Sphere,
};
TrackBall(TrackMode mode = Sphere);
- TrackBall(float angularVelocity, const gfx::Vector3f& axis, TrackMode mode = Sphere);
+ TrackBall(float angularVelocity, const QVector3D& axis, TrackMode mode = Sphere);
// coordinates in [-1,1]x[-1,1]
- void push(const QPointF& p, const gfx::Quaternionf &transformation);
- void move(const QPointF& p, const gfx::Quaternionf &transformation);
- void release(const QPointF& p, const gfx::Quaternionf &transformation);
+ void push(const QPointF& p, const QQuaternion &transformation);
+ void move(const QPointF& p, const QQuaternion &transformation);
+ void release(const QPointF& p, const QQuaternion &transformation);
void start(); // starts clock
void stop(); // stops clock
- gfx::Quaternionf rotation() const;
+ QQuaternion rotation() const;
private:
- gfx::Quaternionf m_rotation;
- gfx::Vector3f m_axis;
+ QQuaternion m_rotation;
+ QVector3D m_axis;
float m_angularVelocity;
QPointF m_lastPos;
diff --git a/demos/boxes/vector.h b/demos/boxes/vector.h
deleted file mode 100644
index bb24531158..0000000000
--- a/demos/boxes/vector.h
+++ /dev/null
@@ -1,602 +0,0 @@
-/****************************************************************************
-**
-** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
-** Contact: Qt Software Information (qt-info@nokia.com)
-**
-** This file is part of the demonstration applications of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** No Commercial Usage
-** This file contains pre-release code and may not be distributed.
-** You may use this file in accordance with the terms and conditions
-** contained in the either Technology Preview License Agreement or the
-** Beta Release License Agreement.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 2.1 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 2.1 requirements
-** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
-**
-** In addition, as a special exception, Nokia gives you certain
-** additional rights. These rights are described in the Nokia Qt LGPL
-** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
-** package.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 3.0 as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU General Public License version 3.0 requirements will be
-** met: http://www.gnu.org/copyleft/gpl.html.
-**
-** If you are unsure which license is appropriate for your use, please
-** contact the sales department at qt-sales@nokia.com.
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-#ifndef VECTOR_H
-#define VECTOR_H
-
-#include <cassert>
-#include <cmath>
-#include <iostream>
-
-namespace gfx
-{
-
-template<class T, int n>
-struct Vector
-{
- // Keep the Vector struct a plain old data (POD) struct by avoiding constructors
-
- static Vector vector(T x)
- {
- Vector result;
- for (int i = 0; i < n; ++i)
- result.v[i] = x;
- return result;
- }
-
- // Use only for 2D vectors
- static Vector vector(T x, T y)
- {
- assert(n == 2);
- Vector result;
- result.v[0] = x;
- result.v[1] = y;
- return result;
- }
-
- // Use only for 3D vectors
- static Vector vector(T x, T y, T z)
- {
- assert(n == 3);
- Vector result;
- result.v[0] = x;
- result.v[1] = y;
- result.v[2] = z;
- return result;
- }
-
- // Use only for 4D vectors
- static Vector vector(T x, T y, T z, T w)
- {
- assert(n == 4);
- Vector result;
- result.v[0] = x;
- result.v[1] = y;
- result.v[2] = z;
- result.v[3] = w;
- return result;
- }
-
- // Pass 'n' arguments to this function.
- static Vector vector(T *v)
- {
- Vector result;
- for (int i = 0; i < n; ++i)
- result.v[i] = v[i];
- return result;
- }
-
- T &operator [] (int i) {return v[i];}
- T operator [] (int i) const {return v[i];}
-
-#define VECTOR_BINARY_OP(op, arg, rhs) \
- Vector operator op (arg) const \
- { \
- Vector result; \
- for (int i = 0; i < n; ++i) \
- result.v[i] = v[i] op rhs; \
- return result; \
- }
-
- VECTOR_BINARY_OP(+, const Vector &u, u.v[i])
- VECTOR_BINARY_OP(-, const Vector &u, u.v[i])
- VECTOR_BINARY_OP(*, const Vector &u, u.v[i])
- VECTOR_BINARY_OP(/, const Vector &u, u.v[i])
- VECTOR_BINARY_OP(+, T s, s)
- VECTOR_BINARY_OP(-, T s, s)
- VECTOR_BINARY_OP(*, T s, s)
- VECTOR_BINARY_OP(/, T s, s)
-#undef VECTOR_BINARY_OP
-
- Vector operator - () const
- {
- Vector result;
- for (int i = 0; i < n; ++i)
- result.v[i] = -v[i];
- return result;
- }
-
-#define VECTOR_ASSIGN_OP(op, arg, rhs) \
- Vector &operator op (arg) \
- { \
- for (int i = 0; i < n; ++i) \
- v[i] op rhs; \
- return *this; \
- }
-
- VECTOR_ASSIGN_OP(+=, const Vector &u, u.v[i])
- VECTOR_ASSIGN_OP(-=, const Vector &u, u.v[i])
- VECTOR_ASSIGN_OP(=, T s, s)
- VECTOR_ASSIGN_OP(*=, T s, s)
- VECTOR_ASSIGN_OP(/=, T s, s)
-#undef VECTOR_ASSIGN_OP
-
- static T dot(const Vector &u, const Vector &v)
- {
- T sum(0);
- for (int i = 0; i < n; ++i)
- sum += u.v[i] * v.v[i];
- return sum;
- }
-
- static Vector cross(const Vector &u, const Vector &v)
- {
- assert(n == 3);
- return vector(u.v[1] * v.v[2] - u.v[2] * v.v[1],
- u.v[2] * v.v[0] - u.v[0] * v.v[2],
- u.v[0] * v.v[1] - u.v[1] * v.v[0]);
- }
-
- T sqrNorm() const
- {
- return dot(*this, *this);
- }
-
- // requires floating point type T
- void normalize()
- {
- T s = sqrNorm();
- if (s != 0)
- *this /= sqrt(s);
- }
-
- // requires floating point type T
- Vector normalized() const
- {
- T s = sqrNorm();
- if (s == 0)
- return *this;
- return *this / sqrt(s);
- }
-
- T *bits() {return v;}
- const T *bits() const {return v;}
-
- T v[n];
-};
-
-#define SCALAR_VECTOR_BINARY_OP(op) \
-template<class T, int n> \
-Vector<T, n> operator op (T s, const Vector<T, n>& u) \
-{ \
- Vector<T, n> result; \
- for (int i = 0; i < n; ++i) \
- result[i] = s op u[i]; \
- return result; \
-}
-
-SCALAR_VECTOR_BINARY_OP(+)
-SCALAR_VECTOR_BINARY_OP(-)
-SCALAR_VECTOR_BINARY_OP(*)
-SCALAR_VECTOR_BINARY_OP(/)
-#undef SCALAR_VECTOR_BINARY_OP
-
-template<class T, int n>
-std::ostream &operator << (std::ostream &os, const Vector<T, n> &v)
-{
- assert(n > 0);
- os << "[" << v[0];
- for (int i = 1; i < n; ++i)
- os << ", " << v[i];
- os << "]";
- return os;
-}
-
-typedef Vector<float, 2> Vector2f;
-typedef Vector<float, 3> Vector3f;
-typedef Vector<float, 4> Vector4f;
-
-template<class T, int rows, int cols>
-struct Matrix
-{
- // Keep the Matrix struct a plain old data (POD) struct by avoiding constructors
-
- static Matrix matrix(T x)
- {
- Matrix result;
- for (int i = 0; i < rows; ++i) {
- for (int j = 0; j < cols; ++j)
- result.v[i][j] = x;
- }
- return result;
- }
-
- static Matrix matrix(T *m)
- {
- Matrix result;
- for (int i = 0; i < rows; ++i) {
- for (int j = 0; j < cols; ++j) {
- result.v[i][j] = *m;
- ++m;
- }
- }
- return result;
- }
-
- T &operator () (int i, int j) {return v[i][j];}
- T operator () (int i, int j) const {return v[i][j];}
- Vector<T, cols> &operator [] (int i) {return v[i];}
- const Vector<T, cols> &operator [] (int i) const {return v[i];}
-
- // TODO: operators, methods
-
- Vector<T, rows> operator * (const Vector<T, cols> &u) const
- {
- Vector<T, rows> result;
- for (int i = 0; i < rows; ++i)
- result[i] = Vector<T, cols>::dot(v[i], u);
- return result;
- }
-
- template<int k>
- Matrix<T, rows, k> operator * (const Matrix<T, cols, k> &m)
- {
- Matrix<T, rows, k> result;
- for (int i = 0; i < rows; ++i)
- result[i] = v[i] * m;
- return result;
- }
-
- T* bits() {return reinterpret_cast<T *>(this);}
- const T* bits() const {return reinterpret_cast<const T *>(this);}
-
- // Simple Gauss elimination.
- // TODO: Optimize and improve stability.
- Matrix inverse(bool *ok = 0) const
- {
- assert(rows == cols);
- Matrix rhs = identity();
- Matrix lhs(*this);
- T temp;
- // Down
- for (int i = 0; i < rows; ++i) {
- // Pivoting
- int pivot = i;
- for (int j = i; j < rows; ++j) {
- if (qAbs(lhs(j, i)) > lhs(pivot, i))
- pivot = j;
- }
- // TODO: fuzzy compare.
- if (lhs(pivot, i) == T(0)) {
- if (ok)
- *ok = false;
- return rhs;
- }
- if (pivot != i) {
- for (int j = i; j < cols; ++j) {
- temp = lhs(pivot, j);
- lhs(pivot, j) = lhs(i, j);
- lhs(i, j) = temp;
- }
- for (int j = 0; j < cols; ++j) {
- temp = rhs(pivot, j);
- rhs(pivot, j) = rhs(i, j);
- rhs(i, j) = temp;
- }
- }
-
- // Normalize i-th row
- rhs[i] /= lhs(i, i);
- for (int j = cols - 1; j > i; --j)
- lhs(i, j) /= lhs(i, i);
-
- // Eliminate non-zeros in i-th column below the i-th row.
- for (int j = i + 1; j < rows; ++j) {
- rhs[j] -= lhs(j, i) * rhs[i];
- for (int k = i + 1; k < cols; ++k)
- lhs(j, k) -= lhs(j, i) * lhs(i, k);
- }
- }
- // Up
- for (int i = rows - 1; i > 0; --i) {
- for (int j = i - 1; j >= 0; --j)
- rhs[j] -= lhs(j, i) * rhs[i];
- }
- if (ok)
- *ok = true;
- return rhs;
- }
-
- Matrix<T, cols, rows> transpose() const
- {
- Matrix<T, cols, rows> result;
- for (int i = 0; i < rows; ++i) {
- for (int j = 0; j < cols; ++j)
- result.v[j][i] = v[i][j];
- }
- return result;
- }
-
- static Matrix identity()
- {
- Matrix result = matrix(T(0));
- for (int i = 0; i < rows && i < cols; ++i)
- result.v[i][i] = T(1);
- return result;
- }
-
- Vector<T, cols> v[rows];
-};
-
-template<class T, int rows, int cols>
-Vector<T, cols> operator * (const Vector<T, rows> &u, const Matrix<T, rows, cols> &m)
-{
- Vector<T, cols> result = Vector<T, cols>::vector(T(0));
- for (int i = 0; i < rows; ++i)
- result += m[i] * u[i];
- return result;
-}
-
-template<class T, int rows, int cols>
-std::ostream &operator << (std::ostream &os, const Matrix<T, rows, cols> &m)
-{
- assert(rows > 0);
- os << "[" << m[0];
- for (int i = 1; i < rows; ++i)
- os << ", " << m[i];
- os << "]";
- return os;
-}
-
-
-typedef Matrix<float, 2, 2> Matrix2x2f;
-typedef Matrix<float, 3, 3> Matrix3x3f;
-typedef Matrix<float, 4, 4> Matrix4x4f;
-
-template<class T>
-struct Quaternion
-{
- // Keep the Quaternion struct a plain old data (POD) struct by avoiding constructors
-
- static Quaternion quaternion(T s, T x, T y, T z)
- {
- Quaternion result;
- result.scalar = s;
- result.vector[0] = x;
- result.vector[1] = y;
- result.vector[2] = z;
- return result;
- }
-
- static Quaternion quaternion(T s, const Vector<T, 3> &v)
- {
- Quaternion result;
- result.scalar = s;
- result.vector = v;
- return result;
- }
-
- static Quaternion identity()
- {
- return quaternion(T(1), T(0), T(0), T(0));
- }
-
- // assumes that all the elements are packed tightly
- T& operator [] (int i) {return reinterpret_cast<T *>(this)[i];}
- T operator [] (int i) const {return reinterpret_cast<const T *>(this)[i];}
-
-#define QUATERNION_BINARY_OP(op, arg, rhs) \
- Quaternion operator op (arg) const \
- { \
- Quaternion result; \
- for (int i = 0; i < 4; ++i) \
- result[i] = (*this)[i] op rhs; \
- return result; \
- }
-
- QUATERNION_BINARY_OP(+, const Quaternion &q, q[i])
- QUATERNION_BINARY_OP(-, const Quaternion &q, q[i])
- QUATERNION_BINARY_OP(*, T s, s)
- QUATERNION_BINARY_OP(/, T s, s)
-#undef QUATERNION_BINARY_OP
-
- Quaternion operator - () const
- {
- return Quaternion(-scalar, -vector);
- }
-
- Quaternion operator * (const Quaternion &q) const
- {
- Quaternion result;
- result.scalar = scalar * q.scalar - Vector<T, 3>::dot(vector, q.vector);
- result.vector = scalar * q.vector + vector * q.scalar + Vector<T, 3>::cross(vector, q.vector);
- return result;
- }
-
- Quaternion operator * (const Vector<T, 3> &v) const
- {
- Quaternion result;
- result.scalar = -Vector<T, 3>::dot(vector, v);
- result.vector = scalar * v + Vector<T, 3>::cross(vector, v);
- return result;
- }
-
- friend Quaternion operator * (const Vector<T, 3> &v, const Quaternion &q)
- {
- Quaternion result;
- result.scalar = -Vector<T, 3>::dot(v, q.vector);
- result.vector = v * q.scalar + Vector<T, 3>::cross(v, q.vector);
- return result;
- }
-
-#define QUATERNION_ASSIGN_OP(op, arg, rhs) \
- Quaternion &operator op (arg) \
- { \
- for (int i = 0; i < 4; ++i) \
- (*this)[i] op rhs; \
- return *this; \
- }
-
- QUATERNION_ASSIGN_OP(+=, const Quaternion &q, q[i])
- QUATERNION_ASSIGN_OP(-=, const Quaternion &q, q[i])
- QUATERNION_ASSIGN_OP(=, T s, s)
- QUATERNION_ASSIGN_OP(*=, T s, s)
- QUATERNION_ASSIGN_OP(/=, T s, s)
-#undef QUATERNION_ASSIGN_OP
-
- Quaternion& operator *= (const Quaternion &q)
- {
- Quaternion result;
- result.scalar = scalar * q.scalar - Vector<T, 3>::dot(vector, q.vector);
- result.vector = scalar * q.vector + vector * q.scalar + Vector<T, 3>::cross(vector, q.vector);
- return (*this = result);
- }
-
- Quaternion& operator *= (const Vector<T, 3> &v)
- {
- Quaternion result;
- result.scalar = -Vector<T, 3>::dot(vector, v);
- result.vector = scalar * v + Vector<T, 3>::cross(vector, v);
- return (*this = result);
- }
-
- Quaternion conjugate() const
- {
- return quaternion(scalar, -vector);
- }
-
- T sqrNorm() const
- {
- return scalar * scalar + vector.sqrNorm();
- }
-
- Quaternion inverse() const
- {
- return conjugate() / sqrNorm();
- }
-
- // requires floating point type T
- Quaternion normalized() const
- {
- T s = sqrNorm();
- if (s == 0)
- return *this;
- return *this / sqrt(s);
- }
-
- void matrix(Matrix<T, 3, 3>& m) const
- {
- T bb = vector[0] * vector[0];
- T cc = vector[1] * vector[1];
- T dd = vector[2] * vector[2];
- T diag = scalar * scalar - bb - cc - dd;
- T ab = scalar * vector[0];
- T ac = scalar * vector[1];
- T ad = scalar * vector[2];
- T bc = vector[0] * vector[1];
- T cd = vector[1] * vector[2];
- T bd = vector[2] * vector[0];
- m(0, 0) = diag + 2 * bb;
- m(0, 1) = 2 * (bc - ad);
- m(0, 2) = 2 * (ac + bd);
- m(1, 0) = 2 * (ad + bc);
- m(1, 1) = diag + 2 * cc;
- m(1, 2) = 2 * (cd - ab);
- m(2, 0) = 2 * (bd - ac);
- m(2, 1) = 2 * (ab + cd);
- m(2, 2) = diag + 2 * dd;
- }
-
- void matrix(Matrix<T, 4, 4>& m) const
- {
- T bb = vector[0] * vector[0];
- T cc = vector[1] * vector[1];
- T dd = vector[2] * vector[2];
- T diag = scalar * scalar - bb - cc - dd;
- T ab = scalar * vector[0];
- T ac = scalar * vector[1];
- T ad = scalar * vector[2];
- T bc = vector[0] * vector[1];
- T cd = vector[1] * vector[2];
- T bd = vector[2] * vector[0];
- m(0, 0) = diag + 2 * bb;
- m(0, 1) = 2 * (bc - ad);
- m(0, 2) = 2 * (ac + bd);
- m(0, 3) = 0;
- m(1, 0) = 2 * (ad + bc);
- m(1, 1) = diag + 2 * cc;
- m(1, 2) = 2 * (cd - ab);
- m(1, 3) = 0;
- m(2, 0) = 2 * (bd - ac);
- m(2, 1) = 2 * (ab + cd);
- m(2, 2) = diag + 2 * dd;
- m(2, 3) = 0;
- m(3, 0) = 0;
- m(3, 1) = 0;
- m(3, 2) = 0;
- m(3, 3) = 1;
- }
-
- // assumes that 'this' is normalized
- Vector<T, 3> transform(const Vector<T, 3> &v) const
- {
- Matrix<T, 3, 3> m;
- matrix(m);
- return v * m;
- }
-
- // assumes that all the elements are packed tightly
- T* bits() {return reinterpret_cast<T *>(this);}
- const T* bits() const {return reinterpret_cast<const T *>(this);}
-
- // requires floating point type T
- static Quaternion rotation(T angle, const Vector<T, 3> &unitAxis)
- {
- T s = sin(angle / 2);
- T c = cos(angle / 2);
- return quaternion(c, unitAxis * s);
- }
-
- T scalar;
- Vector<T, 3> vector;
-};
-
-template<class T>
-Quaternion<T> operator * (T s, const Quaternion<T>& q)
-{
- return Quaternion<T>::quaternion(s * q.scalar, s * q.vector);
-}
-
-typedef Quaternion<float> Quaternionf;
-
-} // end namespace gfx
-
-#endif