Author Topic: Inverted Colors?  (Read 5470 times)

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Inverted Colors?
« on: 2008-09-14, 07:29:04 PM »
Em, if this is a dumb question, call me a nincompoop, but... How could you do Inverted Colors as a Sprite base or Plan base rule???

For all of you going "HUH?" Inverted colors mean opposite colors
Stare at something for 10+ seconds, close your eyes and you'll see... ;)

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2752
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Inverted Colors?
« Reply #1 on: 2008-09-15, 05:26:43 AM »
I don't know if DirectX or OpenGL have built-in filters to invert colors automatically, but without knowing that, the simplest thing I can think of is to have a copy of all your graphics with inverted colors and link all your tiles to normal and inverted frames based on a counter.  When you want to invert the colors, change the counter.  This won't work for animated tiles, though, because a tile can only be linked to 1 counter, and animated tiles need to be linked to an animation counter.  It also wouldn't affect sprites.  Another possibility is to change the tileset linked to the layer to one that refers to all the inverted tiles.  Unfortunately, the framework code defines the tileset for a layer to be read-only, and I'm not sure what the consequences of making it writable would be, but since it's all editable code within the project, it could certainly be done.  Then there are "shaders" which I know nothing about.  You might be able to do something with that.

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Inverted Colors?
« Reply #2 on: 2008-09-15, 07:49:13 AM »
It might be nice to some day put in support for easily applying certain predefined shaders (or adding your own).  It would certainly give an edge to the way games could look.  Imagine applying bloom on a background layer so that the sun behind a tree trunk makes a fuzzy halo around the tree... man, that'd be cool.
Edward Dassmesser

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Inverted Colors?
« Reply #3 on: 2008-09-15, 07:58:48 AM »
Yes, very good idea. i think of nice graphical effects e.g. fighters whos hits create colored halos and stripes, optical vibrations (of the air), or a fisheye camera.

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2752
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Inverted Colors?
« Reply #4 on: 2008-09-15, 05:36:58 PM »
Any idea how to integrate shaders with SGDK2?  I don't know the first thing about shaders.

Tanja

  • Clever
  • Fanatic
  • ***
  • Posts: 606
    • View Profile
Re: Inverted Colors?
« Reply #5 on: 2008-09-16, 04:01:06 AM »
me neither. but i suppose there are ressource-sites out there. many people play around with nice, little shaders i think.

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Re: Inverted Colors?
« Reply #6 on: 2011-06-05, 02:36:11 PM »
Yeah, back to this Old topic again. I found a way to surprisingly get a new window that processes shaders to open outside the game. It works, but I need it to apply the shaders to the actual game. Anyone have any idea how? This could help add wonderful effects to games. I hope its not too late to ask...

Shaders.cs
Code: [Select]
using System;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using System.ComponentModel;
using System.Windows.Forms;
using System.Collections;



namespace CustomObjects
{
   public class Shaders : GameWindow
   
{
string vertexShaderSource = @"
#version 330
 
layout (location = 0) in vec3 Position;
 
uniform float scale;
 
void main()
{
gl_Position = vec4(scale * Position.x,
                   scale * Position.y,
                   Position.z, 1.0);
}";
 
string fragmentShaderSource = @"
#version 330
 
out vec4 FragColor;
 
void main()
{
FragColor = vec4(0.5, 0.8, 1.0, 1.0);
}";
 
 
int vbo,
shaderProgramHandle, vertexShaderHandle, fragmentShaderHandle;
 
int uniformScale;
float variableScale;
 
double time;
 
void CreateVertexBuffer()
{
Vector3[] vertices = new Vector3[3];
vertices[0] = new Vector3(-1f, -1f, 0f);
vertices[1] = new Vector3( 1f, -1f, 0f);
vertices[2] = new Vector3( 0f,  1f, 0f);
 
GL.GenBuffers(1, out vbo);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.BufferData<Vector3>(BufferTarget.ArrayBuffer,
                       new IntPtr(vertices.Length * Vector3.SizeInBytes),
                       vertices, BufferUsageHint.StaticDraw);
}
 
void CreateShaders()
{
shaderProgramHandle = GL.CreateProgram();
 
vertexShaderHandle = GL.CreateShader(ShaderType.VertexShader);
fragmentShaderHandle = GL.CreateShader(ShaderType.FragmentShader);
 
GL.ShaderSource(vertexShaderHandle, vertexShaderSource);
GL.ShaderSource(fragmentShaderHandle, fragmentShaderSource);
 
GL.CompileShader(vertexShaderHandle);
GL.CompileShader(fragmentShaderHandle);
//Console.WriteLine(GL.GetShaderInfoLog(vertexShaderHandle));
//Console.WriteLine(GL.GetShaderInfoLog(fragmentShaderHandle));
 
GL.AttachShader(shaderProgramHandle, vertexShaderHandle);
GL.AttachShader(shaderProgramHandle, fragmentShaderHandle);
GL.LinkProgram(shaderProgramHandle);
//Console.WriteLine(GL.GetProgramInfoLog(shaderProgramHandle));
GL.UseProgram(shaderProgramHandle);
 
uniformScale = GL.GetUniformLocation(shaderProgramHandle, "scale");
}
 
protected override void OnLoad(EventArgs e)
{
GL.ClearColor(Color.Brown);
CreateVertexBuffer();
CreateShaders();
}
 
protected override void OnRenderFrame(FrameEventArgs e)
{
GL.Clear(ClearBufferMask.ColorBufferBit);
 
time = (time >= Math.PI) ? 0.0 : time + e.Time;
 
variableScale = (float)(Math.Sin(time));
GL.Uniform1(uniformScale, variableScale);
 
GL.EnableVertexAttribArray(0);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, 0);
 
GL.DrawArrays(BeginMode.Triangles, 0, 3);
 
GL.DisableVertexAttribArray(0);
 
SwapBuffers();
}

[Description("Tweak the Display with the Shader Effects")]
 static public void ApplyShaders()
{


   
using (Shaders p = new Shaders())
{
p.Run(60);
}
return;
}


}




That function when called opens a new window and draws a triangle with shaders. It was the easiest thing I could find online.

http://www.opentk.com/node/2294

Is there any way to apply shaders to the actual game window? I don't understand the first thing about shaders. Im lucky to get at least THAT far.

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2752
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Inverted Colors?
« Reply #7 on: 2011-06-06, 05:18:10 AM »
Most of that code would probably go in Display.cs, where all the interfaces to OpenTK/OpenGL are in SGDK2 and the project code.  Most of it could probably just be copied in there.  The OnLoad code could probably go in CheckRequirements for lack of a better function (either that or the "Display" constructor).  Some of the code from OnRenderFrame could probably go in the Flush function.

I don't see where ApplyShaders gets called.

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Re: Inverted Colors?
« Reply #8 on: 2011-06-06, 11:46:25 AM »
Does SGDK2 ever use GL.CreateProgram? I tried to locate it but I couldnt find it anywhere in Display.cs. It seems you have to go in this order to use any shaders:

Create it:
int vertex_shader = GL.CreateShader(ShaderType.VertexShader);
int fragment_shader = GL.CreateShader(ShaderType.FragmentShader);

Then,
upload the shader code (GL.ShaderSource)
compile it (GL.CompileShader)
create a program (GL.CreateProgram)
attach the shaders (GL.AttachShader)
link the program (GL.LinkProgram)
use the created program (GL.UseProgram)

But first, I would have to locate where Sgdk2 uses GL.CreateProgram... but if It doesn't (Which I really hope it does because...) I would have to change around the code in Project.cs and Display.cs to a created program. How does SGDK2 Create the GameWindow, it doesnt use GL.CreateProgram, does it?

Or Actually, better yet, what holds the value, if anything for the existing OpenGL Program? (The Current Game Window/Program)
« Last Edit: 2011-06-06, 11:55:27 AM by Pizzaman »

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2752
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Inverted Colors?
« Reply #9 on: 2011-06-07, 05:19:07 AM »
SGDK2's use of GL is *very* limited, so you won't see a lot of example code there.  Basically it just draws squares with graphics on them, so DrawFrame is about the only function that does anything significant.  Afraid I can't point you to any example code for CreateProgram.  Maybe I can look into it later if you still need help.

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Re: Inverted Colors?
« Reply #10 on: 2011-06-08, 10:59:22 AM »
Thanks. I dont think it will ever be possible without changing a large amount of things in the base code... it was just more of a "is this possible" sort of thing. no worries, I have backup ways to do effects without fancy shaders. :) thanks anyways

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Re: Inverted Colors?
« Reply #11 on: 2012-01-02, 04:08:23 AM »
Funny enough, I think I had actually made this compile and run without error, and it seems to work without throwing an InvalidValue or InvalidOperation, only to realize that my Supported version of OpenGL (1.4) can't even use Shaders!

I reached the same error in my game that I had reached in the OpenTK SampleBrowser at GL.LinkProgram at the First Shader example, so that means it's at least functional on someone else's computer.

If anyone is actually interested in testing to tell me if it works, that would be nice. I actually don't plan to use it now, knowing the "high" requirements for Shaders in SGDK2. I also don't want my players saying "Hey that sucks I can't run this!"

It sure would get this curiosity of what could have been if I never stuck with Intel on this laptop. If it does work, (which I am certain in 85 percent) It would make a great showcase example.

Display and GameForm.cs seem to be the only things i had edited. There's a lot of gobbelty goop in Display.cs unrelated to the shaders, (Code I crammed and butchered just to see what effect it would have and effect)

ShadersInitialize(); is called in GameForm- I'm not yet familiar with the Shader Language, so I left the strings that had the source for the fragment and vertex shaders as empty methods.

Source Here:
http://paradigm.heliohost.org/src/shaders/
« Last Edit: 2012-01-02, 04:19:17 AM by Pizzaman »

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2752
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Inverted Colors?
« Reply #12 on: 2012-01-02, 06:55:17 AM »
I get an InvalidOperation error in line 991 during DrawFrame.  My OpenGL version is 2.1.2.

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Re: Inverted Colors?
« Reply #13 on: 2012-01-02, 11:10:28 AM »
Ack- so close! I thought I had finally got it to function properly. I think the problem may be the location of ShadersInitialize, it would be better executed within the Display file between the GL.Enable funcions and CheckError. This is because errors are thrown if ShaderFunctions are used between GL.Begin and GL.End, that's the only possible explanation I can see right now.

It would seem ridiculous to continue to upload new revised source files for someone to test until it functioned properly- especially if I couldn't run it myself. At least this tames my curiosity and puts it at bay.

Thanks bluemonkmn.

v6v

  • Clever
  • Fanatic
  • ***
  • Posts: 500
  • Has renamed his project to Galaxy!
    • View Profile
    • My Developer Page!
    • Email
Re: Inverted Colors?
« Reply #14 on: 2012-03-12, 07:58:20 PM »
After a good amount of time, Solved:

GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);

to

GL.BlendFunc(BlendingFactorSrc.OneMinusDstColor, BlendingFactorDest.OneMinusSrcColor);

in Display.cs

~Sharp