I came across the following legacy immediate code for drawing truchet tiles. I ran it and it works fine:
void setWindow(float left, float right, float bottom, float top)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(left, right, bottom, top);
}
void drawTile()
{
glBegin(GL_LINE_LOOP);
for (float angle = 0.0; angle < 360 * 0.01745329; angle += 0.01745329)
{
glVertex2f(250 * cos(angle), 250 * sin(angle));
}
glEnd();
glFlush();
glBegin(GL_LINE_LOOP);
for (float angle = 0.0; angle < 360 * 0.01745329; angle += 0.01745329)
{
glVertex2f(500+ 250 * cos(angle) , 500 + 250 * sin(angle));
}
glEnd();
glFlush();
}
float random()
{
float lc = 0.0f;
srand(time(NULL));
lc = ((double)rand() / (RAND_MAX));
return lc;
}
void screen()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
srand(time(NULL));
if (rand() % 2 == 0)
{
setWindow(500.0, 0.0, 0.0, 500.0);
}
else
{
setWindow(0.0, 500.0, 0.0, 500.0);
}
glViewport(i * 50, j * 50, 50, 50);
drawTile();
for (int i = 0; i < 99999999; i++);
}
}
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
screen();
}
void setWindow(float left, float right, float bottom, float top)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(left, right, bottom, top);
}
void drawTile()
{
glBegin(GL_LINE_LOOP);
for (float angle = 0.0; angle < 360 * 0.01745329; angle += 0.01745329)
{
glVertex2f(250 * cos(angle), 250 * sin(angle));
}
glEnd();
glFlush();
glBegin(GL_LINE_LOOP);
for (float angle = 0.0; angle < 360 * 0.01745329; angle += 0.01745329)
{
glVertex2f(500+ 250 * cos(angle) , 500 + 250 * sin(angle));
}
glEnd();
glFlush();
}
float random()
{
float lc = 0.0f;
srand(time(NULL));
lc = ((double)rand() / (RAND_MAX));
return lc;
}
void screen()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
srand(time(NULL));
if (rand() % 2 == 0)
{
setWindow(500.0, 0.0, 0.0, 500.0);
}
else
{
setWindow(0.0, 500.0, 0.0, 500.0);
}
glViewport(i * 50, j * 50, 50, 50);
drawTile();
for (int i = 0; i < 99999999; i++);
}
}
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
screen();
}
I tried to convert it to modern opengl but had no success, here is my attempt
void setWindow(float lft, float rght, float bttom, float tp)
{
// Compute the MVP matrix
glm::mat4 ProjectionMatrix = glm::ortho(lft, rght, bttom, tp, -1.0f, 1.0f);
glm::mat4 ViewMatrix = glm::mat4(1.0f);
glm::mat4 ModelMatrix = glm::mat4(1.0f);
MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
ndcT = vp * MVP;
// Send our transformation to the currently bound shader,
// in the "MVP" uniform
//glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
}
float random()
{
float lc = 0.0f;
srand(time(NULL));
lc = ((double)rand() / (RAND_MAX));
return lc;
}
void drawTile()
{
int c=0;
int b = num_loops;
for (float angle = 0.0; angle < 360 * 0.01745329; angle += 0.01745329)
{
g_vertex_buffer_data.push_back(vec2(250 * cos(angle), 250 * sin(angle)));
c++;
}
if (num_loops==0)
first.push_back(0);
else
first.push_back(first[first.size()-1]+count[count.size()-1]);
count.push_back(c);
num_loops++;
c = 0;
for (float angle = 0.0; angle < 360 * 0.01745329; angle += 0.01745329)
{
g_vertex_buffer_data.push_back(vec2(500+ 250 * cos(angle) , 500 + 250 * sin(angle)));
c++;
}
first.push_back(first[first.size()-1]+count[count.size()-1]);
count.push_back(c);
num_loops++;
for (int i=first[b]; i<first[num_loops-1]+count[num_loops-1]; i++) {
fprintf(dbg, "%f\t%f\n", g_vertex_buffer_data[i][0], g_vertex_buffer_data[i][1]);
vec4 nw = MVP * vec4(g_vertex_buffer_data[i][0], g_vertex_buffer_data[i][1], 0.0f, 1.0f);
fprintf(dbg, "%f\t%f\n", nw[0], nw[1]);
nw = vp * nw;
g_vertex_buffer_data[i][0] = nw[0];
g_vertex_buffer_data[i][1] = nw[1];
fprintf(dbg, "%f\t%f\n\n", g_vertex_buffer_data[i][0], g_vertex_buffer_data[i][1]);
}
fprintf(dbg, "\n\n");
}
void screen()
{
//int v1 = rand() % 10;
// set a fixed window
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
srand(time(NULL));
//glViewport(i * 50, j * 50, 50, 50);
vp = glm::translate(glm::scale(glm::translate(mat4(1.0f), vec3(0.1f*i, 0.1f*j, 0.0f)), vec3(0.1f, 0.1f, 1.0f)), vec3(1.0f, 1.0f, 0.0f));
if (rand() % 2 == 0)
{
setWindow(500.0, 0.0, 0.0, 500.0);
}
else
{
setWindow(0.0, 500.0, 0.0, 500.0);
}
drawTile();
//for (int i = 0; i < 99999999; i++);
}
}
glBufferStorage( GL_ARRAY_BUFFER, g_vertex_buffer_data.size()*sizeof(glm::vec2), &g_vertex_buffer_data[0], 0);
}
void setup(void)
{
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
// Create and compile our GLSL program from the shaders
GLuint programID = LoadShaders( "VertexShaderTruchet.vertexshader", "FragmentShaderTruchet.fragmentshader" );
// Get a handle for our "MVP" uniform
//MatrixID = glGetUniformLocation(programID, "MVP");
//glViewport(0, 0, 500, 500);
// Use our shader
glUseProgram(programID);
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute. No particular reason for 0, but must match the layout in the shader.
2, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
screen();
}
//<<<<<<<<<<<<<<<<<<<<<<<< myDisplay >>>>>>>>>>>>>>>>>
// the redraw function
void myDisplay(void)
{
static const float white[] = { 1.0f, 1.0f, 1.0f, 0.0f };
glClearBufferfv(GL_COLOR, 0, white);
glFlush();
glMultiDrawArrays(GL_LINE_LOOP, &first[0], &count[0], num_loops); // indices starting at 0
glFlush();
}
As clear from my code I print out the vertices and most of them are out of the rangle [-1,1]
I do not understand how calling glortho with left>right effects rendering.
Any lead to what is wrong with my MVP, viewport explicit matrix. Also I tried theMVP matrix
passed as uniform to vertex shader and use glviewport but still didn’t work.