首页 > 解决方案 > 基于矩阵在openGL中显示

问题描述

我正在尝试在 openGL 窗口中绘制一些形状。我根据特定矩阵中的值绘制这些形状。我正在使用 glut,它有一个带 1 个参数的函数 glutDisplayFunc,一个不带参数并返回 void 的函数回调。但是我需要根据无法传递给函数回调的矩阵在窗口上绘制图像。

这是一个示例代码

#include<stdio.h>
#include<GL/glut.h>
#include<math.h>
#define pi 3.142857
void mat()
{
        int a[2][2];
    //
    for(int i=0;i<2;i++)
        for (int j = 0; j < 2; ++j)
        {
            scanf("%d",&a[i][j]);
        }
}
// function to initialize
void myInit (void)
{
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glColor3f(0.0, 1.0, 0.0);
    glPointSize(1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-780, 780, -420, 420);
}

void display (void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POINTS);
    float x, y, i;
    for ( i = 0; i < (2 * pi); i += 0.001)
    {
        x = 200 * cos(i);
        y = 200 * sin(i);

        glVertex2i(x, y);
    }
    glEnd();
    glFlush();
}

int main (int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

    // giving window size in X- and Y- direction
    glutInitWindowSize(1366, 768);
    glutInitWindowPosition(0, 0);
    glutCreateWindow("Circle Drawing");
    myInit();
    glutDisplayFunc(display);
    glutMainLoop();
}

我需要能够在函数 mat 中使用矩阵 a 来定义 2 个圆的中心。如何从 mat 函数中绘制窗口?编辑:包含代码并修复了一些错别字

标签: copenglopengl-compat

解决方案


void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    //-----------
    float a[4][4] = {
        1,0,0,0,
        0,1,0,0,
        0,0,1,0,
        0,0,0,1 };

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glLoadMatrixf((float*)a);
    //----------

    glBegin(GL_POINTS);
    float x, y, i;
    for (i = 0; i < (2 * pi); i += 0.001)
    {
        x = 200 * cos(i);
        y = 200 * sin(i);

        glVertex2i(x, y);
    }
    glEnd();
    glFlush();
}

推荐阅读