fix some shapes generation and draw a grid of shapes for an overview
This commit is contained in:
@@ -29,6 +29,14 @@ void App::init()
|
|||||||
"void main(){"
|
"void main(){"
|
||||||
" frag = texture(tex, uv, 0.0);"
|
" frag = texture(tex, uv, 0.0);"
|
||||||
"}";
|
"}";
|
||||||
|
static const char* shader_uv_f =
|
||||||
|
"#version 150\n"
|
||||||
|
"uniform sampler2D tex;"
|
||||||
|
"in vec2 uv;"
|
||||||
|
"out vec4 frag;"
|
||||||
|
"void main(){"
|
||||||
|
" frag = vec4(uv,0,1);"
|
||||||
|
"}";
|
||||||
static const char* shader_color_v =
|
static const char* shader_color_v =
|
||||||
"#version 150\n"
|
"#version 150\n"
|
||||||
"uniform mat4 mvp;"
|
"uniform mat4 mvp;"
|
||||||
@@ -45,9 +53,12 @@ void App::init()
|
|||||||
"}";
|
"}";
|
||||||
shader.create(shader_v, shader_f);
|
shader.create(shader_v, shader_f);
|
||||||
shader_color.create(shader_color_v, shader_color_f);
|
shader_color.create(shader_color_v, shader_color_f);
|
||||||
plane.create<15>(.5f, .5f);
|
shader_uv.create(shader_v, shader_uv_f);
|
||||||
longPlane.create<1>(.3, .05f);
|
plane.create<5>(50, 50);
|
||||||
circle.create<6>(.5f, .25f);
|
longPlane.create<1>(50, 25);
|
||||||
|
circle.create<6>(25, 12);
|
||||||
|
circle1.create<4>(25, 12);
|
||||||
|
circle2.create<15>(25, 12);
|
||||||
if (!tex.load("data/image.png"))
|
if (!tex.load("data/image.png"))
|
||||||
printf("error loading image\n");
|
printf("error loading image\n");
|
||||||
|
|
||||||
@@ -79,24 +90,55 @@ void App::update(float dt)
|
|||||||
theta += M_PI * 0.5f * dt;
|
theta += M_PI * 0.5f * dt;
|
||||||
float red = fabsf(sinf(theta));
|
float red = fabsf(sinf(theta));
|
||||||
|
|
||||||
glm::mat4 proj = glm::perspective(glm::radians(85.f), 1.f, .1f, 100.f);
|
// glm::mat4 proj = glm::perspective(glm::radians(85.f), 1.f, .1f, 100.f);
|
||||||
glm::mat4 model = glm::translate(glm::vec3(0, 0, 0));
|
// glm::mat4 model = glm::translate(glm::vec3(0, 0, 0));
|
||||||
glm::mat4 view = glm::lookAt(glm::vec3(sinf(theta), 0, 1), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
|
// glm::mat4 view = glm::lookAt(glm::vec3(sinf(theta), 0, 1), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
|
||||||
auto mvp = proj * view * model;
|
|
||||||
|
float ar = width / height;
|
||||||
|
glm::mat4 proj = glm::ortho<float>(0, width, height, 0, -1, 1);
|
||||||
|
|
||||||
|
Shape* shapes[] = { &circle, &circle1, &circle2, &plane, &longPlane };
|
||||||
|
|
||||||
//glClearColor(red, 0, 0, 1);
|
//glClearColor(red, 0, 0, 1);
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
shader.use();
|
|
||||||
shader.u_mat4("mvp", glm::mat4());
|
|
||||||
shader.u_int("tex", 0);
|
|
||||||
|
|
||||||
glActiveTexture(GL_TEXTURE0);
|
auto s = glm::scale(glm::vec3(2));
|
||||||
tex.bind();
|
for (int i = 0; i < 5; i++)
|
||||||
circle.draw_fill();
|
{
|
||||||
tex.unbind();
|
shader.use();
|
||||||
|
|
||||||
shader_color.use();
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||||
shader_color.u_mat4("mvp", glm::mat4());
|
glActiveTexture(GL_TEXTURE0);
|
||||||
shader_color.u_vec4("col", {1, 1, 1, 1});
|
tex.bind();
|
||||||
circle.draw_stroke();
|
shader.u_int("tex", 0);
|
||||||
|
shader.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + 120 * 1, 75 + 120 * i, 0.f}) * s);
|
||||||
|
shapes[i]->draw_fill();
|
||||||
|
tex.unbind();
|
||||||
|
|
||||||
|
shader_color.use();
|
||||||
|
shader.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + 120 * 2, 75 + 120 * i, 0.f}) * s);
|
||||||
|
shader_color.u_vec4("col", {1, 1, 1, 1});
|
||||||
|
shapes[i]->draw_stroke();
|
||||||
|
|
||||||
|
shader.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + 120 * 0, 75 + 120 * i, 0.f}) * s);
|
||||||
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||||
|
shapes[i]->draw_fill();
|
||||||
|
|
||||||
|
shader_uv.use();
|
||||||
|
shader_uv.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + 120 * 3, 75 + 120 * i, 0.f}) * s);
|
||||||
|
shader_uv.u_int("tex", 0);
|
||||||
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
||||||
|
shapes[i]->draw_fill();
|
||||||
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||||
|
shader_uv.u_mat4("mvp", proj * glm::translate(glm::vec3{75 + 120 * 4, 75 + 120 * i, 0.f}) * s);
|
||||||
|
shapes[i]->draw_fill();
|
||||||
|
tex.unbind();
|
||||||
|
}
|
||||||
|
// shader.use();
|
||||||
|
// shader.u_mat4("mvp", proj * glm::translate(glm::vec3{width/2, height/2, 0.f}) * glm::scale(glm::vec3(10)));
|
||||||
|
// shader.u_int("tex", 0);
|
||||||
|
// glActiveTexture(GL_TEXTURE0);
|
||||||
|
// tex.bind();
|
||||||
|
// circle.draw_fill();
|
||||||
|
// tex.unbind();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,9 +8,10 @@ class App
|
|||||||
{
|
{
|
||||||
Shader shader;
|
Shader shader;
|
||||||
Shader shader_color;
|
Shader shader_color;
|
||||||
|
Shader shader_uv;
|
||||||
Plane plane;
|
Plane plane;
|
||||||
Plane longPlane;
|
Plane longPlane;
|
||||||
Circle circle;
|
Circle circle, circle1, circle2;
|
||||||
Texture2D tex;
|
Texture2D tex;
|
||||||
public:
|
public:
|
||||||
static App I;
|
static App I;
|
||||||
|
|||||||
@@ -46,15 +46,14 @@ void Shape::draw_stroke() const
|
|||||||
void Plane::create_impl(float w, float h, int div, GLushort *idx, Shape::vertex_t *vertices)
|
void Plane::create_impl(float w, float h, int div, GLushort *idx, Shape::vertex_t *vertices)
|
||||||
{
|
{
|
||||||
count[0] = div * div * 6;
|
count[0] = div * div * 6;
|
||||||
count[1] = 4;
|
count[1] = 8;
|
||||||
ioff[0] = (GLvoid*)4;
|
ioff[0] = (GLvoid*)(8 * sizeof(GLushort));
|
||||||
ioff[1] = (GLvoid*)0;
|
ioff[1] = (GLvoid*)0;
|
||||||
|
|
||||||
const float dx = w / div;
|
const float dx = w / div;
|
||||||
const float dy = h / div;
|
const float dy = h / div;
|
||||||
const float ox = -w * 0.5f;
|
const float ox = -w * 0.5f;
|
||||||
const float oy = -h * 0.5f;
|
const float oy = -h * 0.5f;
|
||||||
int v_index = 0;
|
|
||||||
for (int y = 0; y <= div; y++)
|
for (int y = 0; y <= div; y++)
|
||||||
{
|
{
|
||||||
for (int x = 0; x <= div; x++)
|
for (int x = 0; x <= div; x++)
|
||||||
@@ -65,27 +64,30 @@ void Plane::create_impl(float w, float h, int div, GLushort *idx, Shape::vertex_
|
|||||||
v.pos.z = 0;
|
v.pos.z = 0;
|
||||||
v.pos.w = 1;
|
v.pos.w = 1;
|
||||||
v.uvs = glm::vec2(x, y) / (float)div;
|
v.uvs = glm::vec2(x, y) / (float)div;
|
||||||
vertices[v_index++] = v;
|
*vertices++ = v;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// generate indices
|
// generate indices
|
||||||
auto pidx = idx;
|
*idx++ = 0; // A
|
||||||
*pidx++ = 0;
|
*idx++ = (div+1)*(div); // B
|
||||||
*pidx++ = div;
|
*idx++ = (div+1)*(div); // B
|
||||||
*pidx++ = (div+1)*(div);
|
*idx++ = (div+1)*(div+1)-1; // C
|
||||||
*pidx++ = (div+1)*(div+1)-1;
|
*idx++ = (div+1)*(div+1)-1; // C
|
||||||
|
*idx++ = div; // D
|
||||||
|
*idx++ = div; // D
|
||||||
|
*idx++ = 0; // A
|
||||||
for (int y = 0; y < div; y++)
|
for (int y = 0; y < div; y++)
|
||||||
{
|
{
|
||||||
int i = y * (div+1);
|
int i = y * (div+1);
|
||||||
for (int x = 0; x < div; x++)
|
for (int x = 0; x < div; x++)
|
||||||
{
|
{
|
||||||
*pidx++ = i;
|
*idx++ = i;
|
||||||
*pidx++ = i + div + 1;
|
*idx++ = i + div + 1;
|
||||||
*pidx++ = i + div + 2;
|
*idx++ = i + div + 2;
|
||||||
*pidx++ = i;
|
*idx++ = i;
|
||||||
*pidx++ = i + div + 2;
|
*idx++ = i + div + 2;
|
||||||
*pidx++ = i + 1;
|
*idx++ = i + 1;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -104,11 +106,9 @@ void Circle::create_impl(float radius, int div, GLushort* idx, Shape::vertex_t*
|
|||||||
{
|
{
|
||||||
vertex_t v;
|
vertex_t v;
|
||||||
float theta = (float)i / div * M_PI * 2.f;
|
float theta = (float)i / div * M_PI * 2.f;
|
||||||
v.pos.x = sinf(theta) * radius;
|
glm::vec2 uv = { sinf(theta), cosf(theta) };
|
||||||
v.pos.y = cosf(theta) * radius;
|
v.pos = glm::vec4(uv * radius, 0, 1);
|
||||||
v.pos.z = 0;
|
v.uvs = uv * 0.5f + 0.5f;
|
||||||
v.pos.w = 1;
|
|
||||||
v.uvs = v.pos.xy() * 0.5f + 0.5f;
|
|
||||||
vertices[i+1] = v;
|
vertices[i+1] = v;
|
||||||
|
|
||||||
*pidx++ = 0;
|
*pidx++ = 0;
|
||||||
@@ -134,10 +134,11 @@ void Circle::create_impl(float radius_out, float radius_in, int div, GLushort* i
|
|||||||
for (int i = 0; i < div; i++)
|
for (int i = 0; i < div; i++)
|
||||||
{
|
{
|
||||||
float theta = (float)i / div * M_PI * 2.f;
|
float theta = (float)i / div * M_PI * 2.f;
|
||||||
vertices[i*2].pos = { sinf(theta) * radius_in, cosf(theta) * radius_in, 0, 1 };
|
glm::vec2 uv = { sinf(theta), cosf(theta) };
|
||||||
vertices[i*2].uvs = vertices[i*2].pos.xy() * 0.5f + 0.5f;
|
vertices[i*2].pos = glm::vec4(uv * radius_in, 0, 1);
|
||||||
vertices[i*2+1].pos = { sinf(theta) * radius_out, cosf(theta) * radius_out, 0, 1 };
|
vertices[i*2].uvs = uv * (radius_in/radius_out) * 0.5f + 0.5f;
|
||||||
vertices[i*2+1].uvs = vertices[i*2+1].pos.xy() * 0.5f + 0.5f;
|
vertices[i*2+1].pos = glm::vec4(uv * radius_out, 0, 1);
|
||||||
|
vertices[i*2+1].uvs = uv * 0.5f + 0.5f;
|
||||||
|
|
||||||
*pidx++ = i*2; // A
|
*pidx++ = i*2; // A
|
||||||
*pidx++ = i*2+1; // B
|
*pidx++ = i*2+1; // B
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ public:
|
|||||||
template<int div>
|
template<int div>
|
||||||
bool create(float w, float h)
|
bool create(float w, float h)
|
||||||
{
|
{
|
||||||
static GLushort idx[div * div * 6 + 4];
|
static GLushort idx[div * div * 6 + 8];
|
||||||
static vertex_t vertices[(div+1)*(div+1)];
|
static vertex_t vertices[(div+1)*(div+1)];
|
||||||
create_impl(w, h, div, idx, vertices);
|
create_impl(w, h, div, idx, vertices);
|
||||||
return create_buffers(idx, vertices, sizeof(idx), sizeof(vertices));
|
return create_buffers(idx, vertices, sizeof(idx), sizeof(vertices));
|
||||||
|
|||||||
Reference in New Issue
Block a user