그리드가 표시되지 않음 (모두 검은 색)

Nov 27 2020

어제 같은 질문으로 돌아가고 싶지 않았지만이 기능을 사용하여 그리드를 켜고 끌 수 있으려면 먼저 그리드가 실제로 작동하는지 알아야합니다. 새 프로젝트를 만들고 있습니다. 밤새도록 그리드를 표시하려고하지만 표시되지 않고 화면이 항상 검은 색이고 아무것도 표시되지 않습니다.

#include "include\freeglut.h"   // OpenGL toolkit - in the local shared folder
#include <iostream>

//set up some constants
#define X_CENTRE 0.0      /* centre point of square */
#define Y_CENTRE 0.0
#define LENGTH   1.0      /* lengths of sides of square */

GLfloat red = 1.0, green = 1.0, blue = 1.0;
int w;
int h;

/* reshape callback function
executed when window is moved or resized */
void reshape(int width, int height)
{
glViewport(0, 0, width, height);
/* uses orthographic (parallel) projection
use xmin = -1, xmax = 1
ymin = -1, ymax = 1
znear = -1, zfar = 1 - not relevant here (2D) */
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
}


/* display callback function
called whenever contents of window need to be re-displayed */
//this is the all important drawing method - all drawing code goes in here
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);   /* clear window */
//glColor3f(red, green, blue);        /* white drawing objects */
glColor3f(0.8, 0.8, 0.8);

GLint i;

glEnable(GL_LINE_STIPPLE); //Activates the line-style feature

glLineStipple(1, 0xAAAA);  // Plots a dashed polyline

glBegin(GL_LINES);
for (i = 2; i <= 9; i++)
{
    glVertex3f(i * 0.1 * w, 0.0, 0.0);
    glVertex3f(i * 0.1 * w, 0.9 * h, 0.0);
}

for (i = 1; i <= 9; i++)
{
    glVertex3f(0.1 * w, i * 0.1 * h, 0.0);
    glVertex3f(w, i * 0.1 * h, 0.0);
}
glEnd();
glDisable(GL_LINE_STIPPLE);

glFlush();     /* execute drawing commands in buffer */
}

/* graphics initialisation */
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);   /* window will be cleared to black */
}

//rename this to main(...) and change example 2 to run this main function
int main(int argc, char** argv)
{
/* window management code ... */
/* initialises GLUT and processes any command line arguments */
glutInit(&argc, argv);
/* use single-buffered window and RGBA colour model */
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
/* window width = 400 pixels, height = 400 pixels */
glutInitWindowSize(400, 400);
/* window upper left corner at (100, 100) */
glutInitWindowPosition(100, 100);
/* creates an OpenGL window with command argument in its title bar */
glutCreateWindow("Example 1");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);

glutMainLoop();
return 0;
}

답변

2 Rabbid76 Nov 27 2020 at 12:34

변수 whre가 초기화되지 않았습니다. 1로 변수를 초기화합니다.

int w = 1;
int h = 1;

그러나 창 공간에서 정점 좌표를 설정하려면 직교 투영을 변경해야합니다. 투영 행렬은 뷰포트에 투영되는 관찰자 (뷰어)에 대한 영역 (볼륨)을 정의합니다. 직교 투영에서이 영역 (볼륨)은 뷰어 위치까지 6 개의 거리 (왼쪽, 오른쪽, 아래쪽, 위쪽, 근거리 및 원거리)로 정의됩니다.

glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

glOrtho(0.0, (float)w, (float)h, 0.0, -1.0, 1.0);

기능 reshape:

void reshape(int width, int height)
{
    glViewport(0, 0, width, height);
    /* uses orthographic (parallel) projection
    use xmin = -1, xmax = 1
    ymin = -1, ymax = 1
    znear = -1, zfar = 1 - not relevant here (2D) */    #
    
    w = width;
    h = height;
    glMatrixMode(GL_PROJECTION);
    glOrtho(0.0, (float)w, (float)h, 0.0, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
}