PDA

View Full Version : S2DL questions



jdarling
05-09-2006, 08:54 PM
Ok, so I've been playing more and more with S2DL while trying to get things wrapped up on a few projects. Now I have a few questions:

1) How exactly do I resize the SDL window once its open, and can this be done in mid render (IE: After the clear and before the flip)?
2) How do I go about switching between full screen and windowed mode?
3) Is there a way to load any 3D model formats?
4) Why do some MS Paint bitmap images not render properly (color is all off)
5) Where is the support for GLSL, or should I make use of the standard OpenGL version? Also any samples?
6) Any frigging tutorials out there, not just samples, but tutorials :)?
7) Is S2DL only for 2D SDL applications, or have I not figured out how to integrate 3D yet :)? (I'm guessing that since its Simple 2D that its only for 2D)

Anyways, I know some of the questions will seem a bit ignorant, but I have to ask :). Thanks for any answers.

paul_nicholls
06-09-2006, 03:37 AM
Ok, so I've been playing more and more with S2DL while trying to get things wrapped up on a few projects. Now I have a few questions:

1) How exactly do I resize the SDL window once its open, and can this be done in mid render (IE: After the clear and before the flip)?
2) How do I go about switching between full screen and windowed mode?
3) Is there a way to load any 3D model formats?
4) Why do some MS Paint bitmap images not render properly (color is all off)
5) Where is the support for GLSL, or should I make use of the standard OpenGL version? Also any samples?
6) Any frigging tutorials out there, not just samples, but tutorials :)?
7) Is S2DL only for 2D SDL applications, or have I not figured out how to integrate 3D yet :)? (I'm guessing that since its Simple 2D that its only for 2D)

Anyways, I know some of the questions will seem a bit ignorant, but I have to ask :). Thanks for any answers.

I am not sure about the other questions, but for using GLSL I made a helper unit.

It uses glext.pas that came with JEDI-SDL (you may be able to use that too).

At the moment, it only supports

GL_ARB_Vertex_Shader, GL_ARB_Fragment_Shader and GL_ARB_Program, but the classes can be overriden so that other versions could be used :)

Initializing and loading shaders:


If (Load_GL_ARB_shader_objects) And (Load_GL_ARB_vertex_shader) And (Load_GL_ARB_fragment_shader) Then
Begin
FShaders[0] := TGL_ARB_Vertex_Shader.Create;
FShaders[1] := TGL_ARB_Fragment_Shader.Create;

Try
If (Not FShaders[0].LoadAndCompileShaderSource('pointlight .vert')) Then
Logger.LogError(FShaders[0].GetInfoLog,'''pointlig ht.vert''');

If (Not FShaders[1].LoadAndCompileShaderSource('pointlight .frag')) Then
Logger.LogError(FShaders[1].GetInfoLog,'''pointlig ht.frag''');
Except
On E:Exception Do
Logger.LogError(E.Message,'');
End;

FShaderProgram := TGL_ARB_Program.Create;

For i := 0 To High(FShaders) Do
FShaderProgram.AttachShader(FShaders[i]);

FShaderProgram.]

Using shaders:
[code] FShaderProgram.Enable;

// draw balls
For i := 0 To FBalls.Count - 1 Do
Begin
Ball := TNewtonObject(FBalls.Objects[i]);

FShaderProgram.SetUniform('MaterialAmbient',
[
PlayerColours[Ball.Tag].r * 0.3,
PlayerColours[Ball.Tag].g * 0.3,
PlayerColours[Ball.Tag].b * 0.3,
1
]);

FShaderProgram.SetUniform('MaterialDiffuse',
[
PlayerColours[Ball.Tag].r,
PlayerColours[Ball.Tag].g,
PlayerColours[Ball.Tag].b,
1
]);

FShaderProgram.SetUniform('MaterialSpecular',
[
0.9,0.9,0.9,1
]);

FShaderProgram.SetUniform('MaterialShininess',[50] );


Ball.Render;

// draw balls selection
If (Ball.Selected) Then
RenderBallSelection(Ball);
End;

FShaderProgram.Disable;


Vertex shader:

/* -------------------------------------------------------
This shader implements a point light per pixel using the
diffuse, specular, and ambient terms acoording to "Mathematics of Lighthing"
as found in the book "OpenGL Programming Guide" (aka the Red Book)
--------------------------------------------------------- */

varying vec4 diffuse,ambientGlobal,ambient;
varying vec3 normal,lightDir,halfVector;
varying float dist;

uniform vec4 MaterialAmbient;
uniform vec4 MaterialDiffuse;
void main()
{
vec4 ecPos;
vec3 aux;

/* first transform the normal into eye space and normalize the result */
normal = normalize(gl_NormalMatrix * gl_Normal);

/* now normalize the light's direction. Note that according to the
OpenGL specification, the light is stored in eye space. Also since
we're talking about a directional light, the position field is actually
direction */
ecPos = gl_ModelViewMatrix * gl_Vertex;
aux = vec3(gl_LightSource[0].position-ecPos);
lightDir = normalize(aux);

/* compute the distance to the light source to a varying variable*/
dist = length(aux);

/* Normalize the halfVector to pass it to the fragment shader */
halfVector = normalize(gl_LightSource[0].halfVector.xyz);

/* Compute the diffuse, ambient and globalAmbient terms */
/*
diffuse = gl_FrontMaterial.diffuse * gl_LightSource[0].diffuse;
ambient = gl_FrontMaterial.ambient * gl_LightSource[0].ambient;
ambientGlobal = gl_LightModel.ambient * gl_FrontMaterial.ambient;
*/

diffuse = MaterialDiffuse * gl_LightSource[0].diffuse;
ambient = MaterialAmbient * gl_LightSource[0].ambient;
ambientGlobal = gl_LightModel.ambient * MaterialAmbient;

gl_Position = ftransform();
}

Fragment shader:


/* -------------------------------------------------------

This shader implements a point light per pixel using the
diffuse, specular, and ambient terms acoording to "Mathematics of Lighthing"
as found in the book "OpenGL Programming Guide" (aka the Red Book)

--------------------------------------------------------- */

varying vec4 diffuse,ambientGlobal,ambient;
varying vec3 normal,lightDir,halfVector;
varying float dist;

uniform float MaterialShininess;
uniform vec4 MaterialSpecular;

void main()
{
vec3 n,halfV,viewV,ldir;
float NdotL,NdotHV;
vec4 color = ambientGlobal;
float att;

/* a fragment shader can't write a verying variable, hence we need
a new variable to store the normalized interpolated normal */
n = normalize(normal);

/* compute the dot product between normal and ldir */
NdotL = max(dot(n,normalize(lightDir)),0.0);

if (NdotL > 0.0) {

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

att = 1.0;
color += att * (diffuse * NdotL + ambient);

halfV = normalize(halfVector);
NdotHV = max(dot(n,halfV),0.0);
/* color += att * gl_FrontMaterial.specular * gl_LightSource[0].specular * pow(NdotHV,gl_FrontMaterial.shininess);*/
color += att * MaterialSpecular * gl_LightSource[0].specular * pow(NdotHV,MaterialShininess);
}

gl_FragColor = color;
}

Unit code

Unit GL_Shaders;
{................................................. ......}
// created by Paul Nicholls
// Copyright 2006
// Use for anything that you want, but
// please email me with any improvements that you may make :)
//
// Email:
// paul_nicholls.hotmail.com
{................................................. ......}
Interface

Uses
gl,
glext;

Type
TGL_Shader = Class
Private
Protected
FHandle: GLuint;
Public

Function LoadAndCompileShaderSource(AFileName: String): Boolean; Virtual;

Function GetInfoLog: String; Virtual;

Property Handle: GLuint Read FHandle;
End;

TGL_ARB_Shader = Class(TGL_Shader)
Protected
Public
Constructor Create; Virtual;
Destructor Destroy; Override;

Function LoadAndCompileShaderSource(AFileName: String): Boolean; Override;

Function GetInfoLog: String; Override;
End;

TGL_ARB_Vertex_Shader = Class(TGL_ARB_Shader)
Private
Public
Constructor Create; Override;
Destructor Destroy; Override;
End;

TGL_ARB_Fragment_Shader = Class(TGL_ARB_Shader)
Private
Public
Constructor Create; Override;
Destructor Destroy; Override;
End;

TGL_Program = Class
Private
Protected
FHandle: GLuint;
Public
Constructor Create; Virtual;
Destructor Destroy; Override;

Procedure AttachShader(Const AShader: TGL_Shader); Virtual;
Procedure Link; Virtual;
Procedure Enable; Virtual;
Procedure Disable; Virtual;

Procedure SetUniform(Const AName: String;
Const AValue: GLint); Overload; Virtual;
Procedure SetUniform(Const AName: String;
Const AValues: Array Of GLfloat); Overload; Virtual;
End;

TGL_ARB_Program = Class(TGL_Program)
Public
Constructor Create; Override;
Destructor Destroy; Override;

Procedure AttachShader(Const AShader: TGL_Shader); Override;
Procedure Link; Override;
Procedure Enable; Override;
Procedure Disable; Override;

Procedure SetUniform(Const AName: String;
Const AValue: GLint); Overload; Override;
Procedure SetUniform(Const AName: String;
Const AValues: Array Of GLfloat); Overload; Override;
End;

Implementation

Uses
SysUtils,Classes;

Function TGL_Shader.LoadAndCompileShaderSource(AFileName: String): Boolean;
Begin
Result := False;
End;
{................................................. ......}

{................................................. ......}
Function TGL_Shader.GetInfoLog: String;
Begin
Result := '';
End;
{................................................. ......}

{................................................. ......}
Constructor TGL_ARB_Shader.Create;
Begin
Inherited Create;

FHandle := 0;
End;
{................................................. ......}

{................................................. ......}
Destructor TGL_ARB_Shader.Destroy;
Begin
If &#40;FHandle <> 0&#41; Then
glDeleteObjectARB&#40;FHandle&#41;;

Inherited Destroy;
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
Function TGL_ARB_Shader.LoadAndCompileShaderSource&#40;AFileNam e&#58; String&#41;&#58; Boolean;
Var
ShaderSource &#58; TStringList;
Source &#58; String;
LinkStatus &#58; GLint;
CompileStatus &#58; GLint;
SourceLength &#58; Integer;
Begin
Result &#58;= False;

If &#40;Not FileExists&#40;AFileName&#41;&#41; Then
Exit;

ShaderSource &#58;= TStringList.Create;

Try
ShaderSource.LoadFromFile&#40;AFileName&#41;;

If &#40;ShaderSource.Text = ''&#41; Then
Exit;

Source &#58;= ShaderSource.Text;

SourceLength &#58;= Length&#40;Source&#41;;

glShaderSourceARB&#40;FHandle,1,@Source,@SourceLength&#41; ;

glCompileShaderARB&#40;FHandle&#41;;

glGetObjectParameterivARB&#40;FHandle,GL_OBJECT_LINK_S TATUS_ARB , @LinkStatus&#41;;
glGetObjectParameterivARB&#40;FHandle,GL_OBJECT_COMPIL E_STATUS_ARB, @CompileStatus&#41;;

Result &#58;= &#40;LinkStatus = 1&#41; And &#40;CompileStatus = 1&#41;;
Finally
ShaderSource.Free;
End;
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
Function TGL_ARB_Shader.GetInfoLog&#58; String;
Var
InfoLog &#58; PGLcharARB;
InfoLogLength &#58; GLint;
Begin
glGetObjectParameterivARB&#40;FHandle,GL_OBJECT_INFO_L OG_LENGTH_ARB,@InfoLogLength&#41;;

GetMem&#40;InfoLog,InfoLogLength + 1&#41;;

glGetInfoLogARB&#40;FHandle,InfoLogLength,@InfoLogLeng th,InfoLog&#41;;

Result &#58;= String&#40;InfoLog&#41;;

FreeMem&#40;InfoLog,InfoLogLength + 1&#41;;
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
Constructor TGL_ARB_Vertex_Shader.Create;
Begin
Inherited Create;

FHandle &#58;= glCreateShaderObjectARB&#40;GL_VERTEX_SHADER_ARB&#41;;
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
Destructor TGL_ARB_Vertex_Shader.Destroy;
Begin
Inherited Destroy;
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
Constructor TGL_ARB_Fragment_Shader.Create;
Begin
Inherited Create;

FHandle &#58;= glCreateShaderObjectARB&#40;GL_FRAGMENT_SHADER_ARB&#41;;
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
Destructor TGL_ARB_Fragment_Shader.Destroy;
Begin
Inherited Destroy;
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
Constructor TGL_Program.Create;
Begin
Inherited Create;

FHandle &#58;= 0;
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
Destructor TGL_Program.Destroy;
Begin
If &#40;FHandle <> 0&#41; Then
glDeleteObjectARB&#40;FHandle&#41;;

Inherited Destroy;
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
Procedure TGL_Program.AttachShader&#40;Const AShader&#58; TGL_Shader&#41;;
Begin
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
Procedure TGL_Program.Link;
Begin
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
Procedure TGL_Program.Enable;
Begin
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
Procedure TGL_Program.Disable;
Begin
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
Procedure TGL_Program.SetUniform&#40;Const AName&#58; String;
Const AValue&#58; GLint&#41;;
Begin
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
Procedure TGL_Program.SetUniform&#40;Const AName&#58; String;
Const AValues&#58; Array Of GLfloat&#41;;
Begin
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
Constructor TGL_ARB_Program.Create;
Begin
Inherited Create;

FHandle &#58;= glCreateprogramObjectARB;
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
Destructor TGL_ARB_Program.Destroy;
Begin
Inherited Destroy;
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
Procedure TGL_ARB_Program.AttachShader&#40;Const AShader&#58; TGL_Shader&#41;;
Begin
glAttachObjectARB&#40;FHandle,AShader.Handle&#41;;
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
Procedure TGL_ARB_Program.Link;
Begin
glLinkProgramARB&#40;FHandle&#41;;
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
Procedure TGL_ARB_Program.Enable;
Begin
glUseProgramObjectARB&#40;FHandle&#41;;
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
Procedure TGL_ARB_Program.Disable;
Begin
glUseProgramObjectARB&#40;0&#41;;
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
Procedure TGL_ARB_Program.SetUniform&#40;Const AName&#58; String;
Const AValue&#58; GLint&#41;;
Begin
glUniform1iARB&#40;glGetUniformLocationARB&#40;FHandle,PGL CharArb&#40;AName&#41;&#41;,
AValue&#41;;
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
Procedure TGL_ARB_Program.SetUniform&#40;Const AName&#58; String;
Const AValues&#58; Array Of GLfloat&#41;;
Var
ParamCount&#58; Integer;
NameArb &#58; PGLCharArb;
Begin
ParamCount &#58;= Length&#40;AValues&#41;;

NameArb &#58;= PGLCharArb&#40;AName&#41;;

Case ParamCount Of
1&#58; glUniform1fARB&#40;glGetUniformLocationARB&#40;FHandle, NameArb&#41;,
AValues&#91;0&#93;&#41;;
2&#58; glUniform2fARB&#40;glGetUniformLocationARB&#40;FHandle, NameArb&#41;,
AValues&#91;0&#93;,AValues&#91;1&#93;&#41;;
3&#58; glUniform3fARB&#40;glGetUniformLocationARB&#40;FHandle, NameArb&#41;,
AValues&#91;0&#93;,AValues&#91;1&#93;,AValues&#91;2&#93;&#41;;
4&#58; glUniform4fARB&#40;glGetUniformLocationARB&#40;FHandle, NameArb&#41;,
AValues&#91;0&#93;,AValues&#91;1&#93;,AValues&#91;2&#93;,AValues&#91;3&#93;&#41;;
Else
End;
End;
&#123;................................................. ......&#125;

&#123;................................................. ......&#125;
End.

cairnswm
06-09-2006, 06:04 AM
:arrow: 1) How exactly do I resize the SDL window once its open, and can this be done in mid render (IE: After the clear and before the flip)?

S2DL has no support for resizing the window once its open.

:arrow: 2) How do I go about switching between full screen and windowed mode?

I usually set it in the config file for the project. FullScreen=Y will run the game in full screen mode.

:arrow: 3) Is there a way to load any 3D model formats?

Not in S2DL - the 2D means 2D :) - however I know that SDL would allow 3D models in some formats. I've got a pas file somewhere for loading Milkshape files.

:arrow: 4) Why do some MS Paint bitmap images not render properly (color is all off)

Make sure they are textures in power of 2 size. I know that S2DL does not work with non power of 2 files. I alos only use PNGs and JPGs as files.

:arrow: 5) Where is the support for GLSL, or should I make use of the standard OpenGL version? Also any samples?

Not in S2DL - maybe in normal SDL

:arrow: 6) Any frigging tutorials out there, not just samples, but tutorials ?

There is only one for S2DL. As I have never had any feedback on it I never went ahead and did any more.

:arrow: 7) Is S2DL only for 2D SDL applications, or have I not figured out how to integrate 3D yet ? (I'm guessing that since its Simple 2D that its only for 2D)

S2DL is for 2D only. I personally have no real interest in doing 3D apps at the moment - and there are enough other 3D libraries out there (GLScene, GLXtreem etc)

What version of S2DL are you using. The latest version is 1.10 (Should be in the header of each S2DL file).

PS. Nice to know someone has tried using it :)

WILL
06-09-2006, 08:25 AM
Hmm... do you guys feel that S2DL requires it's own forum?

This thread seems a little out of place in the JEDI-SDL forum, but where would it best be put?

cairnswm
06-09-2006, 10:54 AM
S2DL is a set of wrappers around SDL and OpenGL. It pretty much is JEDI-SDL so I'm quite happy to leave it in the JEDI-SDL forum.

Anyway I dont think there enough traffic for a S2DL forum :)

Also I hope that S2DL will be distributed with JEDI-SDL v 1 - but I still need to package it right for Savage.

jdarling
06-09-2006, 01:00 PM
@paul_nicholls: Any chance you would put that up as a download with a compiling sample?

@cairnswm: I'm using 1.03 right now, so I'll update to 1.10 after I get the other bugs figured out. The problem with the images was with 256x256 24 bit BMP files so everything should have lined up properly with what SDL and S2DL were expecting.

As far as using it, so far its not too bad. I like the fact that I can wrap the calls with standard OpenGL calls to do things such as rotation, translation, and scaling.

A few more questions:
8) Can I capture the current rendered screen to a bitmap easily?

9) Since S2DL doesn't have anything in it for window size and swapping from windowed to full screen, then do the standard SDL calls?

10) What application config file? It looks for one by default some place?

Will, as cairnswm said, since S2DL is built on top of Jedi-SDL it makes sense to keep the conversations about it here (unless of course there get to be alot of them).

EDIT: I can't find the download link to the latest version. Mind placing that up :). Checked your site, but due to my FireFox it doesn't render properly and I can't find the link (if its there). You should consider testing your site out in both FireFox and IE :)

cairnswm
06-09-2006, 01:13 PM
:arrow: 8 ) Can I capture the current rendered screen to a bitmap easily?

I havn't added it

:arrow: 9) Since S2DL doesn't have anything in it for window size and swapping from windowed to full screen, then do the standard SDL calls?

I dont know - lower level stuff than I usually do :)

:arrow: OpenGL calls for Roatation, Blending etc

1.10 includes all these drawign functions (though I believe the rotation ones dont work correctly)

:arrow: 10) What application config file? It looks for one by default some place?

It looks for a default application config file in the Application directory with the same <name as the application>.cfg (I think - cant check right now)

S2DL v 1.10 is downloadable from http://www.cairnsgames.co.za/files/s2dlv1-10.zip
It includes tutorials on using S2DL with Lazarus
I dont know how backwards compatible it is with 1.03

jdarling
06-09-2006, 01:16 PM
Thanks cairnswm I'll check it out once I get the other updates completed (witch may take a while).

WILL
06-09-2006, 04:24 PM
S2DL is a set of wrappers around SDL and OpenGL. It pretty much is JEDI-SDL so I'm quite happy to leave it in the JEDI-SDL forum.

Anyway I dont think there enough traffic for a S2DL forum :)

Also I hope that S2DL will be distributed with JEDI-SDL v 1 - but I still need to package it right for Savage.

Do you mean it is like JEDI-SDL or is meant to be a part of JEDI-SDL?

From waht you're saying here it would only make sense for it to be a temporary patch project to suppliment what JEDI-SDL may lack in features or capability. Otherwise it is classified as a seperate project and only based off the JEDI-SDL concept.

Then again you mention packaging it with JEDI-SDL. So how can it be used with the library to compliment it?

I appologize if I'm asking what might seem obvious, I've not really been following along with S2DL's progress. :?

cairnswm
06-09-2006, 07:00 PM
Hi Will

Not quite sure what you mean.

S2DL needs SDL to work. SDL does not need S2DL to work. S2DL is just an implementation of object wrappers for SDL (and some OpenGL thrown in).

WILL
06-09-2006, 07:14 PM
SDL is the library, like DirectX and OpenGL/OpenAL, etc... this we both know.

But JEDI-SDL is a library that encompases the functionality of SDL into, if you will, an outer shell of that library.

This forum is for JEDI-SDL, not just any SDL library. Much like the (Un)DelphiX forum is for it's component suite.

If S2DL does not use or require JEDI-SDL or does not enhance JEDI-SDL in any way then it does not belong here.

WILL
06-09-2006, 07:15 PM
SDL is the library, like DirectX and OpenGL/OpenAL, etc... this we both know.

But JEDI-SDL is a library that encompases the functionality of SDL into, if you will, an outer shell of that library.

This forum is for JEDI-SDL, not just any SDL library. Much like the (Un)DelphiX forum is for it's component suite.

If S2DL does not use or require JEDI-SDL or does not enhance JEDI-SDL in any way then it does not belong here.

No knowing where else to put this right now, I'll leave these S2DL topics here for the meantime, but we should collectively decide where they are best suited.

cairnswm
06-09-2006, 07:18 PM
S2DL enhances JEDI-SDL - it wraps the JEDI-SDL calls into classes.

S2DL cannot function without JEDI-SDL being installed - it is NOT a new set of SDL wrappers.

WILL
06-09-2006, 07:24 PM
Ok so it's an enhancement that takes standard JEDI-SDL calls and puts them into class objects so the library becomes all object based?

I didn't get that from what you posted previously. :)

Ok thats fine then it does belong here. I just wanted to confirm. (And also so I can also set others straight in the future.)

paul_nicholls
07-09-2006, 02:31 AM
@paul_nicholls: Any chance you would put that up as a download with a compiling sample?


Hi Jeremy, I will have to make up a stand-alone demo app for the shader code as at the moment I only use it in my AbaloneGL game but that shouldn't be too difficult :)

cheers,
Paul.