Ios 使用glDrawArray绘制
在myIos 使用glDrawArray绘制,ios,opengl-es,cocos2d-iphone,Ios,Opengl Es,Cocos2d Iphone,在myCCLayer中的mydraw函数中。我正在画一些线,CGPoints存储在std::vector中。我已经用ccDrawLine成功地画出了线。但是,当使用glDrawArray时,不会显示任何内容。我已经包括了结果的截图。正如您在使用ccDrawnLine时所看到的,它正确地绘制了线。有什么想法吗 [更新]我使用的是cocos2dv2.0,默认情况下它使用的是OpenGLES 2.0 //openGLES #include <OpenGLES/EAGL.h> #includ
CCLayer
中的mydraw
函数中。我正在画一些线,CGPoints
存储在std::vector
中。我已经用ccDrawLine成功地画出了线。但是,当使用glDrawArray
时,不会显示任何内容。我已经包括了结果的截图。正如您在使用ccDrawnLine时所看到的,它正确地绘制了线。有什么想法吗
[更新]我使用的是cocos2dv2.0,默认情况下它使用的是OpenGLES 2.0
//openGLES
#include <OpenGLES/EAGL.h>
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>
- (void) draw {
glLineWidth( 3.0f );
//begin and clear
[renderTexture beginWithClear:waveform4F.r g:waveform4F.g b:waveform4F.b a:waveform4F.a];
vector<CGPoint> vertices = bufferQueue.front();
if (WaveformStyleLined) {
for (int i = 1; i < vertices.size(); i++)
ccDrawLine(vertices[i - 1], vertices[i]);
} else {
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
kmGLPushMatrix();
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, sizeof(CGPoint), &vertices[0]);
glDrawArrays(GL_LINES, sizeof(CGPoint), vertices.size());
kmGLPopMatrix();
}
bufferQueue.pop();
[renderTexture end];
}
我最终使用了
ccDrawPoly
,其中内部使用了glDrawArray
,并实现了与ccDrawLine完全相同的输出,但速度要快得多,因为我没有逐点检索。然而,我仍然好奇如何使用纯OpenGL代码来实现它。任何帮助或指导都将不胜感激
if (WaveformStyleLined) {
for (int i = 1; i < vertices.size(); i++)
ccDrawLine(vertices[i - 1], vertices[i]);
} else {
ccDrawPoly(&vertices[0], vertices.size(), false);
}
if(波形模式){
对于(int i=1;i
我最终使用了ccDrawPoly
,其中内部使用了glDrawArray
,实现了与ccDrawLine完全相同的输出,但速度要快得多,因为我没有逐点检索。然而,我仍然好奇如何使用纯OpenGL代码来实现它。任何帮助或指导都将不胜感激
if (WaveformStyleLined) {
for (int i = 1; i < vertices.size(); i++)
ccDrawLine(vertices[i - 1], vertices[i]);
} else {
ccDrawPoly(&vertices[0], vertices.size(), false);
}
if(波形模式){
对于(int i=1;i
通过添加像DrummerB这样的着色器,线条开始出现。我从Cocos2d复制了相同的着色器代码,它工作起来很有魅力
//openGLES
#include <OpenGLES/EAGL.h>
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>
static BOOL initialized = NO;
static CCGLProgram *shader_ = nil;
static int colorLocation_ = -1;
static ccColor4F color_ = {1,1,1,1};
static int pointSizeLocation_ = -1;
static void lazy_init( void )
{
if( ! initialized ) {
//
// Position and 1 color passed as a uniform (to similate glColor4ub )
//
shader_ = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_Position_uColor];
colorLocation_ = glGetUniformLocation( shader_->program_, "u_color");
pointSizeLocation_ = glGetUniformLocation( shader_->program_, "u_pointSize");
initialized = YES;
}
}
- (void) draw {
glLineWidth( 3.0f );
//begin and clear
[renderTexture beginWithClear:waveform4F.r g:waveform4F.g b:waveform4F.b a:waveform4F.a];
vector<CGPoint> vertices = bufferQueue.front();
if (WaveformStyleLined) {
ccDrawPoly(&vertices[0], vertices.size(), false);
} else {
lazy_init();
[shader_ use];
[shader_ setUniformForModelViewProjectionMatrix];
[shader_ setUniformLocation:colorLocation_ with4fv:(GLfloat*) &color_.r count:1];
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, &vertices[0]);
glDrawArrays(GL_LINE_STRIP, 0, vertices.size());
}
bufferQueue.pop();
[renderTexture end];
}
//openGLES
#包括
#包括
#包括
静态布尔初始化=否;
静态CCGLProgram*着色器=零;
静态int colorLocation=-1;
静态ccColor4F color_U4;={1,1,1,1};
静态int pointSizeLocation_uz=-1;
静态void lazy_init(void)
{
如果(!已初始化){
//
//位置和1种颜色以统一形式传递(与glColor4ub相似)
//
着色器=[[CCShaderCache sharedShaderCache]programForKey:kCCShader\u Position\u uColor];
colorLocation=glGetUniformLocation(着色器->程序“u颜色”);
pointSizeLocation=glGetUniformLocation(着色器->程序“u\u pointSize”);
初始化=是;
}
}
-(作废)提款{
glLineWidth(3.0f);
//开始清理
[renderTexture beginWithClear:WaveOrm4f.RG:WaveOrm4f.GBA:WaveOrm4f.a];
向量顶点=bufferQueue.front();
if(波形模式){
ccDrawPoly(顶点和顶点[0],顶点.size(),false);
}否则{
lazy_init();
[着色器使用];
[shader_uuu设置UniformForModelViewProjectionMatrix];
[shader_uu设置UniformLocation:colorLocation_uu,带4fv:(GLfloat*)和color_ur计数:1];
ccGLEnableVertexAttribs(kCCVertexAttribFlag_位置);
glVertexAttribPointer(kCCVertexAttrib_位置,2,GL_浮动,GL_假,0,&顶点[0]);
glDrawArray(GL_LINE_STRIP,0,顶点.size());
}
bufferQueue.pop();
[渲染器端];
}
通过添加像DrummerB这样的着色器,线条开始出现。我从Cocos2d复制了相同的着色器代码,它工作起来很有魅力
//openGLES
#include <OpenGLES/EAGL.h>
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>
static BOOL initialized = NO;
static CCGLProgram *shader_ = nil;
static int colorLocation_ = -1;
static ccColor4F color_ = {1,1,1,1};
static int pointSizeLocation_ = -1;
static void lazy_init( void )
{
if( ! initialized ) {
//
// Position and 1 color passed as a uniform (to similate glColor4ub )
//
shader_ = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_Position_uColor];
colorLocation_ = glGetUniformLocation( shader_->program_, "u_color");
pointSizeLocation_ = glGetUniformLocation( shader_->program_, "u_pointSize");
initialized = YES;
}
}
- (void) draw {
glLineWidth( 3.0f );
//begin and clear
[renderTexture beginWithClear:waveform4F.r g:waveform4F.g b:waveform4F.b a:waveform4F.a];
vector<CGPoint> vertices = bufferQueue.front();
if (WaveformStyleLined) {
ccDrawPoly(&vertices[0], vertices.size(), false);
} else {
lazy_init();
[shader_ use];
[shader_ setUniformForModelViewProjectionMatrix];
[shader_ setUniformLocation:colorLocation_ with4fv:(GLfloat*) &color_.r count:1];
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, &vertices[0]);
glDrawArrays(GL_LINE_STRIP, 0, vertices.size());
}
bufferQueue.pop();
[renderTexture end];
}
//openGLES
#包括
#包括
#包括
静态布尔初始化=否;
静态CCGLProgram*着色器=零;
静态int colorLocation=-1;
静态ccColor4F color_U4;={1,1,1,1};
静态int pointSizeLocation_uz=-1;
静态void lazy_init(void)
{
如果(!已初始化){
//
//位置和1种颜色以统一形式传递(与glColor4ub相似)
//
着色器=[[CCShaderCache sharedShaderCache]programForKey:kCCShader\u Position\u uColor];
colorLocation=glGetUniformLocation(着色器->程序“u颜色”);
pointSizeLocation=glGetUniformLocation(着色器->程序“u\u pointSize”);
初始化=是;
}
}
-(作废)提款{
glLineWidth(3.0f);
//开始清理
[renderTexture beginWithClear:WaveOrm4f.RG:WaveOrm4f.GBA:WaveOrm4f.a];
向量顶点=bufferQueue.front();
if(波形模式){
ccDrawPoly(顶点和顶点[0],顶点.size(),false);
}否则{
lazy_init();
[着色器使用];
[shader_uuu设置UniformForModelViewProjectionMatrix];
[shader_uu设置UniformLocation:colorLocation_uu,带4fv:(GLfloat*)和color_ur计数:1];
ccGLEnableVertexAttribs(kCCVertexAttribFlag_位置);
glVertexAttribPointer(kCCVertexAttrib_位置,2,GL_浮动,GL_假,0,&顶点[0]);
glDrawArray(GL_LINE_STRIP,0,顶点.size());
}
bufferQueue.pop();
[渲染器端];
}
您正在使用OpenGL ES 2.0吗?你设置了着色器吗?我用的是OpenGLES 1.0。不,我没有设置着色器。我需要吗?为什么要用GL_三角形?这不应该是GL_线吗?我已经在我的顶点中显示了前10个数据。是的,我使用的是OpenGLES2.0,因为我使用的是Cocos2d v2.0。我已经更新了这个问题。谢谢鼓手!我添加了着色器,效果很好。你在使用OpenGL ES 2.0吗?你设置了着色器吗?我用的是OpenGLES 1.0。不,我没有设置着色器。我需要吗?为什么要用GL_三角形?这不应该是GL_线吗?我已经在我的顶点中显示了前10个数据。是的,我使用的是OpenGLES2.0,因为我使用的是Cocos2d v2.0。我已经更新了这个问题。谢谢鼓手!我添加了着色器,效果很好。