如何使用opengl函数制作3D窗口以在c中绘制3D点

如何使用opengl函数制作3D窗口以在c中绘制3D点

本文介绍了如何使用opengl函数制作3D窗口以在c中绘制3D点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以制作2D窗口并绘制点,线等.我想制作3D窗口,以便可以通过绘制3D点/线来绘制3D对象(例如glVertex3d(x,y,z))(如2D glVertex2d(x,y)).

I can make 2D window and draw dots, lines etc. I want to make a 3D window so that I can draw 3D objects by drawing 3D dots/lines(e.g. glVertex3d(x,y,z))(like as 2D glVertex2d(x,y)).

但是我无法创建3D窗口.我只想制作一个3D窗口,为我提供一个绘制3D点的平台.

But I can't make the 3D window. I want to make just a 3D window that give me a platform for drawing 3D points.

我的2D窗口示例代码-

My sample code for 2D window -

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

double x_0 = -100;
double y_0 = -25;

double x_1 = 100;
double y_1 = -25;

double x_2 = 100;
double y_2 = 25;

double x_3 = -100;
double y_3 = 25;

void
drawRectangle()
{
    /* Clear the screen */
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glFlush();

    glBegin(GL_LINES);

    glVertex2d(x_0, y_0);
    glVertex2d(x_1, y_1);

    glVertex2d(x_1, y_1);
    glVertex2d(x_2, y_2);

    glVertex2d(x_2, y_2);
    glVertex2d(x_3, y_3);

    glVertex2d(x_3, y_3);
    glVertex2d(x_0, y_0);

    glEnd();
    glFlush();
}

void
init(void)
{
    /*initialize the x-y co-ordinate*/
    glClearColor(0,0,0,0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-320, 319,-240, 239);
    glClear(GL_COLOR_BUFFER_BIT);

    glFlush();
}

int
main(int argc, char *argv[])
{
    double x_0, y_0, x_1, y_1;
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(400, 400);

    glutCreateWindow("Rectangle in 2D");
    init();
    drawRectangle();
    glutMainLoop();
}

我也尝试跟随,但未显示该行.

I also try following but the line is not shown.

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

void
init(void)
{
    /*initialize the x-y co-ordinate*/
    glClearColor(0,0,0,0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-320, 319, -240, 239, 100, -150);
    glClear(GL_COLOR_BUFFER_BIT);

    glFlush();
}

int
main(int argc, char *argv[])
{
    double x_0, y_0, x_1, y_1;
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(640, 480);
    glutInitWindowPosition(400, 400);

    glutCreateWindow("Special key");
    init();

    glBegin(GL_LINES);
    glVertex3d(0,0,0);
    glVertex3d(0,0,100);
    glVertex3d(0,100,0);
    glVertex3d(0,100,0);
    glEnd();
    glFlush();

    glutMainLoop();
}

修改
在上述MWE中,用glOrtho更新了gluOrtho2D.

Edit
gluOrtho2D is updated with glOrtho in the above MWE.

推荐答案

  • 您可以使用gluPerspective()(或glFrustum())设置透视投影矩阵
  • 然后gluLookAt()将相机"从原点上侦走,这样您就可以看到自己绘制的内容
  • 还有一些基于计时器的动画,因此您可以看到实际的视角
  • 双重缓冲,因为GLUT_SINGLE在现代的复合窗口管理器上有时很奇怪
    • You can use gluPerspective() (or glFrustum()) to set up a perspective projection matrix
    • And gluLookAt() to scoot the "camera" away from the origin so you can see what you draw
    • And a little bit of timer-based animation so you can see the perspective in action
    • Double-buffering because GLUT_SINGLE is sometimes weird on modern composited window managers
    • 一起:

      #include <GL/glut.h>
      
      double rnd( double lo, double hi )
      {
          return lo + ( hi - lo ) * ( rand() / static_cast<double>( RAND_MAX ) );
      }
      
      double angle = 0.0;
      void timer( int value )
      {
          angle += 1.0;
          glutTimerFunc( 16, timer, 0 );
          glutPostRedisplay();
      }
      
      void display()
      {
          glClearColor( 0, 0, 0, 0 );
          glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
      
          glMatrixMode( GL_PROJECTION );
          glLoadIdentity();
          double w = glutGet( GLUT_WINDOW_WIDTH );
          double h = glutGet( GLUT_WINDOW_HEIGHT );
          gluPerspective( 60.0, w / h, 0.1, 1000.0 );
      
          glMatrixMode( GL_MODELVIEW );
          glLoadIdentity();
          gluLookAt( 100, 100, 100, 0, 0, 0, 0, 0, 1 );
      
          glRotated( angle, 0, 0, 1 );
      
          srand( 0 );
          glBegin( GL_LINES );
          for( size_t i = 0; i < 100; i++ )
          {
              glColor3d( rnd( 0.0, 1.0 ), rnd( 0.0, 1.0 ), rnd( 0.0, 1.0 ) );
              glVertex3d( rnd( -50, 50 ), rnd( -50, 50 ), rnd( -50, 50 ) );
              glVertex3d( rnd( -50, 50 ), rnd( -50, 50 ), rnd( -50, 50 ) );
          }
          glEnd();
      
          glutSwapBuffers();
      }
      
      int main( int argc, char *argv[] )
      {
          glutInit( &argc, argv );
          glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB );
          glutInitWindowSize( 640, 480 );
          glutCreateWindow( "Special key" );
          glutDisplayFunc( display );
          glutTimerFunc( 0, timer, 0 );
          glutMainLoop();
      }
      

      这篇关于如何使用opengl函数制作3D窗口以在c中绘制3D点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:48