Hi!
I am trying to render colored quads to the screen, using geometry shaders to convert from points to triangle strips. However, nothing is rendered to the screen. I am sure the data I pass to OpenGL is correct, so I suspect there must be something wrong with my code.
This is what I run on program initialization:
m_program_q = glCreateProgram();
m_vshader_q = glCreateShader(GL_VERTEX_SHADER);
m_gshader_q = glCreateShader(GL_GEOMETRY_SHADER);
m_fshader_q = glCreateShader(GL_FRAGMENT_SHADER);
const GLchar* vsource = LoadShader("buttons.vert");
const GLchar* gsource = LoadShader("buttons.geom");
const GLchar* fsource = LoadShader("buttons.frag");
glShaderSource(m_vshader_q, 1, &vsource, NULL);
glShaderSource(m_gshader_q, 1, &gsource, NULL);
glShaderSource(m_fshader_q, 1, &fsource, NULL);
glAttachShader(m_program_q, m_vshader_q);
glAttachShader(m_program_q, m_gshader_q);
glAttachShader(m_program_q, m_fshader_q);
glCompileShader(m_vshader_q);
glCompileShader(m_gshader_q);
glCompileShader(m_fshader_q);
glLinkProgram(m_program_q);
delete [] vsource;
delete [] gsource;
delete [] fsource;
char buf[512];
glGetProgramInfoLog(m_program_q, 512, NULL, buf);
std::cout << buf;
glGetShaderInfoLog(m_vshader_q, 512, NULL, buf);
std::cout << buf;
glGetShaderInfoLog(m_gshader_q, 512, NULL, buf);
std::cout << buf;
glGetShaderInfoLog(m_fshader_q, 512, NULL, buf);
std::cout << buf;
m_loc_pos = glGetAttribLocation(m_program_q, "in_pos");
m_loc_color = glGetAttribLocation(m_program_q, "in_color");
m_loc_scaler = glGetUniformLocation(m_program_q, "scaler");
glGenBuffers(1, &m_quadbuf_id);
glBindBuffer(GL_ARRAY_BUFFER, m_quadbuf_id);
glGenVertexArrays(1, &m_vao_id);
glBindVertexArray(m_vao_id);
glEnableVertexAttribArray(m_loc_pos);
glEnableVertexAttribArray(m_loc_color);
glVertexAttribIPointer(m_loc_pos, 4, GL_INT, sizeof(QuadData), NULL);
glVertexAttribPointer(m_loc_color, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(QuadData), (void*)offsetof(QuadData, m_color));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
This is how I pass data:
glBindBuffer(GL_ARRAY_BUFFER, m_quadbuf_id);
glBufferData(GL_ARRAY_BUFFER, (m_quaddatasize + 0x20)&(~0x1F), NULL, GL_STATIC_DRAW); // Round up the size to nearest multiple of 32
GLubyte* buf = static_cast<GLubyte*>(glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY));
for(size_t i = 0; i < m_panelrenderers.size(); i++) {
MyPanelRenderer* pr = m_panelrenderers[i];
size_t s = pr->GetQuadSize();
if (pr->NeedUpdate())
{
memcpy(buf, pr->GetQuadData(), s);
pr->SetUpdated();
}
buf += s;
}
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER, 0);
And this is how I draw:
float x = 1.0f/(float)m_windowwidth, y = 1.0f/(float)(-m_windowheight);
glUseProgram(m_program_q);
glUniform4f(m_loc_scaler, x, y, x, y);
glBindVertexArray(m_vao_id);
glDrawArrays(GL_POINTS, 0, m_quadcount);
glUseProgram(0);
glBindVertexArray(0);
Shaders:
#version 330
uniform vec4 scaler;
in ivec4 in_pos;
in vec4 in_color;
out vec4 vertcolor;
void main() {
gl_Position = vec4(in_pos) * scaler;
vertcolor = in_color;
}
#version 330
layout(points) in;
layout(triangle_strip, max_vertices = 4) out;
in vec4 vertcolor[];
flat out vec4 color;
void main() {
vec4 p0 = vec4(gl_in[0].gl_Position.xy, 0.0, 1.0);
vec4 p1 = vec4(gl_in[0].gl_Position.zw, 0.0, 1.0);
gl_Position = p0;
color = vertcolor[0];
EmitVertex();
gl_Position = vec4(p1.x, p0.yzw);
EmitVertex();
color = vertcolor[0];
gl_Position = vec4(p0.x, p1.yzw);
EmitVertex();
gl_Position = p1;
EmitVertex();
}
#version 330
flat in vec4 color;
void main() {
gl_FragColor = ivec4(color);
}
I have tried to find what’s wrong, but whatever I change, there’s still nothing rendered. glGetError() doesn’t return any errors at all.