Skip to content

Commit

Permalink
refactor: separated Sphere into its separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
denizdiktas committed Jun 9, 2023
1 parent 0712485 commit b0a2e09
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 53 deletions.
105 changes: 56 additions & 49 deletions Arrangement_on_surface_2/demo/earth/mainwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ void main()
//float c = clamp(dot(lightDir,triNormal), 0, 1);
vec3 n = normalize(vNormal);
float c = abs( dot(lightDir, n) );
color = vec4(.2, .2,0,1) + vec4(c,c,0,1);
color = vec4(.2, .2,0,1) + 0.8*vec4(c,c,0,1);
//color = vec4(1,1,0,1);
//color = vCol;
Expand Down Expand Up @@ -238,11 +238,57 @@ void MainWidget::init_geometry()
int num_slices, num_stacks;
num_slices = num_stacks = 64;
float r = 3;
create_sphere(num_slices, num_stacks, r);
m_sphere = std::make_unique<Sphere>(num_slices, num_stacks, r);
}

void MainWidget::create_sphere(int num_slices, int num_stacks, float r)


void MainWidget::resizeGL(int w, int h)
{
// Calculate aspect ratio
qreal aspect = qreal(w) / qreal(h ? h : 1);

// near and far plane locations and vertical field-of-view angle in degrees
const qreal z_near = 1.0, z_far = 100.0, fov = 45.0;

// Reset projection
m_projection.setToIdentity();
m_projection.perspective(fov, aspect, z_near, z_far);
}

void MainWidget::paintGL()
{
QMatrix4x4 view;
const QVector3D eye(0, 10, 10), center(0, 0, 0), up(0, 1, 0);
view.lookAt(eye, center, up);

QMatrix4x4 model;
static float angle = 0;
angle += 1;
model.rotate(angle, up);

// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
{
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


glUseProgram(shader);
auto mvp = m_projection * view * model;
glUniformMatrix4fv(m_uniform_mvp, 1, GL_FALSE, mvp.data());

m_sphere->draw();

glUseProgram(0);
}
}


Sphere::Sphere(int num_slices, int num_stacks, float r)
{
initializeOpenGLFunctions();

num_stacks = std::max<int>(2, num_stacks);
std::vector<QVector3D> vertices, normals;

Expand Down Expand Up @@ -406,52 +452,13 @@ void MainWidget::create_sphere(int num_slices, int num_stacks, float r)
// Note: calling this before glBindVertexArray(0) results in no output!
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}


void MainWidget::resizeGL(int w, int h)
void Sphere::draw()
{
// Calculate aspect ratio
qreal aspect = qreal(w) / qreal(h ? h : 1);

// near and far plane locations and vertical field-of-view angle in degrees
const qreal z_near = 1.0, z_far = 100.0, fov = 45.0;

// Reset projection
m_projection.setToIdentity();
m_projection.perspective(fov, aspect, z_near, z_far);
}

void MainWidget::paintGL()
{
QMatrix4x4 view;
const QVector3D eye(0, 10, 10), center(0, 0, 0), up(0, 1, 0);
view.lookAt(eye, center, up);

QMatrix4x4 model;
static float angle = 0;
angle += 1;
model.rotate(angle, up);

// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// DRAW TRIANGLES
glBindVertexArray(m_vao);
{
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


glUseProgram(shader);
auto mvp = m_projection * view * model;
glUniformMatrix4fv(m_uniform_mvp, 1, GL_FALSE, mvp.data());

{
// DRAW TRIANGLE
glBindVertexArray(m_vao);
{
//glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
glDrawElements(GL_TRIANGLES, m_num_indices, GL_UNSIGNED_INT, 0);
}
glBindVertexArray(0);
}
glUseProgram(0);
//glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
glDrawElements(GL_TRIANGLES, m_num_indices, GL_UNSIGNED_INT, 0);
}
}
glBindVertexArray(0);
}
24 changes: 20 additions & 4 deletions Arrangement_on_surface_2/demo/earth/mainwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#ifndef MAINWIDGET_H
#define MAINWIDGET_H

#include <memory>

#include <QOpenGLWidget>
#include <QMatrix4x4>
Expand All @@ -16,9 +17,11 @@
#include <qopenglfunctions_4_5_core.h>


class GeometryEngine;
class Sphere;
using OpenGLFunctionsBase = QOpenGLFunctions_3_3_Core;

class MainWidget : public QOpenGLWidget, protected QOpenGLFunctions_3_3_Core

class MainWidget : public QOpenGLWidget, protected OpenGLFunctionsBase
{
Q_OBJECT

Expand All @@ -42,11 +45,13 @@ class MainWidget : public QOpenGLWidget, protected QOpenGLFunctions_3_3_Core
void init_shader_program();

void init_geometry();
void create_sphere(int num_slices, int num_stacks, float r);


private:
GLuint m_vao, m_vbo, m_ibo, shader, m_num_indices;

std::unique_ptr<Sphere> m_sphere;

GLuint shader;
GLuint m_uniform_mvp; // uniform location for MVP-matrix in the shader

QBasicTimer m_timer;
Expand All @@ -59,4 +64,15 @@ class MainWidget : public QOpenGLWidget, protected QOpenGLFunctions_3_3_Core
QQuaternion m_rotation;
};

class Sphere : protected OpenGLFunctionsBase
{
public:
Sphere(int num_slices, int num_stacks, float r);

void draw();

private:
GLuint m_vao, m_vbo, m_ibo, m_num_indices;
};

#endif // MAINWIDGET_H

0 comments on commit b0a2e09

Please sign in to comment.