Programação com OpenGL/Modern OpenGL Tutorial 04: diferenças entre revisões

[edição verificada][revisão pendente]
Conteúdo apagado Conteúdo adicionado
Abacaxi (discussão | contribs)
Sem resumo de edição
m <source> -> <syntaxhighlight> (phab:T237267)
 
Linha 19:
O GLM é uma biblioteca header-only, então você não precisa modificar o Makefile, contando que os header esteja configurado corretamente no standard path.
Para instalar o GLM no Debian ou no Ubuntu:
<sourcesyntaxhighlight lang="bash">
apt-get install libglm-dev
</syntaxhighlight>
</source>
 
Agora nós podemos adicionar os cabeçalhos da GLM:
<sourcesyntaxhighlight lang="c">
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
</syntaxhighlight>
</source>
 
== Usando 3D Points ==
Linha 35:
 
Vamos definir ele (3 elementos por vértices) para OpenGL no triangle.cpp:
<sourcesyntaxhighlight lang="cpp">
struct attributes {
GLfloat coord3d[3];
GLfloat v_color[3];
};
</syntaxhighlight>
</source>
 
Agora, no init_resources()
 
<sourcesyntaxhighlight lang="cpp">
struct attributes triangle_attributes[] = {
{{ 0.0, 0.8, 0.0}, {1.0, 1.0, 0.0}},
Linha 59:
return 0;
}
</syntaxhighlight>
</source>
Mudaremos a configuração do array de vértices em onDisplay()
 
<sourcesyntaxhighlight lang="cpp">
glVertexAttribPointer(
attribute_coord3d, // atributo
Linha 71:
0 // deslocamento do primeiro elemento.
);
</syntaxhighlight>
</source>
substitua as outras ocorrências de 'attribute_coord2d' correspondente e chame o shader usando a nova coordenada.
<sourcesyntaxhighlight lang="glsl">
attribute vec3 coord3d;
[...]
void main(void) {
gl_Position = vec4(coord3d, 1.0);
</syntaxhighlight>
</source>
 
== Criando uma matriz de transformação ==
Linha 84:
No GLM vem inclusivo funções para calcular rotações, movimentação e escalas das matrizes.
Vamos colocar nossa matriz de transformação em <code>onIdle()</code>, que calculará uma rotação progressiva combinado com movimentos:
<sourcesyntaxhighlight lang="cpp">
void onIdle() {
float move = sinf(glutGet(GLUT_ELAPSED_TIME) / 1000.0 * (2*3.14) / 5); // -1<->+1 a cada 5 segundos
Linha 92:
* glm::rotate(glm::mat4(1.0f), angle, axis_z);
[...]
</syntaxhighlight>
</source>
 
== Passando para matriz de transformação ==
 
Assim como vimos no tutorial anterior, vamos adicionar um novo uniform, com <code>glUniformMatrix4fv</code>:
<sourcesyntaxhighlight lang="cpp">
/* Global */
#include <glm/gtc/type_ptr.hpp>
GLint uniform_m_transform;
</syntaxhighlight>
</source>
<sourcesyntaxhighlight lang="cpp">
/* init_resources() */
uniform_name = "m_transform";
Linha 110:
return 0;
}
</syntaxhighlight>
</source>
<sourcesyntaxhighlight lang="cpp">
/* onIdle() */
glUniformMatrix4fv(uniform_m_transform, 1, GL_FALSE, glm::value_ptr(m_transform));
</syntaxhighlight>
</source>
 
Se você não está usando o GLM, é bom passar um ponteiro para uma array GLfloat[16], deste jeito:
 
<sourcesyntaxhighlight lang="cpp">
GLfloat matrix[16] = {...};
glUniformMatrix4fv(uniform_m_transform, 1, GL_FALSE, matrix);
</syntaxhighlight>
</source>
 
O vertex shader apenas tem que multiplicar o vertex pela matrix, como mostraremos abaixo:
<sourcesyntaxhighlight lang="glsl">
uniform mat4 m_transform;
void main(void) {
gl_Position = m_transform * vec4(coord3d, 1.0);
[...]
</syntaxhighlight>
</source>