-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame.cpp
88 lines (73 loc) · 1.61 KB
/
frame.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
/* frame.cpp
*
* Realidad Virtual y Aumentada.
*
* Practice 2.
* Ray tracing.
*
* Jose Pascual Molina Masso.
* Escuela Superior de Ingenieria Informatica de Albacete.
*/
#include <windows.h>
#include <GL/gl.h>
#include "frame.h"
/* Constructors */
Frame::Frame()
{
allocated = 0;
buffer = NULL;
}
Frame::Frame(int Width, int Height)
{
allocated = 0;
buffer = NULL;
SetSize(Width,Height);
}
/* Destructor */
Frame::~Frame()
{
delete[] buffer;
}
/* Save the new width and height of the image, and if necessary
it allocates more memory for the buffer where it is stored */
int Frame::SetSize(int Width, int Height) {
long newAlloc = ((long)Width)*((long)Height)*3;
if (newAlloc > allocated) {
delete[] buffer;
buffer = new float[newAlloc];
if (buffer == NULL)
return 0;
allocated = newAlloc;
}
width = Width;
height = Height;
return 1;
}
/* Retrieves the width of the image */
int Frame::GetWidth() {
return width;
}
/* Retrieves the height of the image */
int Frame::GetHeight() {
return height;
}
/* Save the color of a pixel at the given position */
int Frame::SetPixel(int x, int y, const glm::vec3& color)
{
float* cptr;
if (buffer == NULL)
return 0;
cptr = const_cast<float*>(buffer + 3*( ((long)y)*width + ((long)x) ));
*(cptr++) = color.x;
*(cptr++) = color.y;
*(cptr) = color.z;
return 1;
}
/* Draw the image with OpenGL */
void Frame::Draw() const
{
glPixelStorei(GL_UNPACK_ROW_LENGTH, width);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glRasterPos2i(0,0); // Position at base of window
glDrawPixels(width, height, GL_RGB, GL_FLOAT, buffer);
}