gpt4 book ai didi

c# - 未在 cmd 提示符下运行时,OpenGL 未显示正确的输出

转载 作者:行者123 更新时间:2023-11-28 07:37:41 29 4
gpt4 key购买 nike

我正在尝试为一年级学生编写一个数学教育系统。我的目标是让所有交互都在一个窗口内运行。当我单击第一个窗口(+ 窗口)时,出现的只是黑色窗口,没有任何输出,但在命令提示符下它运行正常。

#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(即控制台输入/输出)与窗口系统事件处理混合在一起。这是问题的秘诀。 stdio 操作阻塞,直到它们完成。在从 cin 读取的情况下,进程将阻塞,等待来自控制台的输入,直到输入换行符。在此期间,不会处理任何窗口事件,也不会向 OpenGL 窗口绘制任何内容。

简而言之,您不能像以前那样将两者混为一谈。您可以很好地使用输出(coutcerr),如果没有连接控制台,它们将写入 NIL,但尝试读取某些内容会无限期地阻塞。您必须考虑 stdio 以外的其他方式来与事件交互程序中的用户进行交互。 IE。您必须在键盘回调中处理按键事件。

关于c# - 未在 cmd 提示符下运行时,OpenGL 未显示正确的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16420991/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com