Hi. I’m seeing some strange behavior accessing a vertex attribute in my shader. In particular, the y-component appears to be identical to the x-component when I swizzle .yy. ,xx and .yy are no different. When I swizzle .xy, the y-component appears to be correct.
I’ve worked up as contained an example as I can:
#include <stdio.h>
#include <string>
#define GL_GLEXT_PROTOTYPES
#include <GL/glut.h>
GLuint hands_aid;
GLuint hands_pid;
void render() {
glViewport(0, 0, 512, 512);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(hands_pid);
glBindVertexArray(hands_aid);
glDrawArrays(GL_POINTS, 0, 3);
glBindVertexArray(0);
glUseProgram(0);
glutSwapBuffers();
}
int main(int argc, char **argv) {
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(512, 512);
glutInit(&argc, argv);
glutCreateWindow("Test");
glutDisplayFunc(render);
glPointSize(3.0f);
float positions[] = {
-0.2f, 0.1f, 0.7f, 1.0f,
-0.3f, 0.5f, 0.7f, 1.0f,
-0.4f, 0.9f, 0.7f, 1.0f,
};
// Create a vertex array to bundle all vertex state.
glGenVertexArrays(1, &hands_aid);
glBindVertexArray(hands_aid);
// Create a buffer for the spatial coordinates.
GLuint position_bid;
glGenBuffers(1, &position_bid);
glBindBuffer(GL_ARRAY_BUFFER, position_bid);
glBufferData(GL_ARRAY_BUFFER, 3 * 4 * sizeof(float), positions, GL_STATIC_DRAW);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// Compile vertex shader.
std::string src =
"#version 330
"
"
"
"in vec4 position;
"
"
"
"void main() {
"
" gl_Position = vec4(position.yy, 0.0, 1.0);
"
"}
";
// If I change position.yy to position.xy, the values seem correct. But yy
// is no different than xx.
GLuint vertex_sid = glCreateShader(GL_VERTEX_SHADER);
const char *buffer = &src.c_str()[0];
glShaderSource(vertex_sid, 1, (const GLchar **) &buffer, NULL);
glCompileShader(vertex_sid);
// Compile fragment shader.
src =
"#version 330
"
"
"
"out vec4 fragColor;
"
"
"
"void main() {
"
" fragColor = vec4(1.0);
"
"}
";
GLuint fragment_sid = glCreateShader(GL_FRAGMENT_SHADER);
buffer = &src.c_str()[0];
glShaderSource(fragment_sid, 1, (const GLchar **) &buffer, NULL);
glCompileShader(fragment_sid);
// Link.
hands_pid = glCreateProgram();
glAttachShader(hands_pid, vertex_sid);
glAttachShader(hands_pid, fragment_sid);
glBindAttribLocation(hands_pid, 0, "position");
glLinkProgram(hands_pid);
glutMainLoop();
return 0;
}
This image shows that x and y are correctly retrieved from the VBO when I swizzle xy. The x’s are negative, y’s positive:
Here I swizzle xx; x is used for both the x and y position of the vertex:
And here I swizzle yy; y is used for both the x and y position of the vertex, and it looks just like xx:
Any ideas where I’m going wrong? I appreciate your help!
- Chris