glfw integration

This commit is contained in:
tmj-fstate
2017-03-02 04:28:56 +01:00
parent 58b183d462
commit 146d6b036d
153 changed files with 691 additions and 56322 deletions

View File

@@ -1,6 +1,5 @@
#include "stdafx.h"
#include "opengl/glew.h"
#include "skydome.h"
#include "color.h"
@@ -82,6 +81,8 @@ void CSkyDome::Generate() {
int const latitudes = m_tesselation / 2;
int const longitudes = m_tesselation;
std::uint16_t index = 0;
for( int i = 0; i < latitudes; ++i ) {
float lat0 = M_PI * ( -0.5f + (float)( i ) / latitudes );
@@ -106,6 +107,21 @@ void CSkyDome::Generate() {
m_vertices.emplace_back( float3( x * zr1, y * zr1 - offset, z1 ) * radius );
// m_normals.emplace_back( float3( -x * zr1, -y * zr1, -z1 ) );
m_colours.emplace_back( float3( 0.75f, 0.75f, 0.75f ) );
if( j == 0 ) {
// beginning of the strip, don't start indices yet
index += 2;
}
else {
// indices for two triangles
m_indices.emplace_back( index - 2 );
m_indices.emplace_back( index - 1 );
m_indices.emplace_back( index );
m_indices.emplace_back( index );
m_indices.emplace_back( index - 1 );
m_indices.emplace_back( index + 1 );
index += 2;
}
}
}
}
@@ -123,26 +139,35 @@ void CSkyDome::Update( Math3D::vector3 const &Sun ) {
// render skydome to screen
void CSkyDome::Render() {
int const latitudes = m_tesselation / 2;
int const longitudes = m_tesselation;
int idx = 0;
if( m_vertexbuffer == -1 ) {
// build the buffers
::glGenBuffers( 1, &m_vertexbuffer );
::glBindBuffer( GL_ARRAY_BUFFER, m_vertexbuffer );
::glBufferData( GL_ARRAY_BUFFER, m_vertices.size() * sizeof( float3 ), m_vertices.data(), GL_STATIC_DRAW );
for( int i = 0; i < latitudes; ++i ) {
::glGenBuffers( 1, &m_coloursbuffer );
::glBindBuffer( GL_ARRAY_BUFFER, m_coloursbuffer );
::glBufferData( GL_ARRAY_BUFFER, m_colours.size() * sizeof( float3 ), m_colours.data(), GL_DYNAMIC_DRAW );
::glBegin( GL_QUAD_STRIP );
for( int j = 0; j <= longitudes / 2; ++j ) {
::glColor3f( m_colours[ idx ].x, m_colours[ idx ].y, m_colours[ idx ].z );
// ::glNormal3f( m_normals[ idx ].x, m_normals[ idx ].y, m_normals[ idx ].z );
::glVertex3f( m_vertices[ idx ].x, m_vertices[ idx ].y, m_vertices[ idx ].z );
++idx;
::glColor3f( m_colours[ idx ].x, m_colours[ idx ].y, m_colours[ idx ].z );
// ::glNormal3f( m_normals[ idx ].x, m_normals[ idx ].y, m_normals[ idx ].z );
::glVertex3f( m_vertices[ idx ].x, m_vertices[ idx ].y, m_vertices[ idx ].z );
++idx;
}
glEnd();
::glGenBuffers( 1, &m_indexbuffer );
::glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, m_indexbuffer );
::glBufferData( GL_ELEMENT_ARRAY_BUFFER, m_indices.size() * sizeof( unsigned short ), m_indices.data(), GL_STATIC_DRAW );
}
// begin
::glEnableClientState( GL_VERTEX_ARRAY );
::glEnableClientState( GL_COLOR_ARRAY );
// positions
::glBindBuffer( GL_ARRAY_BUFFER, m_vertexbuffer );
::glVertexPointer( 3, GL_FLOAT, sizeof( float3 ), reinterpret_cast<void const*>( 0 ) );
// colours
::glBindBuffer( GL_ARRAY_BUFFER, m_coloursbuffer );
::glColorPointer( 3, GL_FLOAT, sizeof( float3 ), reinterpret_cast<void const*>( 0 ) );
// indices
::glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, m_indexbuffer );
::glDrawElements( GL_TRIANGLES, static_cast<GLsizei>( m_indices.size() ), GL_UNSIGNED_SHORT, reinterpret_cast<void const*>( 0 ) );
// cleanup
::glDisableClientState( GL_COLOR_ARRAY );
::glDisableClientState( GL_VERTEX_ARRAY );
}
//******************************************************************************//
@@ -339,6 +364,13 @@ void CSkyDome::RebuildColors() {
m_averagecolour.x = std::max( m_averagecolour.x, 0.0f );
m_averagecolour.y = std::max( m_averagecolour.y, 0.0f );
m_averagecolour.z = std::max( m_averagecolour.z, 0.0f );
if( m_coloursbuffer != -1 ) {
// the colour buffer was already initialized, so on this run we update its content
::glBindBuffer( GL_ARRAY_BUFFER, m_coloursbuffer );
::glBufferSubData( GL_ARRAY_BUFFER, 0, m_colours.size() * sizeof( float3 ), m_colours.data() );
}
}
//******************************************************************************//