【问题标题】:Drawing shape using opengl使用opengl绘制形状
【发布时间】:2013-12-03 17:54:29
【问题描述】:

如果我将形状描述为封闭的半圆形,而另一半圆形与第一个圆形相连,我该如何绘制类似 (sin) 的形状。使用笛卡尔方法这是我的尝试:

#include <windows.h>   
#include <gl/Gl.h>
#include <gl/glut.h>
#include<math.h>
#include<cstdlib>

 static void myDisplay()
    {
        glClear(GL_COLOR_BUFFER_BIT); // clear the screen
      glColor3f(1.0,0.0,0.0);
       glColor3f(0.0,0.0,0.0);
          glBegin(GL_LINE_LOOP);

         double xc=200, yc=200,r=100;
            double x,y;

            for (x = xc - r; x<= xc + r;x++)
           {
            y = sqrt((r*r)-((xc - x)*(xc - x)));
            glVertex2d(x, yc + y);

           }

            for (x = xc +r ; x<= xc - r ; x++)
            {
            y = sqrt((r*r)-((xc - x)*(xc - x)));

            glVertex2d(x , yc - y);
            }


        glEnd();

        glFlush();
      }
void myInit(void)
{
glClearColor(1.0,1.0,1.0,0.0);       // set white background color
glColor3f(0.0f, 0.0f, 0.0f);          // set the drawing color 
glPointSize(4.0);              // a ‘dot’ is 4 by 4 pixels
glMatrixMode(GL_PROJECTION); 
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}    

void main(int argc, char** argv)
{
glutInit(&argc, argv);          // initialize the toolkit
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // set display mode
glutInitWindowSize(640,480);     // set window size
glutInitWindowPosition(100, 150); // set window position on screen
glutCreateWindow("Line Scan Conversion"); // open the screen window
glutDisplayFunc(myDisplay);     // register redraw function
myInit();

glutMainLoop();              // go into a perpetual loop
}

【问题讨论】:

  • 你的代码有什么问题?
  • 前半部分画圆,后半部分(旁边)我不知道怎么做
  • 只需绘制单独的半圆对象,然后将后半圆倒置。简单的正弦/余弦函数,你所需要的。如果我理解正确的话......

标签: opengl


【解决方案1】:

直接采样函数(sin()):


#include <GL/glut.h>
#include <cmath>

static void myDisplay()
{
    glClearColor( 0, 0, 0, 1 );       // set white background color
    glClear(GL_COLOR_BUFFER_BIT); // clear the screen

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity();
    glOrtho( -0.5, 7, -1.2, 1.2, -1, 1 );

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity();

    // axes
    glColor3ub( 255, 255, 255 );
    glBegin( GL_LINES );
    glVertex2i( 0, 0 );
    glVertex2i( 7, 0 );
    glVertex2i( 0, -1 );
    glVertex2i( 0, 1 );
    glEnd();

    // sin() function plot
    glColor3ub( 255, 0, 0 );
    glBegin(GL_LINE_STRIP);
    const unsigned int samples = 100;
    for( unsigned int i = 0; i <= samples; ++i )
    {
        const float pct = ( (float)i / samples );
        const float x = 2 * 3.14159 * pct;
        const float y = sin( x );
        glVertex2f( x, y );
    }
    glEnd();

    glutSwapBuffers();
}

void main(int argc, char** argv)
{
    glutInit(&argc, argv);          // initialize the toolkit
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); // set display mode
    glutInitWindowSize(600, 600);     // set window size
    glutCreateWindow("Line Scan Conversion"); // open the screen window
    glutDisplayFunc(myDisplay);     // register redraw function
    glutMainLoop();              // go into a perpetual loop
}

【讨论】:

  • 请理解我无法加载图像,因为我的名声很小,我想要像罪而不是罪,但类似的罪将前半圈向上必须关闭,半圈向下也必须关闭使用笛卡尔方法
  • @genpfault 前半部分画圆但后半部分(旁边)第一个我不知道怎么做
【解决方案2】:

您的第二个for 循环中存在问题:

for( x = xc + r; x &lt;= xc - r; x++ )

这是作为x = 400; x &lt;= 0; x++ 执行的,显然永远不会运行。您的问题(除了上面指出的明显问题)是您试图在循环迭代中考虑 x 偏移量。我建议更改为以下内容:

for( x = 0; x &lt; 2r; x++ )

然后代替

glVertex2d(x, yc + y);

glVertex2d(x, yc - y);

glVertex2d((xc - r) + x, yc + y);

glVertex2d((xc + r) + x, yc - y);

或类似的东西(无法实际测试,但你应该明白)。

【讨论】:

  • @ssell 请使用 for( x = 0; x
  • 我会让你的绘图代码类似于:for( x = 0; x &lt; 2r; x++ ){ y = sqrt((r*r)-((xc - x)*(xc - x))); glVertex2d((xc - r) + x, yc + y); } 用于上半部分,然后for( x = 0; x &lt; 2r; x++ ){ y = sqrt((r*r)-((xc - x)*(xc - x))); glVertex2d((xc + r) + x, yc - y); } 用于下半部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-15
相关资源
最近更新 更多