-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglUtils.cpp
More file actions
88 lines (69 loc) · 1.82 KB
/
glUtils.cpp
File metadata and controls
88 lines (69 loc) · 1.82 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
86
87
88
#include <GLFW/glfw3.h>
#include <cmath>
#include <iostream>
#include <SOIL.h>
using namespace std;
#include "glUtils.h"
void glPerspective(float fov, float aspectRatio, float znear, float zfar) {
float ymax = znear * tanf(fov * M_PI / 360.0);
float xmax = ymax * aspectRatio;
glFrustum(-xmax, xmax, -ymax, ymax, znear, zfar);
}
void normalize( float p[3] )
{
float w = sqrt( p[0] * p[0] + p[1] * p[1] + p[2] * p[2] );
p[0] /= w;
p[1] /= w;
p[2] /= w;
}
void cross(float a[3], float b[3], float c[3]){
c[0] = a[1]*b[2] - a[2]*b[1];
c[1] = a[2]*b[0] - a[0]*b[2];
c[2] = a[0]*b[1] - a[1]*b[0];
}
void glLookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx,
GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy,
GLdouble upz)
{
int i;
float forward[3], side[3], up[3];
GLfloat m[4][4] = {{1, 0, 0 ,0},
{0, 1, 0 ,0},
{0, 0, 1 ,0},
{0, 0, 0 ,1}};
forward[0] = centerx - eyex;
forward[1] = centery - eyey;
forward[2] = centerz - eyez;
up[0] = upx;
up[1] = upy;
up[2] = upz;
normalize(forward);
// Side = forward x up
cross(forward, up, side);
normalize(side);
// Recompute up as: up = side x forward
cross(side, forward, up);
m[0][0] = side[0];
m[1][0] = side[1];
m[2][0] = side[2];
m[0][1] = up[0];
m[1][1] = up[1];
m[2][1] = up[2];
m[0][2] = -forward[0];
m[1][2] = -forward[1];
m[2][2] = -forward[2];
glMultMatrixf(&m[0][0]);
glTranslated(-eyex, -eyey, -eyez);
}
int carregaTextura(string imagePath){
int texture = SOIL_load_OGL_texture(
imagePath.c_str(),
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);
if (texture == 0 ) {
cout << "Erro carregando textura..." << endl;
}
return texture;
}