简体   繁体   English

想要在矩形棱镜opengl es ios的一面画出正方形

[英]Want to draw square on one face of a rectangular prism opengl es ios

I have rendered a rectangular prism using opengl es for ios in Xcode. 我在Xcode中使用opengl为ios渲染了一个矩形棱镜。 I want to draw a square on one of the faces of the rectangular prism (front face). 我想在矩形棱柱的一个面(正面)上绘制一个正方形。 I cant use a different data structure to hold the vertices, color co-ordinates and texture because the rectangular prism rotates with touch, and if I use a separate datastructure then the square wont rotate with the rectangular prism. 我不能使用不同的数据结构来保持顶点,颜色坐标和纹理,因为矩形棱镜随触摸旋转,如果我使用单独的数据结构,则方形不会与矩形棱镜一起旋转。 So in the same datastructure I want to make a square. 因此,在相同的数据结构中,我想制作一个正方形。 When I add the co-ordinates to the data structure and specify the points of the triangles it does not render the new square. 当我将坐标添加到数据结构并指定三角形的点时,它不会呈现新的正方形。 Here is the code... 这是代码......

typedef struct {
  float Position[3];
  float Color[4];
  float TexCoord[2];
} Vertex;



const Vertex Vertices[] = {
   // Front
   {{1, -2, 0.10}, {1, 1, 1, 1}, {1, 0}},
   {{1, 2, 0.10}, {1, 1, 1, 1}, {1, 1}},
   {{-1, 2, 0.10}, {1, 1, 1, 1}, {0, 1}},
   {{-1, -2, 0.10}, {1, 1, 1, 1}, {0, 0}},
   // Back
   {{1, 2, -0.10}, {1, 1, 1, 1}, {0, 1}},
   {{-1, -2, -0.10}, {1, 1, 1, 1}, {1, 0}},
   {{1, -2, -0.10}, {1, 1, 1, 1}, {0, 0}},
   {{-1, 2, -0.10}, {1, 1, 1, 1}, {1, 1}},
   // Left
   {{-1, -2, 0.10}, {1, 1, 1, 1}, {1, 0}},
   {{-1, 2, 0.10}, {1, 1, 1, 1}, {1, 1}},
   {{-1, 2, -0.10}, {1, 1, 1, 1}, {0, 1}},
   {{-1, -2, -0.10}, {1, 1, 1, 1}, {0, 0}},
   // Rightself
   {{1, -2, -0.10}, {1, 1, 1, 1}, {1, 0}},
   {{1, 2, -0.10}, {1, 1, 1, 1}, {1, 1}},
   {{1, 2, 0.10}, {1, 1, 1, 1}, {0, 1}},
   {{1, -2, 0.10}, {1, 1, 1, 1}, {0, 0}},
   // Top
   {{1, 2, 0.10}, {1, 1, 1, 1}, {1, 0}},
   {{1, 2, -0.10}, {1, 1, 1, 1}, {1, 1}},
   {{-1, 2, -0.10}, {1, 1, 1, 1}, {0, 1}},
   {{-1, 2, 0.10}, {1, 1, 1, 1}, {0, 0}},
   // Bottom
   {{1, -2, -0.10}, {1, 1, 1, 1}, {1, 0}},
   {{1, -2, 0.10}, {1, 1, 1, 1}, {1, 1}},
   {{-1, -2, 0.10}, {1, 1, 1, 1}, {0, 1}},
   {{-1, -2, -0.10}, {1, 1, 1, 1}, {0, 0}},

  //new square
  {{1, -2, -0.1}, {1, 1, 1, 1}, {1, 0}},
  {{1, -2, 0.1}, {1, 1, 1, 1}, {1, 1}},
  {{-1, -2, 0.1}, {1, 1, 1, 1}, {0, 1}},
  {{-1, -2, -0.1}, {1, 1, 1, 1}, {0, 0}},
};




 const GLubyte Indices[] = {
   // Front
   0, 1, 2,
   2, 3, 0,

   // Back
   4, 6, 5,
   4, 5, 7,
   // Left
   8, 9, 10,
   10, 11, 8,

   // Right
   12, 13, 14,
   14, 15, 12,
   // Top
   16, 17, 18,
   18, 19, 16,
   // Bottom
   20, 21, 22,
   22, 23, 20,
   //new square
   24, 25, 26,
   26, 27, 24,
};

Is the square not rendering because I messed up with my co-ordinates, or is there another method that I need to check and specify I am adding new co-ordinates. 广场是不是因为我弄乱了我的坐标,或者是否有其他方法我需要检查并指定我添加新的坐标。 Also I am using GLKBaseEffect, so no vertex and fragment shaders. 我也在使用GLKBaseEffect,因此没有顶点和片段着色器。

- (void)setupGL {

[EAGLContext setCurrentContext:self.context];
glEnable(GL_CULL_FACE);

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

NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
                          [NSNumber numberWithBool:YES],
                          GLKTextureLoaderOriginBottomLeft, 
                          nil];

NSError * error;    
NSString *path = [[NSBundle mainBundle] pathForResource:@""ofType:@"png"];
GLKTextureInfo * info = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];
if (info == nil) {
    NSLog(@"Error loading file: %@", [error localizedDescription]);
}
self.effect.texture2d0.name = info.name;
self.effect.texture2d0.enabled = true;

// New lines
glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);

// Old stuff
glGenBuffers(1, &_vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
//glBufferData(GL_ARRAY_BUFFER, sizeof(VerticesTwo), VerticesTwo, GL_STATIC_DRAW);

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


// New lines (were previously in draw)
glEnableVertexAttribArray(GLKVertexAttribPosition);        
glVertexAttribPointer(GLKVertexAttribPosition, 3, 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));
glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *) offsetof(Vertex, TexCoord));

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

// New line
glBindVertexArrayOES(0);

_rotMatrix = GLKMatrix4Identity;
_quat = GLKQuaternionMake(0, 0, 0, 1);
_quatStart = GLKQuaternionMake(0, 0, 0, 1);

UITapGestureRecognizer * dtRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTap:)];
dtRec.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:dtRec];
//[self addGestureRecognizer:dtRec];
//[self.view bringSubviewToFront: self.view];


}

Where is the rest of the code? 其余的代码在哪里? How do you draw this? 你怎么画这个?

If I am reading this correct you simply added extra vertices which are exactly the same as the bottom face of your shape which means you just draw the same face twice and there should be no change in the result. 如果我没看错的话,您只需添加额外的顶点即可,这些顶点与形状的底面完全相同,这意味着您只需绘制同一面两次,结果就不会有变化。

No matter the rotations you may still have a square as a separate object. 无论旋转,您可能仍然会有一个正方形作为单独的对象。 You simply use the same matrix as you use to rotate the prism and it will rotate with the square. 您只需使用与旋转棱镜相同的矩阵,它将与方形一起旋转。 You can even use a sub-matrix for the square to position it depending on the prism original coordinates and multiply the prism matrix with this matrix to get the desired result. 您甚至可以使用子矩阵为方块根据棱镜原始坐标定位它,并将棱镜矩阵与此矩阵相乘以获得所需的结果。 This way you would for instance create a square that is rotating around the prism. 这样,您将例如创建一个围绕棱镜旋转的正方形。

But to begin with you could at least change the color of the square to see if you can then find it. 但是首先你可以至少改变方块的颜色,看看你是否可以找到它。 Even an eskimo will not se a white square over a white surface. 即使是爱斯基摩人也不会在白色表面上形成白色方块。

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

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