-
Notifications
You must be signed in to change notification settings - Fork 1
/
canvas.cpp
77 lines (58 loc) · 2.26 KB
/
canvas.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
//
// Created by 孙万捷 on 16/3/4.
//
#include "canvas.h"
Canvas::Canvas(const QGLFormat &format, QWidget *parent) : QGLWidget(format, parent)
{
}
Canvas::~Canvas()
{
}
void Canvas::initializeGL()
{
makeCurrent();
glClearColor(0.f, 0.f, 0.f, 0.f);
glClear(GL_COLOR_BUFFER_BIT);
glGenBuffers(1, &leftPBO);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, leftPBO);
glBufferData(GL_PIXEL_UNPACK_BUFFER, 640 * 640 * 4, NULL, GL_DYNAMIC_DRAW);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
glGenBuffers(1, &rightPBO);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, rightPBO);
glBufferData(GL_PIXEL_UNPACK_BUFFER, 640 * 640 * 4, NULL, GL_DYNAMIC_DRAW);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
CudaSafeCall(cudaGraphicsGLRegisterBuffer(&leftResource, leftPBO, cudaGraphicsMapFlagsNone));
CudaSafeCall(cudaGraphicsGLRegisterBuffer(&rightResource, rightPBO, cudaGraphicsMapFlagsNone));
}
void Canvas::resizeGL(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-640, 640, -640, 640, 0, 10000000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void Canvas::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
size_t size;
CudaSafeCall(cudaGraphicsMapResources(1, &leftResource, 0));
CudaSafeCall(cudaGraphicsResourceGetMappedPointer((void**)&leftImg, &size, leftResource));
CudaSafeCall(cudaGraphicsMapResources(1, &rightResource, 0));
CudaSafeCall(cudaGraphicsResourceGetMappedPointer((void**)&rightImg, &size, rightResource));
volumeBox.Build(1.f, 1.f, 1.f);
camera.Setup(make_float3(0.f, 0.f, 2.f), make_float3(0.f, 0.f, 0.f), make_float3(0.f, 1.f, 0.f), 45.f, 640, 640);
render3d(leftImg, rightImg, volumeBox, camera, 0);
CudaSafeCall(cudaDeviceSynchronize());
CudaSafeCall(cudaGraphicsUnmapResources(1, &leftResource, 0));
CudaSafeCall(cudaGraphicsUnmapResources(1, &rightResource, 0));
glRasterPos2i(-640, -640);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, leftPBO);
glDrawPixels(640, 640, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
glRasterPos2i(0, -640);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, rightPBO);
glDrawPixels(640, 640, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
}