3
// Copyright (C) 2001-present, the Celestia Development Team
4
// Original version by Chris Laurel <claurel@gmail.com>
6
// This program is free software; you can redistribute it and/or
7
// modify it under the terms of the GNU General Public License
8
// as published by the Free Software Foundation; either version 2
9
// of the License, or (at your option) any later version.
11
#include <celrender/gl/buffer.h>
12
#include <celrender/gl/vertexobject.h>
13
#include <celutil/color.h>
15
#include "objectrenderer.h"
16
#include "shadermanager.h"
19
#include "pointstarvertexbuffer.h"
21
namespace gl = celestia::gl;
22
namespace util = celestia::util;
24
PointStarVertexBuffer* PointStarVertexBuffer::current = nullptr;
26
PointStarVertexBuffer::PointStarVertexBuffer(const Renderer &renderer,
27
capacity_t capacity) :
30
m_vertices(std::make_unique<StarVertex[]>(capacity))
34
void PointStarVertexBuffer::startSprites()
36
m_prog = m_renderer.getShaderManager().getShader("star");
37
m_pointSizeFromVertex = true;
40
void PointStarVertexBuffer::startBasicPoints()
42
ShaderProperties shadprop;
43
shadprop.texUsage = TexUsage::VertexColors | TexUsage::StaticPointSize;
44
shadprop.lightModel = LightingModel::UnlitModel;
45
m_prog = m_renderer.getShaderManager().getShader(shadprop);
46
m_pointSizeFromVertex = false;
49
void PointStarVertexBuffer::render()
55
if (m_texture != nullptr)
58
m_bo->invalidateData().setData(
59
util::array_view(m_vertices.get(), m_nStars),
60
gl::Buffer::BufferUsage::StreamDraw);
62
if (m_pointSizeFromVertex)
63
m_vo1->draw(m_nStars);
65
m_vo2->draw(m_nStars);
70
void PointStarVertexBuffer::makeCurrent()
72
if (current == this || m_prog == nullptr)
75
if (current != nullptr)
78
setupVertexArrayObject();
81
m_prog->setMVPMatrices(m_renderer.getCurrentProjectionMatrix(), m_renderer.getCurrentModelViewMatrix());
82
if (m_pointSizeFromVertex)
84
m_prog->samplerParam("starTex") = 0;
88
m_prog->pointScale = m_pointScale;
89
glVertexAttrib1f(CelestiaGLProgram::PointSizeAttributeIndex, 1.0f);
94
void PointStarVertexBuffer::setupVertexArrayObject()
100
m_bo = std::make_unique<gl::Buffer>();
101
m_vo1 = std::make_unique<gl::VertexObject>(gl::VertexObject::Primitive::Points);
102
m_vo2 = std::make_unique<gl::VertexObject>(gl::VertexObject::Primitive::Points);
104
m_vo1->addVertexBuffer(
106
CelestiaGLProgram::VertexCoordAttributeIndex,
108
gl::VertexObject::DataType::Float,
111
offsetof(StarVertex, position));
113
m_vo1->addVertexBuffer(
115
CelestiaGLProgram::ColorAttributeIndex,
117
gl::VertexObject::DataType::UnsignedByte,
120
offsetof(StarVertex, color));
122
m_vo1->addVertexBuffer(
124
CelestiaGLProgram::PointSizeAttributeIndex,
126
gl::VertexObject::DataType::Float,
129
offsetof(StarVertex, size));
131
m_vo2->addVertexBuffer(
133
CelestiaGLProgram::VertexCoordAttributeIndex,
135
gl::VertexObject::DataType::Float,
138
offsetof(StarVertex, position));
140
m_vo2->addVertexBuffer(
142
CelestiaGLProgram::ColorAttributeIndex,
144
gl::VertexObject::DataType::UnsignedByte,
147
offsetof(StarVertex, color));
151
void PointStarVertexBuffer::finish()
157
void PointStarVertexBuffer::enable()
160
glEnable(GL_POINT_SPRITE);
161
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
165
void PointStarVertexBuffer::disable()
168
glDisable(GL_VERTEX_PROGRAM_POINT_SIZE);
169
glDisable(GL_POINT_SPRITE);
173
void PointStarVertexBuffer::setTexture(Texture *texture)
178
void PointStarVertexBuffer::setPointScale(float pointSize)
180
m_pointScale = pointSize;