简体   繁体   English

当不在cmd提示符下运行时OpenGL无法显示正确的输出

[英]OpenGL not displaying the correct output when not running in cmd prompt

I am trying to write a math education system for year 1 student. 我正在尝试为一年级学生编写数学教育系统。 My goal is to have all the interaction run within a window. 我的目标是使所有交互都在一个窗口中运行。 When I click on the first window, (+ window), all that appears is black window without any output, but in command prompt it runs fine. 当我单击第一个窗口(+窗口)时,所有显示的是黑色窗口,没有任何输出,但是在命令提示符下运行正常。

#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <glut.h>
#include <time.h>

using namespace std;
GLfloat xrot;
GLfloat yrot;
bool mouseDown = false;
bool createWindow = false;

int m=0;
int n=0;
int num1=0;
int num2=0;
int counter=0;

//-------------------------------------------------------------------


void handleKeypress(unsigned char key,int x,int y)
{
    switch(key)
    {
        case 27:
        exit(0);
    }
}

void handleResize(int w,int h)

{

    glViewport(0,0,w,h);

    glMatrixMode(GL_PROJECTION);

    gluPerspective(45.0,(double)w/(double)h,1.0,200);

}

void idle()
{
if (!mouseDown)
{
xrot+= 0.3f;
yrot+= 0.4f;
}

glutPostRedisplay();
}

void drawScene1()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

    glBegin(GL_QUADS);
    glColor3f(1.0,0.0,0.0);
    glVertex3f(-1.0f,5.0f,-20.0f);
    glVertex3f(-1.0f,-5.0f,-20.0f);
    glVertex3f(1.0f,-5.0f,-20.0f);
    glVertex3f(1.0f,5.0f,-20.0f);
    glEnd();

    glRotatef(90.0,0.0f,0.0f,1.0f);

    glBegin(GL_QUADS);
    glColor3f(1.0,0.0,0.0);
    glVertex3f(-1.0f,5.0f,-20.0f);
    glVertex3f(-1.0f,-5.0f,-20.0f);
    glVertex3f(1.0f,-5.0f,-20.0f);
    glVertex3f(1.0f,5.0f,-20.0f);
    glutSwapBuffers();


}

void drawScene2()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

    glRotatef(90.0,0.0f,0.0f,1.0f);

    glBegin(GL_QUADS);
    glColor3f(0.0,0.0,1.0);
    glVertex3f(-1.0f,5.0f,-20.0f);
    glVertex3f(-1.0f,-5.0f,-20.0f);
    glVertex3f(1.0f,-5.0f,-20.0f);
    glVertex3f(1.0f,5.0f,-20.0f);
    glEnd();

    glutSwapBuffers();
}

void myInit(void)
{
glEnable(GL_DEPTH_TEST);
glClearColor(1.0, 1.0, 1.0, 0.0); // set the background to white
glColor3f(0.0, 0.0, 0.0);
// set the drawing color to black

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1000.0,1000.0,-1000.0,1000.0,-100.0,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}
//------------------------------------------------------

void triangle()
{
    glBegin(GL_TRIANGLES);
    glColor3f(1.0f,0.0f,0.0f);
    glVertex3f(0.0f,100.0f,0.0f);
    glVertex3f(100.0f,-100.0f,0.0f);
    glVertex3f(-100.0f,-100.0f,0.0f);
    glEnd();
}

void minus()
{
    glPushMatrix();
    glBegin(GL_QUADS);
    glColor3f(0.0f,1.0f,0.0f);
    glVertex3f(-200.0f,50.0f,0.0f);
    glVertex3f(-200.0f,-50.0f,0.0f);
    glVertex3f(200.0f,-50.0f,0.0f);
    glVertex3f(200.0f,50.0f,0.0f);
    glEnd();
    glPopMatrix();
}

void myDisplay(void)
{

// Clear Color and Depth Buffers
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Reset transformations
    glLoadIdentity();

    // Set the camera
    gluLookAt(0, 0, 3, 0 , 0 , 0 , 0.0f,1.0f,0.0f);

    //Generate random number 
    srand (time(NULL));
    num1 = rand() % 10 +1; 
    num2 = rand() % 10 +1;
    if(num1 < num2){
    int temp = num1;
    num1 = num2;
    num2 = temp;
    }

    //rows and column
    m = 2;   //row
    n = 5;   //column

    counter=0; //make sure it zero

    //First Number to be draw
    glPushMatrix(); //set as origin
    glTranslatef( -700.0,700, 0 ); //translate
    //draw
    for(int i = 0; i < m; i++){ //row
        for(int j=0; j < n; j++) { //column
            if(counter>=num1) break; //a=how many triangle need to draw; s=already draw how many triangle; 
            glPushMatrix(); //set transformation
            glTranslatef( i*200.0 ,j*-200.0, 0 ); //translate
            triangle(); //draw triangle
            glPopMatrix(); //reset transformation
            counter++; //counter s
        }
    }
    glPopMatrix(); //reset transformation

    //create plus sign
    glPushMatrix(); //set transformation or object position/orientation/scale for reset
    glTranslatef( 0.0,400, 0 );
    minus();
    glPopMatrix(); //reset transformation to origin

    counter=0; //reset counter
    //Second number to be draw
    glPushMatrix();
    glTranslatef( 500.0,700, 0 );
    for(int i = 0; i < m; i++){
        for(int j=0; j < n; j++) {
            if(counter>=num2) break;
            glPushMatrix();
            glTranslatef( i*200.0 ,j*-200.0, 0 );
            triangle();
            glPopMatrix();
            counter++;
        }
    }
    glPopMatrix();

    //Control
    glutSwapBuffers();//take the drawing to the screen

    //check answer
    /*cout<<num1<<endl; //debug
    cout<<num2<<endl;*/

    int check=0;
    int answer=0;
    cout << "Answer ???"<<endl;
    cin >> answer;
    while(true){
        check=num1-num2;
        if(answer==check){
            cout << "CORRECT!!!"<<endl;
            break;
        } else{
            cout << "TRY AGAIN!!!";
            cout << "Answer ???"<<endl;
            cin >> answer;
        }
    }
}



void mouse (int button, int state, int x, int y){
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN){
    myInit();
    triangle();
    minus();
    myDisplay();
    }
}


int main(int argc,char** argv)

{

    glutInit(&argc,argv);

    myInit();

    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);

    glutInitWindowSize(500,500);


    //window1=

     glutCreateWindow("First window");

    glutDisplayFunc(drawScene1);

    glutKeyboardFunc(handleKeypress);

    glutReshapeFunc(handleResize);
    glutMouseFunc(mouse);

    //create the second  window

    //window2 =

    glutCreateWindow("Second Window");

    //define a window position for second window

    glutPositionWindow(540,40);

    // register callbacks for second window, which is now current

    glutReshapeFunc(handleResize);

    glutDisplayFunc(drawScene2);

    glutKeyboardFunc(handleKeypress);
    glutMouseFunc(mouse);

    glutMainLoop();





    return 0;


}

You're mixing stdio (ie console input/output) with windowing system event processing. 您正在将stdio (即控制台输入/输出)与窗口系统事件处理混合在一起。 This is a recipe for problems. 这是解决问题的良方。 stdio operations block until they're finished. stdio操作将阻塞,直到完成。 In the case of reading from cin the process will block, waiting for input from the console until a linefeed is entered. 在从cin读取的情况下,该过程将阻塞,等待来自控制台的输入,直到输入换行符为止。 During this time no windowing events are processed and nothing drawn to the OpenGL window. 在此期间,不会处理任何窗口事件,也不会在OpenGL窗口中绘制任何内容。

In short, you can't mix the two in the way you did. 简而言之,您不能像以前那样将两者混为一谈。 You can use output ( cout and cerr ) just fine, they will write to NIL if theres no console attached, but trying to read something will block indefinitely. 您可以很好地使用输出( coutcerr ),如果没有附加控制台,它们将写入NIL,但是尝试读取内容将无限期地阻塞。 You'll have to think of other ways than stdio to interact with your user in event-interactive programs. 在事件交互程序中,除了stdio外 ,您还必须考虑其他方法。 Ie you've to process key events in the keyboard callback. 也就是说,您必须在键盘回调中处理按键事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM