PDA

View Full Version : Pass two attributes into vertex shader(Qt5.0.2)



stereoMatching
9th May 2013, 03:33
platform : mac osx 10.8.3
compiler : clang3.2

Learning opengl from this website(ch2, playing with colors)
"opengl":http://www.arcsynthesis.org/gltut/

Trying to pass my data into two attributes, but the second attributes can't have the data correctly

vertex shader

attribute vec4 position;
attribute vec4 color;

varying vec4 theColor;

void main() {
gl_Position = position;
theColor = color;
//theColor = vec4(0.5, 0.8, 0.3, 1.0);
}

fragment shader

varying vec4 theColor;

void main()
{
gl_FragColor = theColor;
}

initialize buffer


std::vector<GLfloat> const vertexPositions{
0.75f, 0.75f, 0.0f, 1.0f,
0.75f, -0.75f, 0.0f, 1.0f,
-0.75f, -0.75f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f
};

positionBufferObject.push_back(0);

glGenBuffers(1, &positionBufferObject.back());
glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject.back());

glBufferData(GL_ARRAY_BUFFER, buffer.size() * sizeof(GLfloat), &buffer[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

paintGL


void ch2FragPosition::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject[0]);

int vertexLocation = program.attributeLocation("position");
program.enableAttributeArray(vertexLocation);
glVertexAttribPointer(vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);

int colorLocation = program.attributeLocation("color");
program.enableAttributeArray(colorLocation);
glVertexAttribPointer(colorLocation, 4, GL_FLOAT, GL_FALSE, 0, (GLvoid const*)(12 * sizeof(GLfloat)));

glDrawArrays(GL_TRIANGLES, 0, 3);

glBindBuffer(GL_ARRAY_BUFFER, 0);
}


The QGLWidget show me a black window, if I alter the vertex shader like these
QGLWidget will show me a triangle with color


attribute vec4 position;
attribute vec4 color;

varying vec4 theColor;

void main() {
gl_Position = position;
//theColor = color;
theColor = vec4(0.5, 0.8, 0.3, 1.0);
}

How could I fix this problem?Thanks

Added after 1 7 minutes:

I found the problem already, it has nothing to do with the opengl, I forgot to point the
class by a proper base class.

base class is : ch1HelloTriangle
derived class : ch2FragPosition

I declared the class liked this



std::unique_ptr<ch2FragPosition> ch2_frag_position(new ch2FragPosition(":ch2/modernOpenGLShader/ch2/positionVertex",
":ch2/modernOpenGLShader/ch2/positionFrag"));
ch2_frag_position->show();

But it should be



std::unique_ptr<ch1HelloTriangle> ch2_frag_position(new ch2FragPosition(":ch2/modernOpenGLShader/ch2/positionVertex",
":ch2/modernOpenGLShader/ch2/positionFrag"));
ch2_frag_position->show();

Thanks for your helps