Sign in to follow this  
?????????

OpenGL [SOLVED] Generic Vertex Attribute

Recommended Posts

[size="2"]
[size="2"]EDIT: Problem partially resolved, see post #8.[/size]
Hello,[/size]
[size="2"]I'm beginning OpenGL 4.2 in C# using the OpenTK binding (I'm aware that OpenTK binds only up to 3.2, so they have deprecated stuff available so I'm not using those functions, ya know... forward compatibility [img]http://public.gamedev.net/public/style_emoticons/default/cool.gif[/img]). Anyways, so I have got this basic pass-through vertex shader: (also embedded in program below)[/size]
[size="2"][source lang="c"][/size]
[size="2"]#version 410[/size]
[size="2"]#pragma debug(on)[/size]

[size="2"]uniform mat4 projection;[/size]
[size="2"]uniform mat4 view;[/size]

[size="2"]vec3 inVertexPos;[/size]

[size="2"]void main()[/size]
[size="2"]{[/size]
[size="2"] gl_Position = vec4(inVertexPos, 1.0) * projection * view;[/size]
[size="2"]}[/size]
[size="2"][/source][/size]
[size="2"]The problem I am having is marked on line 48 (generic vertex attribute location equals -1) below.[/size]

[size="2"]I can't seem to understand the reason for the error (as OpenGL does not really throw an error...), [/size][s]the OpenGL reference suggets it has to do with maximum generic vertex attribs; ....but it clearly doesn't.[/s]
[size="2"]Anyone can shed some light on the problem? I'll be most thankful.[/size]
[size="2"](The entire code-file is attached as a file to the post as well)[/size]
[size="2"]The main program (this is a class constructor, after this function returns the main loop runs calling the render function below):[/size]

[size="2"][source lang="csharp"][/size]
[size="2"]Matrix4 Projection;[/size]
[size="2"]Matrix4 View;
[/size]
int[] ShaderIDs;
int[] ProgramIDs;
uint[] VAOs;
uint[] VBOs;[font="arial, verdana, tahoma, sans-serif"][size="2"]
[size="2"]ushort[] Indices; // Cube indices[/size]
[size="2"]Vector3[] Vertices; // Cube vertices[/size]

[size="2"]public GameCtor()[/size]
[size="2"]{[/size]
[font="arial, verdana, tahoma, sans-serif"][size="2"] [/size][/font][size="2"]// Our uniform matrices[/size]
[font="arial, verdana, tahoma, sans-serif"][size="2"] [/size][/font][size="2"]Projection = Matrix4.CreatePerspectiveOffCenter(-Width / 2, Width / 2, -Height / 2, Height / 2, 0.01f, 500); // Perspective with 0,0 in the center and same as window width and height[/size]
[font="arial, verdana, tahoma, sans-serif"][size="2"] [/size][/font][size="2"]View = Matrix4.LookAt(0, 0, -100, 0, 0, 0, 0, 1, 0); // camera at (z = -100) looking at 0,0,0

[/size][/size][/font][font="arial, verdana, tahoma, sans-serif"][size="2"]
VAOs = new uint[1];
VBOs = new uint[2];
ShaderIDs = new int[2]; // Reserve space for 2 shaders, here we use one vertex shader
ProgramIDs = new int[1]; // Our shader program[/size][/font]
[font="arial, verdana, tahoma, sans-serif"][size="2"]
GL.GenVertexArrays(VAOs.Length, VAOs);[/size][/font]
GL.GenBuffers(VBOs.Length, VBOs);
InitVAOsVBOs();
InitVertexShader(); // Initialize our vertex shader

// Create a program and attach our first shader
ProgramIDs[0] = GL.CreateProgram();
GL.AttachShader(ProgramIDs[0], ShaderIDs[0]);

// Link and print result log
GL.LinkProgram(ProgramIDs[0]);
Console.WriteLine(GL.GetProgramInfoLog(ProgramIDs[0]));

// Start using our shader program
// Required before setting Uniforms,
// otherwise I get an error: InvalidValue
GL.UseProgram(ProgramIDs[0]);

// Set uniform projection and view matrices
int uProjIndex = GL.GetUniformLocation(ProgramIDs[0], "projection");
int uViewIndex = GL.GetUniformLocation(ProgramIDs[0], "view");
GL.UniformMatrix4(uProjIndex, true, ref Projection);
GL.UniformMatrix4(uViewIndex, true, ref View);

// Vertex Position attribute: vec3 inVertexPos;
int inVertexPosIndex = GL.GetAttribLocation(ProgramIDs[0], "inVertexPos"); // returns -1
GL.BindVertexArray(VAOs[0]);
GL.VertexAttribPointer(inVertexPosIndex, 3, VertexAttribPointerType.Float, false, 0, IntPtr.Zero);
GL.EnableVertexAttribArray(inVertexPosIndex);}
[/source]
Buffer initialization:
[source lang="csharp"]

private void InitVAOsVBOs()
{
Vertices = new Vector3[8] {
new Vector3(-80, 80, -80), //0 Top, Left, Front
new Vector3(-80, 80, 80), //1 Top, Left, Back
new Vector3(-80, -80, -80), //2 Top, Right, Front
new Vector3(-80, -80, 80), //3 Top, Right, Back
new Vector3( 80, 80, 80), //4 Bottom, Left, Front
new Vector3( 80, 80, -80), //5 Bottom, Left, Back
new Vector3( 80, -80, -80), //6 Bottom, Right, Front
new Vector3( 80, -80, 80) //7 Bottom, Right, Back
};
GL.BindBuffer(BufferTarget.ArrayBuffer, VBOs[0]);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(Vertices.Length * Vector3.SizeInBytes), Vertices, BufferUsageHint.StaticDraw);
GL.BindVertexArray(VAOs[0]);

Indices = new ushort[24] {
0, 1, 3, 2, // Top face
0, 1, 5, 4, // Left face
1, 3, 7, 5, // Back face
2, 3, 7, 6, // Right face
4, 5, 7, 6, // Bottom face
0, 2, 6, 4 // Front face
};
GL.BindBuffer(BufferTarget.ElementArrayBuffer, VBOs[0]);
GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(Indices.Length * sizeof(ushort)), Indices, BufferUsageHint.StaticDraw);
}[/source]
Vertex Shader initialization:
[source lang="csharp"]
private void InitVertexShader()
{
string VertexShaderSource = // Yes, this is inline shader source
@"#version 410
#pragma debug(on)

uniform mat4 projection;
uniform mat4 view;

vec3 inVertexPos;

void main()
{
gl_Position = vec4(inVertexPos, 1.0) * projection * view;
}";
// Create our vertex shader, upload our source, and compile
ShaderIDs[0] = GL.CreateShader(ShaderType.VertexShader);
GL.ShaderSource(ShaderIDs[0], VertexShaderSource);
GL.CompileShader(ShaderIDs[0]);
Console.WriteLine(GL.GetShaderInfoLog(ShaderIDs[0])); // check the log to make sure it succeeds
}
[/source]
Render code:
[source lang="csharp"]
protected override void OnRenderFrame(FrameEventArgs e)
{
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

// Draw using the indices buffer
GL.DrawElements(BeginMode.Quads, Indices.Length, DrawElementsType.UnsignedShort, 0);

this.SwapBuffers();
}
[/source]

Share this post


Link to post
Share on other sites
Have you verified that inVertexPosIndex returns something other than -1? If you get that and pass it to AttribPointer it will probably fail.

I think you're missing the "in" keyword in your shader for this variable (unless this was removed in a more recent version than I'm familiar with).

"vec3 inVertexPos;"

Share this post


Link to post
Share on other sites
[s]yes, I checked that the location returned for the variable is always 1.
the 4.1 reference sheet says that if the "in" is omitted then it is implied.[/s]
[s]
[/s]
EDIT: The location returned is -1.
EDIT2: Fixed by adding the in keyword, as the OpenTK binding does not support higher than 1.5 GLSL. Edited by wiz3kid

Share this post


Link to post
Share on other sites
[quote name='V-man' timestamp='1313942898' post='4851929']
Do you have a VAO bound?
[/quote]
[s]See the InitBuffers() function. AFAIK, VAOs are deprecated since 3.3... and are removed since 4.0. VBO's are the way to go.[/s]
[s]
[/s]
EDIT: Now I do. [img]http://public.gamedev.net/public/style_emoticons/default/smile.gif[/img] Edited by wiz3kid

Share this post


Link to post
Share on other sites
[quote name='V-man' timestamp='1313967242' post='4852038']
VAO is in glspec33.core.20100311.pdf
glspec40.core.20100311.pdf
glspec41.core.20100725.pdf

You are forced to use them.
[/quote]

[s]Oh, I thought VAOs were the gl*Pointer family of functions....
So, I'm supposed to use glGenVertexArrays() instead of glGenBuffers()?[/s]

EDIT: Read up on it a bit, and I don't understand where the VAO gets its data from. I understood that I have to use both buffers and VAOs to achieve what I want , right? Edited by wiz3kid

Share this post


Link to post
Share on other sites
Problem partially resolved! No errors anymore, using gDEBugger I see that it's rendering 24 vertices each frame.
Now the next problem, I don't see anything on the screen. New code attached below.
I added a fragment shader which sets all the fragments to white color.
I've also attached a zip containing a binary for anyone interested.

[attachment=5009:Game1 - Copy.cs.txt]

[attachment=5010:ShadersAndPrograms.zip]

Share this post


Link to post
Share on other sites
I think
"gl_Position = vec4(inVertexPos, 1.0) * projection * view;"

should be
"gl_Position = projection * view * vec4(inVertexPos, 1.0);"

As you want to apply the view matrix to transform the vertex position into eye space,
and then project it into clipping space by multiplying with the projection matrix.

Share this post


Link to post
Share on other sites
[quote name='FlorianR' timestamp='1314007725' post='4852208']
I think
"gl_Position = vec4(inVertexPos, 1.0) * projection * view;"

should be
"gl_Position = projection * view * vec4(inVertexPos, 1.0);"

As you want to apply the view matrix to transform the vertex position into eye space,
and then project it into clipping space by multiplying with the projection matrix.
[/quote]


Still not working....

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  

  • Announcements

  • Forum Statistics

    • Total Topics
      628333
    • Total Posts
      2982121
  • Similar Content

    • By DejayHextrix
      Hi, New here. 
      I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc.... 
      I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn? 
      How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want? 
      Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn. 
      So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started. 
      Thanks, 
      Dejay Hextrix 
    • By mellinoe
      Hi all,
      First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
      Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
      The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
      Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
    • By aejt
      I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
      I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
      This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
      I have these classes:
      For GPU resources:
      Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
      Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
      ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).  
      And my plan is to define everything into these XML documents to abstract away files:
      Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
      Factory classes for resources:
      For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
      Factory classes for assets:
      Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).
       
       
      Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
      Thanks!
    • By nedondev
      I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
      Thanks.
    • By Abecederia
      So I've recently started learning some GLSL and now I'm toying with a POM shader. I'm trying to optimize it and notice that it starts having issues at high texture sizes, especially with self-shadowing.
      Now I know POM is expensive either way, but would pulling the heightmap out of the normalmap alpha channel and in it's own 8bit texture make doing all those dozens of texture fetches more cheap? Or is everything in the cache aligned to 32bit anyway? I haven't implemented texture compression yet, I think that would help? But regardless, should there be a performance boost from decoupling the heightmap? I could also keep it in a lower resolution than the normalmap if that would improve performance.
      Any help is much appreciated, please keep in mind I'm somewhat of a newbie. Thanks!
  • Popular Now