-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTriangle.cpp
More file actions
143 lines (114 loc) · 4.26 KB
/
Triangle.cpp
File metadata and controls
143 lines (114 loc) · 4.26 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "Triangle.h"
#include <iostream>
#include "shader.h"
#include "texture.h"
using namespace std;
Triangle::Triangle(Program *program) : Mesh(program)
{
// Définition des VertexData
struct Vertex
{
vec3 position;
float p1;
vec3 velocity;
float p2;
vec3 acceleration;
float p3;
vec3 normal;
float p4;
vec3 tangent;
float p5;
vec2 texture;
float p6, p7;
};
Vertex vertices[3];
vertices[0].position = vec3(-0.6f, -0.4f, 0.f);
vertices[1].position = vec3(0.6f, -0.4f, 0.f);
vertices[2].position = vec3(0.f, 0.6f, 0.f);
for(int i=0; i<3; i++)
{
vertices[i].velocity = vec3(0.f, 0.0f, 0.f);
vertices[i].acceleration = vec3(0.f, 0.0f, 0.f);
vertices[i].normal = vec3(0.f, 0.f, 1.f);
vertices[i].tangent = vec3(1.f, 0.f, 0.f);
}
vertices[0].texture = vec2(0.f, 0.f);
vertices[1].texture = vec2(1.f, 0.f);
vertices[2].texture = vec2(0.5f, 1.f);
// {
// {vec3(-0.6f, -0.4f, 0.f), 1.f, vec3(0.f, 0.f, 0.f), 1.f, vec3(0.f, 0.f, 1.f), 1.f, vec3(1.f, 0.f, 0.f), 1.f, vec2(0.f, 0.f), 1.f, 1.f },
// {vec3(0.6f, -0.4f, 0.f), 1.f, vec3(0.f, 0.f, 0.f), 1.f, vec3(0.f, 0.f, 1.f), 1.f, vec3(1.f, 0.f, 0.f), 1.f, vec2(1.f, 0.f), 1.f, 1.f },
// {vec3(0.f, 0.6f, 0.f), 1.f, vec3(0.f, 0.f, 0.f), 1.f, vec3(0.f, 0.f, 1.f), 1.f, vec3(1.f, 0.f, 0.f), 1.f, vec2(0.5f, 1.f), 1.f, 1.f }
// };
//
indices = new GLuint[3];
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
GLint vpos_location, vtex_location, tex_location, vnor_location;
// Creation de la texture
texture = createTexture("textures/Rock_wall/diffuse.png");
// Récupération des "location" des variable "vPos" et "vCol" du pipeline
vpos_location = glGetAttribLocation(program->getId(), "vPos");
vtex_location = glGetAttribLocation(program->getId(), "vTex");
vnor_location = glGetAttribLocation(program->getId(), "vNorm");
tex_location = glGetUniformLocation(program->getId(), "Tex");
// Création du VertexArray
glGenVertexArrays(1, &vertex_array);
glBindVertexArray(vertex_array);
// Définition du vertexBuffer
glGenBuffers(1, &vertex_buffer);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, vertex_buffer);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_COPY);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
GLuint index_buffer;
glGenBuffers(1, &index_buffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * 3, indices, GL_STATIC_DRAW);
program->bind();
// Association des composantes des VertexData à la variable "vPos" du pipeline
glEnableVertexAttribArray(vpos_location);
glVertexAttribPointer(vpos_location, 3, GL_FLOAT, GL_FALSE, sizeof(vertices[0]), (void*) 0);
// Association des composantes des VertexData à la variable "vNorm" du pipeline
glEnableVertexAttribArray(vnor_location);
glVertexAttribPointer(vnor_location, 3, GL_FLOAT, GL_FALSE, sizeof(vertices[0]), (void*) (sizeof(float) * 12));
// Association des composantes suivantes des VertexData à la variable "vTex" du pipeline
glEnableVertexAttribArray(vtex_location);
glVertexAttribPointer(vtex_location, 2, GL_FLOAT, GL_FALSE, sizeof(vertices[0]), (void*) (sizeof(float) * 20));
// Association de la texture à la variable "Tex"
texUnit = 1;
glUniform1i(tex_location, texUnit);
program->unbind();
GLuint acceleration_shader = loadShader("acceleration.comp", GL_COMPUTE_SHADER);
acceleration = new Computer(acceleration_shader);
acceleration->setData(1, vertex_buffer);
GLuint cinematic_shader = loadShader("cinematic.comp", GL_COMPUTE_SHADER);
cinematic = new Computer(cinematic_shader);
cinematic->setData(1, vertex_buffer);
}
Triangle::~Triangle()
{
delete[] indices;
delete acceleration;
delete cinematic;
}
void Triangle::update()
{
acceleration->compute(1, 1, 1);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
cinematic->compute(1, 1, 1);
glMemoryBarrier(GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT);
}
void Triangle::render(mat4 model)
{
getProgram()->begin(model);
// Selection du VertexArray
glBindVertexArray(vertex_array);
// activer la texture
glActiveTexture(GL_TEXTURE0 + texUnit);
glBindTexture(GL_TEXTURE_2D, texture);
// Affichage des 3 1ers sommets en utilisant des triangles.
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, (void*) 0);
getProgram()->end();
}