1 #ifndef B3_USE_GLFW
2 #ifdef _WIN32
3 /*
4 Copyright (c) 2012 Advanced Micro Devices, Inc.
5 
6 This software is provided 'as-is', without any express or implied warranty.
7 In no event will the authors be held liable for any damages arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it freely,
10 subject to the following restrictions:
11 
12 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 acknowledgment in the product documentation would be appreciated but is not required.
13 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
14 3. This notice may not be removed or altered from any source distribution.
15 */
16 //Originally written by Erwin Coumans
17 
18 #include "Win32OpenGLWindow.h"
19 
20 #include "OpenGLInclude.h"
21 
22 //#include "Bullet3Common/b3Vector3.h"
23 
24 #include "Win32InternalWindowData.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 
enableOpenGL()28 void Win32OpenGLWindow::enableOpenGL()
29 {
30 	PIXELFORMATDESCRIPTOR pfd;
31 	int format;
32 
33 	// get the device context (DC)
34 	m_data->m_hDC = GetDC(m_data->m_hWnd);
35 
36 	// set the pixel format for the DC
37 	ZeroMemory(&pfd, sizeof(pfd));
38 	pfd.nSize = sizeof(pfd);
39 	pfd.nVersion = 1;
40 	pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
41 	pfd.iPixelType = PFD_TYPE_RGBA;
42 	pfd.cColorBits = 32;
43 	pfd.cRedBits = 8;
44 	pfd.cGreenBits = 8;
45 	pfd.cBlueBits = 8;
46 	pfd.cAlphaBits = 8;
47 
48 	pfd.cDepthBits = 24;
49 	pfd.cStencilBits = 8;  //1;
50 	pfd.iLayerType = PFD_MAIN_PLANE;
51 	format = ChoosePixelFormat(m_data->m_hDC, &pfd);
52 	SetPixelFormat(m_data->m_hDC, format, &pfd);
53 
54 	// create and enable the render context (RC)
55 	m_data->m_hRC = wglCreateContext(m_data->m_hDC);
56 	wglMakeCurrent(m_data->m_hDC, m_data->m_hRC);
57 
58 	//printGLString("Extensions", GL_EXTENSIONS);
59 }
60 
disableOpenGL()61 void Win32OpenGLWindow::disableOpenGL()
62 {
63 	wglMakeCurrent(NULL, NULL);
64 	wglDeleteContext(m_data->m_hRC);
65 	//	ReleaseDC( m_data->m_hWnd, m_data->m_hDC );
66 }
67 
createWindow(const b3gWindowConstructionInfo & ci)68 void Win32OpenGLWindow::createWindow(const b3gWindowConstructionInfo& ci)
69 {
70 	Win32Window::createWindow(ci);
71 
72 	//VideoDriver = video::createOpenGLDriver(CreationParams, FileSystem, this);
73 	enableOpenGL();
74 
75 	if (!gladLoaderLoadGL())
76 	{
77 		printf("gladLoaderLoadGL failed!\n");
78 		exit(-1);
79 	}
80 }
81 
Win32OpenGLWindow()82 Win32OpenGLWindow::Win32OpenGLWindow()
83 {
84 }
85 
~Win32OpenGLWindow()86 Win32OpenGLWindow::~Win32OpenGLWindow()
87 {
88 }
89 
closeWindow()90 void Win32OpenGLWindow::closeWindow()
91 {
92 	disableOpenGL();
93 
94 	Win32Window::closeWindow();
95 }
96 
startRendering()97 void Win32OpenGLWindow::startRendering()
98 {
99 	pumpMessage();
100 
101 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
102 
103 	//glCullFace(GL_BACK);
104 	//glFrontFace(GL_CCW);
105 	glEnable(GL_DEPTH_TEST);
106 }
107 
renderAllObjects()108 void Win32OpenGLWindow::renderAllObjects()
109 {
110 }
111 
endRendering()112 void Win32OpenGLWindow::endRendering()
113 {
114 	SwapBuffers(m_data->m_hDC);
115 }
116 
fileOpenDialog(char * fileName,int maxFileNameLength)117 int Win32OpenGLWindow::fileOpenDialog(char* fileName, int maxFileNameLength)
118 {
119 #if 0
120 	//wchar_t wideChars[1024];
121 
122 		OPENFILENAME ofn ;
123 	ZeroMemory( &ofn , sizeof( ofn));
124 	ofn.lStructSize = sizeof ( ofn );
125 	ofn.hwndOwner = NULL  ;
126 
127 #ifdef UNICODE
128 	WCHAR bla[1024];
129 	ofn.lpstrFile = bla;
130 	ofn.lpstrFile[0] = '\0';
131 	ofn.nMaxFile = 1023;
132 	ofn.lpstrFilter = L"All Files\0*.*\0URDF\0*.urdf\0.bullet\0*.bullet\0";
133 #else
134 	ofn.lpstrFile = fileName;
135 	ofn.lpstrFile[0] = '\0';
136 	ofn.nMaxFile = 1023;
137 	//ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";
138 	ofn.lpstrFilter = "All Files\0*.*\0URDF\0*.urdf\0.bullet\0*.bullet\0";
139 
140 #endif
141 
142 	ofn.nFilterIndex =1;
143 	ofn.lpstrFileTitle = NULL ;
144 	ofn.nMaxFileTitle = 0 ;
145 	ofn.lpstrInitialDir=NULL ;
146 	ofn.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST ;
147 	GetOpenFileName( &ofn );
148 	return strlen(fileName);
149 #else
150 	return 0;
151 #endif
152 }
153 
getWidth() const154 int Win32OpenGLWindow::getWidth() const
155 {
156 	if (m_data)
157 		return m_data->m_openglViewportWidth;
158 	return 0;
159 }
160 
getHeight() const161 int Win32OpenGLWindow::getHeight() const
162 {
163 	if (m_data)
164 		return m_data->m_openglViewportHeight;
165 	return 0;
166 }
167 
168 #endif
169 
170 #endif  //B3_USE_GLFW
171