Using-QOpenGLFunctions-and-QOpenGLContext: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Add "cleanup" tag)
(Add to category 3D)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
{{Cleanup | reason=Auto-imported from ExpressionEngine.}}
[[Category:3D]]


This simple examle demonstrates to using QOpenGLFunctions and QOpenGLContext.
This simple examle demonstrates to using QOpenGLFunctions and QOpenGLContext.
Line 41: Line 42:
  case QEvent::Close:
  case QEvent::Close:
  glDisableVertexAttribArray(0);
  glDisableVertexAttribArray(0);
  glDeleteBuffers(1, &buffer1);
  glDeleteBuffers(1, &buffer1);
  glDeleteVertexArrays(1, &VertexArrayID);
  glDeleteVertexArrays(1, &VertexArrayID);
  glDeleteProgram(ProgramID);
  glDeleteProgram(ProgramID);
  return QWindow::event(event);
  return QWindow::event(event);
  case QEvent::Resize:
  case QEvent::Resize:
  if(isWindowInitialized && isGLInitialized)glViewport(0,0,width(),height());
  if(isWindowInitialized && isGLInitialized)glViewport(0,0,width(),height());
  return QWindow::event(event);
  return QWindow::event(event);
  default:
  default:
Line 93: Line 94:
const char* fs="#version 330 coren out vec3 color; void main(){ color = vec3(1,0,0);}";
const char* fs="#version 330 coren out vec3 color; void main(){ color = vec3(1,0,0);}";


glShaderSource(vsID, 1, &vs , NULL);
glShaderSource(vsID, 1, &vs , NULL);
  glCompileShader(vsID);
  glCompileShader(vsID);
  GLint isCompiled = 0;
  GLint isCompiled = 0;
  glGetShaderiv(vsID, GL_COMPILE_STATUS, &isCompiled);
  glGetShaderiv(vsID, GL_COMPILE_STATUS, &isCompiled);


if(isCompiled == GL_FALSE){
if(isCompiled == GL_FALSE){
  GLint maxLength = 0;
  GLint maxLength = 0;
  glGetShaderiv(vsID, GL_INFO_LOG_LENGTH, &maxLength);
  glGetShaderiv(vsID, GL_INFO_LOG_LENGTH, &maxLength);
  char info[maxLength];
  char info[maxLength];
  glGetShaderInfoLog(vsID, maxLength, &maxLength, info);
  glGetShaderInfoLog(vsID, maxLength, &maxLength, info);
  printf("%s",info);}
  printf("%s",info);}


glShaderSource(fsID, 1, &fs , NULL);
glShaderSource(fsID, 1, &fs , NULL);
  glCompileShader(fsID);
  glCompileShader(fsID);
  glGetShaderiv(fsID, GL_COMPILE_STATUS, &isCompiled);
  glGetShaderiv(fsID, GL_COMPILE_STATUS, &isCompiled);


if(isCompiled == GL_FALSE){
if(isCompiled == GL_FALSE){
  GLint maxLength = 0;
  GLint maxLength = 0;
  glGetShaderiv(fsID, GL_INFO_LOG_LENGTH, &maxLength);
  glGetShaderiv(fsID, GL_INFO_LOG_LENGTH, &maxLength);
  char info[maxLength];
  char info[maxLength];
  glGetShaderInfoLog(fsID, maxLength, &maxLength, info);
  glGetShaderInfoLog(fsID, maxLength, &maxLength, info);
  printf("%s",info);}
  printf("%s",info);}


Line 129: Line 130:
  };
  };


  glGenVertexArrays(1, &VertexArrayID);
  glGenVertexArrays(1, &VertexArrayID);
  glBindVertexArray(VertexArrayID);
  glBindVertexArray(VertexArrayID);
  glGenBuffers(1, &buffer1);
  glGenBuffers(1, &buffer1);
  glBindBuffer(GL_ARRAY_BUFFER,buffer1);
  glBindBuffer(GL_ARRAY_BUFFER,buffer1);
  glBufferData(GL_ARRAY_BUFFER, sizeof(data1),data1, GL_STATIC_DRAW);
  glBufferData(GL_ARRAY_BUFFER, sizeof(data1),data1, GL_STATIC_DRAW);

Latest revision as of 15:45, 23 November 2016

This article may require cleanup to meet the Qt Wiki's quality standards. Reason: Auto-imported from ExpressionEngine.
Please improve this article if you can. Remove the {{cleanup}} tag and add this page to Updated pages list after it's clean.

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

 //////////// FOR BUILD///////////

//g++ -fPIC -I/usr/include/qt -I/usr/include/qt/QtGui -I/usr/include/qt/QtCore main.cpp -lQt5Gui -lQt5Core -lGL -lpthread

#include <QGuiApplication>
#include <QOpenGLContext>
#include <QWindow>
#include <QOpenGLFunctions_3_3_Core>
#include <stdio.h>

class OpenGLWindow : public QWindow, protected QOpenGLFunctions_3_3_Core{

 public:
 explicit OpenGLWindow();
 ~OpenGLWindow();
 virtual void render();
 bool isWindowInitialized;
 void exposeEvent(QExposeEvent *event);
 bool event(QEvent '''event);

 private:
 QOpenGLContext''' ctx;
 QSurfaceFormat* fmt;
 bool isGLInitialized;
 GLuint VertexArrayID;
 GLuint buffer1;
 GLuint ProgramID;
}; 

bool OpenGLWindow::event(QEvent *event)
{
 switch (event->type()) {
 case QEvent::UpdateRequest:
 render();
 return true;
 case QEvent::Close:
 glDisableVertexAttribArray(0);
 glDeleteBuffers(1, &buffer1);
 glDeleteVertexArrays(1, &VertexArrayID);
 glDeleteProgram(ProgramID);
 return QWindow::event(event);
 case QEvent::Resize:
 if(isWindowInitialized && isGLInitialized)glViewport(0,0,width(),height());
 return QWindow::event(event);
 default:
 return QWindow::event(event);
 }
}

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

OpenGLWindow::OpenGLWindow()
 :ctx(new QOpenGLContext)
 ,fmt(new QSurfaceFormat)
 ,isGLInitialized(0)
{
 setSurfaceType(OpenGLSurface);
 fmt->setRenderableType(QSurfaceFormat::OpenGL);
 fmt->setVersion(3,3);
 resize(640,480);
 fmt->setProfile(QSurfaceFormat::CoreProfile);
 setFormat(*fmt);
 ctx->setFormat('''fmt);
 ctx->create();
}

OpenGLWindow::~OpenGLWindow()
{}

void OpenGLWindow::render(){

 if([[Image:isWindowInitialized || |]]isExposed())return;

 if(!isGLInitialized){

 ctx->makeCurrent(this);
 initializeOpenGLFunctions();

 GLuint vsID = glCreateShader(GL_VERTEX_SHADER);
 GLuint fsID = glCreateShader(GL_FRAGMENT_SHADER);

 const char''' vs="#version 330 coren layout(location = 0) in vec3 vertexPosition_modelspace;void main(){ gl_Position.xyz = vertexPosition_modelspace; gl_Position.w = 1.0;}";

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

glShaderSource(vsID, 1, &vs , NULL);
 glCompileShader(vsID);
 GLint isCompiled = 0;
 glGetShaderiv(vsID, GL_COMPILE_STATUS, &isCompiled);

if(isCompiled == GL_FALSE){
 GLint maxLength = 0;
 glGetShaderiv(vsID, GL_INFO_LOG_LENGTH, &maxLength);
 char info[maxLength];
 glGetShaderInfoLog(vsID, maxLength, &maxLength, info);
 printf("%s",info);}

glShaderSource(fsID, 1, &fs , NULL);
 glCompileShader(fsID);
 glGetShaderiv(fsID, GL_COMPILE_STATUS, &isCompiled);

if(isCompiled == GL_FALSE){
 GLint maxLength = 0;
 glGetShaderiv(fsID, GL_INFO_LOG_LENGTH, &maxLength);
 char info[maxLength];
 glGetShaderInfoLog(fsID, maxLength, &maxLength, info);
 printf("%s",info);}

GLuint ProgramID = glCreateProgram();
 glAttachShader(ProgramID, vsID);
 glAttachShader(ProgramID, fsID);
 glLinkProgram(ProgramID);
 glDeleteShader(vsID);
 glDeleteShader(fsID);

static const GLfloat data1[] = {
 1.0f, 1.0f, 0.0f,
 1.0f, -1.0f, 0.0f,
 0.0f, 1.0f, 0.0f,
 };

 glGenVertexArrays(1, &VertexArrayID);
 glBindVertexArray(VertexArrayID);
 glGenBuffers(1, &buffer1);
 glBindBuffer(GL_ARRAY_BUFFER,buffer1);
 glBufferData(GL_ARRAY_BUFFER, sizeof(data1),data1, GL_STATIC_DRAW);
 glEnableVertexAttribArray(0);
 glVertexAttribPointer(0 ,3 ,GL_FLOAT ,GL_FALSE,0 ,(void*)0 );
 glClearColor(0.0f, 0.0f, 0.4f,1.0f);
 glUseProgram(ProgramID);
 glViewport(0,0,width(),height());
 isGLInitialized=true;
 }

 if(isExposed()){
 glClear( GL_COLOR_BUFFER_BIT );
 glDrawArrays(GL_TRIANGLES, 0, 3);
 ctx->swapBuffers(this);
 }
}

int main(int argc, char *'''argv)
{
 QGuiApplication app(argc, argv);
 OpenGLWindow''' win = new OpenGLWindow();
 win->show();
 win->isWindowInitialized=true;
 return app.exec();
}

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