GLUTBridge¶
This is a very simple c++ source code to show hoa to use NUI inside a GLUT application thanks to nuiGLUTBridge.h. You can find a working project in the examples/testGLUTintegration folder.
1 /*
2 NUI3 demonstration sample - C++ cross-platform GUI framework for OpenGL based applications
3 Copyright (C) 2002-2003 Sebastien Metrot
4
5 licence: see nui3/LICENCE.TXT
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #ifndef __APPLE__
11 #include <GL/gl.h>
12 #include <GL/glu.h>
13 #include <GL/glut.h>
14 #else
15 #include <GLUT/glut.h>
16 #endif
17 #include <iostream>
18 #include <cmath>
19 #include <string>
20
21 #include "nui.h"
22 #include "nuiInit.h"
23 #include "nuiGrid.h"
24
25 #include "nuiGLUTBridge.h"
26
27 double rx = 0., rz = 0.;
28 double translateX = 0.f, translateY = 0.f, translateZ = -10.f;
29 double scaleFactor = .5;
30 int mode = 0;
31
32 nuiGLUTBridge* gpBridge = NULL;
33
34 void glutDisplay(void)
35 {
36 // Display the NUI stuff:
37 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
38
39 glViewport(0, 0, glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
40 glMatrixMode(GL_PROJECTION);
41 glLoadIdentity();
42 /* modify this line to change perspective values */
43 gluPerspective(45.0, (float)glutGet(GLUT_WINDOW_WIDTH)/(float)glutGet(GLUT_WINDOW_HEIGHT), 1.0, 5000.0);
44 glMatrixMode(GL_MODELVIEW);
45 glLoadIdentity();
46
47 //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
48 glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
49
50 glLoadIdentity();
51
52 glColor3f(1.f,0.f,0.f);
53 glTranslatef(translateX, translateY, translateZ);
54 glScalef(2, 2, 2);
55 glRotatef(rx, 0, 1, 0);
56 glRotatef(rz, 1, 0, 0);
57
58 double vectA[4] = {-1,1,0, 1};
59 double vectB[4] = {1,1,0, 1};
60 double vectC[4] = {1,-1,0, 1};
61 double vectD[4] = {-1,-1,0, 1};
62
63 double dx1 = vectB[0] - vectA[0];
64 double dy1 = vectB[1] - vectA[1];
65 double dx2 = vectC[0] - vectD[0];
66 double dy2 = vectC[1] - vectD[1];
67 for (int i=0;i<=20;++i)
68 {
69 double x1=vectA[0]+dx1*i/20.0;
70 double y1=vectA[1]+dy1*i/20.0;
71 double x2=vectD[0]+dx2*i/20.0;
72 double y2=vectD[1]+dy2*i/20.0;
73 glBegin(GL_LINES);
74 glVertex2f(x1,y1);
75 glVertex2f(x2,y2);
76 glEnd();
77 }
78 dx1=vectD[0]-vectA[0];
79 dy1=vectD[1]-vectA[1];
80 dx2=vectC[0]-vectB[0];
81 dy2=vectC[1]-vectB[1];
82 for (int i=0;i<=20;++i)
83 {
84 double x1=vectA[0]+dx1*i/20.0;
85 double y1=vectA[1]+dy1*i/20.0;
86 double x2=vectB[0]+dx2*i/20.0;
87 double y2=vectB[1]+dy2*i/20.0;
88 glBegin(GL_LINES);
89 glVertex2f(x1,y1);
90 glVertex2f(x2,y2);
91 glEnd();
92 }
93
94 gpBridge->Display();
95
96 glFlush();
97 glutSwapBuffers();
98 }
99
100 void InitializeOGL()
101 {
102 glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
103 glShadeModel(GL_SMOOTH);
104 glEnable(GL_DEPTH_TEST);
105
106 return;
107 }
108
109 void glutIdle()
110 {
111 rx += 0.5f;
112 rz += 0.2f;
113 glutPostRedisplay();
114 }
115
116 int main(int argc, char** argv)
117 {
118 // Simple and standard GLUT init:
119 glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL | GLUT_RGBA | GLUT_MULTISAMPLE );
120 glutInit( &argc, argv );
121 glutCreateWindow("NUI over GLUT");
122 InitializeOGL();
123
124 // Init the basic NUI services (strings, streams, unicode, etc...)
125 nuiInit(NULL);
126
127 // Create the NUI bridge which also serves as the main window/widget tree:
128 gpBridge = new nuiGLUTBridge();
129
130 // Register the NUI bridge in GLUT:
131 glutReshapeFunc(&nuiGLUTBridge::glutResize); // called every time the screen is resized
132 glutDisplayFunc(glutDisplay); // called when window needs to be redisplayed
133 glutKeyboardFunc(&nuiGLUTBridge::glutKeyboard); // called when the application receives a input from the keyboard
134 glutMouseFunc(&nuiGLUTBridge::glutMouse); // called when the application receives a input from the mouse
135 glutMotionFunc(&nuiGLUTBridge::glutMotion); // called when the mouse moves over the screen with one of this button pressed
136 glutPassiveMotionFunc(&nuiGLUTBridge::glutMotion); // called when the mouse moves over the screen with one of this button pressed
137 glutIdleFunc(glutIdle); // called whenever the application is idle
138
139 // Create some stupid widgets:
140 nuiGrid* pGrid = new nuiGrid(4, 4);
141 for (uint i = 0; i < 4; i++)
142 {
143 pGrid->SetRowExpand(i, nuiExpandShrinkAndGrow);
144 pGrid->SetColumnExpand(i, nuiExpandShrinkAndGrow);
145 for (uint j = 0; j < 4; j++)
146 {
147 nglString str;
148 str.CFormat(_T("btn(%d,%d)"), i, j);
149 nuiButton* pButton = new nuiButton(str);
150 pButton->SetPosition(nuiCenter);
151 pGrid->SetCell(i, j, pButton);
152 }
153 }
154 gpBridge->AddChild(pGrid);
155 //gpBridge->OnResize(300, 300);
156
157 // Execute the GLUT main loop:
158 glutMainLoop();
159
160 // Exit the application
161 // First destroy the NUI bridge / widget tree:
162 delete gpBridge;
163
164 // Shutdown the basic NUI services:
165 nuiUninit();
166
167 return 0;
168 }