我正在open gl中创建一个井字游戏,我还没有完成,但是每当我运行代码时,我看到的都是黑屏,看不到绘制的线条或X?
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <GLut/glut.h>
using namespace std;
int matrix[ 3 ][ 3 ]; // board for the gameplay of tic tac toe we are using a 3x3 matrix board
int turn; // indicates whose turn it is going to be
bool gameover; // is the game over would you like to end the game
int result; // the ending result of the game
void begin()
{
turn = 1;
for( int i = 0; i < 3; i++ ) // setting up the board and clearing the matrix we start at 1,
// basically clearing our matrix
{
for( int j = 0; j < 3; j++ )
matrix[ i ][ j ] = 0;
}
}
void drawxo() // setting up our X and O if it is 1 then we want to use x and if it is 2 then 0
{
for( int i = 0; i <= 2; i++ )
{
for( int j = 0; j <= 2; j++ )
{
if( matrix[ i ][ j ] == 1 ) // if it is 1 then draw x
{
glBegin( GL_LINES );
glVertex2f( 50 + j * 100 - 25, 100 + i * 100 - 25 );
glVertex2f( 50 + j * 100 + 25, 100 + i * 100 + 25 );
glVertex2f( 50 + j * 100 - 25, 100 + i * 100 + 25 );
glVertex2f( 50 + j * 100 + 25, 100 + i * 100 - 25 );
glEnd();
}
else if( matrix[ i ][ j ] == 2 ) // if it is 2 then draw o
{
glBegin( GL_LINE_LOOP );
glVertex2f( 50 + j * 100 - 25, 100 + i * 100 - 25 );
glVertex2f( 50 + j * 100 - 25, 100 + i * 100 + 25 );
glVertex2f( 50 + j * 100 + 25, 100 + i * 100 + 25 );
glVertex2f( 50 + j * 100 + 25, 100 + i * 100 - 25 );
glEnd();
}
}
}
}
void DrawString( void* font, const char s[], float x, float y )
{
unsigned int i;
glRasterPos2f( x, y );
for( i = 0; i < strlen( s ); i++ )
{
glutBitmapCharacter( font, s[ i ] );
}
}
void drawLines()
{
glBegin( GL_LINES );
glColor3f( 0, 0, 0 );
// 2 vertical lines
glVertex2f( 100, 50 );
glVertex2f( 100, 340 );
glVertex2f( 200, 340 );
glVertex2f( 200, 50 );
// 2 horizontal lines
glVertex2f( 0, 150 );
glVertex2f( 300, 150 );
glVertex2f( 0, 250 );
glVertex2f( 300, 250 );
glEnd();
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT );
glClearColor( 1, 1, 1, 1 );
glColor3f( 0, 0, 0 );
if( turn == 1 )
DrawString( GLUT_BITMAP_HELVETICA_18, "Player1's turn", 100, 30 );
drawxo();
drawLines();
glutSwapBuffers();
}
void KeyPress( unsigned char key, int x, int y )
{
switch( key )
{
case 27: // the escape key to exit the program
exit( 0 );
break;
case 'y':
if( gameover = true )
{
gameover = false;
begin();
}
break;
case 'n':
if( gameover = true )
{
exit( 0 );
}
break;
}
}
int main( int argc, char** argv )
{
begin();
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
glutInitWindowPosition( 550, 200 );
glutInitWindowSize( 300, 350 );
glutCreateWindow( "Tic tac toe" );
glutDisplayFunc( display );
glutKeyboardFunc( KeyPress );
glutIdleFunc( display );
glutMainLoop();
}
答案 0 :(得分:2)
使用默认的标识投影和模型视图矩阵会导致(±1,±1,±1)修剪体积,所有几何都在外部。要么将几何形状固定为适合片段体积,要么调整片段体积以包含几何形状。
由于您所有的绘图代码都假设是300x350倒Y坐标系,因此您可以通过glOrtho()
设置适当的GL_PROJECTION
矩阵:
void display()
{
glClearColor( 1, 1, 1, 1 );
glClear( GL_COLOR_BUFFER_BIT );
// new
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, 300, 0, 350, -1, 1 );
// new
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
...
屏幕截图:
一起:
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <GL/glut.h>
using namespace std;
int matrix[ 3 ][ 3 ]; // board for the gameplay of tic tac toe we are using a 3x3 matrix board
int turn; // indicates whose turn it is going to be
bool gameover; // is the game over would you like to end the game
int result; // the ending result of the game
void begin()
{
turn = 1;
for( int i = 0; i < 3; i++ ) // setting up the board and clearing the matrix we start at 1,
// basically clearing our matrix
{
for( int j = 0; j < 3; j++ )
matrix[ i ][ j ] = 0;
}
}
void drawxo() // setting up our X and O if it is 1 then we want to use x and if it is 2 then 0
{
for( int i = 0; i <= 2; i++ )
{
for( int j = 0; j <= 2; j++ )
{
if( matrix[ i ][ j ] == 1 ) // if it is 1 then draw x
{
glBegin( GL_LINES );
glVertex2f( 50 + j * 100 - 25, 100 + i * 100 - 25 );
glVertex2f( 50 + j * 100 + 25, 100 + i * 100 + 25 );
glVertex2f( 50 + j * 100 - 25, 100 + i * 100 + 25 );
glVertex2f( 50 + j * 100 + 25, 100 + i * 100 - 25 );
glEnd();
}
else if( matrix[ i ][ j ] == 2 ) // if it is 2 then draw o
{
glBegin( GL_LINE_LOOP );
glVertex2f( 50 + j * 100 - 25, 100 + i * 100 - 25 );
glVertex2f( 50 + j * 100 - 25, 100 + i * 100 + 25 );
glVertex2f( 50 + j * 100 + 25, 100 + i * 100 + 25 );
glVertex2f( 50 + j * 100 + 25, 100 + i * 100 - 25 );
glEnd();
}
}
}
}
void DrawString( void* font, const char s[], float x, float y )
{
unsigned int i;
glRasterPos2f( x, y );
for( i = 0; i < strlen( s ); i++ )
{
glutBitmapCharacter( font, s[ i ] );
}
}
void drawLines()
{
glBegin( GL_LINES );
glColor3f( 0, 0, 0 );
// 2 vertical lines
glVertex2f( 100, 50 );
glVertex2f( 100, 340 );
glVertex2f( 200, 340 );
glVertex2f( 200, 50 );
// 2 horizontal lines
glVertex2f( 0, 150 );
glVertex2f( 300, 150 );
glVertex2f( 0, 250 );
glVertex2f( 300, 250 );
glEnd();
}
void display()
{
glClearColor( 1, 1, 1, 1 );
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, 300, 0, 350, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glColor3f( 0, 0, 0 );
if( turn == 1 )
DrawString( GLUT_BITMAP_HELVETICA_18, "Player1's turn", 100, 30 );
drawxo();
drawLines();
glutSwapBuffers();
}
void KeyPress( unsigned char key, int x, int y )
{
switch( key )
{
case 27: // the escape key to exit the program
exit( 0 );
break;
case 'y':
if( gameover = true )
{
gameover = false;
begin();
}
break;
case 'n':
if( gameover = true )
{
exit( 0 );
}
break;
}
}
int main( int argc, char** argv )
{
begin();
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
glutInitWindowSize( 300, 350 );
glutCreateWindow( "Tic tac toe" );
glutDisplayFunc( display );
glutKeyboardFunc( KeyPress );
glutIdleFunc( display );
glutMainLoop();
}