OpenGL正弦波幅度和频率变化

时间:2014-04-08 05:43:25

标签: c++ qt opengl

我写了一个OpenGL程序来画一个正弦波:

#include <QtGui/QtGui>
#include <QtOpenGL/QtOpenGL>
#include <math.h>
#include "globj2.h"
#include <iostream>

using namespace std;

GLobj2::GLobj2(QWidget *parent)
    : QGLWidget(parent)
{

}

GLobj2::~GLobj2()
{

}

//Initialize the GL settings
void GLobj2::initializeGL()
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glPointSize(2.0);
    glLineWidth(1.0);

}

//Set up the viewport based on the screen dimentions
//Function is called implicitly by initializeGL and when screen is resized
void GLobj2::resizeGL( int w, int h )
{
    //algorithm to keep scene "square" (preserve aspect ratio)
    //even if screen is streached
    if(w>h)
        glViewport((w-h)/2, 0, h, h);
    else
        glViewport(0, (h-w)/2, w, w);

    //setup the projection and switch to model view for transformations
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1, 1, -1, 1, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();


    //implicit call to paintGL after resized
}

//Paints the GL scene
void GLobj2::paintGL()
{
    glClear (GL_COLOR_BUFFER_BIT);
    glClear(GL_DEPTH_BUFFER_BIT);

glBegin(GL_LINE_STRIP);
double amp=1;
double freq = 4;
double t=0.0;
for (double x=-1.0; x<=1; x+=0.001)
{
double y = amp * cos(2 * 3.14 * freq * x );
glVertex2f(x, y);
t+=10.0;
}
glEnd();
    glFlush ();
}

我的频率和音量滑块定义如下:

QSlider *volume = new QSlider(Qt::Horizontal);
QLCDNumber *lcd1 = new QLCDNumber;
connect(volume,SIGNAL(valueChanged(int)),lcd1,SLOT(display(int)));
QLabel *label1 = new QLabel;
label1->setText("Volume:");



QDial *frequency = new QDial;
QLCDNumber *lcd2 = new QLCDNumber;
connect(frequency,SIGNAL(valueChanged(int)),lcd2,SLOT(display(int)));
QLabel *label2 = new QLabel;
label2->setText("Frequency:");

现在我想将滑块的频率和音量信号连接到正弦波频率和音量。你能帮我弄清楚如何进行这种连接吗?

0 个答案:

没有答案