网格未显示(全黑) [英] grid not showing (it is all black)

查看:91
本文介绍了网格未显示(全黑)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想从昨天回到同一问题,但是在我能够使用该功能打开和关闭网格之前,我首先需要知道我的网格是否确实在工作,整夜进行新项目以尝试显示网格,但没有显示,屏幕始终是黑色的,根本什么也没有.

I didn't want to go back to the same question from yesterday, however before I am able to use the function to turn on and off the grid, I first need to know if my grid is actually working, I have been making new projects all night trying to display the grid but it isn't showing, the screen is always black and nothing is there at all.

#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;
}

推荐答案

变量 w h a未初始化.通过1初始化变量:

The variables w and h a re not initialized. Initialize the variables by 1:

int w = 1;
int h = 1;


但是,如果要在窗口空间中设置顶点坐标,则必须更改正投影.投影矩阵定义相对于被投影到视口的观察者(观察者)的区域(体积).在正交投影中,此区域(体积)由到观看者位置的6个距离(左,右,下,上,近和远)定义.


However, if you want to set the vertex coordinates in window space, you have to change the orthographic projection. The projection matrix defines the area (volume) with respect to the observer (viewer) which is projected onto the viewport. At orthographic projection, this area (volume) is defined by 6 distances (left, right, bottom, top, near and far) to the viewer's position.

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);

功能重塑:

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);
}

这篇关于网格未显示(全黑)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆