1

我正在尝试在屏幕上显示一个三角形。该代码有效,但没有三角形。我正在使用简单的直通着色器来实现这个目标。

顶点着色器:

#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:"</ 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:"</ 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"< 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();</code>

4

1 回答 1

2

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

  • 有一个错字。这个:

    const char fragmentShader...

    应该是:

    const char* fragmentShader...

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

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

---编辑1---

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

  • 过剩
  • 自由GLUT
  • GLFW
  • SFML
  • 甚至 SDL

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

于 2013-08-18T21:25:14.357 回答