#include "myQGraphicsView.h"
#include <QGraphicsRectItem>
#include <QPointer>
#include <gl/GL.h>
#include <gl/glext.h>
PFNGLGENBUFFERSPROC glGenBuffers = 0;
PFNGLBINDBUFFERPROC glBindBuffer = 0;
PFNGLBUFFERDATAPROC glBufferData = 0;
PFNGLDELETEBUFFERSPROC glDeleteBuffers = 0;
GLfloat vboVerticies2[4 * 3] =
{
-1.0, -1.0, 0.0,
0.0, -1.0, 0.0,
-1.0, 0.0, 0.0,
0.0, 0.0, 0.0
};
{
QPointer<myQGraphicsView> view_;
public:
itemWrap(myQGraphicsView * obj){view_ = obj;}
~itemWrap(){};
{
GLint proj[16];
GLfloat model[16];
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glGetIntegerv(GL_PROJECTION_MATRIX,proj);
glGetFloatv(GL_MODELVIEW_MATRIX,model);
if(!view_.isNull())
{
painter->beginNativePainting();
view_->updateMap();
painter->endNativePainting();
}
}
};
myQGraphicsView
::myQGraphicsView( QWidget *parent
/*= 0*/ ){
ri->setRect(0,0, 100, 100);
scene()->addItem(ri);
ri->setRect(50, 50, 100, 100);
ri->setZValue(10);
scene()->addItem(ri);
}
myQGraphicsView::~myQGraphicsView()
{
glDeleteBuffers(1,&vbo);
}
void myQGraphicsView::updateMap()
{
if (!glGenBuffers)
{
glGenBuffers = (PFNGLGENBUFFERSPROC)wglGetProcAddress("glGenBuffers");
glBindBuffer = (PFNGLBINDBUFFERPROC)wglGetProcAddress("glBindBuffer");
glBufferData = (PFNGLBUFFERDATAPROC)wglGetProcAddress("glBufferData");
glDeleteBuffers = (PFNGLDELETEBUFFERSPROC)wglGetProcAddress("glDeleteBuffers");
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,0,0);
glGenBuffers(1,&vbo);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
//glBufferData(GL_ARRAY_BUFFER,12*sizeof(GLfloat),vboVerticies2,GL_STATIC_DRAW);
glVertexPointer(3,GL_FLOAT,0,0);
}
GLuint glErr = glGetError();
char* gluErrStr = (char*)gluErrorString(glErr);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/*glColor3f(1.0,0.5,0.0);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glVertexPointer(3,GL_FLOAT,0,0);
glDrawArrays(GL_TRIANGLE_STRIP,0,4);*/
glBegin(GL_TRIANGLES);
glVertex3f( 0.1,-0.1,0.0);
glVertex3f( 0.5, 0.2,0.0);
glVertex3f( 0.4, 1.0,0.0);
glEnd();
}
#include "myQGraphicsView.h"
#include <QGraphicsRectItem>
#include <QPointer>
#include <gl/GL.h>
#include <gl/glext.h>
PFNGLGENBUFFERSPROC glGenBuffers = 0;
PFNGLBINDBUFFERPROC glBindBuffer = 0;
PFNGLBUFFERDATAPROC glBufferData = 0;
PFNGLDELETEBUFFERSPROC glDeleteBuffers = 0;
GLfloat vboVerticies2[4 * 3] =
{
-1.0, -1.0, 0.0,
0.0, -1.0, 0.0,
-1.0, 0.0, 0.0,
0.0, 0.0, 0.0
};
class itemWrap: public QGraphicsRectItem
{
QPointer<myQGraphicsView> view_;
public:
itemWrap(myQGraphicsView * obj){view_ = obj;}
~itemWrap(){};
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget /* = 0 */)
{
GLint proj[16];
GLfloat model[16];
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glGetIntegerv(GL_PROJECTION_MATRIX,proj);
glGetFloatv(GL_MODELVIEW_MATRIX,model);
if(!view_.isNull())
{
painter->beginNativePainting();
view_->updateMap();
painter->endNativePainting();
}
}
};
myQGraphicsView::myQGraphicsView( QWidget *parent /*= 0*/ )
:QGraphicsView(parent)
{
setViewport(new QGLWidget);
setScene(new QGraphicsScene(QRect(0,0,100,100), this));
QGraphicsRectItem * ri = new itemWrap(this);
ri->setRect(0,0, 100, 100);
ri->setBrush(QBrush(QColor(0,0,0,0)));
scene()->addItem(ri);
ri = new QGraphicsRectItem();
ri->setRect(50, 50, 100, 100);
ri->setBrush(QBrush(QColor(Qt::red)));
ri->setZValue(10);
scene()->addItem(ri);
}
myQGraphicsView::~myQGraphicsView()
{
glDeleteBuffers(1,&vbo);
}
void myQGraphicsView::updateMap()
{
if (!glGenBuffers)
{
glGenBuffers = (PFNGLGENBUFFERSPROC)wglGetProcAddress("glGenBuffers");
glBindBuffer = (PFNGLBINDBUFFERPROC)wglGetProcAddress("glBindBuffer");
glBufferData = (PFNGLBUFFERDATAPROC)wglGetProcAddress("glBufferData");
glDeleteBuffers = (PFNGLDELETEBUFFERSPROC)wglGetProcAddress("glDeleteBuffers");
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,0,0);
glGenBuffers(1,&vbo);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
//glBufferData(GL_ARRAY_BUFFER,12*sizeof(GLfloat),vboVerticies2,GL_STATIC_DRAW);
glVertexPointer(3,GL_FLOAT,0,0);
}
GLuint glErr = glGetError();
char* gluErrStr = (char*)gluErrorString(glErr);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/*glColor3f(1.0,0.5,0.0);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glVertexPointer(3,GL_FLOAT,0,0);
glDrawArrays(GL_TRIANGLE_STRIP,0,4);*/
glBegin(GL_TRIANGLES);
glVertex3f( 0.1,-0.1,0.0);
glVertex3f( 0.5, 0.2,0.0);
glVertex3f( 0.4, 1.0,0.0);
glEnd();
}
To copy to clipboard, switch view to plain text mode
If I uncomment lines in
glBufferData(GL_ARRAY_BUFFER,12*sizeof(GLfloat),vboVerticies2,GL_STATIC_DRAW);
....
glColor3f(1.0,0.5,0.0);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glVertexPointer(3,GL_FLOAT,0,0);
glDrawArrays(GL_TRIANGLE_STRIP,0,4);
glBufferData(GL_ARRAY_BUFFER,12*sizeof(GLfloat),vboVerticies2,GL_STATIC_DRAW);
....
glColor3f(1.0,0.5,0.0);
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glVertexPointer(3,GL_FLOAT,0,0);
glDrawArrays(GL_TRIANGLE_STRIP,0,4);
To copy to clipboard, switch view to plain text mode
almost everytime program crashes.
Bookmarks