我使用win32线程和OpenGL 2.1。我想要实现的是,在后台加载整个3D场景的同时,渲染一个简单的图像,显示“正在加载”。目前它可以工作,但是我有一个问题,有时我的立方体贴图的一部分会从Mozilla Firefox浏览器中获取数据(这是怎么回事?),并忽略那个小盒子上的纹理,它只是一个精灵并且它在应该在的地方。这种情况每三次尝试中就会发生一次。
这是我的线程代码:
我在Windows 8上使用最新的Nvidia 670GTX驱动程序。有什么想法问题可能出在哪里?
[编辑] 我在我的加载线程末尾添加了glFinish(),现在一切正常。我在某个地方读到,OpenGL不会立即完成所有工作,所以我猜这就是情况,在它完成工作之前,上下文被设置为NULL。
这是我的线程代码:
WindowsThread::WindowsThread(HGLRC graphicsContext, HDC deviceContext) :
graphicsContext_(graphicsContext),
deviceContext_(deviceContext),
running_(false),
task_(0),
mode_(WT_NORMAL)
{
handle_ = CreateThread(0, 0,
(unsigned long (__stdcall *)(void *)) this->staticRun,
(void*) this, CREATE_SUSPENDED, &id_);
if (handle_ == 0) {
LOGE("Unable to create thread.");
return;
}
if (!SetThreadPriority(handle_, THREAD_PRIORITY_NORMAL)) {
LOGE("Unable to set thread priority for thread.");
return;
}
}
WindowsThread::~WindowsThread() {
finishTask();
running_ = false;
WaitForSingleObject(handle_, INFINITE);
CloseHandle(handle_);
wglDeleteContext(graphicsContext_);
}
void WindowsThread::start() {
running_ = true;
if (!ResumeThread(handle_)) {
LOGW("Unable to resume thread.");
}
}
bool WindowsThread::isRunning() {
return running_;
}
void WindowsThread::setTask(Task* task, Mode mode) {
finishTask();
task_ = task;
mode_ = mode;
}
bool WindowsThread::hasTask() {
return task_ != 0;
}
void WindowsThread::finishTask() {
while (task_ != 0) {
Sleep(1);
}
}
void WindowsThread::stop() {
running_ = false;
}
int WindowsThread::staticRun(void* thread) {
return ((WindowsThread*) thread)->run();
}
int WindowsThread::run() {
wglMakeCurrent(deviceContext_, graphicsContext_);
while (running_) {
if (task_ != 0) {
task_->run();
task_ = 0;
}
Sleep(10);
}
wglMakeCurrent(0, 0);
return 1;
}
线程管理器:
WindowsThreadManager::WindowsThreadManager(
System* system, UINT threadPoolSize)
{
if (threadPoolSize == 0) {
SYSTEM_INFO info;
GetSystemInfo(&info);
threadPoolSize = info.dwNumberOfProcessors;
if (threadPoolSize == 0) {
threadPoolSize = 1;
}
}
LOGI("Number of threads used: %d", threadPoolSize);
masterContext_ = wglGetCurrentContext();
HDC hdc = wglGetCurrentDC();
for (UINT i = 0; i < threadPoolSize; i++) {
HGLRC threadContext = wglCreateContext(hdc);
wglShareLists(masterContext_, threadContext);
WindowsThread* thread = new WindowsThread(threadContext, hdc);
thread->start();
threads_.push_back(thread);
}
}
WindowsThreadManager::~WindowsThreadManager() {
for (UINT i = 0; i < threads_.size(); i++) {
delete threads_[i];
}
for (UINT i = 0; i < tasks_.size(); i++) {
delete tasks_[i];
}
}
void WindowsThreadManager::execute(Task* task, Mode mode) {
WindowsThread::Mode wtMode = WindowsThread::WT_NORMAL;
if (mode == TM_GRAPHICS_CONTEXT) {
wtMode = WindowsThread::WT_GRPAHICS_CONTEXT;
}
tasks_.push_back(task);
for (UINT i = 0; i < threads_.size(); i++) {
if (!threads_[i]->hasTask()) {
threads_[i]->setTask(task, wtMode);
return;
}
}
threads_[0]->setTask(task, wtMode);
}
void WindowsThreadManager::joinAll() {
for (UINT i = 0; i < threads_.size(); i++) {
if (threads_[i]->hasTask()) {
threads_[i]->finishTask();
}
}
}
我在Windows 8上使用最新的Nvidia 670GTX驱动程序。有什么想法问题可能出在哪里?
[编辑] 我在我的加载线程末尾添加了glFinish(),现在一切正常。我在某个地方读到,OpenGL不会立即完成所有工作,所以我猜这就是情况,在它完成工作之前,上下文被设置为NULL。