-
Notifications
You must be signed in to change notification settings - Fork 0
/
glwidget.cpp
112 lines (90 loc) · 2.99 KB
/
glwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "glwidget.h"
#include <QCoreApplication>
#include <QKeyEvent>
#include <QDir>
//#include <GL/glew.h>
#include "trianglemesh.hpp"
#include <glm/gtc/matrix_transform.hpp>
#include "scene/node.hpp"
#include "objparser.hpp"
#include "shader.hpp"
#include "shaderstatus.hpp"
GLWidget::GLWidget( const QGLFormat& format, QWidget* parent )
: QGLWidget( format, parent )
{
}
GLWidget::GLWidget(QGLContext* context, QWidget* parent)
:QGLWidget(context, parent)
{
}
GLWidget::~GLWidget(){
Material::exit();
}
void GLWidget::initializeGL()
{
checkGLError("before glew");
//These have to be inited after get GL context
glewExperimental = GL_TRUE; // This is needed for core profile
GLenum err = glewInit();
if (GLEW_OK != err)
{
qDebug("Error: %s\n", glewGetErrorString(err));
}
qDebug("Status: Using GLEW %s",glewGetString(GLEW_VERSION));
checkGLError("after glew"); //using glew, there is an GL error here..
//but it seems safe to ignore it.
//http://www.opengl.org/wiki/OpenGL_Loading_Library
//it only show the error for opengl 3.2..
qDebug("GL Version: %s",glGetString(GL_VERSION));
qDebug("GLSL version:%s",glGetString(GL_SHADING_LANGUAGE_VERSION));
Material::init(); //the matarial have to have a opengl context
QGLFormat glFormat = QGLWidget::format();
if ( !glFormat.sampleBuffers() )
qWarning() << "Could not enable sample buffers";
// Set the clear color to black
glClearColor( 0.2f, 0.0f, 0.0f, 1.0f );
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
QDir currDir;
qDebug()<<currDir.canonicalPath();
ObjParser obj;
//obj.parse(std::string("/home/wujun/workspace/game/opengl/cube.obj"), rootNode.geomrtries);
obj.parse(std::string("model/REORCTaxi/taxi.obj"), rootNode.geomrtries);
//tm.loadObj("/home/wujun/Downloads/qq26-openglcanvas/qt.obj");
//tm.loadObj("/home/wujun/Downloads/qq26-openglcanvas/models/toyplane.obj");
rootNode.globalTransform=Transform();
Camera* camera=new Camera();
cameraControl=new VirtualBallCameraControl(camera);
Camera::setCurrentCamera(camera);
shaderStatus.lightPosition=new glm::vec3(0.0f, 0.0f, 0.0f);
}
void GLWidget::resizeGL( int w, int h )
{
// Set the viewport to window dimensions
//qDebug()<<"resize event";
glViewport( 0, 0, w, qMax( h, 1 ) );
}
void GLWidget::paintGL()
{
// Clear the buffer with the current clearing color
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
rootNode.draw();
}
void GLWidget::keyPressEvent( QKeyEvent* e )
{
switch ( e->key() )
{
case Qt::Key_Escape:
QCoreApplication::instance()->quit();
break;
case Qt::Key_Down:
//(*shaderStatus.view)=glm::rotate(*shaderStatus.view,30.0f, glm::vec3(1.0f,0.0f,0.0f));
this->repaint();
break;
default:
QGLWidget::keyPressEvent( e );
}
}
void GLWidget::mouseMoveEvent(QMouseEvent *e){
cameraControl->mouseMoveEvent(e);
}