Jump to content

  • Log In with Google      Sign In   
  • Create Account

Erik Rufelt

Member Since 17 Apr 2002
Online Last Active Today, 08:41 AM

#5290079 how good is rand() ?

Posted by Erik Rufelt on Yesterday, 10:14 AM

Not too good..

If you Google for its implementation you get a few results. Something like next = (current * A) + B with overflow wrapping around.


EDIT: And it does return the value RAND_MAX at times, not just RAND_MAX-1.

#5289282 Lol, IDirectDrawSurface7::Blt changes Aero scheme to Windows 7 Basic

Posted by Erik Rufelt on 29 April 2016 - 12:26 PM

Not sure if it can be fixed.. OpenGL apps does that sometimes too. I think it's fixed in OpenGL by selecting a pixel-format with the PFD_SUPPORT_COMPOSITION flag. Maybe you can set the pixel-format on the window before creating the DD surface or somehow specify that flag in your caps when you create the primary surface..

#5287807 Window Creation in Win 7

Posted by Erik Rufelt on 20 April 2016 - 12:21 PM

glClearBufferfv and glClear both clear the buffer.. so glClear will overwrite glClearBufferfv with whatever color is set by glClearColor (defaults to black if glClearColor has never been called).

#5286828 Window Creation in Win 7

Posted by Erik Rufelt on 14 April 2016 - 02:03 AM

//The docs say the second arg to glClearBufferfv should be GL_DRAW_BUFFERi to clear and if it is GL_NONE then the operation does nothing. GL_NONE = 0, which you use, so try using GL_DRAW_BUFFER0 instead of 0 as the second arg.(incorrect)

Also remember to remove the glClear after your if-statement so that you don't re-clear it to the default clear color again before swap.

You seem to run into many of these weird problems caused by small mistakes in the details. Make it a habit to read the docs for the functions you use if they don't work right away, as well as use the debugger to single-step through the code, which is extremely helpful to solve these problems a hundred times faster than posting on GD.net about them.

I like this site for GL docs: http://docs.gl/


Do you use Visual Studio? If you don't use the debugger then start doing so, by default just press F9 on a particular line of code and then F5 to run with the debugger and the debugger will break on that line, where you can single-step with F10 and inspect variables with mouse-over. Faster than ExitProcess and std::cout.

#5286608 Window Creation in Win 7

Posted by Erik Rufelt on 13 April 2016 - 02:05 AM

You don't seem to be linking to or initializing GLEW.

Read this page: https://www.opengl.org/wiki/OpenGL_Loading_Library


As well as this one: http://glew.sourceforge.net/basic.html

#5285834 X11 + OpenGL causes segfault

Posted by Erik Rufelt on 08 April 2016 - 10:41 AM

glXMakeCurrent also has a return-code, like the single one you don't check.. :)

Probably the context isn't properly made current there.

If it is, try getting the function pointers for the normal GL functions instead of relying on them by linking.

#5283671 heightmap sample in vertex shader

Posted by Erik Rufelt on 27 March 2016 - 12:06 AM

Sounds like your texture isn't correctly loaded or bound, and your SampleLevel always returns 0.

0 is the default that will be returned if no texture is available to read from.

If you run your game from Visual Studio with the debugger and create your device with D3D11_CREATE_DEVICE_DEBUG you should get debug output in the Output window in Visual Studio if that is the case.

#5277254 Correct way of doing Mouse Picking

Posted by Erik Rufelt on 21 February 2016 - 03:25 AM

One thing that looks like a mistake is this:

"-(2 * mouse.Y) / viewport.Height -1"


Should be +1 at the end, or your values will be between -3 and -1 instead of -1 and 1.

#5277233 Interstellar trade at a relativistic timescale?

Posted by Erik Rufelt on 20 February 2016 - 11:45 PM

Very interesting scenario, though I find it difficult to think of a realistic setup at all without more information. How big is the colony?


If it's something of an off-shore platform out there, that would likely make the colony rather small, and I would expect the crew on the spaceships to also rotate the crew on the colony.

So basically the decision to go work on the colony is a 10-20 year contract in "your time", but a ~ 30-40 year contract in "real time".


If it's a major settlement, I would expect their primary objective is to establish self-sufficiency, and after the first century or so these trade missions will probably mostly be about luxury items or scientific exchange that would impact production on the colony during the decade after the ship arrives. (Depending on whether several ships arrive during the 24 years, or if there is just one or a few ships that go back and fourth).


There is also the question of political stability in the solar system. Are there still economic problems?

I suspect some competition for a chance to jump 24 years into the future whenever there's some form of unrest. If scientific achievements are still happening at the rate they are now, this would likely be extremely appealing even in good conditions for a lot of people, especially if they could go with their families.

#5277182 Vulkan is Next-Gen OpenGL

Posted by Erik Rufelt on 20 February 2016 - 12:42 PM

I think those semaphores are just copied from the multi-threaded multi-queue example, and aren't actually required when acquiring the image and presenting on the same thread and/or the same queue, even without any Wait, if I understand things correctly.


AcquireImage is supposed to block until an image is available or return an error or timeout, and present is supposed to be added to the end of the queue. Queues must be externally synchronized, so the only reason to use a semaphore on AcquireImage is if another thread is about to start submitting command buffers for drawing to the image before AcquireImage returns. (This also makes AcquireImage returning the index of the next buffer redundant, as it must already be known if another thread is already building the command buffer for the frame using that image before it is returned as available).


The semaphore on Present makes sense if the present is submitted to a different queue than is drawing the graphics, even in single-threaded apps, as the last actual drawing command must finish executing on its queue before the image is presented on the other queue, but if submitting the Present on the same queue it is redundant, threaded or not (though threading on one queue might not make much sense.. unless submitting a present to the queue is actually expected to be a slow operation and the main thread wants to start processing the next frame or something without using the queue).

#5277129 Vulkan is Next-Gen OpenGL

Posted by Erik Rufelt on 20 February 2016 - 04:10 AM

Did anyone create anything with it yet?


I tried it out and it works reasonably well, though feels beta. VSync in particular doesn't work at all when I use the SDK, but if I load the functions from the Nvidia driver directly I can't turn it off (no immediate mode reported and ignored if used anyway). Haven't been able to find how they manage to turn it off from the SDK sources... smile.png

Also the SDK crashes whenever I enable the debug validation.. though if I enable the individual layers but leave out the threading validation layer it seems to work and I get debug output.


Haven't found anything about fullscreen mode. I'm reasonably sure when I first tried a fullscreen top-level window I saw a distinct automatic modeswitch like OpenGL does.. but today it stays in "fullscreen windowed".

Only B8G8R8A8 modes reported for me.. no 30-bit backbuffers in the current Nvidia driver apparently.


Documentation feels somewhat lacking. In particular the swap chain parts, where the various examples do things different ways and none quite make sense. When I do get VSync the blocking seems to be in QueuePresent.. whereas I would expect it to just queue up the present and the wait to be in AcquireImage. Perhaps we're supposed to always present on a separate thread or something... I get the delay in present even if I wait for the queue and device to become idle before I call it so it definitely seems to be a wait for the actual VBlank. EDIT: Actually not entirely sure on that, not used enough to this yet to be sure I do things the right way.


Debug validation seems to work, apart from the crash and again problems with the swap-chain. Docs specifically says I need to transition the image between present mode and color attachment, but ignoring that and transitioning it to whatever insane layout I choose at random doesn't give any warnings and works happily.



Didn't get far past the setup yet so everything else may still be great smile.png

#5274454 Vulkan is Next-Gen OpenGL

Posted by Erik Rufelt on 05 February 2016 - 09:07 AM

Also another link for another calendar door. biggrin.png


They could've upgraded the cover image model to say Vulkan and not OpenGL..

#5274178 Window Creation in Win 7

Posted by Erik Rufelt on 03 February 2016 - 11:47 PM

You need to set the window pixel format and then create an OpenGL context.

You should be able to pick out the parts required to get it running in your window from this example:

#include <windows.h>
#include <gl/gl.h>

#pragma comment (lib, "opengl32.lib")


// Main
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	// Window class
	WNDCLASSEX wc = {};
	wc.cbSize = sizeof(wc);
	wc.lpszClassName = TEXT("MyClass");
	wc.hInstance = hInstance;
	wc.lpfnWndProc = WndProc;
	wc.style = CS_OWNDC;

	// Window
	HWND hWnd = CreateWindowEx(
		TEXT("OpenGL Window"),

	// Get DC
	HDC hDC = GetDC(hWnd);

	// Pixel format
	pfd.nSize = sizeof(pfd);
	pfd.nVersion = 1;
	pfd.dwFlags =
	pfd.iPixelType = PFD_TYPE_RGBA;
	pfd.cColorBits = 32;

	int pf = ChoosePixelFormat(hDC, &pfd);
	SetPixelFormat(hDC, pf, &pfd);

	// OpenGL context
	HGLRC hGLRC = wglCreateContext(hDC);

	wglMakeCurrent(hDC, hGLRC);

	// Get OpenGL version and set it as window title
	SetWindowTextA(hWnd, (char*)glGetString(GL_VERSION));

	// Main loop
	ShowWindow(hWnd, nCmdShow);
	while(true) {
		MSG msg;
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != 0) {
			if(msg.message == WM_QUIT)
			else {
		else {
			// Draw
			glClearColor(1.0f, 0.7f, 0.2f, 1.0f);


	// Clean up
	wglMakeCurrent(NULL, NULL);

	UnregisterClass(wc.lpszClassName, hInstance);

	return 0;

// Window proc
	switch(msg) {
		case WM_DESTROY:
		return 0;

		case WM_SIZE:
			// Resize GL rendering area to match window size
			glViewport(0, 0, LOWORD(lParam), HIWORD(lParam));
		return 0;

	return DefWindowProc(hWnd, msg, wParam, lParam);

However, that will only really allow you to use quite old OpenGL functionality without some extra work, as the newer version functions aren't readily available in opengl32.lib. The default library only provides version 1.1 or something like that (even when the context version is 4.5).


To load more functions you need to request their function pointers from WGL while your GL context is current, for example to use the glCompileShader function:

PFNGLCOMPILESHADERPROC glCompileShader = (PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader");

The function pointer will match a function in the NVidia or AMD driver for example, not in the system OpenGL DLL.


This is a bit tedious, and if you want to do it manually the best way is probably to download glcorearb.h from opengl.org and write a script that goes through it and loads all the functions into some appropriate wrapper or global pointers or something.

There are libraries written specifically to take care of all this, such as GLEW for example: http://glew.sourceforge.net/

It can take care of checking all extensions etc. for you and help make sure everything is loaded properly and detect when it isn't. This is especially useful when loading extensions that may be available only on some drivers. If you make sure the context version is 4.x and you load only core functionality matching that version it's just about loading all the function pointers.

#5274108 Multiplication and division of four chars packed in one int

Posted by Erik Rufelt on 03 February 2016 - 02:01 PM

What OS is it for?

On Android there's Renderscript that is specifically made for things like image filters that might work. In general you should be able to do a lot of stuff in GLSL shaders. I would investigate that possibility as it could save you a lot of work and improve performance.

#5272824 CreateSwapChain "fullscreen" random fail with DXGI_STATUS_OCCLUDED

Posted by Erik Rufelt on 27 January 2016 - 06:23 AM

Try creating the swap chain in windowed mode and going to fullscreen with SetFullscreenState.