Jump to content
  • Advertisement
Sign in to follow this  

OpenGL Using Vertex Array Objects With Cg Shaders

This topic is 2594 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

I'm writing an application which uses OpenGL vertex array objects and Cg shaders, however I'm having difficulty getting the latter to read texture coordinates from the former.

The weird thing is, the exact same VAO works fine with a GLSL shader, whilst the Cg shader works fine with legacy OpenGL calls (glTexCoord2f etc.)

The code used to create a vertex array object is shown below:

float vertices[12] = {-0.5f, -0.5f, -0.5f, 0.5f, -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f};

float texCoords[8] = {0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f};

// Create and bind the VAO to store the square.
glGenVertexArrays(1, &vao[0]);

glGenBuffers(2, vbo);

// Vertex VBO.
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), 0);

// Texture VBO.
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), texCoords, GL_STATIC_DRAW);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), 0);

glBindBuffer(GL_ARRAY_BUFFER, 0);

The Cg vertex shader:

struct OUTPUTv
float4 position : POSITION;

float2 texCoord : TEXCOORD0;

OUTPUTv mainV(in float4 position : POSITION, in float2 texCoord : TEXCOORD0, uniform float4x4 modelviewproj)

OUT.position = mul(modelviewproj, position);

OUT.texCoord = texCoord;

return OUT;

The Cg fragment shader:

struct OUTPUTf
float4 color : COLOR;

OUTPUTf mainF(in float2 texCoord : TEXCOORD0, uniform sampler2D diffuseMap : TEX0)

OUT.color = tex2D(diffuseMap, texCoord);

return OUT;

Any advice would be appreciated, thanks!

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!