OpenGL没有绘制线条/文字?

问题描述 投票:-5回答:1

我正在开放创建一个tic tac toe游戏我尚未完成但是每当我运行我的代码时,我看到的都是黑屏我看不到我绘制的线条或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();
}
c++ opengl glut
1个回答
2
投票

使用默认的标识投影和模型视图矩阵会产生(±1,±1,±1)剪切体积,所有几何体都在外面。修复几何体以适合剪辑体积或调整剪辑体积以包含几何体。

由于您的所有绘图代码似乎都假定为300x350倒Y坐标系,您可以通过GL_PROJECTION设置适当的glOrtho()矩阵:

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();
    ...

截图:

screenshot

全部一起:

#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();
}
© www.soinside.com 2019 - 2024. All rights reserved.