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

OpenGL [SOLVED] Generic Vertex Attribute

Recommended Posts

?????????    199
[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
karwosts    840
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
?????????    199
[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
?????????    199
[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
V-man    813
VAO is in glspec33.core.20100311.pdf
glspec40.core.20100311.pdf
glspec41.core.20100725.pdf

You are forced to use them.

Share this post


Link to post
Share on other sites
?????????    199
[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
?????????    199
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
FlorianR    100
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
?????????    199
[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  

  • Similar Content

    • By pseudomarvin
      I assumed that if a shader is computationally expensive then the execution is just slower. But running the following GLSL FS instead just crashes
      void main() { float x = 0; float y = 0; int sum = 0; for (float x = 0; x < 10; x += 0.00005) { for (float y = 0; y < 10; y += 0.00005) { sum++; } } fragColor = vec4(1, 1, 1 , 1.0); } with unhandled exception in nvoglv32.dll. Are there any hard limits on the number of steps/time that a shader can take before it is shut down? I was thinking about implementing some time intensive computation in shaders where it would take on the order of seconds to compute a frame, is that possible? Thanks.
    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
  • Popular Now