Sign in to follow this  
japro

OpenGL Trying to get rid of all the deprecated stuff

Recommended Posts

japro    887
I haven't done that much "modern opengl" stuff so far. So now I try to familiarize myself with the proper way to do things. But this is getting somewhat frustrating since searching only seems give me lots of tutorials that use all the stuff that is deprecated in ogl3 and lots of forum posts that tell me that it is deprecated... Searching for examples (you know REALLY basic ones) is also of no help, since everyone who does ogl3 examples seems to wrap away all the functionality in his own classes and whatnot. I don't want to know how you wrapped the stuff and dissect 3 helper classes, I just want the smallest working example, ARGH.

Well anyway. So I figured Id just use an example I had that works and replace stuff step by step. So the first thing I wanted to do is going to generic attributes for vertex data, normals and texture coordinates.

So I put my VBO in a "std::vector<Vertex> vertices"
[source lang="cpp"]
struct Vertex {
Vertex(const Vector<float, 3> &p, const Vector<float, 3> &n, const Vector<float, 2> &t)
: pos(p), normal(n), texcoord(t) { }
Vector<float, 3> pos;
Vector<float, 3> normal;
Vector<float, 2> texcoord;
};
[/source]
"build" it with
[source lang="cpp"]
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(Vertex), &(vertices[0]), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (char *)0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (char *)0+12);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (char *)0+24);
[/source]
and draw it with:
[source lang="cpp"]
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glDrawArrays(GL_QUADS, 0, vertices.size());
[/source]
And here is the whole setup for the shaders and such in compressed form:
[source lang="cpp"]


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-4.f, 4.f, -3.f, 3.f, 10.f, 1536.f);

glClearColor(0.f, 0.f, 0.f, 0.f);

GLuint vertex_shader;
GLuint fragment_shader;

vertex_shader = glCreateShader(GL_VERTEX_SHADER);
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);

std::string vertex_source =
"#version 130\n"
"in vec3 vertex;\n"
"in vec3 normal;\n"
"in vec2 tcoords;\n"
"out vec4 vcolor;\n"
"void main(void)\n"
"{\n"
" gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * vec4(vertex,0);\n"
" vcolor = gl_Color;\n"
"}\n";

int vertex_length = vertex_source.size();
const char *vs = vertex_source.c_str();

glShaderSource(vertex_shader, 1, &vs, &vertex_length);

std::string fragment_source =
"#version 130\n"
"in vec4 vcolor;\n"
"void main (void)\n"
"{\n"
" gl_FragColor = vcolor;\n"
"}\n";

int fragment_length = fragment_source.size();
const char *fs = fragment_source.c_str();

glShaderSource(fragment_shader, 1, &fs, &fragment_length);

std::cout << "compiling vertex shader" << std::endl;
glCompileShader(vertex_shader);
print_log(vertex_shader);

std::cout << "compiling fragment shader" << std::endl;
glCompileShader(fragment_shader);
print_log(fragment_shader);


GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);

glBindAttribLocation(program, 0, "vertex");
glBindAttribLocation(program, 1, "normal");
glBindAttribLocation(program, 2, "tcoords");

glLinkProgram(program);

glUseProgram(program);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.f, 0.f, -180.f);
[/source]

This only gives me a black screen. As far as I can tell all I did was replace the the glVertexPointer etc. stuff with glVertexAttribPointer, remove the "glEnableClientState" calls (or do I need something equivalent for my generic attributes?) and rename the stuff in the shaders...
Am I binding the attributes wrong? Do I forget something? Is the way i convert "vertex" inside the shader program to a vec4 correct?

Share this post


Link to post
Share on other sites
Shintah    109
Just took a quick glance at the code but one thing I saw is that you don't enable the vertex attribs with glEnableVertexAttribArray.

Share this post


Link to post
Share on other sites
japro    887
Ok, I inserted those and apparantly have a problem with the way the color gets passed?

I feel I have some sort of grave misunderstand how the colors work.

I accidentially uncommented a line from a earlier version in the fragment shader so it read:

[code]
#version 130
in vec4 vcolor;
void main (void)
{
gl_FragColor = color*vcolor;
}
[/code]

this resulted in the whole geometry being painted in white. But why doesnt the use of "color" which isnt declared anywhere produce an error from the compiler? Also changing the line to "gl_FragColor = vec4(1,1,1,1);" gives me a black screen again? WTF

Edit: forget it, apparently I just don't see the error and the fragment shader is not used at all or something

Edit2: I'm now completely confused. Not using the shader program at all still paints the geometry in white. How does the pipline know how to use my "custom" attributes? Or is attribute 0 just getting interpreted as vertex data anyway?

Share this post


Link to post
Share on other sites
japro    887
Ok, one last question. gl_FragColor is deprecated and I'm supposed to declare my own "out" variable. So just put "out vec4 FragColor;" into the shader, used it and it magically works. But this confuses me, shouldn't I have to to something like glBindFragDataLocation(...) and then tell ogl somehow what it is supposed to do with "FragColor"? What happens If i declare multiple such outputs?

Share this post


Link to post
Share on other sites
Shintah    109
The GLSL spec just says:
[quote]Both gl_FragColor and gl_FragData are deprecated; the preferred usage is to explicitly declare these outputs in the fragment
shader using the out storage qualifier.[/quote]

so I guess it's just magic. :P

Not sure what happens if you declare multiple outputs without using them. In the cases where you actually want more then one color output you have to specify this with glDrawBuffers and/or with layout qualifiers in the GLSL shader. Not entirely sure about all the details so check the GL documentation or glsl spec for more details.

Share this post


Link to post
Share on other sites
karwosts    840
[quote]
But this confuses me, shouldn't I have to to something like glBindFragDataLocation(...) and then tell ogl somehow what it is supposed to do with "FragColor"? What happens If i declare multiple such outputs?
[/quote]

If it works anything like attributes, my guess is that it just randomly assigns your N outputs to locations 0..(N-1), with 0 being the one that's picked up by glDrawBuffers by default. If you only have one output it automatically goes to channel 0, so you don't have to call glBindFragDataLocation. Just a guess though.

Regarding all your other problems, it seems like you either have no or very poor error checking. I can't comment on what is in 'print_log' cause I can't see it, but I see that you're not doing any debugging of the program linking infolog, so you could be missing some problems there.

Brush up on the info here, it may help you.

http://www.lighthouse3d.com/tutorials/glsl-tutorial/troubleshooting-the-infolog/

Share this post


Link to post
Share on other sites
Jacob Jingle    226
[quote name='japro' timestamp='1314272439' post='4853593']
I haven't done that much "modern opengl" stuff so far. So now I try to familiarize myself with the proper way to do things. But this is getting somewhat frustrating since searching only seems give me lots of tutorials that use all the stuff that is deprecated in ogl3 and lots of forum posts that tell me that it is deprecated... Searching for examples (you know REALLY basic ones) is also of no help, since everyone who does ogl3 examples seems to wrap away all the functionality in his own classes and whatnot.
[/quote]
IMHO, best samples on the web.
[url="http://www.g-truc.net/"]http://www.g-truc.net/[/url]

Great site:
[url="http://www.opengl-tutorial.org/"]http://www.opengl-tutorial.org/[/url]

Good online book:
[url="http://www.arcsynthesis.org/gltut/index.html"]Learning Modern 3D Graphics Programming[/url]

Share this post


Link to post
Share on other sites
stalef    607
This site helped me alot when I also wanted to do more modern OpenGL:

[url="http://nopper.tv/opengl.html"]http://nopper.tv/opengl.html[/url]

Share this post


Link to post
Share on other sites
Eric Lengyel    3460
[quote name='foobarbazqux' timestamp='1314359962' post='4854017']
GL_QUADS is deprecated, you probably want to use pairs of triangles instead.
[/quote]

This is one of the biggest mistakes ever made by the ARB. Quads are supported directly by the hardware, and there are good reasons to use them instead of pairs of triangles for various things.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By povilaslt2
      Hello. I'm Programmer who is in search of 2D game project who preferably uses OpenGL and C++. You can see my projects in GitHub. Project genre doesn't matter (except MMO's :D).
    • By ZeldaFan555
      Hello, My name is Matt. I am a programmer. I mostly use Java, but can use C++ and various other languages. I'm looking for someone to partner up with for random projects, preferably using OpenGL, though I'd be open to just about anything. If you're interested you can contact me on Skype or on here, thank you!
      Skype: Mangodoor408
    • By tyhender
      Hello, my name is Mark. I'm hobby programmer. 
      So recently,I thought that it's good idea to find people to create a full 3D engine. I'm looking for people experienced in scripting 3D shaders and implementing physics into engine(game)(we are going to use the React physics engine). 
      And,ye,no money =D I'm just looking for hobbyists that will be proud of their work. If engine(or game) will have financial succes,well,then maybe =D
      Sorry for late replies.
      I mostly give more information when people PM me,but this post is REALLY short,even for me =D
      So here's few more points:
      Engine will use openGL and SDL for graphics. It will use React3D physics library for physics simulation. Engine(most probably,atleast for the first part) won't have graphical fron-end,it will be a framework . I think final engine should be enough to set up an FPS in a couple of minutes. A bit about my self:
      I've been programming for 7 years total. I learned very slowly it as "secondary interesting thing" for like 3 years, but then began to script more seriously.  My primary language is C++,which we are going to use for the engine. Yes,I did 3D graphics with physics simulation before. No, my portfolio isn't very impressive. I'm working on that No,I wasn't employed officially. If anybody need to know more PM me. 
       
    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
  • Popular Now