我想在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)的調用? –