Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

BraXi

Member Since 12 Jun 2012
Offline Last Active Dec 09 2012 09:05 AM
-----

Topics I've Started

Weird rendering bug

24 November 2012 - 08:21 AM

Hello, I've recently ran into trouble regarding rendering of Q3 BSP. So I've readen all important data from bsp and checked all pointers to data from mesh and I'm trying to render level with vertex arrays.

Somehow, instead of level I'm seeing weird planes (picture below):
http://s6.ifotos.pl/img/bug3JPG_xnaxrhn.JPG

And this is what I'm trying to render:
http://s4.ifotos.pl/img/radiantsh_xnaxsns.JPG


At the rendering stage I'm rendering only brushes (polygon groups built from triangles) with this function:
void BSPMesh::RenderPolygonFace( int faceIndex )
{
tBSPFace *pFace = &m_pFaces[faceIndex];
glVertexPointer( 3, GL_FLOAT, sizeof(tBSPVertex), &m_pVerts[pFace->startVertIndex].vPosition );
glTexCoordPointer( 2, GL_FLOAT, sizeof(tBSPVertex), &m_pVerts[pFace->startVertIndex].vTextureCoord );
glEnableClientState( GL_VERTEX_ARRAY );
glEnableClientState( GL_TEXTURE_COORD_ARRAY );
if(0)
{
  glEnable( GL_TEXTURE_2D );
  glBindTexture( GL_TEXTURE_2D,  m_textures[pFace->textureID] );
}
glDrawElements( GL_TRIANGLES, pFace->numOfIndices, GL_UNSIGNED_INT, &m_pIndices[pFace->startIndex] );
glDisableClientState( GL_VERTEX_ARRAY );
glDisableClientState( GL_TEXTURE_COORD_ARRAY );
}

I'm using OpenGL 2.0 on a Nvidia GF 7600GS - yeah it's pretty old card.

Sharing data between DLL and program

26 September 2012 - 12:00 PM

So I was experimenting with DLL files and i though about sharing dynamic array of objects between DLL and EXE.
The only thing worth studying was quake 3, which loaded DLL to call game rules and manage objects, while EXE was only providing some data-access functions (for example rendering). After studying the code I've written test application in C that would load DLL file and share specified variables with program.
I was a bit confused when once set variables were unable to update, for example i've initialized myVar with value of 1 and later tried to change it 2. For some reason, value wasn't updated in EXE. I'd like to know how can i link dll dynamicaly so i can update variables in EXE with DLL.

I will attach some code here to show what i mean.

//This code is shared by both DLL and EXE in a "shared.h"
#include <iostream>
#include <windows.h>
using namespace std;
typedef struct
{
void (*TestDLLFunc)();

int testVal;
int *testArray;
} libExport_t;
typedef struct
{
int simpleVal;
} exeExport_t;
typedef libExport_t (*GetAPI_t) (exeExport_t);
libExport_t DLLCode;
exeExport_t EXECode;

// DLL code
#include "shared.h"
void TestFunc()
{
printf( "This message was sent from DLL\n" );

printf( "EXECode.testVal = %i\n", EXECode.testVal ); //this printed value 2012
// this is supposed to update array for EXE
DLLCode.testArray = new int[4]; //c++
for( int i = 0; i < 4; i++ )
  DLLCode.testArray[i] = i;
Sleep( 100 );
DLLCode.testVal = 2;
}

libExport_t GetAPI( exeExport_t import ) // dll's entry point
{
EXECode = import; //initialize exe functions for dll
DLLCode.TestDLLFunc = TestFunc;
DLLCode.testVal = 1;
DLLCode.testArray = NULL;
return DLLCode;
}
// EXE code
#include "shared.h"
HINSTANCE library;
void LoadTestLibrary( const char *name )
{
GetAPI_t GetAPI;

printf( "Loading %s\n", name );
library = LoadLibraryA( name );
if( library == 0 )
  printf( "LoadLibraryA(\"%s\") failed\n", name );
if( ( GetAPI = (GetAPI_t)GetProcAddress( library, "GetAPI" ) ) == 0 )
{
  printf( "Couldn't retrieve vaild adress\n", name );
}
EXECode.simpleVal = 2012;
DLLCode = GetAPI( EXECode );
if( !DLLCode )
  printf( "DLL not loaded\n" );
}

int main()
{
LoadTestLibrary( "test.dll" );

DLLCode.TestDLLFunc();
Sleep( 200 ); // just to make sure everything chaged
if( DLLCode.testArray != NULL )
{
  for( int i = 0; i < 4; i++ )
   printf( "array element is: %i\n", DLLCode.testArray[i] );
}
// this should be 2
printf( "DLLCode.testVal = %i\n", DLLCode.testVal );
return 0;
}

Rendering concept

22 September 2012 - 04:15 PM

Hello, I have one concept idea for rendering and I'm not sure if it's good technique and if there any better.
At initialization i made array with 4096 slots for objects that later will be rendered in 3D mode (players, monsters, props)
and to add object to screen i use:

(pseudo code)
Object *obj = spawnobject();
obj->origin = (0,0,0);
obj->modelIndex = 4; <- the model i want render
obj->show = true;
obj->deleteTime = currenttime + 3000; <- time in miliseconds

spawnobject() is searching for free slots in object array and returns pointer to object.

That way i don't need to add objects to list each time i redraw scene, which i think is faster than creating array of objects each time.

At rendering stage i do this:

for( i=0; i < max objects; i++, object++ )
{
if( !object->used )
continue;

if( object->deletetime >= current time )
{
deleteobject( object );
continue;
}

<at this stage I'm determining which object is visible and if everything is ok i render actual model at given origin>
}


To delete objects i simply memset() all values and change object->used to false to free the slot. Is this method okey? Maybe you have better ideas? I wonder if there are better methods and i would like see your feedback.

Blending multiple lights in deferred renderer

18 August 2012 - 06:13 AM

Hello.
I was looking for methods to render multiple lights without redrawing the scene multiple times and i found article about "Deferred Lighting". It didnt took me much time to render my scene into 4 buffers using FBO, i store 4 textures for position, normals, color and depth, then i render a fullscreen quad in ortho mode and apply lighting shader to it but, as i've suspected my shader doesn't blend multiple lights together so if i draw light B, the light A isnt rendered, now im capable of rendering just one light on scene.

I would like to blend those lights but to be honest GLSL isn't something that i'm experienced in.

This is the light pass shader that i'm using, i found it somewhere but i'm not sure who's the author of it.

Fragment:
varying vec3 p;
varying vec3 sDir;
varying vec4 lpos;
uniform sampler2D normalBuffer;
uniform sampler2D depthBuffer;
uniform sampler2D colorBuffer;
uniform vec3 lightPos;
uniform vec3 lightColor;
uniform float lightRadius;
float color_to_float(vec3 color)
{
const vec3 byte_to_float = vec3(1.0,1.0/256.0,1.0/(256.0*256.0));
return dot(color,byte_to_float);
}
vec3 lighting(vec3 SColor, vec3 SPos, float SRadius, vec3 p, vec3 n, vec3 MDiff, vec3 MSpec, float MShi)
{
vec3 l = SPos-p;
vec3 v = normalize(p);
vec3 h = normalize(v+l);
l = normalize(l);
vec3 Idiff = max(0.0, dot(l, n)) * MDiff * SColor;
float att = max(0.0,1.0-length(l)/SRadius);
vec3 Ispec = pow(max(0.0,dot(h,n)),MShi)*MSpec*SColor;
return att*(Idiff+Ispec);
}
void main()
{
vec3 depthcolor = texture2D(depthBuffer, gl_TexCoord[0].st).rgb;
vec3 n = texture2D(normalBuffer, gl_TexCoord[0].st).rgb;
float pixelDepth = color_to_float(depthcolor);
vec3 WorldPos = pixelDepth * normalize(sDir);
gl_FragColor = vec4(lighting(lightColor, lightPos, lightRadius, WorldPos, n, vec3(1.0,1.0,1.0), vec3(1,1,1) ,1) ,128);
gl_FragColor *= texture2D(colorBuffer, gl_TexCoord[0].st);
}


Vertex:
uniform vec3 lightPos;
attribute vec3 screenDir;
varying vec3 sDir;
varying vec4 lpos;
varying vec4 Ldir;
void main()
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_TexCoord[1] = gl_MultiTexCoord0;
gl_TexCoord[2] = gl_MultiTexCoord0;
gl_Position = ftransform();
sDir = screenDir;
vec4 lp = vec4(lightPos,1);
lpos = lp;
}

[glsl] problem with sending uniform

20 June 2012 - 07:33 AM

Okey so i've used some shaders in my little game which work perfectly and without any troubles but i've hit one problem with light attenuation shader, for some reason i cant send any uniform to it which is very painful for me and i've tried to send uniforms to other shaders which worked without any problems. The problem is that i cannot send my int uniform "lightNum" to attenuation shader, i dont know why its happening because i have that uniform in both vertex and fragment shaders. Shaders compiled without any errors and warnings.
Did somebody had the same trouble before? I can't continue working on light manager without this.

Here's my c++ code to bind and use shader:
shader[1].loadShader( "data/shaders/attenuation.fs", "data/shaders/attenuation.vs" );
	 [....]
    shader[1].begin();
    shader[1].setUniform1i( "lightNum", 1 ); //this throws me an error
	 [.. rendering done here ..]
    shader[1].end();


And here are the shaders:
vertex
varying vec3 normal, lightDir, eyeVec;
varying float att;

uniform int lightNum;

void main()
{
//lightNum = 0;
normal = gl_NormalMatrix * gl_Normal;
vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex);
lightDir = vec3(gl_LightSource[lightNum].position.xyz - vVertex);
eyeVec = -vVertex;

float d = length(lightDir);

att = 1.0 / ( gl_LightSource[lightNum].constantAttenuation + (gl_LightSource[lightNum].linearAttenuation*d) + (gl_LightSource[lightNum].quadraticAttenuation*d*d) );

gl_Position = ftransform(); 
}


fragment
varying vec3 normal, lightDir, eyeVec;
varying float att;

uniform int lightNum;

void main (void)
{
//lightNum = 0;
vec4 final_color = (gl_FrontLightModelProduct.sceneColor * gl_FrontMaterial.ambient) + (gl_LightSource[lightNum].ambient * gl_FrontMaterial.ambient)*att;
	  
vec3 N = normalize(normal);
vec3 L = normalize(lightDir);

float lambertTerm = dot(N,L);

if(lambertTerm > 0.0)
{
  final_color += gl_LightSource[lightNum].diffuse * gl_FrontMaterial.diffuse * lambertTerm * att;
 
  vec3 E = normalize(eyeVec);
  vec3 R = reflect(-L, N);
 
  float specular = pow( max(dot(R, E), 0.0), gl_FrontMaterial.shininess );
 
  final_color += gl_LightSource[lightNum].specular * gl_FrontMaterial.specular * specular * att;
}
gl_FragColor = final_color;  
}

PARTNERS