OpenGL不显示正确的输出时,没有运行在cmd提示符
本文关键字:运行 提示符 cmd 显示 输出 OpenGL | 更新日期: 2023-09-27 18:12:41
我正试图为一年级学生写一个数学教育系统。我的目标是让所有的交互在一个窗口内运行。当我点击第一个窗口(+ window)时,所有出现的都是黑色窗口,没有任何输出,但在命令提示符中运行良好。
#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;
}
您正在混合stdio(即控制台输入/输出)与窗口系统事件处理。这就是问题的根源。工作室操作阻塞直到它们完成。在从读取的情况下,进程将阻塞,等待控制台的输入,直到输入换行。在此期间,不会处理任何窗口事件,也不会向OpenGL窗口绘制任何内容。
简而言之,你不能像以前那样把两者混在一起。你可以使用输出(cout和cerr)很好,如果没有附加控制台,它们将写入NIL,但试图读取某些东西会无限期地阻塞。在事件交互程序中,除了stdio之外,您必须考虑其他与用户交互的方法。例如,你必须在键盘回调中处理键事件。