Reputation: 73
Hi I have been trying to make my first opengl "app" in cocoa using NSOpenGLView. I want to clear the background with blue color and draw red dot but view is white. And it doesn't draw red dot. Maybe I should use this with core video to refresh it in loop. This is the code from "OpenGL Superbible " book so i think that it fault of cocoa.
#import <Cocoa/Cocoa.h>
@interface View : NSOpenGLView
@end
//---------------------------------
#import "View.h"
#include <OpenGL/gl3.h>
@implementation View
GLuint rendering_program;
GLuint VAO;
-(void)awakeFromNib{
NSOpenGLPixelFormatAttribute pixelFormatAttributes[] =
{
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
NSOpenGLPFAColorSize , 24 ,
NSOpenGLPFAAlphaSize , 8 ,
NSOpenGLPFADoubleBuffer ,
NSOpenGLPFAAccelerated ,
NSOpenGLPFANoRecovery ,
0
};
NSOpenGLPixelFormat *pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:pixelFormatAttributes] ;
NSOpenGLContext* glc = [[NSOpenGLContext alloc]initWithFormat:pixelFormat shareContext:nil];
[self setOpenGLContext:glc];
}
-(void)prepareOpenGL{
GLuint vs;
GLuint fs;
const GLchar*vss[] = {
"#version 330 core \n"
"void main (void)\n"
"{\n"
"gl_Position = vec4(0.0,0.0,0.5,1.0);\n"
"}\n"
};
vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, vss, 0);
glCompileShader(vs);
int s;
glGetShaderiv(vs, GL_COMPILE_STATUS, &s);
if(!s)printf("ok");
const GLchar*fss[] = {
"#version 330 core \n"
"out vec4 color;"
"void main (void)\n"
"{\n"
"color = vec4(1.0,0.0,0.0,1.0);\n"
"}\n"
};
fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1,fss, 0);
glCompileShader(fs);
int k;
glGetShaderiv(fs, GL_COMPILE_STATUS, &k);
if(!k)printf("okkk");
rendering_program = glCreateProgram();
glAttachShader(rendering_program,vs );
glAttachShader(rendering_program,fs );
glLinkProgram(rendering_program);
int n;
glGetProgramiv(rendering_program, GL_LINK_STATUS, &n);
glDeleteShader(vs);
glDeleteShader(fs);
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
}
-(void)drawRect:(NSRect)dirtyRect{
glPointSize(40);
glClearColor(0, 0, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(rendering_program);
glDrawArrays(GL_POINTS, 0, 1);
glFlush();
}
@end
Upvotes: 3
Views: 197
Reputation: 210878
Since you are using double buffering (NSOpenGLPFADoubleBuffer
), you have to swap the buffers after rendering:
[[self openGLContext] flushBuffer]
Without double buffering (single buffer), glFlush()
would be sufficient.
See also glFlush()
vs [[self openGLContext] flushBuffer]
.
Upvotes: 1