首页
登录 | 注册

glut 子窗口(嵌套、并列)

#include "display.cpp"
GLuint mainw,subw1;

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(700, 700);
	glutInitWindowPosition(300, 50);

	mainw = glutCreateWindow("Test");
	initScene();
	glutDisplayFunc(glutDisplay);
	glutReshapeFunc(glutReshape);
	glutIdleFunc(glutIdle);

	subw1 = glutCreateSubWindow(mainw,490,490,200,200);//<-glut子窗口
	initScene1();
	glutDisplayFunc(glutDisplay);
	glutReshapeFunc(glutReshape);
	glutIdleFunc(glutIdle);
        
	glutMainLoop();
	return 0;
}
Display.cpp
#include <GL/glut.h>
#include <math.h>

void initScene()
{
	glClearColor(0.0,0.0,0.0,0.0);
	static const GLfloat light_model_ambient[] = {0.3f, 0.3f, 0.3f, 1.0f};
	static const GLfloat light0_diffuse[] = {0.9f, 0.9f, 0.9f, 0.9f};   
	static const GLfloat light0_direction[] = {0.0f, -0.4f, 1.0f, 0.0f};    
    
	/* Enable depth buffering for hidden surface removal. */
	glDepthFunc(GL_LEQUAL);
	glEnable(GL_DEPTH_TEST);
    
	/* Cull back faces. */
	glCullFace(GL_BACK);
	glEnable(GL_CULL_FACE);
    
	/* Other misc features. */
	glEnable(GL_LIGHTING);
	glEnable(GL_NORMALIZE);
	glShadeModel(GL_SMOOTH);
    
	glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
	glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);    
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_model_ambient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
	glLightfv(GL_LIGHT0, GL_POSITION, light0_direction);
	glEnable(GL_LIGHT0); 
}

void initScene1()
{
	glClearColor(0.2,0.2,0.2,0.0);
	static const GLfloat light_model_ambient[] = {0.3f, 0.3f, 0.3f, 1.0f};
	static const GLfloat light0_diffuse[] = {0.9f, 0.9f, 0.9f, 0.9f};   
	static const GLfloat light0_direction[] = {0.0f, -0.4f, 1.0f, 0.0f};    
    
	/* Enable depth buffering for hidden surface removal. */
	glDepthFunc(GL_LEQUAL);
	glEnable(GL_DEPTH_TEST);
    
	/* Cull back faces. */
	glCullFace(GL_BACK);
	glEnable(GL_CULL_FACE);
    
	/* Other misc features. */
	glEnable(GL_LIGHTING);
	glEnable(GL_NORMALIZE);
	glShadeModel(GL_SMOOTH);
    
	glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
	glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);    
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_model_ambient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
	glLightfv(GL_LIGHT0, GL_POSITION, light0_direction);
	glEnable(GL_LIGHT0); 
}

void glutDisplay()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //<--------------

	glutSolidSphere(0.5, 32, 32);

	glutSwapBuffers();
}

void glutReshape(int width, int height)
{
	static const double kPI = 3.1415926535897932384626433832795;
	static const double kFovY = 40;

	double nearDist, farDist, aspect;

	glViewport(0, 0, width, height);

	/* Compute the viewing parameters based on a fixed fov and viewing
	* a canonical box centered at the origin */

	nearDist = 1.0 / tan((kFovY / 2.0) * kPI / 180.0);
	farDist = nearDist + 2.0;
	aspect = (double) width / height;
   
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(kFovY, aspect, nearDist, farDist);

    /* Place the camera down the Z axis looking at the origin */
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();            
	gluLookAt(0, 0, nearDist + 1.0,
              0, 0, 0,
              0, 1, 0);
}

void glutIdle()
{
	glutPostRedisplay();
}

glut 子窗口(嵌套、并列)

窗口并列

#include "display.cpp"

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

	glutInitWindowSize(500, 500);
	glutInitWindowPosition(150, 50);

	glutCreateWindow("Test");
	initScene();
	glutDisplayFunc(glutDisplay);
	glutReshapeFunc(glutReshape);
	glutIdleFunc(glutIdle);

	glutInitWindowPosition(650, 50);
	glutCreateWindow("test1");//<-第二个glut窗口
	initScene1();
	glutDisplayFunc(glutDisplay);
	glutReshapeFunc(glutReshape);
	glutIdleFunc(glutIdle);
        
	glutMainLoop();

	return 0;




2020 jeepxie.net webmaster#jeepxie.net
10 q. 0.009 s.
京ICP备10005923号