简体   繁体   English

如何使用IOS的openGL ES绘制一个简单的矩形?

[英]How to draw a simple rectangle with openGL ES for IOS?

I am extremely new to openGL ES and the tutorials that I found online don't really make any sense to me. 我对openGL ES极为openGL ES ,我在网上找到的教程对我来说真的没有任何意义。 I am trying to just draw a simple 2D rectangle on the screen in IOS Objective C but I can't figure out how to do it. 我试图在IOS Objective C在屏幕上绘制一个简单的2D矩形,但我不知道该怎么做。 I found this on some tutorial but it doesn't do anything: 我在一些教程中找到了它,但是它什么也没做:

- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
GLfloat square[] = {
    0.25, 0.25, 0.0,
    0.75, 0.25, 0.0,
    0.25, 0.75, 0.0,
    0.75, 0.75, 0.0
};
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glVertexPointer(3, GL_FLOAT, 0, square);
glEnableClientState(GL_VERTEX_ARRAY);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 4);
glFlush();


glOrthof(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);


glClear(GL_COLOR_BUFFER_BIT);}

anything that will effectively draw a square/rectangle with some explanation will be very much appreciated. 任何可以有效地绘制square/rectangle并带有一些解释的方法,将不胜感激。 Thanks! 谢谢!

The code you posted might work in OpenGL ES 1.0. 您发布的代码可能在OpenGL ES 1.0中有效。

However, GLKit, which you appear to be using, uses OpenGL ES 2.0. 但是,您似乎正在使用的GLKit使用OpenGL ES 2.0。 None of the function calls you're making have any effect in an OpenGL ES 2.0 context. 您正在执行的函数调用在OpenGL ES 2.0上下文中均无效。

In GLES 1.0, as your code demonstrates, you draw things in "immediate mode." 如代码所示,在GLES 1.0中,您以“立即模式”绘制事物。 Equivalent code in OpenGL ES 2.0 has to create an appropriate shader program, and then provide vertex data to the GPU as a VBO (and potentially also create a VAO for good performance OpenGL ES 2.0中的等效代码必须创建一个适当的着色器程序,然后将顶点数据作为VBO提供给GPU(并可能还会创建VAO以实现良好的性能)

I think explaining even something as simple as how to draw a triangle is beyond the scope of a StackOverflow answer, given the complexity of OpenGL. 考虑到OpenGL的复杂性,我认为即使是像绘制三角形这样简单的事情,也超出了StackOverflow答案的范围。 However, you can study the XCode Game Template (make a new Xcode project, chose the game template, and OpenGL ES as your technology), and googling for "OpenGL ES 2.0 tutorial" will probably provide a wealth of information. 但是,您可以研究XCode游戏模板(创建一个新的Xcode项目,选择游戏模板,并使用OpenGL ES作为技术),并且使用Google搜索“ OpenGL ES 2.0教程”可能会提供大量信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM