- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个我无法解决的问题:
尝试绘制由正方形网格(即两个三角形)形成的表面。当我只使用顶点数据和绘图时一切正常:
//m_totalVertices = m_rows*(m_cols *6) -> m_rows and m_cols are the quantity of columns and rows that build the surface
glDrawArrays(GL_TRIANGLES, 0, m_totalVertices);
例如:
//first Quad
Vertex[0] = -15,5;
Vertex[1] = -5,5;
Vertex[2] = -5,-5;
Vertex[3] = -5,-5;
Vertex[4] = -15,-5;
Vertex[5] = -15,5;
//second Quad
Vertex[0] = -5,5;
Vertex[1] = 5,5;
Vertex[2] = 5,-5;
Vertex[3] = 5,-5;
Vertex[4] = -5,-5;
Vertex[5] = -5,5;
//third Quad
Vertex[0] = 5,5;
Vertex[1] = 15,5;
Vertex[2] = 15,-5;
Vertex[3] = 15,-5;
Vertex[4] = 5,-5;
Vertex[5] = 5,5;
结果:
作为优化问题,我想使用“索引”来减少顶点数据的数量,但表面会变形:
要绘制它,我将使用下一个函数:
//m_totalVertices = m_rows*(m_cols *6) -> m_rows and m_cols are the quantity of columns and rows that build the surface
glDrawElements(GL_TRIANGLES, m_totalVertices, GL_UNSIGNED_INT, 0);
例如:
//first Quad
indices[0] = 0;
indices[1] = 1;
indices[2] = 2;
indices[3] = 2;
indices[4] = 3;
indices[5] = 0;
//second Quad
indices[6] = 1;
indices[7] = 4;
indices[8] = 5;
indices[9] = 5;
indices[10] = 2;
indices[11] = 1;
//third Quad
indices[12] = 4;
indices[13] = 6;
indices[14] = 7;
indices[15] = 7;
indices[16] = 5;
indices[17] = 4;
//first Quad
Vertex[0] = -15,5;
Vertex[1] = -5,5;
Vertex[2] = -5,-5;
Vertex[3] = -15,-5;
//second Quad
Vertex[0] = -5,5;
Vertex[1] = 5,5;
Vertex[2] = 5,-5;
Vertex[3] = -5,-5;
//third Quad
Vertex[0] = 5,5;
Vertex[1] = 15,5;
Vertex[2] = 15,-5;
Vertex[3] = 5,-5;
结果:
这是仅使用顶点数据(无索引)生成表面的类的代码 [WORKS PERFECT]:
#include "plane.h"
#include "matrix.h"
#include "glslshader.h"
#include "texture.h"
plane::plane(GLuint cols, GLuint rows,GLuint segmentSize, mixedColors color, CTexture* texture, GLshort textureMode) :
m_matrix(0),
m_shader(0),
m_cols(cols),
m_rows(rows),
m_segmentSize(segmentSize),
m_selectedColor(color),
m_texture(texture),
m_textureMode(textureMode),
m_totalVertices(rows*(cols*6))
{
//matrix handler
m_matrix = matrix::getInstance();
//check that variables are > 1
if(m_cols < 1){m_cols = 1;}
if(m_rows < 1){m_rows = 1;}
// generate a VAO
glGenVertexArrays(1, &m_uiVAO);
vbo.createVBO();
}
plane::~plane()
{
delete m_shader;
vbo.releaseVBO();
glDeleteVertexArrays(1, &m_uiVAO);
}
bool plane::init()
{
//load the shaders
if(m_texture == NULL)
{
m_shader = new GLSLProgram("data/shaders/shader-primitives.vert", "data/shaders/shader-primitives.frag");
}
else//if primitive use texture...
{
m_shader = new GLSLProgram("data/shaders/shader-texture.vert", "data/shaders/shader-texture.frag");
}
if (!m_shader->initialize())
{
cout << "Los shaders de las primitivas no pudieron ser incializados\n";
return false;
}
m_shader->linkProgram();
//number of QUAD vertex
const GLshort QUAD_ELEMENTS = 6;
GLuint r,c,q,x;
//bind VBO
vbo.bindVBO();
//set the start where you draw the first square of the plane
// so that everything is centered
GLfloat initX,initY;
initX = initY = 0.0f;
//displacement of each QUAD in X and Y axis.
const float MODULE = 0.2f;
//find the init value of X (the start value where you draw the first QUAD)
if(m_cols > 1)
{
initX = (float)((m_cols*MODULE/2.0f))-0.1f;
}
//find the init value of y (the start value where you draw the first QUAD)
if(m_rows > 1)
{
initY = (float)((m_rows*MODULE/2.0f))-0.1f;
}
//QUAD vertex data
vector3f quad[QUAD_ELEMENTS];
quad[0] = vector3f(-0.1f,0.1f,0.0f); quad[1] = vector3f(0.1f,0.1f,0.0f);
quad[2] = vector3f(0.1f,-0.1f,0.0f); quad[3] = vector3f(0.1f,-0.1f,0.0f);
quad[4] = vector3f(-0.1f,-0.1f,0.0f); quad[5] = vector3f(-0.1f,0.1f,0.0f);
x=0;
//put QUAD vertex in the "init position"
for(x=0;x<QUAD_ELEMENTS;++x)
{
quad[x].x = quad[x].x + (-initX);
quad[x].y = quad[x].y + initY;
}
//store de actual Y value of row (Y axis)
GLfloat newRow = 0.0f;
//store de actual X value of column (X axis)
GLfloat newColumn = 0.0f;
GLuint colorCounter = 0;
for(r=0;r<m_rows;++r)
{
//reset newColumn variable to 0.0f for each new column
newColumn = 0.0f;
//for each column
for(c=0;c<m_cols;++c)
{
//copy QUAD vertex data in new array
vector3f quadCopy[QUAD_ELEMENTS] = quad;
//for each column, move 0.1f to right
for(q=0;q<QUAD_ELEMENTS;++q)
{
//move right one MODULE (in x axis)
quadCopy[q].x = float(quadCopy[q].x + newColumn);
//move down one MODULE (in Y axis)
quadCopy[q].y = float(quadCopy[q].y + newRow);
//add vertex data and color to de vbo
vector3f resizedVertex = vector3f(quadCopy[q].x*m_segmentSize,quadCopy[q].y*m_segmentSize,quadCopy[q].z);
vbo.addData(&resizedVertex,sizeof(vector3f));
//every 4 laps reset colorCounter to 0;
cout << "Vertex["<< q << "] = "<< resizedVertex.x << "," << resizedVertex.y << ";\n";
if(colorCounter == 3){colorCounter = 0;}
vbo.addData(&color4f(m_selectedColor.color[colorCounter].r,m_selectedColor.color[colorCounter].g,m_selectedColor.color[colorCounter].b,m_selectedColor.color[colorCounter].a),sizeof(color4f));
++colorCounter;
}
//for each column, move right 0.1f
newColumn += MODULE;
}
newRow -= MODULE;
}
//bind VAO
glBindVertexArray(m_uiVAO);
//bind VBO
vbo.bindVBO();
vbo.uploadDataToGPU(GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(vector3f)+sizeof(color4f), (void*)sizeof(vector3f));
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vector3f)+sizeof(color4f), 0);
return true;
}
void plane::render()
{
//bind shader
m_shader->bindShader();
//send projection matrix to shader
m_shader->sendUniform4x4("projectionMatrix", glm::value_ptr(*m_matrix->getProjectionMatrix()));
//matrix transformations
m_matrix->loadIdentity();
m_matrix->scale(vector3f(m_scale,m_scale,m_scale));
m_matrix->translate(m_pos);
m_matrix->rotateX(m_orientation.x);
m_matrix->rotateY(m_orientation.y);
m_matrix->rotateY(m_orientation.z);
//sendtransformation matrix (modelview)
m_shader->sendUniform4x4("modelViewMatrix", glm::value_ptr(m_matrix->getModelViewMatrix()));
//bind VAO
glBindVertexArray(m_uiVAO);
glDrawArrays(GL_TRIANGLES, 0, m_totalVertices);
}
这是生成带有顶点数据和索引的表面的类(已修改)的代码 [DONT WORK]:
#include "plane.h"
#include "matrix.h"
#include "glslshader.h"
#include "texture.h"
plane::plane(GLuint cols, GLuint rows,GLuint segmentSize, mixedColors color, CTexture* texture, GLshort textureMode) :
m_matrix(0),
m_shader(0),
m_cols(cols),
m_rows(rows),
m_segmentSize(segmentSize),
m_selectedColor(color),
m_texture(texture),
m_textureMode(textureMode),
m_totalVertices(rows*(cols*6))
{
//matrix handler
m_matrix = matrix::getInstance();
//check that variables are > 1
if(m_cols < 1){m_cols = 1;}
if(m_rows < 1){m_rows = 1;}
// generate a VAO
glGenVertexArrays(1, &m_uiVAO);
vbo.createVBO();
vboIndices.createVBO();
}
plane::~plane()
{
delete m_shader;
vbo.releaseVBO();
vboIndices.releaseVBO();
glDeleteVertexArrays(1, &m_uiVAO);
}
bool plane::init()
{
//load the shaders
if(m_texture == NULL)
{
m_shader = new GLSLProgram("data/shaders/shader-primitives.vert", "data/shaders/shader-primitives.frag");
}
else//if primitive use texture...
{
m_shader = new GLSLProgram("data/shaders/shader-texture.vert", "data/shaders/shader-texture.frag");
}
if (!m_shader->initialize())
{
cout << "Los shaders de las primitivas no pudieron ser incializados\n";
return false;
}
m_shader->linkProgram();
//number of QUAD vertex
const GLshort QUAD_ELEMENTS = 4;
GLuint r,c,q,x;
//---------------------------------------------------------------------------
//bind VBO
vboIndices.bindVBO(GL_ELEMENT_ARRAY_BUFFER);
//base indices that are used to generate dynamic indices to all vertex data
GLuint baseIndices[6] = {0,1,2,2,3,0};
GLshort indicesCounter = 0;
cout << "indices==========================================\n";
for(x=0;x<m_totalVertices;++x)
{
if(indicesCounter == 6)
{
cout << "new pack of indices = " << x << "\n";
baseIndices[0] = baseIndices[1];
if(x > 6){baseIndices[1] += 2;}else{if(x == 6){baseIndices[1] += 3;}}
baseIndices[2] = (baseIndices[1]+1);
baseIndices[3] = baseIndices[2];
baseIndices[4] = baseIndices[0]+1;
baseIndices[5] = baseIndices[0];
indicesCounter = 0;
}
vboIndices.addData(&baseIndices[indicesCounter],sizeof(GLuint ));
cout << "indices["<<x<<"] = " << baseIndices[indicesCounter] << "\n";
++indicesCounter;
}
//-------------------------------------------------------------------------------
//bind VBO
vbo.bindVBO();
//set the start where you draw the first square of the plane
// so that everything is centered
GLfloat initX,initY;
initX = initY = 0.0f;
//displacement of each QUAD in X and Y axis.
const float MODULE = 0.2f;
//find the init value of X (the start value where you draw the first QUAD)
if(m_cols > 1)
{
initX = (float)((m_cols*MODULE/2.0f))-0.1f;
}
//find the init value of y (the start value where you draw the first QUAD)
if(m_rows > 1)
{
initY = (float)((m_rows*MODULE/2.0f))-0.1f;
}
//QUAD vertex data
vector3f quad[QUAD_ELEMENTS];
quad[0] = vector3f(-0.1f,0.1f,0.0f); quad[1] = vector3f(0.1f,0.1f,0.0f);
quad[2] = vector3f(0.1f,-0.1f,0.0f); quad[3] = vector3f(-0.1f,-0.1f,0.0f);
//put QUAD vertex in the "init position"
for(x=0;x<QUAD_ELEMENTS;++x)
{
quad[x].x = quad[x].x + (-initX);
quad[x].y = quad[x].y + initY;
}
//store de actual Y value of row (Y axis)
GLfloat newRow = 0.0f;
//store de actual X value of column (X axis)
GLfloat newColumn = 0.0f;
GLuint colorCounter = 0;
for(r=0;r<m_rows;++r)
{
//reset newColumn variable to 0.0f for each new column
newColumn = 0.0f;
//for each column
for(c=0;c<m_cols;++c)
{
//copy QUAD vertex data in new array
vector3f quadCopy[QUAD_ELEMENTS] = quad;
//for each column, move 0.1f to right
for(q=0;q<QUAD_ELEMENTS;++q)
{
//move right one MODULE (in x axis)
quadCopy[q].x = float(quadCopy[q].x + newColumn);
//move down one MODULE (in Y axis)
quadCopy[q].y = float(quadCopy[q].y + newRow);
//add vertex data and color to de vbo
vector3f resizedVertex = vector3f(quadCopy[q].x*m_segmentSize,quadCopy[q].y*m_segmentSize,quadCopy[q].z);
vbo.addData(&resizedVertex,sizeof(vector3f));
//every 4 laps reset colorCounter to 0;
cout << "Vertex["<< q << "] = "<< resizedVertex.x << "," << resizedVertex.y << ";\n";
if(colorCounter == 3){colorCounter = 0;}
vbo.addData(&color4f(m_selectedColor.color[colorCounter].r,m_selectedColor.color[colorCounter].g,m_selectedColor.color[colorCounter].b,m_selectedColor.color[colorCounter].a),sizeof(color4f));
++colorCounter;
}
//for each column, move right 0.1f
newColumn += MODULE;
}
newRow -= MODULE;
}
//bind VAO
glBindVertexArray(m_uiVAO);
//bind VBO
vbo.bindVBO();
vbo.uploadDataToGPU(GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(vector3f)+sizeof(color4f), (void*)sizeof(vector3f));
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(vector3f)+sizeof(color4f), 0);
//load indices data in VBO
vboIndices.bindVBO(GL_ELEMENT_ARRAY_BUFFER);
vboIndices.uploadDataToGPU(GL_STATIC_DRAW);
return true;
}
void plane::render()
{
//bind shader
m_shader->bindShader();
//send projection matrix to shader
m_shader->sendUniform4x4("projectionMatrix", glm::value_ptr(*m_matrix->getProjectionMatrix()));
//matrix transformations
m_matrix->loadIdentity();
m_matrix->scale(vector3f(m_scale,m_scale,m_scale));
m_matrix->translate(m_pos);
m_matrix->rotateX(m_orientation.x);
m_matrix->rotateY(m_orientation.y);
m_matrix->rotateY(m_orientation.z);
/sendtransformation matrix (modelview)
m_shader->sendUniform4x4("modelViewMatrix", glm::value_ptr(m_matrix->getModelViewMatrix()));
//bind VAO
glBindVertexArray(m_uiVAO);
glDrawElements(GL_TRIANGLES, m_totalVertices, GL_UNSIGNED_INT, 0);
}
这是处理 VBO 的类的代码 [这项工作很完美,只放在这里是为了让您了解这些方法的作用]:
#include "common_header.h"
#include "vertexBufferObject.h"
CVertexBufferObject::CVertexBufferObject()
{
bDataUploaded = false;
}
/*-----------------------------------------------
Name: createVBO
Params: a_iSize - initial size of buffer
Result: Creates vertex buffer object.
/*---------------------------------------------*/
void CVertexBufferObject::createVBO(int a_iSize)
{
//obtiene el ID del VBO
glGenBuffers(1, &uiBuffer);
//reserva espacio para los datos que va a alojar
data.reserve(a_iSize);
//setea el tamaño
iSize = a_iSize;
}
/*-----------------------------------------------
Name: releaseVBO
Params: none
Result: Releases VBO and frees all memory.
/*---------------------------------------------*/
void CVertexBufferObject::releaseVBO()
{
//elimina el ID obtenido
glDeleteBuffers(1, &uiBuffer);
bDataUploaded = false;
//libera todos los datos almacenados
data.clear();
}
/*-----------------------------------------------
Name: mapBufferToMemory
Params: iUsageHint - GL_READ_ONLY, GL_WRITE_ONLY...
Result: Maps whole buffer data to memory and
returns pointer to data.
/*---------------------------------------------*/
void* CVertexBufferObject::mapBufferToMemory(int iUsageHint)
{
if(!bDataUploaded)return 0;
void* ptrRes = glMapBuffer(iBufferType, iUsageHint);
return ptrRes;
}
/*-----------------------------------------------
Name: mapSubBufferToMemory
Params: iUsageHint - GL_READ_ONLY, GL_WRITE_ONLY...
uiOffset - data offset (from where should
data be mapped).
uiLength - length of data
Result: Maps specified part of buffer to memory.
/*---------------------------------------------*/
void* CVertexBufferObject::mapSubBufferToMemory(int iUsageHint, unsigned int uiOffset, unsigned int uiLength)
{
if(!bDataUploaded)return 0;
void* ptrRes = glMapBufferRange(iBufferType, uiOffset, uiLength, iUsageHint);
return ptrRes;
}
/*-----------------------------------------------
Name: unmapBuffer
Params: none
Result: Unmaps previously mapped buffer.
/*---------------------------------------------*/
void CVertexBufferObject::unmapBuffer()
{
glUnmapBuffer(iBufferType);
}
/*-----------------------------------------------
Name: bindVBOiDrawingHint)
{
glBufferData(iBufferType, data.size(), &data[0], iDrawingHint);
bDataUploaded = true;
Params: a_iBufferType - buffer type (GL_ARRAY_BUFFER, ...)
Result: Binds this VBO.
/*---------------------------------------------*/
void CVertexBufferObject::bindVBO(int a_iBufferType)
{
iBufferType = a_iBufferType;
glBindBuffer(iBufferType, uiBuffer);
}
/*-----------------------------------------------
Name: uploadDataToGPU
Params: iUsageHint - GL_STATIC_DRAW, GL_DYNAMIC_DRAW...
Result: Sends data to GPU.
/*---------------------------------------------*/
void CVertexBufferObject::uploadDataToGPU(int iDrawingHint)
{
glBufferData(iBufferType, data.size(), &data[0], iDrawingHint);
bDataUploaded = true;
data.clear();
}
/*-----------------------------------------------
Name: addData
Params: ptrData - pointer to arbitrary data
uiDataSize - data size in chars
Result: Adds arbitrary data to VBO.
/*---------------------------------------------*/
void CVertexBufferObject::addData(void* ptrData, unsigned int uiDataSize)
{
data.insert(data.end(), (char*)ptrData, (char*)ptrData+uiDataSize);
}
/*-----------------------------------------------
Name: getDataPointer
Params: none
Result: Returns data pointer (only before uplading).
/*---------------------------------------------*/
void* CVertexBufferObject::getDataPointer()
{
if(bDataUploaded)return 0;
return (void*)data[0];
}
/*-----------------------------------------------
Name: getBuffer
Params: none
Result: Returns VBO ID.
/*---------------------------------------------*/
unsigned int CVertexBufferObject::getBuffer()
{
return uiBuffer;
}
最佳答案
如果没有更多信息,我不能 100% 确定,但无论如何:
正确的索引数组应该是这样的:0,1,2,2,3,0 , 4,5,6,6,7,4 , 8,9,10,10,11,8.
这是因为你使用你的索引就好像你没有重复的顶点,但根据输出,你有 :)。或者,如果您想使用更少的空间,请考虑使用 GL_QUADS 而不是 GL_TRIANGLES,这样您只需为每个四边形指定 4 个索引。
如果你想修复你的顶点,只提交顶点 0,1,2,3, 5,6, 9,10 到你的 float 缓冲区,不要修改你的索引。
关于c++ - 创建索引和 GL_TRIANGLES 表面(仅适用于顶点数据),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19372470/
前言: 有时候,一个数据库有多个帐号,包括数据库管理员,开发人员,运维支撑人员等,可能有很多帐号都有比较大的权限,例如DDL操作权限(创建,修改,删除存储过程,创建,修改,删除表等),账户多了,管理
所以我用 Create React App 创建并设置了一个大型 React 应用程序。最近我们开始使用 Storybook 来处理和创建组件。它很棒。但是,当我们尝试运行或构建应用程序时,我们不断遇
遵循我正在创建的控件的代码片段。这个控件用在不同的地方,变量也不同。 我正在尝试编写指令来清理代码,但在 {{}} 附近插入值时出现解析错误。 刚接触 Angular ,无法确定我错过了什么。请帮忙。
我正在尝试创建一个 image/jpeg jax-rs 提供程序类,它为我的基于 post rest 的 Web 服务创建一个图像。我无法制定请求来测试以下内容,最简单的测试方法是什么? @POST
我一直在 Windows 10 的模拟器中练习 c。后来我改用dev C++ IDE。当我在 C 中使用 FILE 时。创建的文件的名称为 test.txt ,而我给出了其他名称。请帮助解决它。 下面
当我们创建自定义 View 时,我们将 View 文件的所有者设置为自定义类,并使用 initWithFrame 或 initWithCode 对其进行实例化。 当我们创建 customUITable
我正在尝试为函数 * Producer 创建一个线程,但用于创建线程的行显示错误。我为这句话加了星标,但我无法弄清楚它出了什么问题...... #include #include #include
今天在做项目时,遇到了需要创建JavaScript对象的情况。所以Bing了一篇老外写的关于3种创建JavaScript对象的文章,看后跟着打了一遍代码。感觉方法挺好的,在这里与大家分享一下。 &
我正在阅读将查询字符串传递给 Amazon 的 S3 以进行身份验证的文档,但似乎无法理解 StringToSign 的创建和使用方式。我正在寻找一个具体示例来说明 (1) 如何构造 String
前言:我对 C# 中任务的底层实现不太了解,只了解它们的用法。为我在下面屠宰的任何东西道歉: 对于“我怎样才能开始一项任务但不等待它?”这个问题,我找不到一个好的答案。在 C# 中。更具体地说,即使任
我有一个由一些复杂的表达式生成的 ILookup。假设这是按姓氏查找人。 (在我们简单的世界模型中,姓氏在家庭中是唯一的) ILookup families; 现在我有两个对如何构建感兴趣的查询。 首
我试图创建一个 MSI,其中包含 和 exe。在 WIX 中使用了捆绑选项。这样做时出错。有人可以帮我解决这个问题。下面是代码: 错误 error LGH
在 Yii 中,Create 和 Update 通常使用相同的形式。因此,如果我在创建期间有电子邮件、密码、...other_fields...等字段,但我不想在更新期间专门显示电子邮件和密码字段,但
上周我一直在努力创建一个给定一行和一列的 QModelIndex。 或者,我会满足于在已经存在的 QModelIndex 中更改 row() 的值。 任何帮助,将不胜感激。 编辑: QModelInd
出于某种原因,这不起作用: const char * str_reset_command = "\r\nReset"; const char * str_config_command = "\r\nC
现在,我有以下由 original.df %.% group_by(Category) %.% tally() %.% arrange(desc(n)) 创建的 data.frame。 DF 5),
在今天之前,我使用/etc/vim/vimrc来配置我的vim设置。今天,我想到了创建.vimrc文件。所以,我用 touch .vimrc cat /etc/vim/vimrc > .vimrc 所
我可以创建一个 MKAnnotation,还是只读的?我有坐标,但我发现使用 setCooperative 手动创建 MKAnnotation 并不容易。 想法? 最佳答案 MKAnnotation
在以下代码中,第一个日志语句按预期显示小数,但第二个日志语句记录 NULL。我做错了什么? NSDictionary *entry = [[NSDictionary alloc] initWithOb
我正在使用与此类似的代码动态添加到数组; $arrayF[$f+1][$y][$x+1] = $value+1; 但是我在错误报告中收到了这个: undefined offset :1 问题:尝试创
我是一名优秀的程序员,十分优秀!