【问题标题】:Merge color vertices at center rather than at one vertex?在中心而不是在一个顶点合并颜色顶点?
【发布时间】:2019-05-09 20:07:58
【问题描述】:

我正在尝试制作一个色板工具,在其中你给它 n 种颜色,它会形成一个 n 边形,这些颜色在中心合并。

到目前为止,它只是制作了一个 n-gon(没有指定颜色,它现在随机生成它们)。

但是颜色不会在中心合并,而是在单个顶点处合并。

有什么解决办法吗?

#include <GLFW/glfw3.h>
#include <iostream>
#include <cmath>
float randfloat(){
  float r = ((float)(rand() % 10))/10;
  return r;
}
int main() {
  int side_count;
  std::cout<<"Type the no. of sides: "<<std::endl;
  std::cin>>side_count;
  srand(time(NULL));
  std::cout<<randfloat()<<std::endl;
  std::cout<<randfloat()<<std::endl;
  float rs[side_count];
  float gs[side_count];
  float bs[side_count];
  for (int i=0;i<side_count;i++)
  {
    rs[i] = randfloat();
    gs[i] = randfloat();
    bs[i] = randfloat();
  }
  GLFWwindow* window;
  if (!glfwInit())
    return 1;
  window = glfwCreateWindow(800, 800, "Window", NULL, NULL);
  if (!window) {
    glfwTerminate();
    return 1;
  }
  glfwMakeContextCurrent(window);
  if(glewInit()!=GLEW_OK)
    std::cout<<"Error"<<std::endl;

  while(!glfwWindowShouldClose(window)) {
    glClear(GL_COLOR_BUFFER_BIT);
    glClearColor(0.11f,0.15f,0.17f,1.0f);
    glBegin(GL_POLYGON);
      //glColor3f(1.0f,0.0f,0.0f);glVertex3f(-0.5f,0.0f,0.0f);
      for(int i=0; i<side_count;i++)
      {
        float r = rs[i];
        float g = gs[i];
        float b = bs[i];
        float x = 0.5f * sin(2.0*M_PI*i/side_count);
        float y = 0.5f * cos(2.0*M_PI*i/side_count);
        glColor3f(r,g,b);glVertex2f(x,y);
      }
    glEnd();
    glfwSwapBuffers(window);
    glfwPollEvents();
  }
  glfwTerminate();
  return 0;
}

【问题讨论】:

  • Rabbid 的代码在技术上符合您的要求,但我怀疑它作为颜色选择器的用处。问题在于,对于三种颜色(三角形),您想使用重心坐标(不是该代码的作用)混合颜色。对于超过三种颜色,重心坐标不明确,使得这样的二维选择器不明确。

标签: c++ opengl glfw opengl-compat


【解决方案1】:

您所要做的就是在圆形的中心向GL_POLYGON 基元添加一个新点:

glBegin(GL_TRIANGLE_FAN);

glColor3f(0.5f, 0.5f, 0.5f);
glVertex2f(0, 0);

for(int i=0; i <= side_count; i++)
{
    float r = rs[i % side_count];
    float g = gs[i % side_count];
    float b = bs[i % side_count];
    float x = 0.5f * sin(2.0*M_PI*i/side_count);
    float y = 0.5f * cos(2.0*M_PI*i/side_count);
    glColor3f(r, g, b);
    glVertex2f(x, y);
}

glEnd();

注意,您必须定义中心点的颜色。在代码 sn-p 中,我选择了 (0.5, 0.5, 0.5)。
除了GL_POLYGON,也可以使用GL_TRIANGLE_FAN

【讨论】:

  • 非常感谢您帮我找到GL_TRIANGLE_FAN,但是至于中心颜色是否可以混合所有颜色,就像在 rgb 三角形中,中心是白色的
  • @ChrisMathewGeorge glColor3f(1, 1, 1)可以选择白色,但不能自动混合所有颜色
  • 我做了一些事情,比如把它改成glColor4f(0.0f, 0.0f, 0.0f, 0.0f)。这样会好吗。
  • @ChrisMathewGeorge 然后形状的中心是黑色的。不是你可以计算 CPU 上的平均颜色并使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-16
  • 1970-01-01
  • 2015-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-03
相关资源
最近更新 更多