初始化
GLFWwindow* initGL(void) {
//初始化
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(600, 600, "OSC_XY", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return NULL;
}
//绑定上下文
glfwMakeContextCurrent(window);
//根据窗口动态调整视口的大小
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return NULL;
}
//允许Debug调试信息查看
glEnable(GL_DEBUG_OUTPUT);
glDebugMessageCallback(MessageCallback, 0);
return window;
}
//视口跟随窗口调整回调函数
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
参数设置
每帧采样数据量(点)nSamples
采样间隔sampleRate
XY数据数组 x_data[nSamples] y_data[nSamples]
XY总数据量 scratchBuffer[4nSamples+4nSamples]:一个坐标点横纵轴共分裂为4个点,组成一个正方形
Render::Render(int sampleRate, int nSamples, int lineSize, int width, int height) {
this->nSamples = nSamples;
this->lineSize = lineSize;
this->sampleRate = sampleRate;
this->width = width;
this->height = height;
this->x_data = new float[nSamples];
this->y_data = new float[nSamples];
this->scratchBuffer = new float[nSamples * 8]();
this->quadIndex = makeQuadIndex();
this->vertexIndex = makeVertexIndex(); //顶点索引
//绘制纹理,暂时不用
this->outQuadArray = makeOutQuad();
//暂时没用
if (makeFrameBuffer() != 0) {
io.Err("Can Not Make FrameBuffers");
return;
}
else {
io.Log("FrameBuffers Generated!!!");
}
if (this->lineVao == 0 || this->textureVao == 0 ) {
glGenVertexArrays(1, &this->lineVao);
glGenVertexArrays(1, &this->textureVao);
}
return;
}
正方形顶点索引 quadIndex :每一个Y轴坐标被拆分成4个顶点,顶点坐标索引nSamples*4 VBO
unsigned int Render::makeQuadIndex() {
int len = this->nSamples * 4;
if (len < 0) {
return -1;
}
short* index = new short[len];
for (int i = len; i--;) {
index[i] = i;
}
unsigned int vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(short)* len, index, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, NULL);
return vbo;
}
正方形绘制索引 vertexIndex: 每一个Y轴坐标被拆分成4个顶点,组成一个正方形,一个正方形被分隔为两个三角形,总共EBO的索引为nSamples*6 EBO
unsigned int Render::makeVertexIndex() {
//-1是因为WebGL中会闪屏,最后一个点
int len = (this->nSamples-1) * 2 * 3;
if (len < 6) {
return -1;
}
unsigned short* index = new unsigned short[len];
for (int i = 0, pos = 0; i < len;) {
index[i++] = pos;
index[i++] = pos + 2;
index[i++] = pos + 1;
index[i++] = pos + 1;
index[i++] = pos + 2;
index[i++] = pos + 3;
pos += 4;
}
unsigned int ebo;
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short)*len, index, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, NULL);
return ebo;
}
生成正弦波数据
float* IO::genDatas(string anxi, int len) {
if (len <= 0)
{
return new float[1]{-1};
}
float* wfm = new float[len];
if (anxi.compare("x"))
{
for (int i = 0; i < len; i++) {
wfm[i] = sin(i / 200.0);
}
}
else {
for (int i = 0; i < len; i++) {
wfm[i] = cos(i / 1000.0);
}
}
return wfm;
}