配置GoogleTest
采用TDD(Test-Driven-Development)方式开发引擎,使用GoogleTest进行程序测试,便于开发过程中发现错误。
GoogleTest
-
下载:Github
-
环境搭建
- 拷贝googletest目录中的src与include中gtest文件夹,放置于Middleware/gtest
- 建立新项目engineTester,将gtest/src/gtest-all.cc添加至项目中(此cpp为所有程序代码的集合)
- 添加myMain.cpp,编译成功
-
使用方法
- 新建MyTempTestFile.cpp,包含gtest/test.h
#include <gtest/gtest.h>
TEST(MyTestSuiteName, MyTestName) {
int a = 1, b = 2, c = a + b;
EXPECT_EQ(c, 3);
EXPECT_TRUE(c == 3);
}
初步编写Vector2D
//Vector2D.h
namespace Math {
struct Vector2D {
float x;
float y;
explicit Vector2D(const float& x = 0.0f, const float& y = 0.0f) :x(x), y(y) {}
};
inline Vector2D operator + (const Vector2D& a, const Vector2D& b);
inline Vector2D operator * (const Vector2D& a, const float& b);
inline Vector2D operator * (const float& a, const Vector2D& b);
#include "Vector2D.inl"
}
//Vector2D.inl
Vector2D operator + (const Vector2D& a, const Vector2D& b) {
return Vector2D(a.x + b.x, a.y + b.y);
}
Vector2D operator * (const Vector2D & a, const float& b) {
return Vector2D(a.x * b, a.y * b);
}
Vector2D operator *(const float& a, const Vector2D & b) {
return Vector2D(a * b.x, a * b.y);
}
实现openGL多帧绘制
-
QTimer && Q_OBJECT
- QTimer主要实现设定时间或立即触发某一个槽,这里用来触发myUpdate
- connect(&myTimer,SIGNAL(timeout()),this,SLOT(myUpdate));
- 这里需要myUpdate是一个槽信号,所以需要加private slots:
- 当对象需要信号槽时,需要令其为Q_OBJECT,在类内添加Q_OBJECT宏
- 此时还需要利用QT程序中的moc.exe对Q_OBJECT程序进行重新编译
- 查找到moc.exe后执行" moc.exe MyGlWindow.h > MyGlWindow_moc.cpp"到项目中
- 最后编译成功,循环绘制
#ifndef SANDBOX_MY_GL_WINDOW
#define SANDBOX_MY_GL_WINDOW
#include <QtOpenGL/qgl.h>
#include <QtCore/qtimer.h>
class MyGlWindow : public QGLWidget
{
Q_OBJECT
QTimer myTimer;
protected:
void initializeGL();
void paintGL();
void resizeGL(int, int);
private slots:
void myUpdate();
};
#endif
简易的图形移动
- glBufferData不传递verts内容,只开辟verts大小内存,并设置为GL_DYNAMIC_DRAW
- 在每次绘制前对verts进行矢量位移,通过glBufferSubData传递修改后的verts
- update中设置移动速度,迭代位移矢量,重绘图像
#include <GL/glew.h>
#include "MyGlWindow.h"
#include <cassert>
#include <Math/Vector2D.h>
using Math::Vector2D;
namespace {
Vector2D verts[] = {
Vector2D(+0.1f,-0.1f),
Vector2D(+0.0f,+0.1f),
Vector2D(-0.1f,-0.1f),
};
Vector2D shipPosition(0.0f, 0.0f);
const int VERTS_NUM = sizeof(verts) / sizeof(*verts);
}
void MyGlWindow::initializeGL() {
GLenum errorCode = glewInit();
assert(errorCode == 0);
GLuint myBufferID;
glGenBuffers(1, &myBufferID);
glBindBuffer(GL_ARRAY_BUFFER, myBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof verts, NULL,
GL_DYNAMIC_DRAW);
connect(&myTimer, SIGNAL(timeout()), this, SLOT(myUpdate()));
myTimer.start(0);
}
void MyGlWindow::paintGL() {
glClear(GL_COLOR_BUFFER_BIT);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
glBufferData(GL_ARRAY_BUFFER, sizeof verts, verts, GL_DYNAMIC_DRAW);
Vector2D transVerts[VERTS_NUM];
for (int i = 0; i < VERTS_NUM; i++)
transVerts[i] = verts[i] + shipPosition;
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof transVerts, transVerts);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
void MyGlWindow::myUpdate() {
Vector2D velocity(0.001f,0.001f);
shipPosition = shipPosition + velocity;
repaint();
}
void MyGlWindow::resizeGL(int w, int h) {
glViewport(0, 0, w, h);
}
项目目录
来源:CSDN
作者:peter_819
链接:https://blog.csdn.net/peter_819/article/details/104187724