87 lines
3.9 KiB
HTML
87 lines
3.9 KiB
HTML
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
||
|
<html>
|
||
|
<head>
|
||
|
<title>OpenAL Tutoral</title>
|
||
|
<meta http-equiv="content-type" content="text/html; charset=windows-1252">
|
||
|
<meta name="author" content="Brian Matzon">
|
||
|
<meta name="description" content="Basic sound using OpenAL">
|
||
|
</head>
|
||
|
<body>
|
||
|
<h1 align="center">Array Conversion<br>
|
||
|
<font size="-1">(by Brian Matzon <<a href="mailto:brian@matzon.dk">brian@matzon.dk</a>>)</font><br>
|
||
|
</h1>
|
||
|
<b>1.0 About this document</b><br>
|
||
|
This document describes the typical rules for converting arrays often used in
|
||
|
C/C++ OpenAL (and indeed OpenGL too) code.<br>
|
||
|
It is not bullet proof, but should handle most cases.<br>
|
||
|
<br>
|
||
|
<b>1.1 Array to ByteBuffer<br>
|
||
|
</b>When using an array of some data type in C/C++
|
||
|
you will typically convert that<br>
|
||
|
to the corresponding ByteBuffer type. ie:<p
|
||
|
style="border-style: solid; border-width: 1px; padding: 3px; background-color: rgb(255,255,204);"><tt>ALfloat
|
||
|
floatv[3];</tt></p>
|
||
|
<p>becomes</p>
|
||
|
<p
|
||
|
style="border-style: solid; border-width: 1px; padding: 3px; background-color: rgb(255,255,204);"><tt>
|
||
|
FloatBuffer floatv = createFloatBuffer(3);</tt></p>
|
||
|
<p>In this example, <tt>createFloatBuffer</tt>
|
||
|
is this utility method:</p>
|
||
|
<p
|
||
|
style="border-style: solid; border-width: 1px; padding: 3px; background-color: rgb(255,255,204);"><tt>
|
||
|
public FloatBuffer createFloatBuffer(int size) {<br>
|
||
|
|
||
|
//allocate bytebuffer, using 4 bytes per float<br>
|
||
|
|
||
|
ByteBuffer temp = ByteBuffer.allocateDirect(4*size);<br>
|
||
|
|
||
|
temp.order(ByteOrder.nativeOrder());<br>
|
||
|
|
||
|
<br>
|
||
|
|
||
|
return temp.asFloatBuffer();<br>
|
||
|
}</tt></p>
|
||
|
<p>
|
||
|
<b>1.2 Examples</b><br>
|
||
|
Using the above FloatBuffer, you would typically use it like this (examples
|
||
|
taken from altest.c/ALTest.java):</p>
|
||
|
<p>
|
||
|
<b>1.2.1 Example 1</b></p>
|
||
|
<p
|
||
|
style="border-style: solid; border-width: 1px; padding: 3px; background-color: rgb(255,255,204);"><tt>
|
||
|
alGetListenerfv(AL_POSITION, floatv);</tt></p>
|
||
|
<p>
|
||
|
becomes</p>
|
||
|
<p
|
||
|
style="border-style: solid; border-width: 1px; padding: 3px; background-color: rgb(255,255,204);"><tt>
|
||
|
al.getListenerfv(AL.POSITION, Sys.getDirectBufferAddress(floatv));</tt></p>
|
||
|
<p><b>1.2.2 Example 2</b></p>
|
||
|
<p
|
||
|
style="border-style: solid; border-width: 1px; padding: 3px; background-color: rgb(255,255,204);"><tt>
|
||
|
if (floatv[0] != 100.0)) {</tt></p>
|
||
|
<p>becomes:</p>
|
||
|
<p
|
||
|
style="border-style: solid; border-width: 1px; padding: 3px; background-color: rgb(255,255,204);"><tt>
|
||
|
if (floatv.get(0) != 100.0f) {</tt></p>
|
||
|
<p><b>1.2.3 Example 3</b></p>
|
||
|
<p
|
||
|
style="border-style: solid; border-width: 1px; padding: 3px; background-color: rgb(255,255,204);"><tt>
|
||
|
alGetListener3f(AL_POSITION, &floatv[0], <br>
|
||
|
|
||
|
&floatv[1], <br>
|
||
|
|
||
|
&floatv[2]);</tt></p>
|
||
|
<p>becomes</p>
|
||
|
<p
|
||
|
style="border-style: solid; border-width: 1px; padding: 3px; background-color: rgb(255,255,204);"><tt>
|
||
|
al.getListener3f(AL.POSITION, Sys.getDirectBufferAddress(floatv), <br>
|
||
|
|
||
|
Sys.getDirectBufferAddress(floatv) + 4, <br>
|
||
|
|
||
|
Sys.getDirectBufferAddress(floatv) + 8);</tt></p>
|
||
|
<p>the last case is a bit special, since we start of by getting the base
|
||
|
address of the buffer, and then add the datatype size to the base address<br>
|
||
|
to get the address of that specific index. This is just how it has to
|
||
|
be in Java.</p>
|
||
|
</body>
|
||
|
</html>
|