我创造了一个地面,但我无法在openGL中看到地面。我该如何解决?

时间:2015-12-10 08:34:06

标签: c++ visual-studio opengl 3d

此代码没有错误,但在运行程序后,屏幕上看不到我创建的地面。我想在屏幕上看到整个地面。

如何修复代码?

#define A glVertex3f (0.5,  1.2, 0)
#define B glVertex3f (1, 2, 0)
#define C glVertex3f (2, 1.4, 0)
#define D glVertex3f ( 0.5,  0.5, -0.5)
#define E glVertex3f (-0.5,  0.5,  0.5)
#define F glVertex3f (-0.5, -0.5,  0.5)
#define G glVertex3f ( 0.5, -0.5,  0.5)
#define H glVertex3f ( 0.5,  0.5,  0.5)
#define _USE_MATH_DEFINES
#include <cmath>
#include <stdlib.h>
#include <math.h>
#include <glut.h>
float distance = 5.0;
int longitude = 0, latitude = 0, ainc = 5;
int lastx = -1, lasty = -1;
int x=0,z=0;
int y;
int data[17][21]=
{{14 ,25, 45 ,55 ,68 ,70 ,84 ,91 ,97, 101 ,105 ,105 ,105, 105 ,110 ,110,   110, 110 ,110, 110, 110},
{5, 18, 43, 62 ,73, 82, 88, 94, 99, 102 ,105, 105 ,105, 105, 110, 110 ,110 ,110 ,110, 110, 110},
{5, 18 ,38 ,56, 69, 77, 86, 94, 99, 103, 106, 105, 105, 105, 110, 110, 110, 110, 110, 110, 110},
{5 ,9 ,31, 48, 60, 71, 81, 87, 95, 101, 106, 105, 105, 105, 110, 110, 110, 110, 110, 110, 110},
{5, 5, 18, 37, 49, 56, 62, 81, 91, 94, 101, 105, 105, 105, 110, 110, 110 ,110 ,110, 110, 110},
{5, 5, 12, 23 ,34, 40, 53 ,66 ,77 ,82, 97, 103, 105, 105, 109, 110, 110, 110, 110, 115, 115},
{4 ,5 ,8 ,15, 20, 24, 35, 39, 40, 77, 92, 101, 104, 104 ,105, 110, 110, 110, 115, 115, 115},
{5, 7 ,22, 36, 46, 48, 48, 44 ,50, 58, 80, 96, 96, 97, 106, 110, 110, 115, 115, 115, 115},
{4, 15 ,31 ,46 ,61, 68, 69, 63, 53, 50, 67, 82, 84, 103, 108, 110, 110, 115, 115, 115, 115},
{4, 12, 31, 46, 64, 78, 82, 80, 69, 54, 73, 71, 92, 105, 108, 110, 110, 115, 115, 115, 115},
{6, 26 ,35 ,45, 63, 75, 84, 87, 84, 74 ,77, 80, 96, 103, 108, 110, 110, 110, 115, 115, 115},
{21, 30, 46, 57 ,64 ,76 ,85 ,92 ,92, 87 ,79 ,80 ,86 ,102, 106, 110, 105 ,110, 115, 115, 115},
{27, 40, 48 ,62 ,75 ,84 ,92, 96, 97 ,94 ,88 ,80 ,80 ,91, 104, 105, 105, 105, 110, 115, 115},
{33, 43, 55, 65, 75, 87, 96, 101, 101, 101, 97, 92, 80, 80, 98, 105, 105, 105, 105, 110, 115},
{45, 50, 58, 68, 80, 91, 99, 102, 105, 105, 105, 99, 90, 80, 80, 97, 105, 105, 105, 110, 100},
{50, 60, 65, 71, 84, 95, 101, 105, 105, 107, 107, 106, 102, 101, 92, 80, 98, 104, 105, 100, 100},
{60, 70, 76, 83, 88 ,96, 103, 106 ,107, 108 ,110, 109 ,108 ,108, 106, 101 ,90, 100, 100, 100, 100}};
void display (void)
{
float xc, yc, zc;
int type = GL_TRIANGLE_STRIP;   // or GL_LINE_LOOP
xc = distance * cos (latitude /180.0*M_PI) * cos (longitude/180.0*M_PI);
yc = distance * sin (latitude /180.0*M_PI);
zc = distance * cos (latitude /180.0*M_PI) * sin (longitude/180.0*M_PI);
glLoadIdentity ();
gluLookAt (xc, yc, zc,  0.0, 0.0, 0.0,  0.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
glColor3f (1.0, 1.0, 1.0);
glBegin (type);  
    for (int i = 0; i < 17; i++)
{

    z=z+20;
    for (int k = 0; k < 21; k=k+1)
    {
        y=data[i][k];
        if(y>0&&y<50)
            glColor3f(0.0, 1.0, 0.0);
        if(y>=50&&y<80)
            glColor3f(1.0, 1.0, 0.0);
        if(y>=80)
            glColor3f(1.0, 0.0, 0.0);


        glVertex3f(x, data[i][k], z);
        x=x+20;

        }

    }


  glEnd();  // front



  glutSwapBuffers ();
  }

 void keyboard (unsigned char key, int x, int y)
 {
 switch (key) {
 case 27: case 'q': case 'Q':
exit (EXIT_SUCCESS);
break;
 }
} 

void special (int key, int x, int y)
{
switch (key) {
case GLUT_KEY_UP:
distance *= 2;
break;
case GLUT_KEY_DOWN:
distance /= 2;
break;
}
glutPostRedisplay ();
}

void click (int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
lastx = x;
lasty = y;
}
}

void mouse (int x, int y)
{
if (x > lastx) {
longitude = (longitude + ainc) % 360;
} else if (x < lastx) {
longitude = (longitude - ainc) % 360;
}
if (y > lasty) {
latitude = (latitude + ainc) % 360;
 } else if (y < lasty) {
latitude = (latitude - ainc) % 360;
 }
 lastx = x;
 lasty = y;
 glutPostRedisplay ();
 }

 void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (65.0, (GLfloat) w / (GLfloat) h, 1.0, 20.0);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glTranslatef (0.0, 0.0, -5.0);
}


 int main (int argc, char *argv[])
 {
 glutInit (&argc, argv);
   glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
 glutCreateWindow (argv[0]);
 glMatrixMode (GL_PROJECTION);
 glLoadIdentity ();
 gluPerspective(50.0, 1.0, 3.0, 7.0);
 glMatrixMode (GL_MODELVIEW);
 glutDisplayFunc (display);
 glutKeyboardFunc (keyboard);
 glutSpecialFunc (special);
 glutMouseFunc (click);
 glutMotionFunc (mouse);
 glutReshapeFunc (reshape);
  glEnable (GL_DEPTH_TEST);
  glutMainLoop ();
 return EXIT_SUCCESS;
 }

1 个答案:

答案 0 :(得分:0)

我可以看到一些你可以查看的东西 1)在重构函数中调用glTranslatef(0.0,0.0,-5.0),之后在显示函数中调用loadIdentity(),它将重置矩阵。因此,翻译可能不适用于您正在绘制的对象 2)同时尝试将Z值向内更改Z轴。 3)更改投影远点和FOV角度,因为X值非常高。  我注意到你的x,y,z是全局值,并且不会在重塑时重置 希望它有所帮助。