【问题标题】:OpenGL not drawing lines/text?OpenGL不画线/文本?
【发布时间】:2019-02-18 16:57:37
【问题描述】:

我正在 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();
}

【问题讨论】:

    标签: c++ opengl glut


    【解决方案1】:

    使用默认的恒等投影和模型视图矩阵会产生 (±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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多