Celestia

Форк
0
/
pointstarvertexbuffer.cpp 
181 строка · 4.6 Кб
1
// starfield.cpp
2
//
3
// Copyright (C) 2001-present, the Celestia Development Team
4
// Original version by Chris Laurel <claurel@gmail.com>
5
//
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.
10

11
#include <celrender/gl/buffer.h>
12
#include <celrender/gl/vertexobject.h>
13
#include <celutil/color.h>
14
#include "glsupport.h"
15
#include "objectrenderer.h"
16
#include "shadermanager.h"
17
#include "render.h"
18
#include "texture.h"
19
#include "pointstarvertexbuffer.h"
20

21
namespace gl = celestia::gl;
22
namespace util = celestia::util;
23

24
PointStarVertexBuffer* PointStarVertexBuffer::current = nullptr;
25

26
PointStarVertexBuffer::PointStarVertexBuffer(const Renderer &renderer,
27
                                             capacity_t capacity) :
28
    m_renderer(renderer),
29
    m_capacity(capacity),
30
    m_vertices(std::make_unique<StarVertex[]>(capacity))
31
{
32
}
33

34
void PointStarVertexBuffer::startSprites()
35
{
36
    m_prog = m_renderer.getShaderManager().getShader("star");
37
    m_pointSizeFromVertex = true;
38
}
39

40
void PointStarVertexBuffer::startBasicPoints()
41
{
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;
47
}
48

49
void PointStarVertexBuffer::render()
50
{
51
    if (m_nStars != 0)
52
    {
53
        makeCurrent();
54

55
        if (m_texture != nullptr)
56
            m_texture->bind();
57

58
        m_bo->invalidateData().setData(
59
            util::array_view(m_vertices.get(), m_nStars),
60
            gl::Buffer::BufferUsage::StreamDraw);
61

62
        if (m_pointSizeFromVertex)
63
            m_vo1->draw(m_nStars);
64
        else
65
            m_vo2->draw(m_nStars);
66
        m_nStars = 0;
67
    }
68
}
69

70
void PointStarVertexBuffer::makeCurrent()
71
{
72
    if (current == this || m_prog == nullptr)
73
        return;
74

75
    if (current != nullptr)
76
        current->finish();
77

78
    setupVertexArrayObject();
79

80
    m_prog->use();
81
    m_prog->setMVPMatrices(m_renderer.getCurrentProjectionMatrix(), m_renderer.getCurrentModelViewMatrix());
82
    if (m_pointSizeFromVertex)
83
    {
84
        m_prog->samplerParam("starTex") = 0;
85
    }
86
    else
87
    {
88
        m_prog->pointScale = m_pointScale;
89
        glVertexAttrib1f(CelestiaGLProgram::PointSizeAttributeIndex, 1.0f);
90
    }
91
    current = this;
92
}
93

94
void PointStarVertexBuffer::setupVertexArrayObject()
95
{
96
    if (!m_initialized)
97
    {
98
        m_initialized = true;
99

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);
103

104
        m_vo1->addVertexBuffer(
105
            *m_bo,
106
            CelestiaGLProgram::VertexCoordAttributeIndex,
107
            3,
108
            gl::VertexObject::DataType::Float,
109
            false,
110
            sizeof(StarVertex),
111
            offsetof(StarVertex, position));
112

113
        m_vo1->addVertexBuffer(
114
            *m_bo,
115
            CelestiaGLProgram::ColorAttributeIndex,
116
            4,
117
            gl::VertexObject::DataType::UnsignedByte,
118
            true,
119
            sizeof(StarVertex),
120
            offsetof(StarVertex, color));
121

122
        m_vo1->addVertexBuffer(
123
            *m_bo,
124
            CelestiaGLProgram::PointSizeAttributeIndex,
125
            1,
126
            gl::VertexObject::DataType::Float,
127
            false,
128
            sizeof(StarVertex),
129
            offsetof(StarVertex, size));
130

131
        m_vo2->addVertexBuffer(
132
            *m_bo,
133
            CelestiaGLProgram::VertexCoordAttributeIndex,
134
            3,
135
            gl::VertexObject::DataType::Float,
136
            false,
137
            sizeof(StarVertex),
138
            offsetof(StarVertex, position));
139

140
        m_vo2->addVertexBuffer(
141
            *m_bo,
142
            CelestiaGLProgram::ColorAttributeIndex,
143
            4,
144
            gl::VertexObject::DataType::UnsignedByte,
145
            true,
146
            sizeof(StarVertex),
147
            offsetof(StarVertex, color));
148
    }
149
}
150

151
void PointStarVertexBuffer::finish()
152
{
153
    render();
154
    current = nullptr;
155
}
156

157
void PointStarVertexBuffer::enable()
158
{
159
#ifndef GL_ES
160
    glEnable(GL_POINT_SPRITE);
161
    glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
162
#endif
163
}
164

165
void PointStarVertexBuffer::disable()
166
{
167
#ifndef GL_ES
168
    glDisable(GL_VERTEX_PROGRAM_POINT_SIZE);
169
    glDisable(GL_POINT_SPRITE);
170
#endif
171
}
172

173
void PointStarVertexBuffer::setTexture(Texture *texture)
174
{
175
    m_texture = texture;
176
}
177

178
void PointStarVertexBuffer::setPointScale(float pointSize)
179
{
180
    m_pointScale = pointSize;
181
}
182

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.