Jump to content

  • Log In with Google      Sign In   
  • Create Account

Erik Rufelt

Member Since 17 Apr 2002
Online Last Active Today, 02:00 PM

#5177468 'Holes' with Procedurally Generated Sphere for OpenGL

Posted by Erik Rufelt on 01 September 2014 - 12:46 PM

Start by breaking out of the loop early, first after the first iteration, then after the second, and so on, for both the sectors and the ring.

That should go from no polygons to a single triangle at the top of the sphere, and then if that looks OK then go to a full ring, and when that looks OK go to 2 rings.

It should make it much easier to isolate the problem, than trying to see why the full sphere looks like it does.


In this case I'm reasonably sure your problem is in building the indices. Are you drawing with GL_TRIANGLES?

If so you need 6 indices per quad, as it's built from 2 separate triangles. From your indices you need to draw a separate quad or triangle-strip for each sector.

If it still looks strange, do glDisable(GL_CULL_FACE) and see if it fixes it, which would mean face-winding is inverted.

#5177088 Compiling 64-bit Applications in Visual Studio Express 2013

Posted by Erik Rufelt on 30 August 2014 - 12:41 PM

You can do 64 bit.

#5176796 Updating dynamic vertex buffer

Posted by Erik Rufelt on 28 August 2014 - 06:31 PM

Some more info about the reason behind the error..

Map() returns a HRESULT value, which failed when you didn't use discard, meaning the buffer wasn't mapped and the exception arose when trying to memcpy to an invalid address in pData (NULL, as you ZeroMemory it).


Discard-write means that whatever was in the buffer before is gone after mapping, which is generally faster and better, but ofcourse it fails if you want to replace just 10 vertices in a larger buffer that contains 100 vertices, for example.

If you want to do reasonably rare partial updates of a buffer (not every frame), use DEFAULT usage and UpdateSubresource to update the buffer instead of mapping a dynamic resource.



I wasn't able to find the reference page for it right now, but I doubt you can map dynamic vertex-buffers without discard... as it would require read CPU-access for the driver to know what part of the buffer you happened to over-write while the resource was mapped (or at least you would need to specify read-access for the CPU when creating the buffer, which is a bad idea for performance).

#5176790 Updating dynamic vertex buffer

Posted by Erik Rufelt on 28 August 2014 - 06:04 PM

Use D3D11_MAP_WRITE_DISCARD instead, if you're replacing the contents of the entire buffer.

#5176739 Assignment operator and virtual inheritance

Posted by Erik Rufelt on 28 August 2014 - 02:06 PM

Seems very complicated... perhaps virtual assignment operators and/or making sure you can't assign a class to a different type.

I think implementing a swap() might be even worse.

#5176550 C/C++ tilt sensor for Android/iOS

Posted by Erik Rufelt on 27 August 2014 - 06:01 PM

You can get the accelerometer like this in C++:

ASensorManager *sensorManager = ASensorManager_getInstance();
const ASensor *accelSensor = ASensorManager_getDefaultSensor(sensorManager, ASENSOR_TYPE_ACCELEROMETER);

Then create an event queue for the sensors.

ASensorEventQueue *sensorEventQueue = ASensorManager_createEventQueue(sensorManager, looper, LOOPER_ID_USER, NULL, NULL);

If you use the android_native_app_glue "framework" the looper is in the struct android_app that is passed to your android_main.


Then when your main-loop poll (ALooper_pollAll?) returns LOOPER_ID_USER (I guess you can build your own messages by adding +1 +2 etc to LOOPER_ID_USER), handle the sensor-events:

ASensorEvent sensorEvent;
ssize_t sensorResult = ASensorEventQueue_getEvents(sensorEventQueue, &sensorEvent, 1);
while(sensorResult > 0) {
	sensorResult = ASensorEventQueue_getEvents(sensorEventQueue, &sensorEvent, 1);

And finally handle the ASensorEvent:

switch(sensorEvent.type) {

I'm not really an expert at this stuff, but that's how I do it when using android_native_app_glue and run the main-loop from android_main.


EDIT: You also need to enable the sensor I think with ASensorEventQueue_enableSensor, and you can disable it when your app loses focus for example to avoid using it unnecessarily. And there's an ASensorEventQueue_setEventRate for setting how often you want data events from it.


Check <android/sensor.h>, it's the header with the functions and structures and has some comments in it.

#5175732 Using precomputed stencil volumes and Carmack's Reverse for hole rendering

Posted by Erik Rufelt on 23 August 2014 - 08:47 PM

You should definitely be able to do that.. by first drawing all your visible geometry only to the depth-buffer, then drawing the subtracted object like a stencil volume, and then finally drawing all your visible geometry again to the color-buffer. Set stencil-testing for the visible pass to only allow geometry outside of the shadow volume (same as when using stencil-shadows with additive lighting passes).

#5175143 So... C++14 is done :O

Posted by Erik Rufelt on 20 August 2014 - 05:30 PM

#include <iterator>

class BitIter {
		BitIter(unsigned int num, unsigned int bitNum) : num(num), bitNum(bitNum) {}

		BitIter operator++() {
			return *this;
		bool operator!=(const BitIter &other) const {
			return (other.bitNum != bitNum);
		unsigned int operator*() const {
			return ((num >> bitNum) & 1u);

		unsigned int num;
		unsigned int bitNum;

namespace std {
	auto begin(const unsigned int &integer) -> BitIter {
		return BitIter(integer, 0);
	auto end(const unsigned int &integer) -> BitIter {
		return BitIter(integer, sizeof(unsigned int) * 8);

#include <iostream>

int main() {
	unsigned int integer = 0xabcd1234u;

	for(auto bit : integer)
		std::cout << bit;
	std::cout << '\n';

	return 0;

Somewhat useful? :)

#5175049 Best way to render text with DirectX11

Posted by Erik Rufelt on 20 August 2014 - 10:31 AM

(And MS, why did you leave D2D/D3D11 interop out of Win7!?!?)


Didn't they fix this in the platform update a year ago?

I'm reasonably sure... but Google doesn't give me any clear answer right now... 11.1 device (at any feature level).



You can combine sprite-sheets with signed distance fields for pretty good quality in magnified and 3D transformed text.


There was a thread recently which links to a pretty cool method: http://www.gamedev.net/topic/659230-font-rendering/

#5170849 1px Bordered Quad

Posted by Erik Rufelt on 01 August 2014 - 05:38 AM

Probably the rasterization rules that only draw pixels where the center is inside it (or perhaps the edges, check the rasterization rules for your DX version). Try offsetting all coords a half pixel (dx and dy are half pixel offsets). As of now you begin your line on a pixel edge, and include half a pixel in each direction, so you include two halves of two different adjacent pixels, instead of including one full pixel.


When you do 2.0f / width then you include one full pixel in each direction, which includes the pixel centers of the adjacent pixels in both directions.

#5170399 Equal distribution of points on the surface of a sphere

Posted by Erik Rufelt on 30 July 2014 - 11:12 AM

You can repeatedly subdivide an http://en.wikipedia.org/wiki/Icosahedron to get a sphere covered in equally sized triangles. Something like the following http://en.wikipedia.org/wiki/Geodesic_grid

#5169298 Declaring temporary variable to save 1 multiply?

Posted by Erik Rufelt on 26 July 2014 - 08:36 AM

The compiler will do that automatically when it makes sense, if optimizations are turned on. If the temporary doesn't make sense it will probably remove it and turn the second example into the first example.


In general however I would probably recommend using the temporary for expensive operations.. but I'm not sure a multiply applies.. if it was sqrt or sin/cos I would use the temporary.


EDIT: For float operations there is one more thing to consider.. the order of operations can slightly alter the results (as floats are approximations). For example (a * b) / c is not necessarily equal to a * (b / c). Therefore using a temporary can often be a good idea, as the compiler can be prevented from performing optimizations it could normally do (though this can depend on compiler settings).

#5169003 Terraforming by aliens

Posted by Erik Rufelt on 24 July 2014 - 05:13 PM

From our perspective I guess it would be a derivative of the name we have for them, or for the process they perform or the substance they replace or insert. Like deoxygenize or acidize.

#5156021 What am I forgetting to optimize?

Posted by Erik Rufelt on 26 May 2014 - 09:08 AM

Your screenshot indicates you're running on Intel graphics, if you're on a laptop with dual graphics, make sure your app uses the right GPU.

#5154766 OpenGL 2.1 / ES 2 streaming vertex buffer update performance

Posted by Erik Rufelt on 20 May 2014 - 02:24 AM

I've found it's often faster to use glBufferData to overwrite the entire buffer rather than use glBufferSubData, even if only part of the buffer actually needs to be updated. If you have lots of data that can be updated, divide it into multiple buffers of for example 256 vertices per buffer and try to use a method that updates as few buffers as possible each frame.