Using-QOpenGLFunctions-and-QOpenGLContext: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
This simple examle demonstrates to using QOpenGLFunctions and QOpenGLContext.<br /> OpenGL 3.3 Core Profile support require for compile and run this demo.According to recent Qt 5.2.0<br /> This demo tested on kernel 3.12.8 mesa 10.0.2 xorg 1.15 and intel ivy bridge graphics.Worked perfectly.
This simple examle demonstrates to using QOpenGLFunctions and QOpenGLContext.<br />OpenGL 3.3 Core Profile support require for compile and run this demo.According to recent Qt 5.2.0<br /><code><br /> //////////// FOR BUILD///////////
 
//g++ -fPIC -I/usr/include/qt -I/usr/include/qt/QtGui -I/usr/include/qt/QtCore main.cpp -lQt5Gui -lQt5Core -lGL <s>lpthread
<br />#include &lt;QGuiApplication&amp;gt;<br />#include &lt;QOpenGLContext&amp;gt;<br />#include &lt;QWindow&amp;gt;<br />#include &lt;QOpenGLFunctions_3_3_Core&amp;gt;<br />#include &lt;stdio.h&amp;gt;
<br />class OpenGLWindow : public QWindow, protected QOpenGLFunctions_3_3_Core{
<br /> public:<br /> explicit OpenGLWindow();<br /> ~OpenGLWindow();<br /> virtual void render();<br /> bool isWindowInitialized;<br /> void exposeEvent(QExposeEvent *event);<br /> bool event(QEvent '''event);
<br /> private:<br /> QOpenGLContext''' ctx;<br /> QSurfaceFormat* fmt;<br /> bool isGLInitialized;<br /> GLuint VertexArrayID;<br /> GLuint buffer1;<br /> GLuint ProgramID;<br />};
<br />bool OpenGLWindow::event(QEvent *event)<br />{<br /> switch (event</s>&gt;type()) {<br /> case QEvent::UpdateRequest:<br /> render();<br /> return true;<br /> case QEvent::Close:<br /> glDisableVertexAttribArray(0);<br /> glDeleteBuffers(1, &amp;buffer1);<br /> glDeleteVertexArrays(1, &amp;VertexArrayID);<br /> glDeleteProgram(ProgramID);<br /> return QWindow::event(event);<br /> case QEvent::Resize:<br /> if(isWindowInitialized &amp;&amp; isGLInitialized)glViewport(0,0,width(),height());<br /> return QWindow::event(event);<br /> default:<br /> return QWindow::event(event);<br /> }<br />}
 
void OpenGLWindow::exposeEvent(QExposeEvent *event)<br />{<br /> Q_UNUSED(event);<br /> if (isExposed())render();<br />}
 
OpenGLWindow::OpenGLWindow()<br /> :ctx(new QOpenGLContext)<br /> ,fmt(new QSurfaceFormat)<br /> ,isGLInitialized(0)<br />{<br /> setSurfaceType(OpenGLSurface);<br /> fmt-&gt;setRenderableType(QSurfaceFormat::OpenGL);<br /> fmt-&gt;setVersion(3,3);<br /> resize(640,480);<br /> fmt-&gt;setProfile&amp;amp;#40;QSurfaceFormat::CoreProfile&amp;amp;#41;;<br /> setFormat(*fmt);<br /> ctx-&gt;setFormat('''fmt);<br /> ctx-&gt;create();<br />}
<br />OpenGLWindow::~OpenGLWindow()<br />{}
<br />void OpenGLWindow::render(){
<br /> if([[Image:isWindowInitialized || |]]isExposed())return;
<br /> if(!isGLInitialized){
<br /> ctx-&gt;makeCurrent(this);<br /> initializeOpenGLFunctions();
<br /> GLuint vsID = glCreateShader(GL_VERTEX_SHADER);<br /> GLuint fsID = glCreateShader(GL_FRAGMENT_SHADER);
<br /> const char''' vs=&quot;#version 330 coren layout(location = 0) in vec3 vertexPosition_modelspace;void main(){ gl_Position.xyz = vertexPosition_modelspace; gl_Position.w = 1.0;}&quot;;
 
const char* fs=&quot;#version 330 coren out vec3 color; void main(){ color = vec3(1,0,0);}&quot;;
 
glShaderSource(vsID, 1, &amp;vs , NULL);<br /> glCompileShader(vsID);<br /> GLint isCompiled = 0;<br /> glGetShaderiv(vsID, GL_COMPILE_STATUS, &amp;isCompiled);
 
if(isCompiled == GL_FALSE){<br /> GLint maxLength = 0;<br /> glGetShaderiv(vsID, GL_INFO_LOG_LENGTH, &amp;maxLength);<br /> char info[maxLength];<br /> glGetShaderInfoLog(vsID, maxLength, &amp;maxLength, info);<br /> printf(&quot;%s&amp;quot;,info);}
 
glShaderSource(fsID, 1, &amp;fs , NULL);<br /> glCompileShader(fsID);<br /> glGetShaderiv(fsID, GL_COMPILE_STATUS, &amp;isCompiled);
 
if(isCompiled == GL_FALSE){<br /> GLint maxLength = 0;<br /> glGetShaderiv(fsID, GL_INFO_LOG_LENGTH, &amp;maxLength);<br /> char info[maxLength];<br /> glGetShaderInfoLog(fsID, maxLength, &amp;maxLength, info);<br /> printf(&quot;%s&amp;quot;,info);}
 
GLuint ProgramID = glCreateProgram();<br /> glAttachShader(ProgramID, vsID);<br /> glAttachShader(ProgramID, fsID);<br /> glLinkProgram(ProgramID);<br /> glDeleteShader(vsID);<br /> glDeleteShader(fsID);
 
static const GLfloat data1[] = {<br /> –1.0f, –1.0f, 0.0f,<br /> 1.0f, <s>1.0f, 0.0f,<br /> 0.0f, 1.0f, 0.0f,<br /> };
<br /> glGenVertexArrays(1, &amp;VertexArrayID);<br /> glBindVertexArray(VertexArrayID);<br /> glGenBuffers(1, &amp;buffer1);<br /> glBindBuffer(GL_ARRAY_BUFFER,buffer1);<br /> glBufferData(GL_ARRAY_BUFFER, sizeof(data1),data1, GL_STATIC_DRAW);<br /> glEnableVertexAttribArray(0);<br /> glVertexAttribPointer(0 ,3 ,GL_FLOAT ,GL_FALSE,0 ,(void*)0 );<br /> glClearColor(0.0f, 0.0f, 0.4f,1.0f);<br /> glUseProgram(ProgramID);<br /> glViewport(0,0,width(),height());<br /> isGLInitialized=true;<br /> }
<br /> if(isExposed()){<br /> glClear( GL_COLOR_BUFFER_BIT );<br /> glDrawArrays(GL_TRIANGLES, 0, 3);<br /> ctx</s>&gt;swapBuffers(this);<br /> }<br />}
 
int main(int argc, char *'''argv)<br />{<br /> QGuiApplication app(argc, argv);<br /> OpenGLWindow''' win = new OpenGLWindow();<br /> win-&gt;show();<br /> win-&gt;isWindowInitialized=true;<br /> return app.exec&amp;amp;#40;&amp;#41;;<br />}<br /></code><br />This demo tested on kernel 3.12.8 mesa 10.0.2 xorg 1.15 and intel ivy bridge graphics.Worked perfectly.

Revision as of 09:46, 24 February 2015

This simple examle demonstrates to using QOpenGLFunctions and QOpenGLContext.
OpenGL 3.3 Core Profile support require for compile and run this demo.According to recent Qt 5.2.0

<br /> //////////// FOR BUILD///////////

//g++ -fPIC -I/usr/include/qt -I/usr/include/qt/QtGui -I/usr/include/qt/QtCore main.cpp -lQt5Gui -lQt5Core -lGL <s>lpthread
<br />#include &lt;QGuiApplication&amp;gt;<br />#include &lt;QOpenGLContext&amp;gt;<br />#include &lt;QWindow&amp;gt;<br />#include &lt;QOpenGLFunctions_3_3_Core&amp;gt;<br />#include &lt;stdio.h&amp;gt;
<br />class OpenGLWindow : public QWindow, protected QOpenGLFunctions_3_3_Core{
<br /> public:<br /> explicit OpenGLWindow();<br /> ~OpenGLWindow();<br /> virtual void render();<br /> bool isWindowInitialized;<br /> void exposeEvent(QExposeEvent *event);<br /> bool event(QEvent '''event);
<br /> private:<br /> QOpenGLContext''' ctx;<br /> QSurfaceFormat* fmt;<br /> bool isGLInitialized;<br /> GLuint VertexArrayID;<br /> GLuint buffer1;<br /> GLuint ProgramID;<br />}; 
<br />bool OpenGLWindow::event(QEvent *event)<br />{<br /> switch (event</s>&gt;type()) {<br /> case QEvent::UpdateRequest:<br /> render();<br /> return true;<br /> case QEvent::Close:<br /> glDisableVertexAttribArray(0);<br /> glDeleteBuffers(1, &amp;buffer1);<br /> glDeleteVertexArrays(1, &amp;VertexArrayID);<br /> glDeleteProgram(ProgramID);<br /> return QWindow::event(event);<br /> case QEvent::Resize:<br /> if(isWindowInitialized &amp;&amp; isGLInitialized)glViewport(0,0,width(),height());<br /> return QWindow::event(event);<br /> default:<br /> return QWindow::event(event);<br /> }<br />}

void OpenGLWindow::exposeEvent(QExposeEvent *event)<br />{<br /> Q_UNUSED(event);<br /> if (isExposed())render();<br />}

OpenGLWindow::OpenGLWindow()<br /> :ctx(new QOpenGLContext)<br /> ,fmt(new QSurfaceFormat)<br /> ,isGLInitialized(0)<br />{<br /> setSurfaceType(OpenGLSurface);<br /> fmt-&gt;setRenderableType(QSurfaceFormat::OpenGL);<br /> fmt-&gt;setVersion(3,3);<br /> resize(640,480);<br /> fmt-&gt;setProfile&amp;amp;#40;QSurfaceFormat::CoreProfile&amp;amp;#41;;<br /> setFormat(*fmt);<br /> ctx-&gt;setFormat('''fmt);<br /> ctx-&gt;create();<br />}
<br />OpenGLWindow::~OpenGLWindow()<br />{}
<br />void OpenGLWindow::render(){
<br /> if([[Image:isWindowInitialized || |]]isExposed())return;
<br /> if(!isGLInitialized){
<br /> ctx-&gt;makeCurrent(this);<br /> initializeOpenGLFunctions();
<br /> GLuint vsID = glCreateShader(GL_VERTEX_SHADER);<br /> GLuint fsID = glCreateShader(GL_FRAGMENT_SHADER);
<br /> const char''' vs=&quot;#version 330 coren layout(location = 0) in vec3 vertexPosition_modelspace;void main(){ gl_Position.xyz = vertexPosition_modelspace; gl_Position.w = 1.0;}&quot;;

const char* fs=&quot;#version 330 coren out vec3 color; void main(){ color = vec3(1,0,0);}&quot;;

glShaderSource(vsID, 1, &amp;vs , NULL);<br /> glCompileShader(vsID);<br /> GLint isCompiled = 0;<br /> glGetShaderiv(vsID, GL_COMPILE_STATUS, &amp;isCompiled);

if(isCompiled == GL_FALSE){<br /> GLint maxLength = 0;<br /> glGetShaderiv(vsID, GL_INFO_LOG_LENGTH, &amp;maxLength);<br /> char info[maxLength];<br /> glGetShaderInfoLog(vsID, maxLength, &amp;maxLength, info);<br /> printf(&quot;%s&amp;quot;,info);}

glShaderSource(fsID, 1, &amp;fs , NULL);<br /> glCompileShader(fsID);<br /> glGetShaderiv(fsID, GL_COMPILE_STATUS, &amp;isCompiled);

if(isCompiled == GL_FALSE){<br /> GLint maxLength = 0;<br /> glGetShaderiv(fsID, GL_INFO_LOG_LENGTH, &amp;maxLength);<br /> char info[maxLength];<br /> glGetShaderInfoLog(fsID, maxLength, &amp;maxLength, info);<br /> printf(&quot;%s&amp;quot;,info);}

GLuint ProgramID = glCreateProgram();<br /> glAttachShader(ProgramID, vsID);<br /> glAttachShader(ProgramID, fsID);<br /> glLinkProgram(ProgramID);<br /> glDeleteShader(vsID);<br /> glDeleteShader(fsID);

static const GLfloat data1[] = {<br /> 1.0f, 1.0f, 0.0f,<br /> 1.0f, <s>1.0f, 0.0f,<br /> 0.0f, 1.0f, 0.0f,<br /> };
<br /> glGenVertexArrays(1, &amp;VertexArrayID);<br /> glBindVertexArray(VertexArrayID);<br /> glGenBuffers(1, &amp;buffer1);<br /> glBindBuffer(GL_ARRAY_BUFFER,buffer1);<br /> glBufferData(GL_ARRAY_BUFFER, sizeof(data1),data1, GL_STATIC_DRAW);<br /> glEnableVertexAttribArray(0);<br /> glVertexAttribPointer(0 ,3 ,GL_FLOAT ,GL_FALSE,0 ,(void*)0 );<br /> glClearColor(0.0f, 0.0f, 0.4f,1.0f);<br /> glUseProgram(ProgramID);<br /> glViewport(0,0,width(),height());<br /> isGLInitialized=true;<br /> }
<br /> if(isExposed()){<br /> glClear( GL_COLOR_BUFFER_BIT );<br /> glDrawArrays(GL_TRIANGLES, 0, 3);<br /> ctx</s>&gt;swapBuffers(this);<br /> }<br />}

int main(int argc, char *'''argv)<br />{<br /> QGuiApplication app(argc, argv);<br /> OpenGLWindow''' win = new OpenGLWindow();<br /> win-&gt;show();<br /> win-&gt;isWindowInitialized=true;<br /> return app.exec&amp;amp;#40;&amp;#41;;<br />}<br />


This demo tested on kernel 3.12.8 mesa 10.0.2 xorg 1.15 and intel ivy bridge graphics.Worked perfectly.