GLWidget
::GLWidget(QWidget* parent
) : testImage("C:\\blah\\Test.png"),
testTexId(0) {
setAutoFillBackground(false);
}
//--- This does NOT work. ------------------------
painter.begin(this);
painter.
setRenderHint(QPainter::Antialiasing);
painter.setBackground(Qt::black);
painter.
setPen(QPen(Qt
::red,
2));
painter.drawLine(0, 0, 300, 300);
painter.drawPixmap(50, 50, testImage);
painter.end();
}
//--------------------------------------------------
//--- But this DOES. -----------------------------
void GLWidget::initializeGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
glShadeModel(GL_FLAT);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_COLOR_LOGIC_OP);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Set up orthographic view
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, 800, 600, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// The SOIL image loading code
unsigned char* img = NULL;
int ch, width, height;
img = SOIL_load_image("C:\\blah\\Test.png", &width, &height, &ch, SOIL_LOAD_AUTO);
testTexId = SOIL_create_OGL_texture(img, width, height, ch, SOIL_CREATE_NEW_ID, SOIL_FLAG_TEXTURE_REPEATS);
SOIL_free_image_data(img);
}
void GLWidget::paintGL() {
glDisable(GL_TEXTURE_2D);
glBegin(GL_LINES);
glVertex2i(0, 0); glVertex2i(300, 300);
glEnd();
glEnable(GL_TEXTURE_2D);
glPushMatrix();
glTranslated(10, 10, 0);
glRotated(12.0f, 0.0f, 0.0f, 1.0f);
// Draw quad with texture
glBindTexture(GL_TEXTURE_2D, testTexId);
glBegin(GL_QUADS);
glTexCoord2f( 0, 0); glVertex2i(0, 0);
glTexCoord2f( 0, 1); glVertex2i(0, 200);
glTexCoord2f(1, 1); glVertex2i(200, 200);
glTexCoord2f(1, 0); glVertex2i(200, 0);
glEnd();
glPopMatrix();
}
//--------------------------------------------------
GLWidget::GLWidget(QWidget* parent) :
QGLWidget(QGLFormat(QGL::SampleBuffers), parent),
testImage("C:\\blah\\Test.png"),
testTexId(0) {
setAutoFillBackground(false);
}
//--- This does NOT work. ------------------------
void GLWidget::paintEvent(QPaintEvent*) {
QPainter painter;
painter.begin(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBackground(Qt::black);
painter.setPen(QPen(Qt::red, 2));
painter.drawLine(0, 0, 300, 300);
painter.drawPixmap(50, 50, testImage);
painter.end();
}
//--------------------------------------------------
//--- But this DOES. -----------------------------
void GLWidget::initializeGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
glShadeModel(GL_FLAT);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_COLOR_LOGIC_OP);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Set up orthographic view
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, 800, 600, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// The SOIL image loading code
unsigned char* img = NULL;
int ch, width, height;
img = SOIL_load_image("C:\\blah\\Test.png", &width, &height, &ch, SOIL_LOAD_AUTO);
testTexId = SOIL_create_OGL_texture(img, width, height, ch, SOIL_CREATE_NEW_ID, SOIL_FLAG_TEXTURE_REPEATS);
SOIL_free_image_data(img);
}
void GLWidget::paintGL() {
glDisable(GL_TEXTURE_2D);
glBegin(GL_LINES);
glVertex2i(0, 0); glVertex2i(300, 300);
glEnd();
glEnable(GL_TEXTURE_2D);
glPushMatrix();
glTranslated(10, 10, 0);
glRotated(12.0f, 0.0f, 0.0f, 1.0f);
// Draw quad with texture
glBindTexture(GL_TEXTURE_2D, testTexId);
glBegin(GL_QUADS);
glTexCoord2f( 0, 0); glVertex2i(0, 0);
glTexCoord2f( 0, 1); glVertex2i(0, 200);
glTexCoord2f(1, 1); glVertex2i(200, 200);
glTexCoord2f(1, 0); glVertex2i(200, 0);
glEnd();
glPopMatrix();
}
//--------------------------------------------------
To copy to clipboard, switch view to plain text mode
Bookmarks