1 年生向けの数学教育システムを作成しようとしています。私の目標は、すべての対話をウィンドウ内で実行することです。最初のウィンドウ (+ ウィンドウ) をクリックすると、出力のない黒いウィンドウが表示されますが、コマンド プロンプトでは問題なく動作します。
#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;
}