Jump to content

  • Log In with Google      Sign In   
  • Create Account


Henri Korpela

Member Since 20 Nov 2011
Offline Last Active Nov 16 2012 02:38 PM

Topics I've Started

Utilizing vectors to move entities in 3D space

14 November 2012 - 04:07 PM

Hi, folks and welcome to the topic!

First of all, I'd like to point out that I'm no math genius, but I really got hooked on vectors seeing their benefits in programming entity movement in virtual 2D and 3D environments.

There are several vector classes written in C/C++ to be found on the Internet. However, as I'm willing to learn more about vectors all the time, I dediced to create my own class template in C++ called Vector3.

As far as I know, vector consists of a direction and a magnitude. Direction can be represents with the help of other vectors or by an angle. Magnitude on the hand, is simply the line segment from one point to another. Sounds reasonably simple.

Now, I'm also willing to create a class called Entity. In this class I'd like to have a Vector3-class instance as a member. Via member function called "move" I'd like to use the vector to move some entity, like this:

Entity bob("Bob");
bob.move(30, 0, 1); // '30' and '0' are both angles, '1' represents velocity, which is in meters per second.

Now, if I know both the direction given in angles and the magnitude, not to mention the vector start point in 3D space, how is it possible to calculate the vector end point or do I need to calculate the vector end point in order to move the entity? For me, it makes sense that the movement of an entity happens from one point to another, but how is this movement usually implemented by vectors?

OpenAL + stb_vorbis - playing .ogg files?

24 April 2012 - 02:23 PM

Hello, game developers!

I have been wandering around the Internet & managed to find a nifty little .c source file called stb_vorbis. I ended up using it due to it's easy integration to my game engine & because libvorbis' libraries didn't link themselves properly using MinGW.

After I had set up the stb_vorbis by turning it into a header file & fixing some warnings in that file the compiler helpfully informed me about, I started to look for tutorials. Well, eventually, I found some, sort of:

http://code.google.com/p/irrlamb/source/browse/trunk/source/engine/audio.cpp?r=726
https://gist.github.com/965399

Especially the last link turned out to be great, because it offered me a way to stream an .ogg file. I wanted to have an object-oriented approach to handle .ogg files, both by buffers and streams, but couldn't get it to work. Now, I still have the same problem.

By browsing the forums, I concluded that stb_vorbis is a good solution. However, there are no actual tutorials to help you to set it up. That's sad.

So, the question is, has anybody ever managed to get stb_vorbis to load and play .ogg files via OpenAL? It'd be very good thing to have a good solution for all other people to solve this similiar problem.

Here's my buffer class:

audio_buffer.hpp
/// -----------
/// @author God
/// -----------
#ifndef __TIM_AUDIO_BUFFER_HPP__
#define __TIM_AUDIO_BUFFER_HPP__
namespace Tim{
    namespace Audio{
        /// -------------------------------------------------------------------------
        /// @class    Buffer
        /// @brief    Represents an audio buffer, where audio files can be loaded into.
        /// -------------------------------------------------------------------------
        class Buffer{
		    /// Friends:
            friend class Source;
        public:
            /// Enumerations:
            enum Formats{FORMAT_AUTODETECT, FORMAT_MP3, FORMAT_OGG, FORMAT_VOC, FORMAT_WAV};
            /// Constructors & destructors:
            explicit Buffer(char const* file, Audio::Buffer::Formats const file_format = Audio::Buffer::FORMAT_AUTODETECT);
            ~Buffer(void);
        private:
            /// Static member functions:
            static void _check_file(Audio::Buffer& buffer);
            static void _load_ogg(Audio::Buffer& buffer);
            static void _load_wav(Audio::Buffer& buffer);
            /// Member data:
            Audio::Buffer::Formats const _m_iFormat;
            char const* _m_cpFile;
            int mutable _m_iChannels;
            unsigned int _m_uiBuffer;
        };
    }
}
#endif // __TIM_AUDIO_BUFFER_HPP__

audio_buffer.cpp
#include <cstdio>
#include <cstring>
#include <vector>
#include <tim/al/al.h>
#include <tim/al/alc.h>
#include <tim/stb_vorbis/stb_vorbis.h>
#include <tim/audio_buffer.hpp>
using namespace Tim;

/// -------------------------------------------------------------------------
/// @class    Buffer
/// @brief    Represents an audio buffer, where audio files can be loaded into.
/// -------------------------------------------------------------------------
/// Constructors & destructors:
Audio::Buffer::Buffer(char const* file, Audio::Buffer::Formats const file_format) : _m_iFormat(file_format), _m_cpFile(file){
    ::alGenBuffers(1, &_m_uiBuffer);
    _check_file(*this);
}
Audio::Buffer::~Buffer(void){
    ::alDeleteBuffers(1, &_m_uiBuffer);
}
/// Static member functions:
void Audio::Buffer::_check_file(Audio::Buffer& buffer){
    switch(buffer._m_iFormat){
        case FORMAT_MP3:
            break;
        case FORMAT_OGG:
            _load_ogg(buffer);
            break;
        case FORMAT_VOC:
            break;
        case FORMAT_WAV:
            _load_wav(buffer);
            break;
        default:
            switch(buffer._m_cpFile[::strlen(buffer._m_cpFile) - 3]){
                case 79:    // case 'O':
                case 111:    // case 'o':
                    _load_ogg(buffer);
                    break;
                case 87:    // case 'W':
                case 119:    // case 'w':
                    _load_wav(buffer);
                    break;
            }
            break;
    }
}
void Audio::Buffer::_load_ogg(Audio::Buffer& buffer){
    ::stb_vorbis* __file = ::stb_vorbis_open_filename(const_cast<char*>(buffer._m_cpFile), NULL, NULL);
    ::stb_vorbis_info __info = ::stb_vorbis_get_info(__file);

    int const __length_samples = (::stb_vorbis_stream_length_in_samples(__file) * __info.channels);
    ::ALshort* __buffer = new ::ALshort[__length_samples];

    ::alGenBuffers(1, &buffer._m_uiBuffer);
    ::stb_vorbis_get_samples_short_interleaved(__file, __info.channels, __buffer, __length_samples);
    ::alBufferData(buffer._m_uiBuffer, ((__info.channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16), __buffer, (__length_samples * sizeof(::ALshort)), __info.sample_rate);

    delete[] __buffer;
    ::stb_vorbis_close(__file);
}
void Audio::Buffer::_load_wav(Audio::Buffer& buffer){
    ::FILE* __file = ::fopen(buffer._m_cpFile, "rb");
    short __bits_psample, __bytes_psample, __channels, __type_format;
    long __bytes_psec_avg, __rate_sample, __size, __size_chunk, __size_data;

    for(__size_data = 0; __size_data < 4; ++__size_data) // __size_data-variable represents a temporary iterator.
	    ::getc(__file);
    ::fread(&__size, sizeof(long), 1, __file);
    for(__size_data = 0; __size_data < 8; ++__size_data) // __size_data-variable represents a temporary iterator.
	    ::getc(__file);

    ::fread(&__size_chunk, sizeof(long), 1, __file);
    ::fread(&__type_format, sizeof(short), 1, __file);
    ::fread(&__channels, sizeof(short), 1, __file);
    ::fread(&__rate_sample, sizeof(long), 1, __file);
    ::fread(&__bytes_psec_avg, sizeof(long), 1, __file);
    ::fread(&__bytes_psample, sizeof(short), 1, __file);
    ::fread(&__bits_psample, sizeof(short), 1, __file);

    for(__size_data = 0; __size_data < 4; ++__size_data) // __size_data-variable represents a temporary iterator.
	    ::getc(__file);
    ::fread(&__size_data, sizeof(long), 1, __file);

    unsigned char* __buffer = new unsigned char[__size_data];
    ::fread(__buffer, sizeof(unsigned char), __size_data, __file);
    ::fclose(__file); // Opened file no longer needed, so it will be closed.

    ::alGenBuffers(1, &buffer._m_uiBuffer);
    ::alBufferData(buffer._m_uiBuffer, (__bits_psample == 8) ? ((__channels == 1) ? AL_FORMAT_MONO8 : AL_FORMAT_STEREO8) : ((__channels == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16), __buffer, __size_data, __rate_sample);

    delete[] __buffer; // A TEMPORARY BUFFER MUST DIE!
}

#undef __TIM_TEMPORARY_SIZE_BUFFER

GLFW - Window close callback?

23 November 2011 - 10:20 AM

Hello, I created a very primitive application which is both DirectX & OpenGL compatible. As I already mentioned, it's primitive, but it works... almost. There is a slight problem and it's about the callback, which handles the window closure in GLFW API. So, the question is: what kind of function definition will handle the callback properly? I know that the callback in GLFW is this:

int glfwSetWindowCloseCallback(void); I'm looking for proper implementation of the callback function.

PLEASE NOTE: WinAPI is out of the question, because the goal of my OpenGL implementation of my application is to be compatible with non-Microsoft systems. This is why I have two different APIS: DirectX & OpenGL.

PARTNERS