Jump to content

  • Log In with Google      Sign In   
  • Create Account

link102

Member Since 25 Nov 2009
Offline Last Active Jul 30 2014 10:06 AM

Topics I've Started

[glsl] converting screen coordinates to view coordinates and back again

03 July 2014 - 10:25 AM

For the past 3 days I've been trying to get ssao working without much result. I've narrowed the problem down to the way I'm converting screen coordinates to view coordinates and back again. Below is the code I use to test the conversion.
 
Attached are the results of the shader and the depth buffer. [removed for now, please check back later]
 
Anyone able to point out what's going wrong here?
Are the screenToViewSpace & viewToScreenSpace functions correct?

#version 420

in vec2 vTexCoord;
in vec3 vNormal;

layout(binding = 0) uniform sampler2D uTexDiffuse;
layout(binding = 2) uniform sampler2D uTexDepth;

uniform mat4 projectionMatrix;
uniform mat4 invProjectionMatrix;

out vec4 out_fragColor;

vec3 screenToViewSpace(in vec2 texCoord, in float depth) {	
	vec4 position = vec4(texCoord, depth, 1.0);
	
	// unpack
	position.xyz = position.xyz * 2.0 - 1.0;
	
	// unproject
	position = invProjectionMatrix * position;
	position /= position.w;
	
	return position.xyz;
}

vec2 viewToScreenSpace(in vec3 unprojectedPosition) {
	vec4 position = vec4(unprojectedPosition, 0.0);
	
	// project
	position = projectionMatrix * position;
	position /= position.w;
	
	// pack
	position = position * 0.5 + 0.5;
	
	return position.xy;
}

void main() {
	float depth = texture(uTexDepth, vTexCoord).r;
	
	if(vTexCoord.x < 0.5) {
		// convert to view space, then back to screen space
		out_fragColor.xy = viewToScreenSpace(screenToViewSpace(vTexCoord, depth));
	} else {
		out_fragColor.xy = vTexCoord;
	}
}

Are attributes shared between programs?

31 May 2013 - 09:36 AM

Hi GameDev,
 
I'm currently working on a game in OpenGL ES 2.0 (Android) and I've ran into a bit of an issue:
 
I have 2 meshes, both have their own shader object (code below). During initialization I upload the vertexdata (setPositionAttribute & setTextureAttribute). When it's time to draw though, I can only see the 2nd object. So I'm assuming the vertexdata from the 2nd object has replaced the vertexdata from the 1st.
 
Is this assumption (vertexdata is being shared by all program objects) correct?
Is there a way to separate the vertexdata or do I have to upload it each frame?
 
Edit: problem solved. I forgot to use VBOs. More info here


Quick question about vertex arrays

30 October 2011 - 02:34 PM

Is there any way of adding data into the vertice container as if it's an array?

struct CUSTOMVERTEX { FLOAT x, y, z; };

CUSTOMVERTEX Vertices[] = {
		{ 0.0f, 0.0f, 0.0f},
		{ 1.0f, 0.0f, 0.0f},
		{ 0.0f, 0.0f, 1.0f},
		{ 1.0f, 0.0f, 1.0f},
};

	d3ddev->CreateVertexBuffer(sizeof(Vertices) * sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &v_buffer, NULL);

	VOID* pVoid;
	v_buffer->Lock(0, 0, (void**)&pVoid, 0);
	memcpy(pVoid, Vertices, sizeof(Vertices));
	v_buffer->Unlock();
from that into something like this:

// NOTE: speudo code

CUSTOMVERTEX Vertices[];
Vertices.add(0.0f, 0.0f, 0.0f);
Vertices.add(1.0f, 0.0f, 0.0f);
Vertices.add(0.0f, 0.0f, 1.0f);
Vertices.add(1.0f, 0.0f, 1.0f);

d3ddev->CreateVertexBuffer(sizeof(Vertices) * sizeof(CUSTOMVERTEX), 0, CUSTOMFVF, D3DPOOL_MANAGED, &v_buffer, NULL);

	VOID* pVoid;
	v_buffer->Lock(0, 0, (void**)&pVoid, 0);
	memcpy(pVoid, Vertices, sizeof(Vertices));
	v_buffer->Unlock();
If you're wondering, I'm planning on stuffing this into a for loop.

Question about cpp classes and objects

04 June 2011 - 12:21 PM

2 Quick questions;

Is it possible for a cpp class to stack like so?

WINDOW Window("title");
CHILDWINDOW Window.Child("child title");
BUTTON Window.child.Button("Submit", 10, 10, 100, 20);

And is it possible for an object to access a variable from it's parent?
(the child window needs to access hInstance and hWnd from the main window)

Thank you.

Need a bit of help finding a bug

09 December 2010 - 09:45 AM

Sorry about the bad title, it's not very descriptive.

This program is supposed to load a bitmap and output it on the screen. However, the program compiles fine, but when it runs, it just shows a black screen, then exits with no errors.
I've traced the bug to "initDirect3D(hWnd, iWith, iHeight)" (in main.cpp). That's where the program ends.

What I'd like you to help me with is;
A: What did I do wrong? Why isn't this working.
B: Is there anything else I should change? Do you notice any pitfalls I might have stepped in or do you see any programming conventions that don't make sense?

If you need to know more about this project's setup just ask and I'll post some more details a.s.a.p

main.cpp

#include <windows.h>
#include <tchar.h>
#include <d3d9.h>
#include <d3dx9.h>
#include "direct3D.h"

HINSTANCE hInst;
HWND hWnd;

int iWidth = 1280;
int iHeight = 960;

bool InitWindow(HINSTANCE hInstance, int iWidth, int iHeight);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow){
if(!InitWindow(hInstance, iWidth, iHeight)){
return false;
}
if(!initDirect3D(hWnd, iWidth, iHeight)){
return false;
}

MSG msg = {0};
while(msg.message != WM_QUIT){
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
} else {
render();
}
}

cleanUp();
return (int) msg.wParam;
}

bool InitWindow(HINSTANCE hInstance, int iWidth, int iHeight){
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = 0;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = TEXT("DirectX 1");
wcex.hIconSm = 0;
RegisterClassEx(&wcex);

RECT rect = {0, 0, iWidth, iHeight};
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);

hWnd = CreateWindow(TEXT("DirectX 1"), //class name
TEXT("DirectX 1"), //window title
WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE,
CW_USEDEFAULT, //initial x position
CW_USEDEFAULT, //initial y postion
iWidth, //width
iHeight, //height
NULL, //parent handle
NULL, //menu handle
hInstance, //instance handle
NULL);

if(!hWnd){
return false;
}

ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);

return true;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
switch(message){
case WM_KEYDOWN:
switch(wParam){
case VK_ESCAPE:
PostQuitMessage(0);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}

return DefWindowProc(hWnd, message, wParam, lParam);
}





direct3D.h
LPDIRECT3D9			pD3D;		//the Direct3d object
LPDIRECT3DDEVICE9 pd3dDevice; //the Direct3D device
IDirect3DSurface9* surface;

bool initDirect3D(HWND hWnd, int iWidth, int iHeight){
pD3D = NULL;
pd3dDevice = NULL;
HRESULT hResult;

if(NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION))){
return false;
}

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = FALSE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferWidth = iWidth;
d3dpp.BackBufferHeight = iHeight;
d3dpp.hDeviceWindow = hWnd;

if(FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_REF,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&pd3dDevice))){
return false;
}

hResult = pd3dDevice->CreateOffscreenPlainSurface(iWidth, iHeight, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &surface, NULL);
if(FAILED(hResult)){
return NULL;
}

hResult = D3DXLoadSurfaceFromFile(surface, NULL, NULL, L"test.bmp", NULL, D3DX_DEFAULT, 0, NULL);
if(FAILED(hResult)){
return NULL;
}

return true;
}

void render(void){
IDirect3DSurface9* backbuffer = NULL;

if(pd3dDevice == NULL){
return;
}
pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(62, 62, 68), 1.0f, 0);
pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
pd3dDevice->StretchRect(surface, NULL, backbuffer, NULL, D3DTEXF_NONE);

pd3dDevice->Present(NULL, NULL, NULL, NULL);
}

void cleanUp(void){
if(pd3dDevice != NULL){
pd3dDevice->Release();
}
if(pD3D != NULL){
pD3D->Release();
}
}





thank you.

[Edited by - link102 on December 9, 2010 4:12:13 PM]

PARTNERS