PDA

View Full Version : simple program for creating a large square and concentric smaller squares in it...



a-sylum
09-17-2014, 04:41 PM
hi, i can't get this simple program to work. im using linux, i dont know whether its a glx problem or not so ill post it here first. im trying to make squares each with a different color that fit inside eachother and leave an L shape where the bigger first square is not covered by the smaller one

im doing this by starting with a simple identity matrix and lowering x and y 1 values by 0.2 to create smaller squares. im giving the matrix and color values through uniform variables in my shaders.
what happens is that the last drawn square is the only square drawn and all the space around it where the other squares should be is the color i set the window to clear to
1439

here is my attributes for my fbattribute config:

int doublebufferattributes[] = {
GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_DOUBLEBUFFER, True, /* Request a double-buffered color buffer with */
GLX_RED_SIZE, 1, /* the maximum number of bits per component */
GLX_GREEN_SIZE, 1,
GLX_BLUE_SIZE, 1,
None
};

here is my init function:

void init()
{
if (glewInit() != GLEW_OK)
{
printf("glew could not load");
exit(1);
}

glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glClearColor(0, 0, 0, 1);
const char *fshader =
"#version 430 core\n"
"out vec4 fColor;\n"
"uniform vec4 Color;\n"
"void main()\n"
"{\n"
"fColor = Color;\n"
"}\n";

const char *vshader =
"#version 430 core\n"
"layout (location = 0) in vec3 vPosition;\n"
"uniform mat3 Basicmat;\n"
"void main()\n"
"{\n"
"gl_Position = vec4(Basicmat * vPosition, 1);\n"
"}\n";

GLuint prog = glCreateProgram();

GLuint vert = glCreateShader(GL_VERTEX_SHADER);
GLuint frag = glCreateShader(GL_FRAGMENT_SHADER);

GLint vlen = (GLint)strlen(vshader);
GLint flen = (GLint)strlen(fshader);

glShaderSource(vert, 1, &vshader, &vlen);
glShaderSource(frag, 1, &fshader, &flen);
glCompileShader(vert);
glCompileShader(frag);


glAttachShader(prog, vert);
glAttachShader(prog, frag);
glLinkProgram(prog);
glUseProgram(prog);

GLint statusvert;
GLint statusfrag;

glGetShaderiv(vert, GL_COMPILE_STATUS, &statusvert);
glGetShaderiv(frag, GL_COMPILE_STATUS, &statusfrag);

printf("vert: %d\nfrag: %d\n", statusvert, statusfrag);
printf("GL_TRUE: %d\nGL_FALSE: %d\n", GL_TRUE, GL_FALSE);

colorloc = glGetUniformLocation(prog, "Color");
matloc = glGetUniformLocation(prog, "Basicmat");

GLfloat vertices[] = {
0, 0, 0,
0, 1, 0,
1, 1, 0,
0, 0, 0,
1, 1, 0,
1, 0, 0
};

glGenBuffers(1, &buf);
glBindBuffer(GL_ARRAY_BUFFER, buf);
glBufferData(GL_ARRAY_BUFFER, sizeof vertices, vertices, GL_STATIC_DRAW);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void *)0);
glEnableVertexAttribArray(0);


}

here is my display function:

void displayfunc()
{
glClear(GL_COLOR_BUFFER_BIT);

glUniform4f(colorloc, 1, 0, 0, 1);
glUniformMatrix3fv(matloc, 1, GL_FALSE, Basicmat);

glDrawArrays(GL_TRIANGLES, 0 , 6);

glUniform4f(colorloc, 0, 1, 0, 1);

Basicmat[0] = 0.8;
Basicmat[4] = 0.8;

glUniformMatrix4fv(matloc, 1, GL_FALSE, Basicmat);

glDrawArrays(GL_TRIANGLES, 0 , 6);

glUniform4f(colorloc, 0, 0, 1, 1);

Basicmat[0] = 0.6;
Basicmat[4] = 0.6;

glUniformMatrix4fv(matloc, 1, GL_FALSE, Basicmat);

glDrawArrays(GL_TRIANGLES, 0 , 6);
}


it seems as though the gldrawarrays clears the screen.

thanks for the help

a-sylum
09-17-2014, 06:44 PM
nevermind :)