• Advertisement
Sign in to follow this  

OpenGL Material / Shader and vertex buffers

This topic is 4784 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm working on my 3D engine, and have something that works with OpenGL and DirectX in separate dll, I recently add material scripts, with different technique, pass, and texture layer, (something that looks like Ogre materials) and now I'm trying to add Cg shaders. Now I create my mesh with a default vertex format, but I have to change it to work with multiple pass and multiple texture layers. I need to know which vertex format to use when I create the vertex buffer for a mesh, Is it a good idea to define the vertex format in the material script ? or it's possible (better ?) to find which vertex elements are needed while reading the material script ? Another problem, at the beginning I thought I will need, for a mesh, only one vertex buffer for each different material used in this mesh, but now, I think some material will need a different vertex shader at each pass, so each pass could have a different vertex declaration, and need a different vertex buffer. How to create needed vertex buffers and vertex streams for a mesh with a given material script, without storing many times the same vertex elements for each passes ? Thanks.

Share this post


Link to post
Share on other sites
Advertisement

Please, no one has an answer, or advice about my question ?
or you don't understand my problem ?

Should I try to reformulate my question ?


Thanks.



Share this post


Link to post
Share on other sites
Hmm... I think I see what you're asking.

I can't give you much input on the OpenGL side, I haven't worked with it in a while, but with Direct3D you could create all the different data for your mesh in separate streams quite easily, which would mean you don't store redundant data as you'd not really be using a different vertex buffer, just perhaps assigning the streams differently to fit the declaration.

As for the vertex format/elements/declaration you either need to define this in the script somehow (perhaps with a components field, which just lists what that material requires) or you do it intelligently at runtime by reading what the material contains. IIRC from that big Material/Shader Implementation thread, there was a field in the shader which specified what data that shader needed from the geometry in order to render correctly.

Have I understood the question correctly?

-Mezz

Share this post


Link to post
Share on other sites

Thank you for the answer,
it seems more simple to define the vertex declaration in the script,
but I'm not sure to understand for vertex streams.

My problem is not specific to OpenGL or Direct3D,
I don't understand how to find which vertex elements and streams to use,
if I have multiple pass.

Example :

If I have an Ambient pass, that need vertex position, and texture coordinates,
then a Diffuse pass for each lights... (ex : for bump-mapping)

I need the vertex position for these two pass,
but I don't want to store the position twice.

How to know that I must have the position, or other elements,
in a separate stream ?


Share this post


Link to post
Share on other sites
OK, it seems like you're going to need to define the vertex properties inside the script.

If you think about what a vertex declaration is - it's really just a list of things that make up a vertex e.g. position, normal, texture coordinates.

Each of those elements could be in a separate stream, just as long as you make them available to the shader which will need them when rendering. I'd probably create all the vertex data you need in different streams, then you just need to figure out from the declaration in the script which streams need to be set for that shader to work.

I hope that answers a bit more.

-Mezz

Share this post


Link to post
Share on other sites

I understand.
I think, for now, I will define vertex declaration in the script,
and do as you say, to have something that works,
later I could try to find a way to "guess" the vertex declaration
while reading the script.

Thanks again.


Share this post


Link to post
Share on other sites
Have you looked into the CgFX or .FX file format? Even though you may not use them explicitly, they could give you good ideas. Check out the "Semantics" and "Annotations" sections of the DX documentation on Effect files.

Essentially you can read meta-data about the vertex shader, including any necessary constants and the vertex elements that are required. I think your scripts are very similar to these two file formats, you'll have to compare.

If you can spare the bandwidth and you don't want to implement the steams as Mezz suggested, you could simply just determine the largest vertex format that you will need for the most complex shader, and use that one for all of your draw calls. I find that the simplest solution is sometimes the best! [smile]

Share this post


Link to post
Share on other sites

I don't want to use .fx or CgFX, only need Vertex Shader and Pixel Shader,
because later I may add the possibility to use Assembly, or GLSL programs.
but I will have a better look at them.

I can use the same vertex format for all pass but I will have unused elements
and have to change the vertex declaration in the Cg program too.

Share this post


Link to post
Share on other sites
the object you are rendering should determine most of the vertex format. static world geometry probably doesn't need bone weights for example. but some parts of the vertex format should be determined by the shader which is usually defined by the material somehow. the shader would specify if you needed normal, tangent, and bitangent vectors or any other user defined variables. you could always define the vertex format in the material script, but that might restrict which materials can be applied to what objects.

so position is always a given, you can pretty much always assume that. texcoords should be derived from the shader/material and possibly the type of texture. for example, the base texture always needs it own texcoords, lightmap probably needs it own (if u use them), normal map can probably use the base texcoords, etc. the material would define if you need tangent space or not. the object itself would determine if you need bone weights or some specular color.

one thing that will make life easier is to build the vertex format in the same way for every shader. essentially everything is going to be in the same order, you just might not use the same number of components each time. as long as you stick to the same conventions when writing your shaders, you can use the same function for determining all vertex formats. plus you will also want a function that makes sure your vertex format is the right amount of bytes.

Share this post


Link to post
Share on other sites
I understand how to create the function that is determining the vertex format,
but this doesn't solve the problem if I have multiple pass,
or I will have to use a different stream for each elements,
and select needed streams when rendering a pass.

Thanks.

Share this post


Link to post
Share on other sites
maybe you could make restriction that multiple passes all need the same vertex format? for speed purposes you will probably want each mesh to have its own vertex buffer (when i say mesh i mean group of triangles with the same material/shader propeties). if you are using a vertex buffer for multiple passes, you shouldn't need to touch the vertex data. in order to truly change the vertex format per pass you would have to lock/unlock the vertex buffer and rearrange everything.

btw, in your original post you had mentioned that each pass would need a different vertex shader. thats probably a bad idea. switching between programmable shaders and fixed function is quite expensive.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By LifeArtist
      Good Evening,
      I want to make a 2D game which involves displaying some debug information. Especially for collision, enemy sights and so on ...
      First of I was thinking about all those shapes which I need will need for debugging purposes: circles, rectangles, lines, polygons.
      I am really stucked right now because of the fundamental question:
      Where do I store my vertices positions for each line (object)? Currently I am not using a model matrix because I am using orthographic projection and set the final position within the VBO. That means that if I add a new line I would have to expand the "points" array and re-upload (recall glBufferData) it every time. The other method would be to use a model matrix and a fixed vbo for a line but it would be also messy to exactly create a line from (0,0) to (100,20) calculating the rotation and scale to make it fit.
      If I proceed with option 1 "updating the array each frame" I was thinking of having 4 draw calls every frame for the lines vao, polygons vao and so on. 
      In addition to that I am planning to use some sort of ECS based architecture. So the other question would be:
      Should I treat those debug objects as entities/components?
      For me it would make sense to treat them as entities but that's creates a new issue with the previous array approach because it would have for example a transform and render component. A special render component for debug objects (no texture etc) ... For me the transform component is also just a matrix but how would I then define a line?
      Treating them as components would'nt be a good idea in my eyes because then I would always need an entity. Well entity is just an id !? So maybe its a component?
      Regards,
      LifeArtist
    • By QQemka
      Hello. I am coding a small thingy in my spare time. All i want to achieve is to load a heightmap (as the lowest possible walking terrain), some static meshes (elements of the environment) and a dynamic character (meaning i can move, collide with heightmap/static meshes and hold a varying item in a hand ). Got a bunch of questions, or rather problems i can't find solution to myself. Nearly all are deal with graphics/gpu, not the coding part. My c++ is on high enough level.
      Let's go:
      Heightmap - i obviously want it to be textured, size is hardcoded to 256x256 squares. I can't have one huge texture stretched over entire terrain cause every pixel would be enormous. Thats why i decided to use 2 specified textures. First will be a tileset consisting of 16 square tiles (u v range from 0 to 0.25 for first tile and so on) and second a 256x256 buffer with 0-15 value representing index of the tile from tileset for every heigtmap square. Problem is, how do i blend the edges nicely and make some computationally cheap changes so its not obvious there are only 16 tiles? Is it possible to generate such terrain with some existing program?
      Collisions - i want to use bounding sphere and aabb. But should i store them for a model or entity instance? Meaning i have 20 same trees spawned using the same tree model, but every entity got its own transformation (position, scale etc). Storing collision component per instance grats faster access + is precalculated and transformed (takes additional memory, but who cares?), so i stick with this, right? What should i do if object is dynamically rotated? The aabb is no longer aligned and calculating per vertex min/max everytime object rotates/scales is pretty expensive, right?
      Drawing aabb - problem similar to above (storing aabb data per instance or model). This time in my opinion per model is enough since every instance also does not have own vertex buffer but uses the shared one (so 20 trees share reference to one tree model). So rendering aabb is about taking the model's aabb, transforming with instance matrix and voila. What about aabb vertex buffer (this is more of a cosmetic question, just curious, bumped onto it in time of writing this). Is it better to make it as 8 points and index buffer (12 lines), or only 2 vertices with min/max x/y/z and having the shaders dynamically generate 6 other vertices and draw the box? Or maybe there should be just ONE 1x1x1 cube box template moved/scaled per entity?
      What if one model got a diffuse texture and a normal map, and other has only diffuse? Should i pass some bool flag to shader with that info, or just assume that my game supports only diffuse maps without fancy stuff?
      There were several more but i forgot/solved them at time of writing
      Thanks in advance
    • By RenanRR
      Hi All,
      I'm reading the tutorials from learnOpengl site (nice site) and I'm having a question on the camera (https://learnopengl.com/Getting-started/Camera).
      I always saw the camera being manipulated with the lookat, but in tutorial I saw the camera being changed through the MVP arrays, which do not seem to be camera, but rather the scene that changes:
      Vertex Shader:
      #version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoord; out vec2 TexCoord; uniform mat4 model; uniform mat4 view; uniform mat4 projection; void main() { gl_Position = projection * view * model * vec4(aPos, 1.0f); TexCoord = vec2(aTexCoord.x, aTexCoord.y); } then, the matrix manipulated:
      ..... glm::mat4 projection = glm::perspective(glm::radians(fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); ourShader.setMat4("projection", projection); .... glm::mat4 view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp); ourShader.setMat4("view", view); .... model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f)); ourShader.setMat4("model", model);  
      So, some doubts:
      - Why use it like that?
      - Is it okay to manipulate the camera that way?
      -in this way, are not the vertex's positions that changes instead of the camera?
      - I need to pass MVP to all shaders of object in my scenes ?
       
      What it seems, is that the camera stands still and the scenery that changes...
      it's right?
       
       
      Thank you
       
    • By dpadam450
      Sampling a floating point texture where the alpha channel holds 4-bytes of packed data into the float. I don't know how to cast the raw memory to treat it as an integer so I can perform bit-shifting operations.

      int rgbValue = int(textureSample.w);//4 bytes of data packed as color
      // algorithm might not be correct and endianness might need switching.
      vec3 extractedData = vec3(  rgbValue & 0xFF000000,  (rgbValue << 8) & 0xFF000000, (rgbValue << 16) & 0xFF000000);
      extractedData /= 255.0f;
    • By Devashish Khandelwal
      While writing a simple renderer using OpenGL, I faced an issue with the glGetUniformLocation function. For some reason, the location is coming to be -1.
      Anyone has any idea .. what should I do?
  • Advertisement