-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObject.cpp
More file actions
79 lines (63 loc) · 2.62 KB
/
Object.cpp
File metadata and controls
79 lines (63 loc) · 2.62 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
#include "Object.h"
#include <GL/glew.h>
#include <string>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
namespace MAGE {
Object::Object(std::vector<float> _vertices) : vertices(_vertices) {
Position = glm::vec3(0.0f);
Rotation = glm::vec3(0.0f);
Scale = glm::vec3(1.0f);
Momentum = glm::vec3(0.0f);
Inertia = 10.0f;
Gravity = glm::vec3(0.0f, -1.5f, 0.0f);
CapsuleRadius = 1.0f;
}
void Object::GenBuffers(int indexSize) {
bufferSize = indexSize;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), vertices.data(), GL_STATIC_DRAW);
}
void Object::SetVertexAttrib(int index, unsigned int type, int size, int stride, int offset) {
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(index, size, type, GL_FALSE, stride, (void*)offset);
glEnableVertexAttribArray(index);
}
void Object::Draw() {
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glUseProgram(program);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, bufferSize);
}
/*void TexturedObject::Draw() {
Object::Draw();
texture.Bind();
}
TexturedObject::TexturedObject(Texture texture, std::vector<float> vertices) : Object(vertices), texture(texture) { }*/
void Object::BindProgram(unsigned int _program) {
program = _program;
}
void Object::SetUniformM4(const std::string& name, glm::mat4 matrix) {
glUniformMatrix4fv(glGetUniformLocation(program, name.data()), 1, GL_FALSE, glm::value_ptr(matrix));
}
glm::mat4 Object::GetTransform() {
glm::mat4 translation = glm::translate(glm::mat4(1.0f), Position);
glm::mat4 rotationX = glm::rotate(glm::mat4(1.0f), glm::radians(Rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
glm::mat4 rotationY = glm::rotate(glm::mat4(1.0f), glm::radians(Rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
glm::mat4 rotationZ = glm::rotate(glm::mat4(1.0f), glm::radians(Rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
glm::mat4 scale = glm::scale(glm::mat4(1.0f), Scale);
//glm::mat4 view = glm::perspective(glm::radians(45.0f), 16.0f/9.0f, 0.1f, 100.0f);
return translation * rotationX * rotationY * rotationZ * scale;// * view;
};
void Object::ApplyForce(glm::vec3 f) {
Momentum=f;
}
void Object::TickMovement(float deltaTime) {
Position+=Momentum*deltaTime;
Position+=Gravity*deltaTime;
Momentum*=0.9f;
}
}