Sunday, March 7, 2010

OpenGL

So I downloaded and compiled freeglut, a toolkit for OpenGL.
Then I downloaded GLEW, but it seemed somewhat resource-hungry for its use, because it keeps a complete list of function-pointers to the extensions.
Extensions for OpenGL are functions, which are not officially supported but help greatly with using newer stuff. There are common extensions, which have been approved the ARB, vendor-specific extensions and the ones that multiple vendors have agreed upon.
You can't call the functions like you are used to in a programming-language. You have to query for them in run-time and hope that they're available and supported by the hardware.

To write modern OpenGL 3.x code you need to use extensions.


So I've looked in the smooth_triangle-example of freeglut and ported that code to a very bad C++-version using fancy stuff like std::vectors and std::strings. I made the design so that I should be able to copy most of that code if I'm getting serious.
My and their way of getting extensions is not hidden and looks beautiful and requires more work to set up, but I prefer that solution.
Basically, I need to declare the function myself and then I have a init-function to initialize all functions.


After two stupid mistakes, that took a while to find and were easy and fast to fix after that, I had it running.
I played around with the Shader a little, changed the triangle into a quad and it looked quite pretty. So pretty, that I wanted a wallpaper-formated picture. So I removed the borders I had on the quad, enabled fullscreen-mode and took this:
And it started out with this:

What I've been doing to create this effect is calculating the light-intensity via the formula color.r * 0.3 + color.g * 0.59 + color.b * 0.11. I multiply by different numbers instead of just taking the average, because green looks brighter than red, which looks brighter than blue. The specific numbers seem to be common, you can simply google for them.
To create corners (sharpen the intensity-effect) I've before multiplied the color-value, which is in range [0..1], four-times with itself (that will darken dark colors more than bright ones).
To make this intensity-effect more visible, I multiplied with 8 and finally multiplied the intensity with the actual color-value.
Or in GLSL:

#version 140

smooth in vec4 fg_SmoothColor;
out vec4 fg_FragColor;

void main(void){
  float i = dot(pow(fg_smoothColor.rgb, vec3(4, 4, 4)), vec3(0.3, 0.59, 0.11)) * 8;
  fg_FragColor = vec4(i, i, i, 1) * fg_SmoothColor;
}


Here's a little less intense version. People with darker desktops (like me) will prefer it. If I darked it more, it doesn't look good anymore.
If anyone wants another resolution, just leave a comment. I can then let my computer calculate a perfectly scaled image, without quality loss :)


I also have a small bug, and that is I get a INVALID_OPERATION error in my exit-function, but I simply blame GLUT for that.

No comments: