我想在OpenGL中画一个棋盘。我可以完全按照我的需要绘制游戏板的正方形。但我也想在游戏板的周围添加一个小寄宿生。不知何故,我的外围比我想要的要大得多。实际上,边框的每个边缘都是整个游戏棋盘本身的确切宽度。OpenGL中的3D绘图
我的方法是绘制一个中性的灰色矩形来表示将切割成木板的整个“木板”。然后,在这块板的内部,我放置了64个游戏方块,这些方块应该完全居中,并且占据板块所需的较小的二维空间。我乐于接受更好的方式,但请记住,我不是很聪明。
编辑:在下面的图片中,所有灰色区域应该是单个正方形大小的1/2左右。但正如你所看到的,每个边缘都是整个游戏板的大小。显然我不理解某些东西。
下面是我写的显示功能。为什么我的“平板”太大?
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();
}
您可以发布您当前结果的屏幕截图吗? –
好主意。完成。 – Alex
我不认为你的程序中的任何地方都有对glClearColor(0.3,0.3,0.3)的调用? –