本文介绍了OpenGL子窗口问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我是约阿尼纳大学的学生,并且有一个OpenGL项目.
我必须创建一个类似tetris的游戏,并切丁要使用子窗口来制作它.
我成功创建了子窗口,并且可以成功地在subwindows方法(glutDisplayFunc())中打印文本,但是当我尝试在subwindows方法之外打印文本时,它是不可能的!!!!看一下主要文本和其他文本!!!!!!

有人可以帮我吗?
这是我的代码:

Hi to all
I am student in university of ioannina and i have a project in OpenGL.
I have to create a game like tetris and i diceded to make it with subwindows.
I create subwindows succesfully and i can print a text in subwindows method(glutDisplayFunc()) succesfully, but when i try to print a text outside of subwindows method its IMPOSSIBLE!!!!! TAKE A LOOK AT THE TEXT IN MAIN AND THE OTHER TEXT!!!!!!

Can someone help me???
Here is my code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cmath>
#include <sstream>

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#define WIDTH 302
#define HEIGHT 522

using namespace std;

int mainWin, subWin1, subWin2;

void processKeyboardKeys(unsigned char key, int x, int y)
{
	switch(key)
	{
		case 27:
			glutSetWindow(mainWin);
			exit(0);
		break;
	}
}

void printString(string s, int length, double x, double y, int subwintoprint)
{
	glutSetWindow(subwintoprint);
	glRasterPos2i(x,y);
	for(int i=0; i<length; i++)
	{
		glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, s[i]);
	}
	glFlush();
}

void mainDisplay(void)
{
	glutSetWindow(mainWin);
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);

	glColor3f(1.0, 1.0, 1.0);
	glBegin(GL_LINE_LOOP);
		glVertex2f(0, 61);
		glVertex2f(WIDTH, 61);
		glVertex2f(0, 62);
		glVertex2f(WIDTH, 62);
	glEnd();
	glBegin(GL_LINE_LOOP);
		glVertex2f(WIDTH-61, 63);
		glVertex2f(WIDTH-61, HEIGHT);
		glVertex2f(WIDTH-60, 63);
		glVertex2f(WIDTH-60, HEIGHT);
	glEnd();

	string score = "mainWin";
	glColor3f(1.0, 1.0, 1.0);
	printString(score.data(), score.size(), 40, 250, mainWin);


	glutKeyboardFunc(processKeyboardKeys); // Main window will close with ESC(escape button) character

	glFlush();
}

void subWin1Display(void)
{
	glutSetWindow(subWin1);
	glClearColor(0.0, 0.0, 1.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();
	gluOrtho2D(0, WIDTH, 0, 60);

	string score = "subWin1";
	glColor3f(1.0, 1.0, 1.0);
	printString(score.data(), score.size(), 40, 25, subWin1);

	glFlush();
}

void subWin2Display(void)
{
	glutSetWindow(subWin2);
	glClearColor(0.0, 1.0, 1.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();
	gluOrtho2D(0, 60, 0, HEIGHT-62);

	string f = "sub";
	glColor3f(0.0, 0.0, 0.0);
	printString(f.data(), f.size(), 0, 200, subWin2);

	string g = "Win2";
	glColor3f(0.0, 0.0, 0.0);
	printString(g.data(), g.size(), 0, 180, subWin2);

	glFlush();
}

void SubWindows(void)
{
	//Sub window 1
	subWin1 = glutCreateSubWindow(mainWin, 0, HEIGHT-60, WIDTH, 60);
	glutDisplayFunc(subWin1Display);

	//Sub window 2
	subWin2 = glutCreateSubWindow(mainWin, WIDTH-60, 0, 60, (HEIGHT-63));
	glutDisplayFunc(subWin2Display);
}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

	glutInitWindowPosition (300, 300);
	glutInitWindowSize (WIDTH, HEIGHT);

	// Main window
	mainWin = glutCreateWindow ("BRAXAPSA");
	gluOrtho2D(0, WIDTH, 0, HEIGHT);
	glutDisplayFunc(mainDisplay);


	SubWindows();

        //THE FOLLOWING TEXT WILL NOT BE PRINTED IN mainWin
	string g = "HELP ME!!!";
	glColor3f(1.0, 1.0, 1.0);
	printString(g.data(), g.size(), 0, 180, mainWin);

	glutMainLoop();

	return 0;
}

推荐答案

void subWin2Display(void)
{
        int OldWindow = glutGetWindow();
	glutSetWindow(subWin2);
	glClearColor(0.0, 1.0, 1.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glLoadIdentity();
	gluOrtho2D(0, 60, 0, HEIGHT-62);

	string f = "sub";
	glColor3f(0.0, 0.0, 0.0);
	printString(f.data(), f.size(), 0, 200, subWin2);

	string g = "Win2";
	glColor3f(0.0, 0.0, 0.0);
	printString(g.data(), g.size(), 0, 180, subWin2);

	glFlush();
	glutSetWindow(OldWindow);

}



注意:您还需要更新void subWin1Display(void).



Note: You''ll need to update void subWin1Display(void) also.


void mainDisplay(void)
{
.....
// draw the string with strText global variable
    glColor3f(1.0, 1.0, 1.0);
    printString(strText.data(), strText.size(), 0, 180, mainWin);
}





int main(int argc, char *argv[])
{
....// change the text and request redraw..
strText = "HELP ME!!!"
glutPostRedisplay();
}



这篇关于OpenGL子窗口问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:56