2

我对 OpenGL 的着色器有疑问。

#ifdef GL_ES
precision lowp float;
#endif

varying vec4 v_fragmentColor;

void main()
{
    gl_FragColor = v_fragmentColor;
}

第二个

attribute vec4 a_position;
attribute vec4 a_color;
uniform mat4 u_MVPMatrix;


#ifdef GL_ES
varying lowp vec4 v_fragmentColor;
#else
varying vec4 v_fragmentColor;
#endif

void main()
{
    gl_Position = u_MVPMatrix * a_position;
    v_fragmentColor = a_color;
}

首先我做二维投影

- (void)makeProjection2D
{
    float scaleFactor = 1.0;
    CGSize size = self.bounds.size;

    glViewport(0, 0, size.width, size.height);

    kmGLMatrixMode(KM_GL_PROJECTION);
    kmGLLoadIdentity();

    kmMat4 orthoMatrix;
    kmMat4OrthographicProjection(&orthoMatrix, 0, size.width / scaleFactor, 0, size.height / scaleFactor, -size.width, size.width);
    kmGLMultMatrix( &orthoMatrix );

    kmGLMatrixMode(KM_GL_MODELVIEW);
    kmGLLoadIdentity();
}

下一步是创建着色器。

随后是 OpenGL 初始化。

GLint dim[2] = {1025, 769};
CGLSetParameter([[self openGLContext] CGLContextObj], kCGLCPSurfaceBackingSize, dim);
CGLEnable([[self openGLContext] CGLContextObj], kCGLCESurfaceBackingSize);

// Setup OpenGL states
glMatrixMode(GL_PROJECTION);
CGRect frame = self.bounds;

glGetError();

// Setup the view port in Pixels
glOrtho(0, frame.size.width, 0, frame.size.height, -1, 1);
glViewport(0, 0, frame.size.width, frame.size.height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnableClientState(GL_VERTEX_ARRAY);

glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

self.pointSize = pointSizeForDrawing;
GLint zeroOpacity = 0;
[[self openGLContext] setValues:&zeroOpacity forParameter:NSOpenGLCPSurfaceOpacity];

如果我尝试在屏幕上绘制四个大像素之类的东西

glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER, framebuffer);

glClearColor(1.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPointSize(100.0);
glColor3b(1.0, 1.0, 1.0);
GLfloat i[8] = {44.0, 44.0, 100.0, 100.0, 300.0, 300.0,  500.0, 500.0};

glVertexPointer(2, GL_FLOAT, 0, i);
glDrawArrays(GL_POINTS, 0, 4);

glBindFramebufferEXT( GL_READ_FRAMEBUFFER_EXT, framebuffer );
glBindFramebufferEXT( GL_DRAW_FRAMEBUFFER_EXT, 0 );
glViewport(0, 0, self.bounds.size.width, self.bounds.size.height);
glBlitFramebufferEXT( 0, 0, self.bounds.size.width, self.bounds.size.height, 0, 0, self.bounds.size.width, self.bounds.size.height, GL_COLOR_BUFFER_BIT, GL_NEAREST );
glSwapAPPLE();

只有一个是可见的,如果我删除制作 2d 投影和着色器,那么所有 4 个都是可见的。

使用着色器的代码适用于 iOS,但不适用于 OSX,知道为什么吗?为了初始化着色器,我使用cocos2d的 CCGLProgram如果您需要更多信息,请告诉我。

4

0 回答 0