Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


[SOLVED] Generic Vertex Attribute


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 anonymous22   Members   -  Reputation: 199

Like
0Likes
Like

Posted 20 August 2011 - 07:28 PM


EDIT: Problem partially resolved, see post #8.
Hello,

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 Posted Image). Anyways, so I have got this basic pass-through vertex shader: (also embedded in program below)
[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]
The problem I am having is marked on line 48 (generic vertex attribute location equals -1) below.

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

[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]

Attached Files



Sponsor:

#2 karwosts   Members   -  Reputation: 840

Like
1Likes
Like

Posted 20 August 2011 - 09:27 PM

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;"
My Projects:
Portfolio Map for Android - Free Visual Portfolio Tracker
Electron Flux for Android - Free Puzzle/Logic Game

#3 anonymous22   Members   -  Reputation: 199

Like
0Likes
Like

Posted 21 August 2011 - 04:22 AM

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.



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, 22 August 2011 - 04:56 AM.


#4 V-man   Members   -  Reputation: 805

Like
1Likes
Like

Posted 21 August 2011 - 10:08 AM

Do you have a VAO bound?
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

#5 anonymous22   Members   -  Reputation: 199

Like
0Likes
Like

Posted 21 August 2011 - 11:11 AM

Do you have a VAO bound?

See the InitBuffers() function. AFAIK, VAOs are deprecated since 3.3... and are removed since 4.0. VBO's are the way to go.


EDIT: Now I do. Posted Image

Edited by wiz3kid, 21 August 2011 - 06:33 PM.


#6 V-man   Members   -  Reputation: 805

Like
1Likes
Like

Posted 21 August 2011 - 04:54 PM

VAO is in glspec33.core.20100311.pdf
glspec40.core.20100311.pdf
glspec41.core.20100725.pdf

You are forced to use them.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

#7 anonymous22   Members   -  Reputation: 199

Like
0Likes
Like

Posted 21 August 2011 - 05:24 PM

VAO is in glspec33.core.20100311.pdf
glspec40.core.20100311.pdf
glspec41.core.20100725.pdf

You are forced to use them.


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


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, 21 August 2011 - 06:33 PM.


#8 anonymous22   Members   -  Reputation: 199

Like
0Likes
Like

Posted 21 August 2011 - 08:52 PM

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.

Attached File  Game1 - Copy.cs.txt   5.83KB   35 downloads

Attached File  ShadersAndPrograms.zip   1.64MB   22 downloads

#9 FlorianR   Members   -  Reputation: 100

Like
0Likes
Like

Posted 22 August 2011 - 04:08 AM

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.

#10 anonymous22   Members   -  Reputation: 199

Like
0Likes
Like

Posted 22 August 2011 - 04:25 AM

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.



Still not working....




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS