OpenGL子窗口问题 [英] OpenGL subwindows problem

查看:81
本文介绍了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;
}

推荐答案

在无效的subWin2Display(void)中,您将窗口设置为subWin2,并且永不更改.
首先使用glutGetWindow获取原始窗口,然后在函数退出之前使用glutSetWindow还原该窗口.

对调用glutSetWindow的任何函数都执行此操作.

In void subWin2Display(void) you''re setting your window to subWin2 and never changing it back.

Use glutGetWindow to get the original window first, then restore that with glutSetWindow before the function exits.

Do this for any function that calls glutSetWindow.

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.


在GLUT中,控制功能负责绘制框架.其他线程无法使用OpenGL命令绘制.您可以创建一个全局变量来保存数据.在mainDisplay中,可以使用OpenGL命令显示它.当您想要更改字符串时,请从其他函数更新该字符串,然后调用glutPostRedisplay()以再次重画该框架.现在,整个框架将以新文本重绘.请注意在两个线程之间同步字符串的访问.

In GLUT, control function is responsible to draw the frame. Other threads can''t draw with OpenGL commands. You can create a global variable to hold the data. In mainDisplay you can display it with OpenGL commands. When you want change the string, update the string from other function and call glutPostRedisplay() to redraw the frame again. Now entire frame will be redrawn with new text. Please take care to synchronize the access of string between two the threads.

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.但是看着你,我觉得我可以尝试一下.


如果您在mainDisplay()函数中编写以下代码段,则可能有效.
//以下文本将不会在mainWin中打印
字符串g =帮助我!";
glColor3f(1.0,1.0,1.0);
printString(g.data(),g.size(),0,180,mainWin);
I am not that good at OpenGL. But looking at your I felt I give a try.


If you write the below snippet in mainDisplay() function then It might work.
//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);


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆