1
Graphics / Re: Using sf::Views and OpenGL
« on: May 24, 2018, 05:33:53 pm »
Thanks.
So I've been trying to get a buffer working.
This works except I can't get the colour into the shader...
Vertex Shader:
Fragment Shader:
I also tried it with proper VBOs but I couldn't get any data into the shader (it renders a white dot at 0,0,0, well, hopefully 10,000 white dots but I can't tell):
Init:
Render:
I still have the problem of syncing a sf::view to an OpenGL viewport/camera but somehow that seems like a simpler problem to have...
So I've been trying to get a buffer working.
This works except I can't get the colour into the shader...
struct PointBuffer
{
GLfloat x, y, z;
GLfloat r, g, b;
};
vector<PointBuffer> mPointBuffer;
... fill the array with positions and colours ...
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(PointBuffer), &mPointBuffer[0]);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3, GL_FLOAT, sizeof(PointBuffer), &mPointBuffer[0].r);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -100.f);
glDrawArrays(GL_POINTS, 0, mPointBuffer.size());
{
GLfloat x, y, z;
GLfloat r, g, b;
};
vector<PointBuffer> mPointBuffer;
... fill the array with positions and colours ...
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(PointBuffer), &mPointBuffer[0]);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3, GL_FLOAT, sizeof(PointBuffer), &mPointBuffer[0].r);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -100.f);
glDrawArrays(GL_POINTS, 0, mPointBuffer.size());
Vertex Shader:
#version 330 core
in vec3 vertexPosition;
in vec3 vertexColor;
out vec4 fragmentColor;
void main(){
gl_Position.xyz = vertexPosition;
gl_Position.w = 1.0;
fragmentColor = vec4(vertexColor, 1.0);
}
in vec3 vertexPosition;
in vec3 vertexColor;
out vec4 fragmentColor;
void main(){
gl_Position.xyz = vertexPosition;
gl_Position.w = 1.0;
fragmentColor = vec4(vertexColor, 1.0);
}
Fragment Shader:
#version 330 core
in vec4 fragmentColor;
out vec4 color;
void main(){
color = fragmentColor;
// color = vec4(1,1,1,1);
}
in vec4 fragmentColor;
out vec4 color;
void main(){
color = fragmentColor;
// color = vec4(1,1,1,1);
}
I also tried it with proper VBOs but I couldn't get any data into the shader (it renders a white dot at 0,0,0, well, hopefully 10,000 white dots but I can't tell):
Init:
glGenBuffers(1, &mElementBufferObject);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mElementBufferObject);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int), &mElementBuffer[0], GL_STATIC_DRAW);
glGenBuffers(1, &mVertexBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, mVertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(PointBuffer), &mPointBuffer[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mElementBufferObject);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int), &mElementBuffer[0], GL_STATIC_DRAW);
glGenBuffers(1, &mVertexBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, mVertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(PointBuffer), &mPointBuffer[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
Render:
glBindBuffer(GL_ARRAY_BUFFER, mVertexBufferObject);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(PointBuffer), BUFFER_OFFSET(0));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(PointBuffer), BUFFER_OFFSET(3 * sizeof(GLfloat)));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mElementBufferObject);
glDrawArrays(GL_POINTS, 0, mPointBuffer.size());
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(PointBuffer), BUFFER_OFFSET(0));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(PointBuffer), BUFFER_OFFSET(3 * sizeof(GLfloat)));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mElementBufferObject);
glDrawArrays(GL_POINTS, 0, mPointBuffer.size());
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
I still have the problem of syncing a sf::view to an OpenGL viewport/camera but somehow that seems like a simpler problem to have...