如何使用opengl函数制作用于在c中绘制3D点的3D窗口? [英] How to make a 3D window for drawing 3D points in c using opengl functions?
本文介绍了如何使用opengl函数制作用于在c中绘制3D点的3D窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以制作 2D 窗口并绘制点、线等.我想制作一个 3D 窗口,以便我可以通过绘制 3D 点/线来绘制 3D 对象(例如 glVertex3d(x,y,z))(如 2DglVertex2d(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.
我的二维窗口示例代码 -
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();
}
编辑gluOrtho2D
更新为上述 MWE 中的 glOrtho
.
Edit
gluOrtho2D
is updated with glOrtho
in the above MWE.
推荐答案
- 您可以使用
gluPerspective()
(或glFrustum()
)来设置透视投影矩阵 - 和
gluLookAt()
将相机"从原点移开,以便您可以看到您绘制的内容 - 还有一点点基于计时器的动画,这样您就可以看到正在运行的透视图
- 双缓冲,因为
GLUT_SINGLE
在现代复合窗口管理器上有时很奇怪 - You can use
gluPerspective()
(orglFrustum()
) 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函数制作用于在c中绘制3D点的3D窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文