행렬 기반의 OpenGL로 표시 (Displaying in openGL based on a matrix)


문제 설명

행렬 기반의 OpenGL로 표시 (Displaying in openGL based on a matrix)

OpenGL 창에서 몇 가지 모양을 그리려고 합니다. 특정 행렬의 값을 기반으로 이러한 모양을 그립니다. 1개의 매개변수를 사용하는 glutDisplayFunc 함수, 인수를 사용하지 않고 void를 반환하는 함수 콜백이 있는 glut을 사용하고 있습니다. 하지만 함수 콜백에 전달할 수 없는 행렬을 기반으로 창에 이미지를 그려야 합니다.

이것은 예제 코드입니다.

#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개의 원의 중심을 정의합니다. 매트 기능 내에서 창을 어떻게 그리나요? 편집: 포함된 코드 및 일부 오타 수정


참조 솔루션

방법 1:

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 &lt; (2 * pi); i += 0.001)
{
    x = 200 * cos(i);
    y = 200 * sin(i);

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

}
</code></pre>

방법 2:

In general you can load the current model view matrix, by setting the GL_MODELVIEW matrix mode (glMatrixMode), and loading the matrix by glLoadMatrixf.

Optionally the matrix can be multiplied to the current matrix by glMultMatrix.</p>

But in both cases, the matrix has to be 4x4 Transformation matrix. The parameter to both functions is a pointer to an array of 16 floats respectively an 2 dimensional 4x4 float‑array. Init a 4x4 Identity matrix and read the upper left 2x2, to set up a rotation matrix around the z‑axis:

Further, I recommend to read an rotation angle in degree and to calculate the rotation axis by the trigonometric functions sin respectively cos. Finally read the xy translation components:

#define _USE_MATH_DEFINES
#include <math.h>
float a[4][4];

void mat()
{
    // init identity matrix
    for(int i = 0; i < 4; i++)
        for (int j = 0; j < 4; ++j)
            a[i][j] = (i==j) ? 1.0f : 0.0f; 

    // read the angle in degrees
    float angle_degree;
    scanf("%f", &angle_degree);

    // convert the angle to radian
    float angle_radiant = angle_degree * (float)M_PI / 180.0f;

    // set rotation around z‑axis
    float cos_ang = cos(angle_radiant); 
    float sin_ang = sin(angle_radiant); 
    a[0][0] = cos_ang; 
    a[0][1] = ‑sin_ang;
    a[1][0] = sin_ang;
    a[1][1] = cos_ang;  

    // read translation
    scanf("%f", &a[3][0]);
    scanf("%f", &a[3][1]);
}
void display (void)
{
    glMatrixMode(GL_MODELVIEW);
    glLoadMatrixf(&a[0][0]);

    // [...]
}

(by D.Kashyapismail akın çelikRabbid76)

참조 문서

  1. Displaying in openGL based on a matrix (CC BY‑SA 2.5/3.0/4.0)

#opengl-compat #opengl #C






관련 질문

레거시 OpenGL에서 독립적인 투명도 주문 (Order independent transparency in legacy OpenGL)

나는 검은 질감을 가지고있다. (I have a black texture)

시작/끝 각도 문제가 있는 OpenGL 타원 (OpenGL Ellipse with start/end angle issues)

행렬 기반의 OpenGL로 표시 (Displaying in openGL based on a matrix)

Python에서 OpenGL로 텍스처를 렌더링하는 것은 (Rendering a texture with OpenGL in Python is not)

OpenTK C#에서 객체 방향 가져오기 (Get orientation of object in OpenTK C#)







코멘트