프로그래밍과 잡담

[Qt] OpenGL 사용하기.. 본문

프로그래밍/Qt

[Qt] OpenGL 사용하기..

크레온 2010. 1. 28. 14:32

나도 공부하는 입장이라 뭐라 설명 할 수 없다..

그냥 대충 이런게 있다는것만 써 놓는거라서..


Qt 의 경우 OpenGL을 지원을 하는데 glut 는 안되는거 같다.. 뭐 사용하는 방법이 있겠지만 난 모르겠다..

간단한 프로그램이나 보여주겠다..

화면에 네모 나오는 거다..

더보기
 ///헤더 파일  
 #include <QtGui/QWidget> 
 #include "ui_opengl1.h" 
 #include <qgl.h>   
 class OpenGL1: public QGLWidget 
 { 
 	Q_OBJECT  
 	public:     
    	OpenGL1(QWidget *parent = 0);     
        ~OpenGL1();      
        void resizeGL(int w, int h);      
        void paintEvent(QPaintEvent *event);     
        void draw();  private:     
        Ui::OpenGL1Class ui;      
        GLfloat x;     
        GLfloat y;     
        GLfloat rsize;      
        GLfloat windowWidth;     
        GLfloat windowHeight;  
  };
더보기
 //소스 파일  
 #include "opengl1.h" 
 #include <math.h>  
 
 OpenGL1::OpenGL1(QWidget *parent) :     QGLWidget(parent) {      
 /* 아래의 메소드는 현제의 위젯을 OpenGL 용으로 바꾸어 주는 메소드이다. */     
     makeCurrent();      
     resize(640, 480);     
     x = 0.0f;     
     y = 0.0f;     
     rsize = 25; 
 }  
 
 OpenGL1::~OpenGL1() {  }   //OpenGL의 창의 크기가 변경될 때 일어나는  이벤트 메소드이다. 
 
 void OpenGL1::resizeGL(int w, int h) {     
 	GLfloat aspectRatio;      // Prevent a divide by zero     
    if (h == 0)         
    	h = 1;      // Set Viewport to window dimensions     
    glViewport(0, 0, w, h);      
    // Reset coordinate system     
    glMatrixMode(GL_PROJECTION);     
    
    glLoadIdentity();      
    // Establish clipping volume (left, right, bottom, top, near, far)     
    aspectRatio = (GLfloat) w / (GLfloat) h;     
    
    if (w <= h) { 
        glOrtho(-100.0, 100.0, -windowHeight, windowHeight, 1.0, -1.0);     
    } else {
    	windowWidth = 100 * aspectRatio;         
        windowHeight = 100;         
        glOrtho(-windowWidth, windowWidth, -100.0, 100.0, 1.0, -1.0);     
    }      
    
    glMatrixMode(GL_MODELVIEW);     
    glLoadIdentity(); 
    
  }  
  
  // 이 메소드는 무엇인가를 그릴때 발생하는 메소드이다. 이 안에 그리는 이벤트나  함수를 추가하면된다. 
  void OpenGL1::paintEvent(QPaintEvent *event) {     
  	draw(); 
  }  
  
  void OpenGL1::draw() {     
  	// Clear the window with current clearing color     
    glClear(GL_COLOR_BUFFER_BIT);      
    // Set current drawing color to red     
    //           R     G       B     
    glColor3f(1.0f, 0.0f, 0.0f);      
    // Draw a filled rectangle with current color     
    glRectf(x, y, x + rsize, y + rsize);      
    // Flush drawing commands and swap     
    swapBuffers(); 
  }

resize하고  draw 함수 소스는 OpenGL SUPERBIBLE이라는 책에서 참고 했다.

이 책은 glut를 사용하기 때문에 Qt에서 사용하기 위해서는 몇개를 바꿔줘야한다.


swapBuffers() : 이 메소드는 Qt에서 제공되는 메소드인데.. glut 의 glutSwapBuffers()하고 대응되는 메소드이다.


위에 소스를 사용하기 위해서는 pro 파일을 수정을 해 줘야한다.
Qt += opengl을 추가 해줘야 한다.


실행결과..

 
반응형
Comments