MSDN.WhiteKnight - Stack Overflow answers
Ответ на "Простой способ визуализации в СИ"
Answer 927951
Можно использовать библиотеку GLUT
#include <stdlib.h> #include "glut.h" //подключение библиотек импорта (используются директивы, специфичные для MS Visual C++) #pragma comment(lib, "opengl32.lib") #pragma comment(lib, "glu32.lib") #pragma comment(lib, "glut32.lib") //POINT3D - представляет точку в пространстве typedef struct { float x; float y; float z; } POINT3D; //SHAPE3D - представляет трехмерную фигуру typedef struct { POINT3D* points; //массив вершин int count_points; //число вершин } SHAPE3D; SHAPE3D* shapes = NULL; //массив фигур для отображения int n_shapes = 0; //количество фигур void DrawShapes(SHAPE3D* shapes, int n_shapes) //выводит массив фигур { int i=0; int j=0; glBegin(GL_LINES); for(i=0; i<n_shapes; i++){ for(j=0; j<shapes[i].count_points - 1; j++){ glVertex3f(shapes[i].points[j].x,shapes[i].points[j].y,shapes[i].points[j].z); glVertex3f(shapes[i].points[j+1].x,shapes[i].points[j+1].y,shapes[i].points[j+1].z); } } glEnd(); glFlush(); } void Draw(){ DrawShapes(shapes,n_shapes); } int main(int argc, char* argv[]) { //заполним массив тестовыми данными (2 грани куба) n_shapes = 2; shapes = (SHAPE3D*) malloc(sizeof(SHAPE3D) * n_shapes); shapes[0].count_points = 5; shapes[0].points = (POINT3D*) malloc(sizeof(POINT3D) * shapes[0].count_points); shapes[0].points[0].x = 0.0; shapes[0].points[0].y = 0.0; shapes[0].points[0].z = 0.0; shapes[0].points[1].x = 0.0; shapes[0].points[1].y = 1.0; shapes[0].points[1].z = 0.0; shapes[0].points[2].x = 1.0; shapes[0].points[2].y = 1.0; shapes[0].points[2].z = 0.0; shapes[0].points[3].x = 1.0; shapes[0].points[3].y = 0.0; shapes[0].points[3].z = 0.0; shapes[0].points[4].x = 0.0; shapes[0].points[4].y = 0.0; shapes[0].points[4].z = 0.0; shapes[1].count_points = 5; shapes[1].points = (POINT3D*) malloc(sizeof(POINT3D) * shapes[1].count_points); shapes[1].points[0].x = 0.0; shapes[1].points[0].y = 0.0; shapes[1].points[0].z = 0.0; shapes[1].points[1].x = 0.0; shapes[1].points[1].y = 0.0; shapes[1].points[1].z = 1.0; shapes[1].points[2].x = 1.0; shapes[1].points[2].y = 0.0; shapes[1].points[2].z = 1.0; shapes[1].points[3].x = 1.0; shapes[1].points[3].y = 0.0; shapes[1].points[3].z = 0.0; shapes[1].points[4].x = 0.0; shapes[1].points[4].y = 0.0; shapes[1].points[4].z = 0.0; //инициализация GLUT glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(600,400); glutInitWindowPosition(100,100); glutCreateWindow("Graphics"); //установка параметров камеры glMatrixMode(GL_PROJECTION); gluPerspective( 40.0, 1.0, 1.0, 10.0); glMatrixMode(GL_MODELVIEW); gluLookAt( 5.0, 5.0, 5.0, //eye coordinates 0.0, 0.0, 0.0, //center coordinates 0.0, 1.0, 0.0 //up direction ); //запуск цикла отрисовки glutDisplayFunc(Draw); glutMainLoop(); free(shapes[0].points); free(shapes[1].points); free(shapes); return 0; }
Content is retrieved from StackExchange API.
Auto-generated by ruso-archive tools.