如何使用用户给定值/请求值使用openGL绘图?

时间:2012-02-14 17:03:57

标签: objective-c opengl plot glut

限制:

  • 我无法更改框架
  • 我必须使用中的功能 程序(我心爱的老师不喜欢我们使用除了她自己以外的任何功能)

这是代码,它就像这样(只是不像我想的那样顺利)

#include<OpenGL/gl.h>
#include<OpenGL/glu.h>
#include<GLUT/glut.h>
#include <stdlib.h>
#include <math.h>

int lb=-30, ub=30;
double cpoints=9999999;

void init (void)
{
    glClearColor(1,1,1,1);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(-30,30,-30,30);
}

void graphfunct2D(void)
{
    double dx, xi, yi;
    dx = (ub-lb)*1.0/cpoints;
    glColor3f(0,0,0);
    glClear(GL_COLOR_BUFFER_BIT);
    xi = lb;
    yi = xi*sin(xi);
    int i;
    for (i=0; i<=cpoints;i++)
    {
        glBegin(GL_POINTS);
        glVertex2i(xi,yi);
        glEnd();
        xi = xi+dx;
        yi = xi*sin(xi);
    }
    glFlush();
}

int main (int argc, char** argv)
{
    glutInit(&argc, argv);
    /*printf("lower bound: ");
    scanf(" %d",&lb);
    printf("upper bound:");
    scanf(" %d",&ub);
    printf("Give me the number of points to plot (int)");
    scanf(" %d",&cpoints);*/
    glutInitWindowSize(500,500);
    glutCreateWindow("Graph function in 2D");
    init();
    glutDisplayFunc(graphfunct2D);
    glutMainLoop();
}

我希望用户能够提供上限和下限并根据这两个来绘制函数但是当我运行程序时输入未注释它不显示任何东西(当我不要求它们并使用那些我定义它运行得很好)

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

#include <GL/glut.h>
#include <stdlib.h>
#include <math.h>

double lb=-30, ub=30;
unsigned int cpoints=2048;
void graphfunct2D(void)
{
    double dx, xi, yi;
    int i;
    dx = (ub-lb)/cpoints;

    glClearColor(1, 1, 1, 1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho( -30, 30, -30, 30, -1, 1 );
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glColor3ub(0,0,0);
    glBegin(GL_LINE_STRIP);
    for( i = 0; i <= cpoints; i++ )
    {
        xi = (i * dx) + lb;
        yi = xi * sin(xi);
        glVertex2f( xi, yi );
    }
    glEnd();
    glutSwapBuffers();
}

int main (int argc, char** argv)
{
    printf("lower bound: ");
    scanf(" %d",&lb);
    printf("upper bound:");
    scanf(" %d",&ub);
    printf("Give me the number of points to plot (int)");
    scanf(" %d",&cpoints);

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
    glutInitWindowSize(500,500);
    glutCreateWindow("Graph function in 2D");
    glutDisplayFunc(graphfunct2D);
    glutMainLoop();
}