1
votes

I'm trying to get a triangle on screen. The code works except there is no triangle. I'm using simple pass-through shaders to achieve this goal.

Vertex shader:

#version 150

in      vec3 vPos;
uniform mat4 projection_matrix;

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

Fragment shader:

#version 150

out vec4 fColor;

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

Code:

/* 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>

1
Context kCGLPFAOpenGLProfile, static_cast<CGLPixelFormatAttribute>(kCGLOGLPVersion_3_2_Core), kCGLPFAAccelerated, kCGLPFANoRecovery, kCGLPFAColorSize, static_cast<CGLPixelFormatAttribute>(24), kCGLPFADepthSize, static_cast<CGLPixelFormatAttribute>(16), /* kCGLPFADoubleBuffer, Flickering */ kCGLPFASupersample, static_cast<CGLPixelFormatAttribute>(0)Bob Dolman

1 Answers

2
votes

I've just checked your code on Windows.

  • There was one typo. This:

    const char fragmentShader ...

    supposed to be:

    const char* fragmentShader ...

  • And you've forget to swap buffers: glSwapAPPLE(); or aglSwapBuffers();

So if you'll fix these issues and other your code (mostly window and context creation code) is right, it will draw your triangle.

---Edit1---

To avoid problems with OS-specific, non-OpenGL stuff, you probably will want to use some OpenGL utility library, which will get rid of window creation, context creation and processing OS events. Some options:

  • GLUT
  • FreeGLUT
  • GLFW
  • SFML
  • or even SDL

All of them are easy to use and can handle OS-specific stuff on our three major desktop platforms.