Author Topic: [C++][OpenGL] How to load shaders / textures from the memory?  (Read 13686 times)

0 Members and 1 Guest are viewing this topic.

Offline Blacksheep8Bit

  • *Custum Title goes here*
  • C= 64
  • **
  • Posts: 91
  • Karma: 36
  • *useless thing to write goes here*
    • View Profile
So, i was wondering if theres a way to load shaders and textures (.PNG) from the memory, the same way we make to load XM musics using MiniFMOD/uFMOD


And about shaders, how to apply them to polygons? I Am so noob :/
Challenge Trophies Won:

Offline LittleWhite

  • Senior Member
  • Amiga 1200
  • ********
  • Posts: 418
  • Karma: 31
  • It's me!
    • View Profile
Hello,

Let say, you have picture data (uncompressed) stored in memory.
In OpenGL, you can use glTexImage2D() to pass your data to OpenGL (and like this, having a texture). To pass it, you just use the pointer on the data you have in memory.
For shaders, it the same.
For Shader, you can even write you shader in the code source like this :
Code: [Select]
const char* myPixShader = "int main() { gl_FragColor = vec4(1.0,0.0,0.0,1.0);"
The hardest part, maybe, is "how to load data from a file to memory" :D
The demoscene will never die, never!

Offline Blacksheep8Bit

  • *Custum Title goes here*
  • C= 64
  • **
  • Posts: 91
  • Karma: 36
  • *useless thing to write goes here*
    • View Profile
Hello,

Let say, you have picture data (uncompressed) stored in memory.
In OpenGL, you can use glTexImage2D() to pass your data to OpenGL (and like this, having a texture). To pass it, you just use the pointer on the data you have in memory.
For shaders, it the same.
For Shader, you can even write you shader in the code source like this :
Code: [Select]
const char* myPixShader = "int main() { gl_FragColor = vec4(1.0,0.0,0.0,1.0);"
The hardest part, maybe, is "how to load data from a file to memory" :D
Thanks for the help, but how to apply the texture or shader to something? ( <-- I Am a complete noob )
Challenge Trophies Won:

Offline LittleWhite

  • Senior Member
  • Amiga 1200
  • ********
  • Posts: 418
  • Karma: 31
  • It's me!
    • View Profile
How to apply ?

You have created your texture and shader and now you have the OpenGL ids (similar to pointers).
Since OpenGL is working like a state machine, you have to enable the texture (is called "bind") with glBindTexture().
Same goes for all others OpenGL objects (shaders / buffers ...)

Once is bound, you are using it. For instance, if you order an draw command (glDrawArray) these shader / texture will be used during rendering.

(And then, we have to talk about what is inside the shaders ? )
The demoscene will never die, never!

Offline Blacksheep8Bit

  • *Custum Title goes here*
  • C= 64
  • **
  • Posts: 91
  • Karma: 36
  • *useless thing to write goes here*
    • View Profile
My main problem is how to load and compile a shader, i can't find any well explained tutorial about loading shaders and compiling them in the internet, most of them are outdated, require strange libs or are designed for professionals using professional terms and etc.

I Need a simple, basic and well explained tutorial :/
NeHe's tutorial is fine, i just can't understand what he uses, what are the libs, where to download, how to use, i am just lost (as usual)
Challenge Trophies Won:

Offline LittleWhite

  • Senior Member
  • Amiga 1200
  • ********
  • Posts: 418
  • Karma: 31
  • It's me!
    • View Profile
Ok ... I see. Bad that you don't speak french, I know a tutoriel "good".

NeHe, is ok, but maybe too old.
Maybe you can check that -> http://www.swiftless.com/opengltuts/opengl4tuts.html
But I am not sure about the quality.

You need OpenGL (normally already on your machine) and GLEW ( if you are with Windows) http://glew.sourceforge.net/
You need GLM if you are doing OpenGL 3.x  http://glm.g-truc.net/ to handle matrices.
You need SDL or other stuff to open the windows (you can use FreeGLUT / WinAPI / Qt / GTK / wxWidget or whatever handling windows as replacement :p ) SDL will help you to load files :)

Ok, It was not so easy :P
If you don't get it ... just try to open an GL window with SDL :)
The demoscene will never die, never!

Offline Blacksheep8Bit

  • *Custum Title goes here*
  • C= 64
  • **
  • Posts: 91
  • Karma: 36
  • *useless thing to write goes here*
    • View Profile
Ok ... I see. Bad that you don't speak french, I know a tutoriel "good".

NeHe, is ok, but maybe too old.
Maybe you can check that -> http://www.swiftless.com/opengltuts/opengl4tuts.html
But I am not sure about the quality.

You need OpenGL (normally already on your machine) and GLEW ( if you are with Windows) http://glew.sourceforge.net/
You need GLM if you are doing OpenGL 3.x  http://glm.g-truc.net/ to handle matrices.
You need SDL or other stuff to open the windows (you can use FreeGLUT / WinAPI / Qt / GTK / wxWidget or whatever handling windows as replacement :p ) SDL will help you to load files :)

Ok, It was not so easy :P
If you don't get it ... just try to open an GL window with SDL :)
Nah, handling windows it's ok, i've made my own framework with multi sampling and everything else, i just need to know how to load shaders :D

Thanks for the links, i am reading it, if i have any problem i will post here, thanks!

editOh, Opengl 4.0 :/
Don't you have any opengl 2.0 tutorial? I Am trying to make it run in old computers, since i don't have a new computer one to test it LOL
« Last Edit: March 04, 2012 by BlackSheep8Bit »
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
If you're working under Windows you link against an OpenGL 1.1 library from 1996.
GLSL was introduced in OpenGL 2.0, though. So all the required functions are not available out of the box.
Instead you have to load these functions manually using wglGetProcAddress with the prototypes defined in glext.h:
Code: [Select]
#include "glext.h"
PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObject = 0;
glCreateShaderObject = (PFNGLCREATESHADEROBJECTARBPROC) wglGetProcAddress("glCreateShaderObjectARB");
...
unsigned int shader= glCreateShaderObject ( GL_VERTEX_SHADER_ARB );
As this is quite troublesome (especially if you want to support different operating systems which ship with different versions of OpenGL) it makes sense to use a lib like glew instead.

« Last Edit: March 04, 2012 by hellfire »
Challenge Trophies Won:

Offline Blacksheep8Bit

  • *Custum Title goes here*
  • C= 64
  • **
  • Posts: 91
  • Karma: 36
  • *useless thing to write goes here*
    • View Profile
If you're working under Windows you link against an OpenGL 1.1 library from 1996.
GLSL was introduced in OpenGL 2.0, though. So all the required functions are not available out of the box.
Instead you have to load these functions manually using wglGetProcAddress with the prototypes defined in glext.h:
Code: [Select]
#include "glext.h"
PFNGLCREATESHADEROBJECTARBPROC glCreateShaderObject = 0;
glCreateShaderObject = (PFNGLCREATESHADEROBJECTARBPROC) wglGetProcAddress("glCreateShaderObjectARB");
...
unsigned int shader= glCreateShaderObject ( GL_VERTEX_SHADER_ARB );
As this is quite torublesome (especially if you want to support different operating systems which support different versions OpenGL) it makes sense to use a lib like glew instead.
Oh really? Damn, things are getting more dificult every second, is DirectX the same thing? I Will try using this "wglGetProcAdress" you told me, maybe i should try glew, i am just confused, but i will figure it out! Thanks!
Challenge Trophies Won:

Offline LittleWhite

  • Senior Member
  • Amiga 1200
  • ********
  • Posts: 418
  • Karma: 31
  • It's me!
    • View Profile
DirectX is far easier since Microsoft helps its integration (but not for OpenGL).

With GLEW seriously, it becomes really easy (but some people does not like it).
It also exist an kind of same thing that GLEW called GLEE but ... not sure that it is up to date.

The wglGetProcAdress is not that difficult. Use a macro (or other advanced C / C++ technique) and you wil have nice results :p
But better do not reinvent the wheel ... you should use GLEW I think :D
The demoscene will never die, never!

Offline Blacksheep8Bit

  • *Custum Title goes here*
  • C= 64
  • **
  • Posts: 91
  • Karma: 36
  • *useless thing to write goes here*
    • View Profile
DirectX is far easier since Microsoft helps its integration (but not for OpenGL).

With GLEW seriously, it becomes really easy (but some people does not like it).
It also exist an kind of same thing that GLEW called GLEE but ... not sure that it is up to date.

The wglGetProcAdress is not that difficult. Use a macro (or other advanced C / C++ technique) and you wil have nice results :p
But better do not reinvent the wheel ... you should use GLEW I think :D
The main problem is, i am making 64kbs, and glew has a 300kbs dll, so i have 2 choises: Go DirectX or make it the in the almost impossible extreme hard way :P
Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
Quote
or make it the in the almost impossible extreme hard way
I'm sorry if I created the impression that it's extremely hard to use shader with OpenGL - it's definitely not.
I just tried to point out how it works and why it is this way.
The way OpenGL defines extensions is actually quite nice because you can use features as soon as your graphics vendor implemented them in the driver and you don't have to wait until some new sdk version is out.


Challenge Trophies Won:

Offline Blacksheep8Bit

  • *Custum Title goes here*
  • C= 64
  • **
  • Posts: 91
  • Karma: 36
  • *useless thing to write goes here*
    • View Profile
Quote
or make it the in the almost impossible extreme hard way
I'm sorry if I created the impression that it's extremely hard to use shader with OpenGL - it's definitely not.
I just tried to point out how it works and why it is this way.
The way OpenGL defines extensions is actually quite nice because you can use features as soon as your graphics vendor implemented them in the driver and you don't have to wait until some new sdk version is out.
Yeah, but i simply don't understand how they work, why there aren't good tutorials in the internet? :/
I am seriously thinking in change to DirectX since it's more stable and updated to the new needs of the graphics industry (wow, i spoke like a professional!)
Challenge Trophies Won:

Offline Voltage

  • Professor
  • Pentium
  • *****
  • Posts: 857
  • Karma: 53
    • View Profile
64k you say....

shader functionality you say....

check out IQ's frameworks: http://www.iquilezles.org/www/material/isystem1k4k/isystem1k4k.htm

Specifically check out the i1k_OGLShader and 4k version.

You can either use his source or find out how he handles things.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Sounds like you are trying to bite off an awful lot in one mouthful.  Why not try something simpler first?  Say, some textured stuff, not limited to 64k, using immediate mode?
Quote
... change to DirectX since it's more stable and updated to the new needs of the graphics industry
That's quite patently not true.  OpenGL is bulletproof and updated regularly to keep up with new gear and works on a wide range of hardware where DirectX isn't available.

Jim
Challenge Trophies Won:

Offline Blacksheep8Bit

  • *Custum Title goes here*
  • C= 64
  • **
  • Posts: 91
  • Karma: 36
  • *useless thing to write goes here*
    • View Profile
Yeah, IQ's 1kb framework has a really good source, here if you guys are intersted, i've made some simple changes, but it works like the original :D

Code: [Select]
#define glCreateProgram ((PFNGLCREATEPROGRAMPROC) wglGetProcAddress("glCreateProgram"))
#define glCreateShader  ((PFNGLCREATESHADERPROC) wglGetProcAddress("glCreateShader"))
#define glShaderSource  ((PFNGLSHADERSOURCEPROC) wglGetProcAddress("glShaderSource"))
#define glCompileShader ((PFNGLCOMPILESHADERPROC) wglGetProcAddress("glCompileShader"))
#define glAttachShader  ((PFNGLATTACHSHADERPROC) wglGetProcAddress("glAttachShader"))
#define glLinkProgram   ((PFNGLLINKPROGRAMPROC) wglGetProcAddress("glLinkProgram"))
#define glUseProgram    ((PFNGLUSEPROGRAMPROC) wglGetProcAddress("glUseProgram"))



class Shaders
{

public:
static void CreateShader(const static char* shader, GLenum type) // GL_FRAGMENT_SHADER
{
GLint p = glCreateProgram();
GLint f = glCreateShader(type);

glShaderSource(f, 1, &shader, 0);
glCompileShader(f);
glAttachShader(p,f);
glLinkProgram(p);
glUseProgram(p);
};


static void CleanShader()
{
glUseProgram(0);
};
};
« Last Edit: March 06, 2012 by BlackSheep8Bit »
Challenge Trophies Won:

Offline jace_stknights

  • Amiga 1200
  • ****
  • Posts: 399
  • Karma: 32
  • PEEK & POKE are not MOVEM!
    • View Profile
    • ST Knights WebSite
Wow! there are specialists here!  :clap:

I also a BIG noob in opengl... I know where to go now ;)
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Nice One BlackSheep! :)

I've currently got glew.h and glew32.dll. I don't like using dlls that much out of personal preference.
And that's just what I've been looking for!!

I tried that out above with the defines, but it told me all sorts of probs and didn't like it. I'm not sure if you need something else other than gl.h and glu.h. Visual Studio 2010 doesn't have glext.h. Im wondering how you got that to work.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline hellfire

  • Sponsor
  • Pentium
  • *******
  • Posts: 1294
  • Karma: 466
    • View Profile
    • my stuff
I tried that out above with the defines, but it told me all sorts of probs and didn't like it.
I'm not sure if you need something else other than gl.h and glu.h.
Just get glext.h from here.
With those defines you fetch the function-pointer from the driver everytime you call it.
Better store them once, like this:
Code: [Select]
PFNGLUSEPROGRAMPROC glUseProgram= 0;
glUseProgram= (PFNGLUSEPROGRAMPROC) wglGetProcAddress("glUseProgram");


Challenge Trophies Won: