Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


dave j

Member Since 11 Oct 2002
Offline Last Active Jul 16 2015 03:19 AM

#5233775 4 unsigned chars to float?

Posted by dave j on 09 June 2015 - 06:45 AM

well i am using my code i showed you a litle bit modified coz it had bugs, since i can define a 'vector' that represents value from minimum to maximum (0..1) my function works fine


If you're still interested in using floats as texture data directly, i.e. lying to GL that your block of memory containing floats is RGBA texture data and reconstructing the floats in the fragment shader, I've corrected the function I posted above so it will convert a float read as texture data into the original float. I've restricted it to use only language constructs that are available in the version of GLSL associated with ES 2.0 and wrapped it in a little program so you can play around with it.

#include <stdio.h>
#include <stdint.h>
#include <glm/glm.hpp>

static const float TwoExp23 = exp2(23.0f);

float tex2float(glm::vec4 tex)
{
   tex *= 255.0f;    // Scale texture values so we can more easily deal with bits.
   
   int exp = (tex.a > 127.0f ? tex.a - 128.0f : tex.a) * 2.0f + (tex.b > 127.0f ? 1.0f : 0.0f); // 7 LSB of a and 1 MSB of b.

   if (exp == 0.0f)     // Exponent is zero for zero and for very small numbers (< 1 * 2^-126) so treat all these as zero.
      return 0.0f;

   if (exp == 255.0f)   // Exponent == 255 means + or - infinity and NaN. We deal with +/- infinity here but you can probably ignore it.
      return (tex.a > 127.0f ? -1.0f : 1.0f) / 0.0f;

   float sig = tex.b > 127.0f ? tex.b - 128.0f : tex.b;  // Significand is the 7 LSB of b...
   sig = sig * 65536.0f + tex.g * 256.0f + tex.r;        // ...followed by g and r.
   sig = 1.0f + sig / TwoExp23;                          // Scale to < 0 and add the implied 1 at the beginning.
   if (tex.a > 127.0f)                                   // If the sign bit is set negate the significand.
      sig = -sig;
   
   return sig * exp2(exp-127.0f);                        // Multiply by the exponent (with the exponent bias applied).
}

int main(int argc, char **argv)
{
   float f1 = -1234.5678f;

   uint8_t *bytes = reinterpret_cast<uint8_t*>(&f1);

   glm::vec4 tex = glm::vec4(bytes[0], bytes[1], bytes[2], bytes[3]);
   
   tex /= 255.0f;       // Texture values are normalized to 0..1 so scale down the bytes to fit.

   float f2 = tex2float(tex);

   printf("f1 = %f\n", f1);
   printf("f2 = %f\n", f2);

   return 0;
}
Having said that, you probably ought to check the precision of texture data (using glGetIntegerv with GL_x_BITS where x is RED, GREEN, BLUE or ALPHA) to make sure they are all 8. If they aren't, you won't be able to use floats directly and will have to do some sort of conversion after all.

Additionally, floats in fragment shaders might not have the full precision that they do on desktop hardware - they are allowed to have as few as 10 binary digits (which translates to just over 3 decimal digits). So don't be surprised if the reconstructed floats don't match the input ones exactly.

You can find the OpenGL ES documentation describing all this at the Khronos site.


#5213477 Give me your Java and Python code

Posted by dave j on 28 February 2015 - 06:34 AM

For Python, many people's Python games are listed on pygame.org. They usually have links to the source.


#5195141 Trying to optimize a mesh in python, but it's WAY too slow :P

Posted by dave j on 28 November 2014 - 05:06 AM

Ah, ok. Well I finally got it working with the help of an octree. I will try using your methon with the hash maps. I was originally using separate lists inside of each octree node, but I might try to use a map instead. I did use a hash map for changing the indices, but I didn't think of adding up the position values X3. I was too afraid that that would result in a collision, which CAN happen if you have a cube shaped mesh with the same values in different coordinate axis (Example: Vertex1(0, 0, 1), Vertex2(1, 0, 0).

Don't worry about calculating a hash - putting it into the map will do that anyway. Just concatenate everything into a string e.g.:

key = pack("ffffffff", x, y, z, nx, ny, nz, u, v)

Your code can then become something like:
vertexCache = {}

// Get a dictionary of unique vertices.
maxIndex = 0
for v in oldVertexList:
   key = pack("ffffffff", v.x, v.y, v.z, v.nx, v.ny, v.nz, v.u, v.v)
   if not key in VertexCache:
      vertexCache[key] = maxIndex
      maxIndex += 1

// Convert it into a list in index order for unpacking and writing out later.
newVertexList = list(repeat(-1,maxIndex))
for (k,v) in iter(vertexCache.items()):
   newVertexList[v] = k

// Create a new index list referring to items in newVertexList
newIndexList = []
for index in oldIndexList:
   v = oldVertexList[index]
   key = pack("ffffffff", v.x, v.y, v.z, v.nx, v.ny, v.nz, v.u, v.v)
   newIndexList.append(vertexCache[key])



#5171246 Status of GL extension libraries

Posted by dave j on 03 August 2014 - 05:28 AM

The OpenGL APIs are defined in XML specification files these days to allow people to automate the generation of header files. You can find the details here. As well as the XML specs, there's enough documentation and example code that developing something to produce your own core profile version specific header files should be relatively easy.


#5166377 hyphotetical raw gpu programming

Posted by dave j on 12 July 2014 - 04:29 AM

For the gory details of a relatively simple[1] GPU, Broadcom have released documentation for the Raspberry Pi's GPU.


[1] Simpler than the AMD GCN described by phantom at least.


#5135429 Large textures are really slow...

Posted by dave j on 28 February 2014 - 12:45 PM

The tile-based architectures of most mobile GPUs have a different set of optimal use cases compared to more traditional GPUs. Check the advice in Performance Tuning for Tile-Based Architectures from the OpenGL Insights book to make sure you're not doing anything that triggers poor performance.


#5128133 how to prevent save cheating? (for games where saving is NOT allowed)

Posted by dave j on 02 February 2014 - 05:16 AM

I don't think it's a case of making it less easy for them to do as less desirable. If you allow the player to save whenever and wherever they like they'll always be tempted to save just before a difficult bit and reload if they fail. Instead you could have a limited number of save locations that are sufficiently far apart that going back to a saved game would lose a significant amount of progress. This would at least discourage them from going back to a saved game even if it didn't prevent it.


#5106643 Looking for procedural 3D mesh generation resources

Posted by dave j on 03 November 2013 - 05:13 AM

The Virtual Terrain Project is a good place to start. It has a page listing papers on procedural building and city generation. It also contains information about procedurally generating other things such as terrain and plants.


#5095166 BASIC

Posted by dave j on 19 September 2013 - 07:18 AM

Many many years ago I was asked to alter a program written in BASIC by someone who had been learning to program as they went along. It was obvious that they only discovered subroutines part way through when I found the equivalent of the following in several places throughout the code:

10 REM early bit of code
20 GOSUB 40
30 GOTO 60
40 REM stuff they later decided they wanted to use in several places
50 RETURN
60 REM some more program
70 GOSUB 40
80 REM rest of program
90 END



#5092965 Best method to learn Java?

Posted by dave j on 10 September 2013 - 04:27 AM

As others have said, forget 3D games for the moment. There are a number of projects that are designed to help teach Java programming by getting you to customise a game. Robocode is one example - you write code to control a robot tank that battles against others. This will not only help you learn programming in Java but also introduce you to some of the concepts of game programming. It doesn't cover that much of Java though so you'll still need to look at other tutorials.


#5089459 How Useful is SVG?

Posted by dave j on 27 August 2013 - 04:48 AM

I think the problem you might have with using SVG directly is rendering speed for use in games. Many mobile GPUs support OpenVG in hardware and Nvidia have an OpenGL extension that can render it which will both help but I don't know how quick they are. For other GPUs there are some libraries that attempt to render SVG or OpenVG using OpenGL (See Sugar and ShivaVG).

The big problem you might face is different rendering speeds for more complex objects. With rasters, everything is just a rectangle so rendering speed is based on size. With vector shapes, more detail, particularly if your shapes have many complex curved paths, clip paths, etc. will take longer to render. You might want artists to avoid using particularly slow to render features.


#5081164 "New" to OOP

Posted by dave j on 28 July 2013 - 04:50 AM

it also helps to use a language that forces you to stay in OOP land such as Java and C#.


I found this. When I first started learning OOP I was trying to do it in C++ having been a very competent C programmer for years. I could never stop automatically thinking of solutions in C. I switched to Smalltalk and all soon became clear. You're probably better off with something like Java today - it wasn't around when I first tried OOP.


#5070804 I Want To Write Embedded Software etc

Posted by dave j on 18 June 2013 - 04:30 AM

The best way would be to get an Arduino or a Raspberry Pi and have a go.

There are other boards that are suitable but both the above have large communities and many resources aimed at those learning.

Embedded implies connecting to hardware, which is the fun bit, so you'll need to know the basics of digital electronics. There are lots of tutorials for both the above boards that explain how to use them to interact with particular sensors/controllers/etc. and will explain the relevant bits of electronics.


#5068242 The documentation says those bits are reserved...

Posted by dave j on 08 June 2013 - 10:08 AM

That won't solve the problem if the library is dynamically linked, though, as soon as you'll roll out a version of the library which actually uses those reserved bits the crappy application will still be using the bits incorrectly and you're back to square one.


Surely the crappy application would never have worked when they tried to use the reserved bits with an earlier version of the dynamic library so they'd have changed it to use another method for their flags.


#5067990 The documentation says those bits are reserved...

Posted by dave j on 07 June 2013 - 06:22 AM

...that must mean we can use them.

Many APIs have flags words to contain a various options. Frequently not all the bits are used. Despite the fact that the documentation mentioned that all unused bits in flag words were reserved and should be set to zero, a GUI project that sat on top of OS/2's Presentation Manager (think Windows GUI - they are almost identical) used the reserved bits for it's own purposes. A few years later IBM changed OS/2 to use some of those bits. Not only did the framework need changing to not use those bits, 600 screens needed updating as well (think changing 600 Windows .RC files).

Top tip:

If you are documenting an API that has flag words, define every single unused bit in every flag word as "must be zero". If you define a standard for unused bits once at the begining of your documentation some idiot will not notice/ignore it.




PARTNERS