在OpenGL的3d图画

时间:2013-09-23 17:44:14

标签: opengl

我正在尝试在OpenGL中绘制棋盘。我可以完全按照自己的意愿绘制游戏板的方块。但我也希望在游戏板的周边添加一个小型边界。不知何故,我的外围比我想要的更大。实际上,边框的每个边缘都是整个游戏板本身的精确宽度。

我的方法是画一个中性的灰色矩形来代表木板的整个“板”,这些木板将被切割成板。然后,在这个平板的内部,我放置了64个游戏正方形,这些正方形应该正好居中,并且只需要稍微小一点的空间就像平板一样。我对更好的方式持开放态度,但请记住,我不是很聪明。

编辑:在下面的图像中,所有灰色区域应该是单个正方形大小的1/2。但正如您所看到的,每个边缘都是整个游戏板的大小。显然,我不理解某事。

enter image description here

这是我写的显示功能。为什么我的“板坯”太大了?

void display()
{
    // Clear the image
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Reset any previous transformations
    glLoadIdentity();

    // define the slab
    float square_edge = 8;
    float border = 4;
    float slab_thickness = 2;
    float slab_corner = 4*square_edge+border;

    // Set the view angle
    glRotated(ph,1,0,0);
    glRotated(th,0,1,0);
    glRotated(zh,0,0,1);

    float darkSquare[3] = {0,0,1};
    float lightSquare[3] = {1,1,1};

    // Set the viewing matrix
    glOrtho(-slab_corner, slab_corner, slab_corner, -slab_corner, -slab_corner, slab_corner);

    GLfloat board_vertices[8][3] = {
        {-slab_corner, slab_corner, 0},
        {-slab_corner, -slab_corner, 0},
        {slab_corner, -slab_corner, 0},
        {slab_corner, slab_corner, 0},
        {-slab_corner, slab_corner, slab_thickness},
        {-slab_corner, -slab_corner, slab_thickness},
        {slab_corner, -slab_corner, slab_thickness},
        {slab_corner, slab_corner, slab_thickness}
    };

    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_INT, 0, board_vertices);

    // this defines each of the six faces in counter clockwise vertex order
    GLubyte slabIndices[] = {0,3,2,1,2,3,7,6,0,4,7,3,1,2,6,5,4,5,6,7,0,1,5,4};

    glColor3f(0.3,0.3,0.3); //upper left square is always light
    glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, slabIndices);

    // draw the individual squares on top and centered inside of the slab
    for(int x = -4; x < 4; x++) {
        for(int y = -4; y < 4; y++) {
            //set the color of the square
            if ( (x+y)%2 ) glColor3fv(darkSquare);
            else glColor3fv(lightSquare);

            glBegin(GL_QUADS);
                glVertex2i(x*square_edge, y*square_edge);
                glVertex2i(x*square_edge+square_edge, y*square_edge);
                glVertex2i(x*square_edge+square_edge, y*square_edge+square_edge);
                glVertex2i(x*square_edge, y*square_edge+square_edge);
            glEnd();
        }
    }

    glFlush();
    glutSwapBuffers();
}

1 个答案:

答案 0 :(得分:1)

glVertexPointer(3, GL_INT, 0, board_vertices);

指定board_vertices包含整数,但它实际上是GLfloat类型。这可能是问题吗?