Originally Posted by
jdarling
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:
[code] 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 ,'''pointlight.vert''');
If (Not FShaders[1].LoadAndCompileShaderSource( ;'pointlight.frag')) Then
Logger.LogError(FShaders[1].GetInfoLog ,'''pointlight.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:
Code:
/* -------------------------------------------------------
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:
Code:
/* -------------------------------------------------------
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
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 (FHandle <> 0) Then
glDeleteObjectARB(FHandle);
Inherited Destroy;
End;
{.......................................................}
{.......................................................}
Function TGL_ARB_Shader.LoadAndCompileShaderSource(AFileName: String): Boolean;
Var
ShaderSource : TStringList;
Source : String;
LinkStatus : GLint;
CompileStatus : GLint;
SourceLength : Integer;
Begin
Result := False;
If (Not FileExists(AFileName)) Then
Exit;
ShaderSource := TStringList.Create;
Try
ShaderSource.LoadFromFile(AFileName);
If (ShaderSource.Text = '') Then
Exit;
Source := ShaderSource.Text;
SourceLength := Length(Source);
glShaderSourceARB(FHandle,1,@Source,@SourceLength);
glCompileShaderARB(FHandle);
glGetObjectParameterivARB(FHandle,GL_OBJECT_LINK_STATUS_ARB , @LinkStatus);
glGetObjectParameterivARB(FHandle,GL_OBJECT_COMPILE_STATUS_ARB, @CompileStatus);
Result := (LinkStatus = 1) And (CompileStatus = 1);
Finally
ShaderSource.Free;
End;
End;
{.......................................................}
{.......................................................}
Function TGL_ARB_Shader.GetInfoLog: String;
Var
InfoLog : PGLcharARB;
InfoLogLength : GLint;
Begin
glGetObjectParameterivARB(FHandle,GL_OBJECT_INFO_LOG_LENGTH_ARB,@InfoLogLength);
GetMem(InfoLog,InfoLogLength + 1);
glGetInfoLogARB(FHandle,InfoLogLength,@InfoLogLength,InfoLog);
Result := String(InfoLog);
FreeMem(InfoLog,InfoLogLength + 1);
End;
{.......................................................}
{.......................................................}
Constructor TGL_ARB_Vertex_Shader.Create;
Begin
Inherited Create;
FHandle := glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
End;
{.......................................................}
{.......................................................}
Destructor TGL_ARB_Vertex_Shader.Destroy;
Begin
Inherited Destroy;
End;
{.......................................................}
{.......................................................}
Constructor TGL_ARB_Fragment_Shader.Create;
Begin
Inherited Create;
FHandle := glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
End;
{.......................................................}
{.......................................................}
Destructor TGL_ARB_Fragment_Shader.Destroy;
Begin
Inherited Destroy;
End;
{.......................................................}
{.......................................................}
Constructor TGL_Program.Create;
Begin
Inherited Create;
FHandle := 0;
End;
{.......................................................}
{.......................................................}
Destructor TGL_Program.Destroy;
Begin
If (FHandle <> 0) Then
glDeleteObjectARB(FHandle);
Inherited Destroy;
End;
{.......................................................}
{.......................................................}
Procedure TGL_Program.AttachShader(Const AShader: TGL_Shader);
Begin
End;
{.......................................................}
{.......................................................}
Procedure TGL_Program.Link;
Begin
End;
{.......................................................}
{.......................................................}
Procedure TGL_Program.Enable;
Begin
End;
{.......................................................}
{.......................................................}
Procedure TGL_Program.Disable;
Begin
End;
{.......................................................}
{.......................................................}
Procedure TGL_Program.SetUniform(Const AName: String;
Const AValue: GLint);
Begin
End;
{.......................................................}
{.......................................................}
Procedure TGL_Program.SetUniform(Const AName: String;
Const AValues: Array Of GLfloat);
Begin
End;
{.......................................................}
{.......................................................}
Constructor TGL_ARB_Program.Create;
Begin
Inherited Create;
FHandle := glCreateprogramObjectARB;
End;
{.......................................................}
{.......................................................}
Destructor TGL_ARB_Program.Destroy;
Begin
Inherited Destroy;
End;
{.......................................................}
{.......................................................}
Procedure TGL_ARB_Program.AttachShader(Const AShader: TGL_Shader);
Begin
glAttachObjectARB(FHandle,AShader.Handle);
End;
{.......................................................}
{.......................................................}
Procedure TGL_ARB_Program.Link;
Begin
glLinkProgramARB(FHandle);
End;
{.......................................................}
{.......................................................}
Procedure TGL_ARB_Program.Enable;
Begin
glUseProgramObjectARB(FHandle);
End;
{.......................................................}
{.......................................................}
Procedure TGL_ARB_Program.Disable;
Begin
glUseProgramObjectARB(0);
End;
{.......................................................}
{.......................................................}
Procedure TGL_ARB_Program.SetUniform(Const AName: String;
Const AValue: GLint);
Begin
glUniform1iARB(glGetUniformLocationARB(FHandle,PGLCharArb(AName)),
AValue);
End;
{.......................................................}
{.......................................................}
Procedure TGL_ARB_Program.SetUniform(Const AName: String;
Const AValues: Array Of GLfloat);
Var
ParamCount: Integer;
NameArb : PGLCharArb;
Begin
ParamCount := Length(AValues);
NameArb := PGLCharArb(AName);
Case ParamCount Of
1: glUniform1fARB(glGetUniformLocationARB(FHandle, NameArb),
AValues[0]);
2: glUniform2fARB(glGetUniformLocationARB(FHandle, NameArb),
AValues[0],AValues[1]);
3: glUniform3fARB(glGetUniformLocationARB(FHandle, NameArb),
AValues[0],AValues[1],AValues[2]);
4: glUniform4fARB(glGetUniformLocationARB(FHandle, NameArb),
AValues[0],AValues[1],AValues[2],AValues[3]);
Else
End;
End;
{.......................................................}
{.......................................................}
End.
Bookmarks