用OpenGL和GLSL创建三角形

vpfxa7rd  于 2023-05-17  发布在  其他
关注(0)|答案(1)|浏览(164)

我想在屏幕上显示一个三角形。代码可以工作,只是没有三角形。我使用简单的通过着色器来实现这个目标。

顶点着色器:

#version 150

in      vec3 vPos;
uniform mat4 projection_matrix;

void main () {
 gl_Position = vec4 (vPos, 1.0);
}

片段着色器:

#version 150

out vec4 fColor;

void main () {
 fColor = vec4 (1.0, 0.0, 0.0, 1.0);
}

编码

/* Default rendering state */
  bool rendering = true;
  /* Create a mouse event */
  CGEventRef event = CGEventCreate(NULL);

  /* Set Vertex data (triangle)*/
  GLfloat vertextData[] = {
    -1.0,-1.0,1.0,
    0.0,1.0,1.0,
    1.0,-1.0,1.0
  };

  /* Initializing vertex array object */  
  GLuint GEVertexArrayObject = NULL;
  /* Create vertex array object(s) (APPLE Extention)*/
  glGenVertexArraysAPPLE(1, &GEVertexArrayObject);
  /* Bind vertex array object(s) and set set it -current- (APPLE Extention)*/
  glBindVertexArrayAPPLE(GEVertexArrayObject);
  /* Initializing vertex array buffer */
  GLuint GEVertexBufferObject = NULL;
  /* Create vertex-buffer objects */
  glGenBuffers(1, &GEVertexBufferObject);
  /* Specify type of buffer and bind (selects)*/
  glBindBuffer(GL_ARRAY_BUFFER, GEVertexBufferObject);
  /* Declare attribute (define data) and bind to vertex buffer object named as index # 0 */
  glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
  /* Enable attribute so that the shader can use it. Name it as index # 0 */
  glEnableVertexAttribArray(0);
  /* Transfer the actual vertex data into the buffer object */
  glBufferData(GL_ARRAY_BUFFER,sizeof(vertextData),&vertextData,GL_STATIC_DRAW);
  /* Clearing screen settings */
  glClearColor(1,1,1,0);
  /* Vertex Shader */
  const char *vertexShader = {
    "#version 150\n"
    "in vec3 vPos;"
    "uniform mat4 projection_matrix;"
    "void main(){"
    " gl_Position = vec4(vPos,1.0);"
    "}"
  };
  /* Fragment Shader */
  const char *fragmentShader = {
    "#version 150\n"
    "out vec4 fColor;"
    "void main(){"
    " fColor = vec4(1.0,0.0,0.0,1.0);" 
    "}"
  };
  /* Create shader object */
  GLuint vertexShaderObject = glCreateShader(GL_VERTEX_SHADER);
  GLuint fragmentShaderObject = glCreateShader(GL_FRAGMENT_SHADER);
  /* Associate source with shader object */
  glShaderSource(vertexShaderObject,1, &vertexShader, NULL);
  glShaderSource(fragmentShaderObject,1, &fragmentShader, NULL);
  /* Compile vertex shader */
  glCompileShader(vertexShaderObject);
  /* Validate if the compiling went well */
  GLint errorVertexShaderObject = NULL;
  glGetShaderiv(vertexShaderObject,GL_COMPILE_STATUS,&errorVertexShaderObject);
  if(errorVertexShaderObject==GL_FALSE){
    /* Compiling went wrong, get info from the log */
    /* Check the size of the log */
    GLint infologlengthVertexShaderObject = NULL;
    glGetShaderiv(vertexShaderObject,GL_INFO_LOG_LENGTH,&infologlengthVertexShaderObject);
    GLchar *infologVertexShaderObject = new GLchar[infologlengthVertexShaderObject+1];
    /* Retrieve info from the log */
    GLsizei infologsizeVertexShaderObject = NULL;
    glGetShaderInfoLog(vertexShaderObject,infologlengthVertexShaderObject,NULL,infologVertexShaderObject);
    /* Show info of log*/
    std::cout<<"X VERTEX SHADER:"<<std::endl;
    std::cout<<infologVertexShaderObject;
    delete [] infologVertexShaderObject;
  }else{
    std::cout<<"√ Vertex shader compiled\n";
  }
  /* Compile fragment shader */
  glCompileShader(fragmentShaderObject);
  /* Validate if the compiling went well */
  GLint errorFragmentShaderObject = NULL;
  glGetShaderiv(fragmentShaderObject,GL_COMPILE_STATUS,&errorFragmentShaderObject);
  if(errorFragmentShaderObject==GL_FALSE){
    /* Compiling went wrong, get info from the log */
    /* Check the size of the log */
    GLint infologlengthFragmentShaderObject = NULL;
    glGetShaderiv(fragmentShaderObject,GL_INFO_LOG_LENGTH,&infologlengthFragmentShaderObject);
    GLchar *infologFragmentShaderObject = new GLchar[infologlengthFragmentShaderObject+1];
    /* Retrieve info from the log */
    GLsizei infologsizeFragmentShaderObject = NULL;
    glGetShaderInfoLog(fragmentShaderObject,infologlengthFragmentShaderObject,NULL,infologFragmentShaderObject);
    /* Show info of log*/
    std::cout<<"X FRAGMENT SHADER:"<<std::endl;
    std::cout<<infologFragmentShaderObject;
    delete [] infologFragmentShaderObject;
  }else{
    std::cout<<"√ Fragment shader compiled\n";
  }
  /* Create shader program */
  GLuint shaderProgram = glCreateProgram();
  /* Poppulate shader program with shaders*/
  glAttachShader(shaderProgram, vertexShaderObject);
  glAttachShader(shaderProgram, fragmentShaderObject);
  /* Layout location = 0 */
  glBindAttribLocation(shaderProgram,0,"vPos");
  /* Create an executable shader program*/
  glLinkProgram(shaderProgram);
  GLint errorShaderProgram = NULL;
  glGetProgramiv(shaderProgram,GL_LINK_STATUS,&errorShaderProgram); 
  if(errorShaderProgram==GL_TRUE){std::cout<<"√ Linked successful\n";}
  /* Set shader program as current */
  glUseProgram(shaderProgram);

  /* Keeps rendering until rendering = false */
  while(rendering){
    /* Stop rendering if the escape key is pressed */
    if(CGEventSourceKeyState(kCGEventSourceStateCombinedSessionState,53)){
      /* Stop rendering */
      rendering = false;
      /* Exit with escape-key - notification */
      std::cout<<"- (info) The user has quit the program with the escape-key"<<std::endl;
      /* Finish current loop without executing upcoming methods */
      continue;
    };

    /* Get Mouse position */
    CGPoint positionMouse = CGEventGetLocation(event);
  
    /* Clear buffer */
    glClear(GL_COLOR_BUFFER_BIT);
    
    /* Draw to screen */
    glDrawArrays(GL_TRIANGLES,0,3);

    /* Flush opengl Commands*/
    glFlush();
    //glFinish();
6fe3ivhb

6fe3ivhb1#

我刚刚在Windows上检查了你的代码。

  • 有一个错字。这一点:

const char fragmentShader ...
应该是:
const char* fragmentShader ...

  • 您忘记交换缓冲区:glSwapAPPLE();aglSwapBuffers();

因此,如果您修复了这些问题,并且您的其他代码(主要是窗口和上下文创建代码)是正确的,它将绘制三角形。

---编辑1---

为了避免特定于操作系统的非OpenGL问题,您可能需要使用一些OpenGL实用程序库,这将摆脱窗口创建,上下文创建和处理操作系统事件。一些选项:

  • GLUT
  • FreeGLUT
  • GLFW
  • SFML语言
  • 甚至SDL

所有这些都易于使用,可以在我们的三个主要桌面平台上处理操作系统特定的东西。

相关问题