Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/opengl/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;用OpenGL绘制数组_C++_Opengl_Opengl Es_Opengl Es 2.0 - Fatal编程技术网

C++ C++;用OpenGL绘制数组

C++ C++;用OpenGL绘制数组,c++,opengl,opengl-es,opengl-es-2.0,C++,Opengl,Opengl Es,Opengl Es 2.0,我不熟悉“新OpenGL”的做事方法 我想我误解了,或者不理解使用OpenGL绘制数据的一些步骤 假设我可以打开OpenGL上下文,请您(阅读此问题的SO用户)澄清在OpenGL中渲染描述直线的点阵列所需的所有步骤 目前我一直在这样做: // In main: float* my_points = new float[100]; // Fill my_points with some data to be drawn as a continuous line on the screen in x

我不熟悉“新OpenGL”的做事方法

我想我误解了,或者不理解使用OpenGL绘制数据的一些步骤

假设我可以打开OpenGL上下文,请您(阅读此问题的SO用户)澄清在OpenGL中渲染描述直线的点阵列所需的所有步骤

目前我一直在这样做:

// In main:
float* my_points = new float[100];
// Fill my_points with some data to be drawn as a continuous line on the screen in x-y space.



// Inside esMainLoop, inside drawing function:
GLfloat* points = new GLfloat[3 * 100]; // x,, y, z points
// Copy data to points

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, points); // No idea what this is or how it works
glEnableVertexAttribArray(0);
glDrawArrays(GL_LINE_STRIP, 0, 100);



// In function called when CTRL+C is pressed:
delete my_points;
以前(几年前)我使用过像glbindbuffer、glGenBuffers之类的东西。我也不了解这些东西,或者我是否需要它们,因此我提出了这个问题

glVertexAttribPointer(index​, size​, type​, normalized​, stride​, pointer​);
此函数获取当前绑定的
GL\u VERTEX\u BUFFER
,并告诉opengl在指定的
索引处查找顶点属性数据的位置

特别是
是从缓冲区开始的偏移量,如果绑定了默认缓冲区0,则它引用程序内存。在这种情况下,它指向
数组


类型
表示缓冲区中二进制数据的类型,
大小
表示属于单个顶点的数据元素的数量,
规范化
告诉opengl是否需要将整数值映射到0.0-1.0

编辑更新程序以显示随时间变化的数据

下面的程序从OpenGL 3.2+中的数组生成一个带有旋转白色矩形的窗口:

除了一些
glBind…(0)
调用之外,这个例子非常简单——在OpenGL中用“新”的方式绘制简单的东西,不幸的是,这个过程非常复杂

#include "gl2stuff.h"

int main()
{
  if (!glfwInit())                                                                                                                        
    return -1;                                                                                                                            
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,2);
  glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE);
  glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
  GLFWwindow* window(glfwCreateWindow(200,150,"", NULL,NULL));
  glfwMakeContextCurrent(window);
  glewExperimental = GL_TRUE;
  glewInit();

  // points for a 2D rectangle
  GLfloat points[]={-0.5,-0.5,0.5,-0.5,0.5,0.5,-0.5,0.5,-0.5,-0.5};
  GLfloat nPoints=5;

  /* vertex shader: read an array of 2D-points
     in the input attribute "vertex" and just pass
     them on as your position in the [-1,1]x[-1,1]x[-1,1]
     viewing volume
  */
  const char* vShader =
    "#version 150 core\n"
    "in vec2 vertex;"
    "void main() {gl_Position = vec4(vertex,0,1);}";
  /* fragment shader:
     draw everything in white
  */
  const char* fShader =
    "#version 150 core\n"
    "out vec4 color;"
    "void main() {color=vec4(1,1,1,1);}";

  /* create an OpenGL program, compile the shaders, 
     attach them to the program and link 
  */
  GLuint program(glCreateProgram());
  GLuint vertexShader(glCreateShader(GL_VERTEX_SHADER));
  GLuint fragmentShader(glCreateShader(GL_FRAGMENT_SHADER));
  glShaderSource(vertexShader, 1, &vShader, NULL);
  glCompileShader(vertexShader);
  glAttachShader(program, vertexShader);
  glShaderSource(fragmentShader, 1, &fShader, NULL);
  glCompileShader(fragmentShader);
  glAttachShader(program, fragmentShader);
  glLinkProgram(program);

  /* create a vertex buffer object (VBO)
     for your data and transfer the data
     from host memory to GPU memory
  */
  GLuint vBuffer;
  glGenBuffers(1,&vBuffer);
  glBindBuffer(GL_ARRAY_BUFFER,vBuffer);
  glBufferData(GL_ARRAY_BUFFER,sizeof(points),points,GL_STATIC_DRAW);
  glBindBuffer(GL_ARRAY_BUFFER,0);

  // generate vertex array object (VAO)
  GLuint vertexArray;
  glGenVertexArrays(1,&vertexArray);

  /* enable attribute array for "vertex" in your
     shader and bind vBuffer to it; a VAO must
     be bound while doing this (this stores the
     the information about the vertex attributes;
     you can conveniently switch between multiple
     VAOs, but you need at least one)
  */
  glBindVertexArray(vertexArray);
  GLuint vertexLocation(GLuint(glGetAttribLocation(program,"vertex")));
  glEnableVertexAttribArray(vertexLocation);
  glBindBuffer(GL_ARRAY_BUFFER,vBuffer);
  glVertexAttribPointer(vertexLocation, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid*)0 );
  glBindBuffer(GL_ARRAY_BUFFER,0);                                                           
  glBindVertexArray(0);

  /* finally we are ready to draw; 
     before drawing, make sure you call glUseProgram()
     and bind the correct VAO. If there is only one
     program and/or VAO it's ok if you just do these once 
     before you enter your drawing loop
  */
  glUseProgram(program);
  glBindVertexArray(vertexArray);

  while(true)
    {
      glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
      glDrawArrays(GL_LINE_STRIP, 0, nPoints);
      glfwSwapBuffers(window);

      // modify data and update buffer
      double t(glfwGetTime());
      points[0]=cos(t)*0.5;points[1]=sin(t)*0.5;
      points[2]=cos(t+1.57)*0.5;points[3]=sin(t+1.57)*0.5;
      points[4]=cos(t+3.14)*0.5;points[5]=sin(t+3.14)*0.5;
      points[6]=cos(t+4.71)*0.5;points[7]=sin(t+4.71)*0.5;
      glBindBuffer(GL_ARRAY_BUFFER,vBuffer);
      glBufferData(GL_ARRAY_BUFFER,sizeof(points),points,GL_DYNAMIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER,0);

    } 
  glBindVertexArray(0); 
  return 0;
}

请注意,此程序仅用于说明。通常,将时间均匀传递给着色器,并让着色器处理旋转

有关如何编程保留模式OpenGL的精彩教程,请参见。这本书非常值得一读。我还建议大家学习现代OpenGL。谢谢大家,我也会看看这些。如果有人想获得额外的声誉/帮助我,请鼓励大家发布答案。谢谢,今天我将再次阅读这本书,因为它有助于引导我的思维。我真正感兴趣的是绘制一个可以改变的数组的内容。例如,你画了一个盒子,而我对画一个移动的盒子或旋转的盒子或类似的东西感兴趣。(我实际上在画一个图形。)关键的部分是我不知道如何告诉OpenGL点的数组已经改变,然后如何让OpenGL绘制新的数组,而不是它已经在图形内存中的数组。如果你想修改它,那么我可能可以接受这是一个。。。。。。回答,但无论如何我会给你一个+1。同样,对于阅读本文的人来说,用GL_LINE_LOOP绘制一个框可能更好,但我认为Roberto使用了我已经提供的内容,并且没有意识到我绘制的是一条不会连接回自身的线。显示一个变化的数组只需要少量添加(请参阅编辑的程序):您必须更新阵列,然后再次调用
glBufferData
。如果只更改了数组的一部分,请查看
glBufferSubData
glMapBufferRange
,以避免一直上载整个数组;另外,我将代码改为使用第_行,您是否失去兴趣?这个答案正是你所要求的,我不会介意你一再向我保证的一些声誉。我还没有时间实施这些更改或阅读你更新的答案,我将在将来的某个时候对其进行审查