GLPaint in OpenGL ES 2.0

本文探讨了使用OpenGLES2.0在iOS设备上绘制点时遇到的问题,并提供了详细的代码示例及解决方案。文章指出glPointSize()在OpenGLES2.0中不可用,并建议通过修改顶点着色器来调整点大小。
Do anyone tried GLPaint sample application with OpenGl ES 2.0 ? I had a try an got errors with glMatrixMode(), glPointSize(), glOrthof(), glTexEnvf() methods .

 

 

 

All of those errors are because the code uses functions and constants that were removed in OpenGL ES 2.0. If you want to make that app use OpenGL ES 2.0, you will have to replace those calls with code that uses OpenGL ES 2.0 functions only.

There are significant differences between OpenGL ES 1.1 and OpenGL ES 2.0. Porting an app from 1.1 to 2.0 is not trivial. You will need to learn quite a bit about both OpenGL ES 1.1 and OpenGL ES 2.0 to port the app.

For the specific functions you mentioned:

glMatrixMode and glOrthof have no replacements in OpenGL ES 2.0. You are expected to provide your own vector/matrix math code. If you are targeting iOS 5.0, you will want to look at theGLKMath part of the GLKit framework.

glPointSize is replaced by the gl_PointSize variable in the vertex shader.

glTexEnvf is replaced by vertex and fragment shaders.

 

 

 

 

 

 

 

ask:

New with OpenGL, I am successfully displaying lines using GLKit on iOS 5 with GL_LINE_LOOP, but displaying points with GL_POINTS is giving me trouble. Specifically, I am unable to display anything but a single pixel. I've scoured the web with no success, so I'm hoping someone can point out what I'm missing.

Here is my test code...

 

//positions and colors, vertices and indices...
typedef struct {
    float Position[2];
    float Color[4];
} Vertex;

const Vertex Vertices[] = {
    {{50, 50}, {0, 0, 1, 1}},
    {{200, 50}, {0, 0, 1, 1}},
    {{200, 200}, {0, 0, 1, 1}},
    {{50, 200}, {0, 0, 1, 1}}
};

const GLubyte Indices[] = {
    0, 1, 2, 3
};


@interface AppViewController() {
    GLuint _vertexBuffer;
    GLuint _indexBuffer;
}

...
//some unrelated properties, the @implementation and other methods
...

- (void)viewDidLoad {
    [super viewDidLoad];

    self.context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    if (!self.context) NSLog(@"Failed to create ES context");

    GLKView *view = (GLKView *)self.view;
    view.context = self.context;
    [EAGLContext setCurrentContext:self.context];

    self.effect = [[GLKBaseEffect alloc] init];

    GLKMatrix4 projectionMatrix = GLKMatrix4MakeOrtho(0, self.view.bounds.size.width, 0, self.view.bounds.size.height, -1024, 1024);
    self.effect.transform.projectionMatrix = projectionMatrix;

    glGenBuffers(1, &_vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

    glGenBuffers(1, &_indexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
}

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {    
    glClearColor(1, 1, 1, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    [self.effect prepareToDraw];

    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);

    glEnableVertexAttribArray(GLKVertexAttribPosition);        
    glVertexAttribPointer(GLKVertexAttribPosition, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Position));
    glEnableVertexAttribArray(GLKVertexAttribColor);
    glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, Color));

    //this worked for drawing my lines...
    //glLineWidth(10);
    //glDrawElements(GL_LINE_LOOP, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);

    //this does NOT work for drawing 10-pixel points. single pixel only.
    glEnable(GL_POINT_SMOOTH);
    glPointSize(10);
    glDrawElements(GL_POINTS, sizeof(Indices)/sizeof(Indices[0]), GL_UNSIGNED_BYTE, 0);
}

I don't think glPointSize() is part of OpenGL ES 2.0.

Try setting…

gl_PointSize = 10;

…in your vertex shader.

 

 

 

 

 

 

 

 

 

I have a simple OpenGL ES 2.0 code to draw points. Here is the VAO setup:

    staticconstGLfloat squareVertices[]={
       
-0.5f,-0.5f,-1.0f,
       
0.5f,-0.5f,-1.0f,
       
0.5f,  0.5f,-1.0f,
       
-0.5f,0.5f,  -1.0f
   
};

   
staticconstGLfloat squareColors[]={
       
1.0f,0.0f,0.0f,1.0f,
       
0.0f,1.0f,0.0f,1.0f,
       
0.0f,0.0f,1.0f,1.0f,
       
1.0f,0.0f,1.0f,1.0f
   
};
    glGenVertexArraysOES
(1,&_vertexArray);
    glBindVertexArrayOES
(_vertexArray);

    glGenBuffers
(1,&_vertexBuffer);
    glBindBuffer
(GL_ARRAY_BUFFER, _vertexBuffer);
    glBufferData
(GL_ARRAY_BUFFER,sizeof(squareVertices), squareVertices, GL_STATIC_DRAW);

    glEnableVertexAttribArray
(ATTRIB_VERTEX);
    glVertexAttribPointer
(ATTRIB_VERTEX,3, GL_FLOAT, GL_FALSE,0,0);

    glGenBuffers
(1,&_colorP_VBO);
    glBindBuffer
(GL_ARRAY_BUFFER, _colorP_VBO);
    glBufferData
(GL_ARRAY_BUFFER,sizeof(squareColors), squareColors, GL_STATIC_DRAW);

    glEnableVertexAttribArray
(ATTRIB_COLOR);
    glVertexAttribPointer
(ATTRIB_COLOR,4, GL_FLOAT, GL_FALSE,0,0);
    glBindVertexArrayOES
(0);

and here is the render code:

    [glView setDisplayFramebuffer:glView.viewFrameBuffer];

    glUseProgram
(simpleShaderProgram);
    glBindVertexArrayOES
(_vertexArray);

//  glLineWidth(5.0f);
//      glDrawArrays(GL_LINE_LOOP, 0, 4);
//      glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
//  glEnable(GL_POINT_SMOOTH);

    glPointSize
(25.0f);
    glDrawArrays
(GL_POINTS,0,4);

    glBindVertexArrayOES
(0);

    glUseProgram
(0);

    glBindFramebuffer
(GL_FRAMEBUFFER,0);//unbind the FBO  
   
[glView presentFramebuffer:glView.viewFrameBuffer];

When I use GL_LINE_LOOP or GL_TRIANGLE_FAN it draw colorful square on screen. However when i use GL_POINTS the renderer does not draw vertices and screen is blank. I appreciate if somebody let me know what i am missing. Thanks

The followings are my vertex and fragment shaders:

attribute vec4 position;
attribute vec4 v_color
;

varying vec4 colorVarying
;


void main()
{
    colorVarying
= v_color;
    gl_Position
=  position;

}

------------------------------------------------
precision mediump
float;
varying vec4 colorVarying
;


void main()
{
    gl_FragColor
= colorVarying;
}




I've added my vertex shader code. My problem is that I don't see any points drawn on the screen. not even 1 pixel.








OpenGL ES on iPhone doesn't draw anything

I'm an absolute beginner in OpenGL ES programming in iOS.

This is my first attempt to draw some simple 2D primitives with OpenGL ES onto a view.

Here is the class declaration:

@interfaceOGLGameCanvas:UIView<GameCanvas>{
   
EAGLContext* context;
   
Game* game;
   
GLuint framebuffer, renderbuffer, depthbuffer;
}

Here is my initialization code:

-(void)initialize {

   
// Get the layer and set properties

   
CAEAGLLayer* layer =(CAEAGLLayer*)self.layer;

    layer
.opaque = NO;
    layer
.drawableProperties =[NSDictionary dictionaryWithObjectsAndKeys:
                           
[NSNumber numberWithBool:NO], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat,nil];


   
// Set the context

    context
=[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];

   
if(!context ||![EAGLContext setCurrentContext:context])
       
DLog(@"Cannot create EAGLContext!");


   
// Create the color buffer and the render buffer

    glGenFramebuffers
(1,&framebuffer);
    glGenRenderbuffers
(1,&renderbuffer);
    glBindFramebuffer
(GL_FRAMEBUFFER, framebuffer);
    glBindRenderbuffer
(GL_RENDERBUFFER, renderbuffer);

   
[context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)layer];
    glFramebufferRenderbuffer
(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer);

   
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
   
if(status != GL_FRAMEBUFFER_COMPLETE)
       
NSLog(@"Failed to make complete frame buffer: %x", status);


   
// Get width and height of the render buffer

   
GLint width, height;
    glGetRenderbufferParameteriv
(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH,&width);
    glGetRenderbufferParameteriv
(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT,&height);

   
// Create and start animation loop

   
CADisplayLink* displayLink =[CADisplayLink displayLinkWithTarget:self selector:@selector(drawFrame:)];
   
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

}

and my drawing code:

-(void)drawFrame:(CADisplayLink*)sender {

    glLoadIdentity
();
    glClearColor
(0.0,0.0,0.0,1.0);
    glClear
(GL_COLOR_BUFFER_BIT);

    glEnableClientState
(GL_VERTEX_ARRAY);
   
GLfloat vertices[]={-20.0f,2.5f,0.0f,0.0f,1.5f,7.5f};
    glVertexPointer
(2, GL_FLOAT,0, vertices);
    glColor4f
(1.0,0.0,0.0,1.0);
    glPointSize
(5.0);
    glDrawArrays
(GL_POINTS,0,3);
    glDisableClientState
(GL_VERTEX_ARRAY);

   
[context presentRenderbuffer:GL_RENDERBUFFER];

}

The canvas gets cleared (in fact, it becomes black, or red, or whatever I set into glClearColor), but no points are drawn.

I'm pretty sure I'm forgetting something basic and essential.

Thanks for your help.

 

 

The problem is here:

GLfloat vertices[]={-20.0f,2.5f,0.0f,0.0f,1.5f,7.5f};

The normalized device coordinates lay in range [-1..1] so you're drawing them outside the visible area.

And here:

You have to do this before drawing.

glMatrixMode(GL_PROJECTION);
glLoadIdentity
();
glMatrixMode
(GL_MODELVIEW);
glLoadIdentity
();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值