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!


Member Since 13 Jun 2008
Offline Last Active May 20 2015 05:00 PM

Topics I've Started

Shader semantics

29 December 2012 - 10:10 PM

I want to use a matrix for my instance position and stuff instead of just a vector, but I don't see a semantic for anything more than float4. I'm using a second vertex buffer for my instance data.

Looking here, I see nothing for a matrix / float4x4.

This, however, makes it look like I can do it, but I have to have 4 seperate parts for the input layout, then I can just simply use it as a float4x4 in the shader? Is that right?

But what is WORLDVIEW? Why isn't it on the MSDN list?

Also, should I be striving to pad my structures out to a specific size?

missing type specifier, why?

01 July 2010 - 08:21 PM

#pragma once
#include "math.h"
#include "vector.h"

namespace bnr {
namespace math {
struct Matrix
void zero ();
void copy (const Matrix& m);

void identity ();
void perspective(f32 fovy, f32 aspect, f32 zn, f32 zf);

void add (const Matrix& m);
void subtract (const Matrix& m);
void multiply (const Matrix& m);
void divide (const Matrix& m);

void scale (const Vector& v);
void scale (f32 x, f32 y, f32 z);
void translate (const Vector& v);
void translate (f32 x, f32 y, f32 z);
void rotate (f32 x, f32 y, f32 z, f32 a);
void rotateX (f32 a);
void rotateY (f32 a);
void rotateZ (f32 a);

__declspec(align(16)) union
f32 _11, _12, _13, _14,
_21, _22, _23, _24,
_31, _32, _33, _34,
_41, _42, _43, _44;

f32 m[4][4];
__m128 m128[4];
inline void Matrix::zero() { memset(this->m, 0, 64); }
inline void Matrix::copy(const Matrix& m) { memcpy(this->m, m.m, 64); }

... hundreds of lines of other functions...


#pragma once
#include "math.h"
#include "matrix.h"

namespace bnr {
namespace math {
struct Vector
Vector() : x(0.0f), y(0.0f), z(0.0f), w(1.0f) { };
Vector(f32 xp, f32 yp, f32 zp) : x(xp), y(yp), z(zp), w(1.0f) { };
Vector(f32 xp, f32 yp, f32 zp, f32 wp) : x(xp), y(yp), z(zp), w(wp) { };

void multiply (const Matrix& m);
void multiply (const Vector& v, const Matrix& m);

__declspec(align(16)) union
struct{ f32 x, y, z, w; };
f32 v[4];
__m128 v128;
inline void Vector::multiply(const Matrix& m) { this->multiply(*this, m); }

inline void Vector::multiply(const Vector& v, const Matrix& m)
__m128 vx = _mm_shuffle_ps(v.v128, v.v128, 0x00); //broadcasts
__m128 vy = _mm_shuffle_ps(v.v128, v.v128, 0x55);
__m128 vz = _mm_shuffle_ps(v.v128, v.v128, 0xAA);
__m128 vw = _mm_shuffle_ps(v.v128, v.v128, 0xFF);

vx = _mm_mul_ps(vx, m.m128[0]);
vy = _mm_mul_ps(vy, m.m128[1]);
vz = _mm_mul_ps(vz, m.m128[2]);
vw = _mm_mul_ps(vw, m.m128[3]);

vx = _mm_add_ps(vx, vy);
vx = _mm_add_ps(vx, vz);
this->v128 = _mm_add_ps(vx, vw);

first error points here:
void multiply	(const Matrix& m);

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
syntax error : missing ',' before '&'

and 20 other repeating errors for every instance of Matrix within the header. I don't get it, what am I missing?

Clip space to screen space

15 June 2010 - 01:58 PM

D3DXVECTOR4 v4(1, 2, 3, 1);
D3DXMatrixPerspectiveFovLH(&m, math::degToRad(65.0f), 1.6f, 0.1f, 100.0f);
D3DXVec4Transform(&v, &v4, &m);

So, with this, the vertex (as represented by the vector) is effectively in clip space now, correct? I'm not sure how to do the final transformation from that point to screen space however.

w ends up as 3.

//assuming 320 * 180 as res
v.x /= v.w;
v.y /= v.w;

int sx = (v.x * 160) + 160;
int sy = (v.y * 90) + 90;

I feel like I'm missing something here. Does anyone have any insight? Should x and y after the divide be clamped or discarded if over / under a certain value? I can't seem to find too much information on this, so it's kind of shaky. Ultimately, I'm trying to mimic whatever behavior DX does but it doesn't seem to explain clip space or the transform like this anywhere in its documents.

Linking problem, redefinition

27 May 2010 - 02:57 PM

I'm playing around with completely overhauling my code for a more object oriented / expandable approach and I'm having a linking problem that I'm too dumb to figure out. I don't get it why it's already defined, what am I missing?
1>gdi.obj : error LNK2005: "public: __thiscall bnr::video::Color::Color(void)" (??0Color@video@bnr@@QAE@XZ) already defined in renderer.obj

1>gdi.obj : error LNK2005: "public: __thiscall bnr::video::Color::Color(unsigned int)" (??0Color@video@bnr@@QAE@I@Z) already defined in renderer.obj

1>gdi.obj : error LNK2005: "public: __thiscall bnr::video::Color::Color(unsigned char,unsigned char,unsigned char,unsigned char)" (??0Color@video@bnr@@QAE@EEEE@Z) already defined in renderer.obj
#pragma once
#include "types.h"

namespace bnr	{
namespace video	{
	struct Color
		Color(u32 c);
		Color(u8 a, u8 r, u8 g, u8 b);

				u8 r;
				u8 g;
				u8 b;
				u8 a;

			u8	c8[4];
			u32 c32;
	Color::Color()							{	c32 = 0x00000000;	}
	Color::Color(u32 c)						{	c32 = c;			}
	Color::Color(u8 a, u8 r, u8 g, u8 b)	{	c8[0] = r;	
												c8[1] = g;
												c8[2] = b;
												c8[3] = a;			}	//hmm... byte ordering

#pragma once
#include "color.h"
#include "matrix.h"

namespace bnr	{
namespace video	{
		BNR_WORLD		= 0,
		BNR_VIEW		= 1,

	class Renderer
		virtual ~Renderer	(){ };

		virtual void init	(s32 x, s32 y)	= 0;
		virtual void destroy()				= 0;
		virtual void draw	()				= 0;

		virtual void drawPixel	(s32 x, s32 y, const Color& col)					= 0;
		virtual void drawLine	(s32 x1, s32 y1, s32 x2, s32 y2, const Color& col)	= 0;

		void setTransform	(TRANSFORMTYPE flag, const math::Matrix& m);

		s32	width;
		s32	height;		

		math::Matrix world;
		math::Matrix view;
		math::Matrix projection;

#pragma once
#include <Windows.h>
#include "renderer.h"

namespace bnr	{
namespace video	{
	class Gdi: public Renderer
		Gdi	();
		Gdi	(s32 w, s32 h);

		void init	(s32 x, s32 y);
		void destroy();
		void draw	();

		void drawPixel	(s32 x, s32 y, const Color& col);
		void drawLine	(s32 x1, s32 y1, s32 x2, s32 y2, const Color& col);

		HWND		hwnd;
		HDC			hdc,
		HBITMAP		dib,
		void*		bits;	//pointer to "framebuffer"

#include "gdi.h"

namespace bnr	{
namespace video	{
	Gdi::Gdi()				{	init(320, 180);	}
	Gdi::Gdi(s32 w, s32 h)	{	init(w, h);		}
	Gdi::~Gdi()				{	destroy();		}

	void Gdi::init(s32 w, s32 h)
		width	= w;
		height	= h;

		memset(&bi, 0, sizeof(BITMAPINFO));
		bi.bmiHeader.biSize     = sizeof(BITMAPINFO);
		bi.bmiHeader.biWidth    = width;
		bi.bmiHeader.biHeight   = -height;	//top down dib
		bi.bmiHeader.biPlanes   = 1;
		bi.bmiHeader.biBitCount = 32;

		hdc		= GetDC(GetForegroundWindow());
		hdcMem	= CreateCompatibleDC(hdc);
		dib		= CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, &bits, NULL, 0);
		hbmOld	= (HBITMAP)SelectObject(hdcMem, dib);

	void Gdi::destroy()
		SelectObject(hdcMem, hbmOld);
		ReleaseDC(hwnd, hdc);

	void Gdi::draw(void)
		BitBlt(hdc, 0, 0, width, height, hdcMem, 0, 0, SRCCOPY);
		memset(bits, 0, (width * height) * 4);

	void Gdi::drawPixel(s32 x, s32 y, const Color& col)
		if((x | y) >= 0 && x < width  && y < height) //check bounds
			u32* dst = (u32*)bits + (y * width + x);
			if(col.a == 0x00)	{	*dst = 0x00000000;	return;		}			
			if(col.a == 0xFF)	{	*dst = col.c32;		return;		}
				DWORD dst_ag = *dst		& 0xFF00FF00;
				DWORD dst_rb = *dst		& 0x00FF00FF;
				DWORD src_ag = col.c32	& 0xFF00FF00;
				DWORD src_rb = col.c32	& 0x00FF00FF;

				dst_ag = src_ag - dst_ag;
				dst_rb = src_rb - dst_rb;

				dst_ag *= col.a;
				dst_rb *= col.a;  
				dst_ag >>= 8;
				dst_rb >>= 8;

				dst_ag = dst_ag & 0xFF00FF00;
				dst_rb = dst_rb & 0x00FF00FF;
				*dst = ((dst_ag | dst_rb) + *dst);

	void Gdi::drawLine(s32 x1, s32 y1, s32 x2, s32 y2, const Color& col)
		//not implemented

How can I improve my code?

13 May 2010 - 06:05 PM

http://www.mediafire.com/?wmjzdilzj2z It's simplistic and small. Really, I'd like ideas of things to look into and some overall insight. Now that I've finished class, I have plenty of time over the summer to read something. Up until now I've really only dabbled in C and for the most part, I've just went with the flow and picked up what I needed as I went. I tried to quickly bring over my C version of this to a more C++ish manor but I think I'm falling into the so called C with classes problem that I hear about. Looking at the DX / GL docs it was pretty easy to get everything working, but what can I be doing differently / better?