-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.c
252 lines (194 loc) · 6.14 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* OpenGL example with C and Tcl/Tk
* https://codeplea.com/opengl-with-c-and-tcl-tk
*
* Copyright (c) 2015-2017 Lewis Van Winkle
* Linux x11 code provided by Todd Norland
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgement in the product documentation would be
* appreciated but is not required.
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
* 3. This notice may not be removed or altered from any source distribution.
*/
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#include <windows.h>
#elif __linux__
#include <X11/Xlib.h>
#include <GL/glx.h>
#endif
#include <tcl.h>
#include <tk.h>
#include <GL/gl.h>
/* OpenGL Hooks*/
#ifdef _WIN32
HDC dc;
#elif __linux__
Display *__glDisplay__ = (Display *)NULL;
Window __glWindow__;
#endif
int SetRenderWindow(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
{
#ifdef _WIN32
/* Grab the HWND from Tcl. */
HWND hwnd;
Tcl_GetIntFromObj(interp, objv[2], (int*)&hwnd);
/* Setup OpenGL. */
dc = GetDC(hwnd);
/* Windows code, setup OpenGL. */
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(pfd));
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
GLuint pixelFormat = ChoosePixelFormat(dc, &pfd);
SetPixelFormat(dc, pixelFormat, &pfd);
HGLRC rc = wglCreateContext(dc);
wglMakeCurrent(dc, rc);
#elif __linux__
Tk_Window __glTkwin__;
XVisualInfo *__glVisinfo__ = (XVisualInfo *)NULL;
int dummy;
int attribs[] =
{GLX_USE_GL, GLX_RGBA, GLX_DOUBLEBUFFER, GLX_DEPTH_SIZE, 0, None};
GLXContext __glGLXContext__ = (void *)NULL;
__glTkwin__ = Tk_NameToWindow(interp, Tcl_GetString (objv[1]), Tk_MainWindow(interp));
if (__glTkwin__ == NULL) {
fprintf(stderr, "__glTkwin__ is NULL\n" );
exit(-1);
}
Tk_MakeWindowExist( __glTkwin__);
/* setup X11 implementation of OpenGL */
__glDisplay__ = Tk_Display(__glTkwin__);
if (__glDisplay__ == NULL) {
fprintf(stderr, "__glDisplay__ is NULL\n" );
exit(-1);
}
if (!glXQueryExtension(__glDisplay__, &dummy, &dummy)) {
fprintf(stderr, "require GLX Extesion\n" );
exit(-1);
}
__glWindow__ = Tk_WindowId( __glTkwin__ );
__glVisinfo__ =
glXChooseVisual( __glDisplay__, DefaultScreen(__glDisplay__), attribs);
if (__glVisinfo__ == NULL) {
fprintf(stderr, "__glVisinfo__ is NULL\n" );
exit(-1);
}
__glGLXContext__ =
glXCreateContext(__glDisplay__, __glVisinfo__, 0, GL_TRUE);
if (__glGLXContext__ == NULL) {
fprintf(stderr, "__glGLXContext__ is NULL\n" );
exit(-1);
}
glXMakeCurrent( __glDisplay__, __glWindow__, __glGLXContext__ );
#endif
glEnable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(1.0, 1.0, 1.0, 0.0f);
glClearDepth(1.0);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glMatrixMode(GL_PROJECTION);
return TCL_OK;
}
int Resize(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
{
int w, h;
Tcl_GetIntFromObj(interp, objv[1], &w);
Tcl_GetIntFromObj(interp, objv[2], &h);
/* printf("Resized to: %d x %d.\n", w, h); */
float dx = (float)w/h;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2*dx, 2*dx, -2, 2, -2, 2);
return TCL_OK;
}
void Loop(ClientData cd)
{
Tcl_Interp *interp = (Tcl_Interp*)cd;
Tcl_CreateTimerHandler(25, Loop, interp);
static float rot = 0;
rot += .5;
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(rot, 1.0f, 1.0f, 0.5f);
/* Draw a simple cube. */
glBegin(GL_QUADS);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glColor3f(1.0f, 0.5f, 0.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f,-1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f,-1.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f);
glVertex3f( 1.0f, 1.0f,-1.0f);
glEnd();
glFlush();
#ifdef _WIN32
SwapBuffers(dc);
#elif __linux__
glXSwapBuffers(__glDisplay__, __glWindow__);
#endif
}
void Cleanup(ClientData cd)
{
//Any cleanup code here.
printf("Done\n");
}
int Init(Tcl_Interp *interp)
{
Tcl_Init(interp);
Tk_Init(interp);
Tcl_CreateObjCommand(interp, "SetRenderWindow", SetRenderWindow, 0, 0);
Tcl_CreateObjCommand(interp, "Resize", Resize, 0, 0);
Tcl_CreateTimerHandler(10, Loop, interp);
Tcl_CreateExitHandler(Cleanup, 0);
return TCL_OK;
}
int main(int argc, char *argv[])
{
char *p[] = {"OpenGL Test", "gui.tcl"};
Tk_Main(2, p, Init);
return 0;
}