Added: world coordinate axes

This commit is contained in:
denizdiktas 2023-06-13 18:56:06 +03:00
parent 452cd888ff
commit 74b54a3298
6 changed files with 128 additions and 8 deletions

View File

@ -22,6 +22,7 @@ qt_add_executable(earth
Shader_program.h Shader_program.cpp
Sphere.h Sphere.cpp
Tools.h
World_coordinate_axes.h World_coordinate_axes.cpp
)

View File

@ -0,0 +1,76 @@
#include "World_coordinate_axes.h"
#include <qvector3d.h>
#include <vector>
World_coord_axes::World_coord_axes(float length)
{
initializeOpenGLFunctions();
const auto a = length;
const float c = 0.0;
std::vector<QVector3D> vertex_data {
QVector3D(0,0,0), QVector3D(1,0,0),
QVector3D(a,0,0), QVector3D(1,0,0),
QVector3D(0,0,0), QVector3D(0,1,0),
QVector3D(0,a,0), QVector3D(0,1,0),
QVector3D(0,0,0), QVector3D(c,c,1),
QVector3D(0,0,a), QVector3D(c,c,1)
};
// DEFINE OPENGL BUFFERS
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
// Vertex Buffer
glGenBuffers(1, &m_vbo);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
auto vertex_buffer_size = sizeof(QVector3D) * vertex_data.size();
auto vertex_buffer_data = reinterpret_cast<const void*>(vertex_data.data());
glBufferData(GL_ARRAY_BUFFER,
vertex_buffer_size,
vertex_buffer_data,
GL_STATIC_DRAW);
// Position Vertex-Attribute
GLint position_attrib_index = 0;
const void* position_offset = 0;
GLsizei stride = 6 * sizeof(float);
glVertexAttribPointer(position_attrib_index,
3,
GL_FLOAT, GL_FALSE,
stride,
position_offset);
glEnableVertexAttribArray(position_attrib_index);
// Color Vertex-Attribute
GLint color_attrib_index = 1;
auto* color_offset = reinterpret_cast<const void*>(3 * sizeof(float));
glVertexAttribPointer(color_attrib_index,
3,
GL_FLOAT,
GL_FALSE,
stride,
color_offset);
glEnableVertexAttribArray(color_attrib_index);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void World_coord_axes::draw()
{
glBindVertexArray(m_vao);
{
const int count = 2 * 3; // = 2 * number of lines
glDrawArrays(GL_LINES, 0, count);
}
glBindVertexArray(0);
}

View File

@ -0,0 +1,21 @@
#ifndef WORLD_COORD_AXES_H
#define WORLD_COORD_AXES_H
#include "Common_defs.h"
#include <qvector4d.h>
class World_coord_axes : protected OpenGLFunctionsBase
{
public:
World_coord_axes(float length);
void draw();
private:
GLuint m_vao, m_vbo;
};
#endif

View File

@ -82,6 +82,13 @@ void MainWidget::initializeGL()
m_timer.start(12, this);
}
#include "World_coordinate_axes.h"
std::unique_ptr<World_coord_axes> m_world_coord_axes;
void MainWidget::init_camera()
{
m_camera.set_pos(0, 0, 10);
@ -94,6 +101,9 @@ void MainWidget::init_geometry()
m_sphere = std::make_unique<Sphere>(num_slices, num_stacks, r);
const float c = 0.8;
m_sphere->set_color(c, c, c, 1);
m_world_coord_axes = std::make_unique<World_coord_axes>(5);
}
void MainWidget::init_shader_programs()
{
@ -147,10 +157,10 @@ void MainWidget::paintGL()
const auto mvp = projection * view * model;
// Clear color and depth buffer
//auto& sp = m_sp_smooth;
auto& sp = m_sp_color_only;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// SPHERE
{
auto& sp = m_sp_smooth;
sp.use();
sp.set_uniform("u_mvp", mvp);
sp.set_uniform("u_color", m_sphere->get_color());
@ -159,4 +169,15 @@ void MainWidget::paintGL()
sp.unuse();
}
// WORLD COORDINATE AXES
{
auto& sp = m_sp_color_only;
sp.use();
sp.set_uniform("u_mvp", mvp);
m_world_coord_axes->draw();
sp.unuse();
}
}

View File

@ -1,13 +1,11 @@
#version 330
uniform vec4 u_color;
in vec3 v_color;
out vec4 out_color;
void main()
{
out_color = u_color;
out_color = vec4(v_color, 1);
}

View File

@ -2,12 +2,15 @@
#version 330
layout (location = 0) in vec3 a_pos;
layout (location = 1) in vec3 a_normal;
layout (location = 1) in vec3 a_color;
uniform mat4 u_mvp;
out vec3 v_color;
void main()
{
v_color = a_color;
gl_Position = u_mvp * vec4(a_pos.xyz, 1);
}