C ++使用OpenGL绘制数组

时间:2014-08-21 16:32:17

标签: 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-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;

以前(几年前)我曾经使用过glBindBuffers,glGenBuffers等等。我不了解这些东西,或者我是否需要它们,因此这个问题。

2 个答案:

答案 0 :(得分:0)

glVertexAttribPointer(index​, size​, type​, normalized​, stride​, pointer​);

此函数采用当前绑定的GL_VERTEX_BUFFER并告诉opengl在指定的index

中查找顶点属性数据的位置

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

type表示缓冲区中二进制数据的类型,size表示属于单个顶点的数据元素数量,normalized告诉opengl是否需要映射整数值被映射到0.0-1.0

答案 1 :(得分:0)

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

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

rotating rectangle

除了一些glBind...(0)电话,这个例子很少 - 简单的事情是" new"不幸的是,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;
}

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