-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOGLWidget.cpp
More file actions
85 lines (68 loc) · 1.97 KB
/
OGLWidget.cpp
File metadata and controls
85 lines (68 loc) · 1.97 KB
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
#include "OGLWidget.h"
#include <QOpenGLContext>
#include <QTimer>
#include <iostream>
#include <cstdlib>
OGLWidget::OGLWidget(QWidget *parent) : QOpenGLWidget(parent)
{
timer = new QTimer();
connect (timer, SIGNAL(timeout()), this, SLOT(repaint()));
}
OGLWidget::~OGLWidget()
{
// stop and wait for rendering loop to terminate
renderer->stop();
renderer->wait();
timer->stop();
delete timer;
delete renderer;
delete csurface;
}
void OGLWidget::initializeGL()
{
// glewInit has to be called after the opengl rendering context creation
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (GLEW_OK != err)
{
// Problem: glewInit failed, something is seriously wrong.
std::cerr << "Error: " << glewGetErrorString(err);
}
// the QOffscreenSurface needs to be created on the GUI thread
csurface = new QOffscreenSurface();
csurface->setFormat(context()->format());
csurface->create();
// renderer = new CollisionDetection(csurface, this->context());
renderer = new OffscreenRenderer(csurface);
doneCurrent();
renderer->start();
timer->start(500);
}
void OGLWidget::resizeGL(int w, int h)
{
double aspect;
vwidth = w;
vheight = h;
glViewport(0, 0, w, h);
glLoadIdentity();
if(h == 0)
h = 1;
aspect = 1.0 * w / h;
if (aspect<0.0001)
aspect=1.0;
if(aspect <= 1)
glFrustum(-0.0005, 0.0005, -0.0005/aspect, 0.0005/aspect, 0.001, 1000.0);
else
glFrustum(-0.0005*aspect, 0.0005*aspect, -0.0005, 0.0005, 0.001, 1000.0);
}
void OGLWidget::paintGL()
{
glClearColor(0.35f, 0.35f, 0.35f, 1.0f);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_TRIANGLES);
glVertex3f( -5.0f, 5.0f, -15.0f);
glVertex3f( 5.0f, -5.0f, -15.0f);
glVertex3f( 5.0f, 5.0f, -15.0f);
glEnd();
}