iphone - iOS OpenGL ES 2.0 绘制3D线并设置颜色

标签 iphone ios opengl-es opengl-es-2.0 glkit

我可以使用以下代码在 iPhone 上的 OpenGL ES 2.0 中成功绘制线条。我正在禁用纹理和混合,但我的 GLKBaseEffects useConstantColor 似乎没有为线条着色 - 它始终是黑色!我无法设置颜色!我该怎么做?

// Turn off texturing
self.effect.texture2d0.enabled = NO;
self.effect.texture2d1.enabled = NO;

// Turn off blending
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);

// Draw a line
self.effect.useConstantColor = GL_TRUE;

// Make the line a red color
self.effect.constantColor = GLKVector4Make(
                                           1.0f, // Red
                                           0.0f, // Green
                                           0.0f, // Blue
                                           1.0f);// Alpha

// Prepare the effect for rendering
[self.effect prepareToDraw];

GLfloat line[] = { -1.0, -1.0, -6.0, 1.0, 1.0, 5.0, 1.0, -1.0, 2.0 };

// Create an handle for a buffer object array
GLuint bufferObjectNameArray;

// Have OpenGL generate a buffer name and store it in the buffer object array
glGenBuffers(1, &bufferObjectNameArray);

// Bind the buffer object array to the GL_ARRAY_BUFFER target buffer
glBindBuffer(GL_ARRAY_BUFFER, bufferObjectNameArray);

// Send the line data over to the target buffer in GPU RAM
glBufferData(
             GL_ARRAY_BUFFER,   // the target buffer
             sizeof(line),      // the number of bytes to put into the buffer
             line,              // a pointer to the data being copied
             GL_STATIC_DRAW);   // the usage pattern of the data

// Enable vertex data to be fed down the graphics pipeline to be drawn
glEnableVertexAttribArray(GLKVertexAttribPosition);

// Specify how the GPU looks up the data
glVertexAttribPointer(
                      GLKVertexAttribPosition, // the currently bound buffer holds the data
                      3,                       // number of coordinates per vertex
                      GL_FLOAT,                // the data type of each component
                      GL_FALSE,                // can the data be scaled
// * INCORRECT *      3,                       // how many bytes per vertex (3 floats per vertex)
                      0,                       // stride (0 bytes between coordinates.) ~Olie
// * INCORRECT *      NULL);                   // offset to the first coordinate, in this case 0 
                      line);                   // pointer to the buffer to draw. ~Olie

// Set the line width
glLineWidth(5.0);

// Render the line
glDrawArrays(GL_LINE_LOOP, 0, 3);

// Turn on blending
glDisable(GL_BLEND);

// Turn on texturing
self.effect.texture2d0.enabled = YES;
self.effect.texture2d1.enabled = YES;

最佳答案

尝试添加这个:

// Turn off lighting
self.effect.light0.enabled = GL_FALSE;

关于iphone - iOS OpenGL ES 2.0 绘制3D线并设置颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16473775/

相关文章:

ios - 嵌入Youtube 360​​视频在iOS Safari中不起作用

objective-c - 词法或预处理器问题

ios - GLKTextureLoader 未在 "The new iPad"上加载 jpg

iphone - 如何在 OpenGL ES 1.1 中优化大型模型的渲染?

iphone - 如何从子类的UITableViewCell中访问UITableViewController变量?

iphone - NSString 的 stringByAppendingPathComponent : removes a '/' in http://

iphone - 当用户在主屏幕应用程序中启动 "new window"时

jquery - 有没有办法使用桌面浏览器测试我的网站在移动设备上的行为和外观?

ios - 在 iOS IKEv2 VPN 配置中使用自定义端口?

opengl - EGL和OpenGL有什么关系?