【发布时间】:2009-02-08 05:46:54
【问题描述】:
我正在制作一个执行一些自定义图像处理的应用程序。该程序将由控制台中的一个简单菜单驱动。用户将输入图像的文件名,该图像将使用 openGL 在窗口中显示。当用户选择对图像进行一些处理时,处理完成,openGL窗口应该重新绘制图像。
我的问题是我的图像从未被绘制到窗口上,而是窗口始终是黑色的。我认为这可能与我在程序中组织线程的方式有关。主执行线程处理菜单输入/输出和图像处理并调用 Display 方法,而第二个线程运行 openGL 主循环。
这是我的主要代码:
#include <iostream>
#include <GL/glut.h>
#include "ImageProcessor.h"
#include "BitmapImage.h"
using namespace std;
DWORD WINAPI openglThread( LPVOID param );
void InitGL();
void Reshape( GLint newWidth, GLint newHeight );
void Display( void );
BitmapImage* b;
ImageProcessor ip;
int main( int argc, char *argv[] ) {
DWORD threadID;
b = new BitmapImage();
CreateThread( 0, 0, openglThread, NULL, 0, &threadID );
while( true ) {
char choice;
string path = "TestImages\\";
string filename;
cout << "Enter filename: ";
cin >> filename;
path += filename;
b = new BitmapImage( path );
Display();
cout << "1) Invert" << endl;
cout << "2) Line Thin" << endl;
cout << "Enter choice: ";
cin >> choice;
if( choice == '1' ) {
ip.InvertColour( *b );
}
else {
ip.LineThinning( *b );
}
Display();
}
return 0;
}
void InitGL() {
int argc = 1;
char* argv[1];
argv[0] = new char[20];
strcpy( argv[0], "main" );
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( 800, 600 );
glutCreateWindow( "ICIP Program - Character recognition using line thinning, Hilbert curve, and wavelet approximation" );
glutDisplayFunc( Display );
glutReshapeFunc( Reshape );
glClearColor(0.0,0.0,0.0,1.0);
glEnable(GL_DEPTH_TEST);
}
void Reshape( GLint newWidth, GLint newHeight ) {
/* Reset viewport and projection parameters */
glViewport( 0, 0, newWidth, newHeight );
}
void Display( void ) {
glClear (GL_COLOR_BUFFER_BIT); // Clear display window.
b->Draw();
glutSwapBuffers();
}
DWORD WINAPI openglThread( LPVOID param ) {
InitGL();
glutMainLoop();
return 0;
}
这是我的 BitmapImage 绘制方法:
void BitmapImage::Draw() {
cout << "Drawing" << endl;
if( _loaded ) {
glBegin( GL_POINTS );
for( unsigned int i = 0; i < _height * _width; i++ ) {
glColor3f( _bitmap_image[i*3] / 255.0, _bitmap_image[i*3+1] / 255.0, _bitmap_image[i*3+2] / 255.0 );
// invert the y-axis while drawing
glVertex2i( i % _width, _height - (i / _width) );
}
glEnd();
}
}
对这个问题有什么想法吗?
编辑:通过从每 500 毫秒调用 glutPostRedisplay() 的 openglThread 启动一个 glutTimer 从技术上解决了这个问题。现在这没问题,但我更喜欢这样一种解决方案,在这种解决方案中,每次我对位图进行更改时我只需要重新显示(以节省处理时间),并且我不必运行另一个线程(计时器是我假设的另一个线程)。这主要是因为主处理线程将做大量密集的工作,我想将大部分资源专用于该线程而不是其他任何东西。
【问题讨论】:
-
投票结束,为什么这段代码不起作用。
标签: c++ multithreading opengl console