【问题标题】:Drawing multiple objects in OpenGL using C使用 C 在 OpenGL 中绘制多个对象
【发布时间】:2013-05-02 01:38:23
【问题描述】:

我有一个OpenGL 程序可以通过鼠标点击来绘制一个圆圈。该程序运行良好,除非当我尝试绘制多个圆圈时,前一个圆圈消失了。代码如下:

#include <GL/glut.h>
#include<math.h>
#include<stdio.h>
struct position
{
    float x;
    float y;
};
typedef struct position Position;

Position start;
Position finish;

void setPixel(int x, int y)
{
    glBegin(GL_POINTS);
    glVertex2f(x, y);
    glEnd();
}

void circle(int a0, int b0, int a1, int b1)
{
    int i, x, y, x1, y1, r, p;

    x1 = (a0+a1)/2;
    y1 = (b0+b1)/2;

    r = sqrt((((a1-x1)*(a1-x1))+((b1-y1)*(b1-y1))));

    p = (5/4-r);
    x = 0;
    y = r;
    while(x <= y)
    {
        setPixel( x+x1,  y+y1);
        setPixel( x+x1, -y+y1);
        setPixel(-x+x1, -y+y1);
        setPixel(-x+x1,  y+y1);
        setPixel( y+x1,  x+y1);
        setPixel( y+x1, -x+y1);
        setPixel(-y+x1, -x+y1);
        setPixel(-y+x1,  x+y1);

        x = x+1;
        if (p<0)
            p = p+2*x+1;
        else {
            y = y-1;
            p = p+2*x-2*y+1;
        }
    }
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    glPushMatrix();

    circle(start.x, start.y, finish.x, finish.y);

    glPopMatrix();

    glutSwapBuffers();
}

void reshape( int w, int h)
{
    glViewport( 0, 0, w, h);
    glMatrixMode( GL_PROJECTION);
    glLoadIdentity();
    glOrtho( 0, w, h, 0, -1, 1);
}

void mouse(int button, int state, int x, int y)
{
    switch(button)
    {
        case GLUT_LEFT_BUTTON:

           if(state == GLUT_DOWN)
           {
               start.x = x; //x1
               start.y = y; //y1
           }

           if(state == GLUT_UP)
           {
               finish.x = x; //x2
               finish.y = y; //y2
           }
           break;

           glutPostRedisplay();
    }
}

void motion( int x, int y)
{
    finish.x = x;
    finish.y = y;
    glutPostRedisplay();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("");
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

    glutMainLoop();
    return 0;
}

如何同时显示多张图片?

【问题讨论】:

  • glutPostRedisplay(); 在 switch-case 中看起来无法访问代码
  • 我查过了。那不是问题。实际上, glutPostRedisplay() 不应该在那里。我的错误

标签: c opengl graphics


【解决方案1】:

如果你想画多个东西,那就画多个东西。你只画一个圆圈。如果你改变那个圆圈的位置,它只会被画在不同的地方。

但是我真的不建议在 GL 中进行这样的绘制。使用固定功能管道将单个像素绘制为GL_POINTS 效率极低。 GL 并非设计为光栅绘图 API。

【讨论】:

  • 嗨 JasonD,我也面临这个问题。我已经在 Android 的 OpenGLES2.0 中开发了 Autocad 应用程序。我的问题是,我可以同时绘制多条线和圆。应用程序在 Google Nexus7 中运行良好。但是,当我在三星 Galaxy Note II 中运行我的应用程序时,我不能画多条线。当我画一条线时,前面的线会自动删除。这是我的问题。只需查看此链接,它还包含示例代码..如果可能请帮助我..stackoverflow.com/questions/17229066/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-04
  • 1970-01-01
  • 2016-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多