所以,我有一个我正在尝试构建的项目。我的目标是拥有一个包含我的关卡结构的 GL_QUADS 列表。基本上我希望能够从列表中添加和删除多维数据集,然后让 OpenGL 渲染它。
我的问题是数组似乎不可扩展。我已经编写了一个代码,可以让我按下空格并创建一个立方体,但是我需要以某种方式创建使创建立方体的点数组随意更长。所以基本上,如果有一种方法可以根据需要扩展数组,我会全力以赴。
这是一些代码......您可能甚至不需要看它来回答问题。我已经 sudo 编码了不相关的部分。
#include <GLTools.h>
#include <GLMatrixStack.h>
#include <GLFrame.h>
#include <GLFrustum.h>
#include <GLBatch.h>
#include <GLGeometryTransform.h>
#include <math.h>
#ifdef __APPLE__
#include <glut/glut.h>
#else
#define FREEGLUT_STATIC
#include <GL/glut.h>
#endif
// An assortment of needed classes
GLShaderManager shaderManager;
GLMatrixStack modelViewMatrix;
GLMatrixStack projectionMatrix;
GLFrame cameraFrame;
GLFrame objectFrame;
GLFrustum viewFrustum;
GLBatch triangleBatch;
GLBatch QuadStrip;
GLGeometryTransform transformPipeline;
M3DMatrix44f shadowMatrix;
GLfloat vGreen[] = { 0.5f, 1.0f, 0.0f, 1.0f };
GLfloat vPyramid[12][3] = { //COORDS};
//This function is triggered when space is pressed. It takes parameters passed to it to create the 8 points of the cube then makes a Quad_Strip mesh from it
void CreatePlat(float centerx, float centery, float centerz, float width, float length, float depth)
{
GLfloat vP1[3] = { (centerx - (width/2)), (centery), (centerz + (length/2))}; //top left
GLfloat vP2[3] = { (centerx + (width/2)), (centery), (centerz + (length/2))}; //top right
GLfloat vP3[3] = { (centerx + (width/2)), (centery),(centerz - (length/2))}; //bottom right
GLfloat vP4[3] = { (centerx - (width/2)), (centery), (centerz - (length/2))}; //bottom left
GLfloat vP5[3] = { (centerx - (width/2)), (centery - depth), (centerz + (length/2))};
GLfloat vP6[3] = { (centerx + (width/2)), (centery - depth), (centerz + (length/2))};
GLfloat vP7[3] = { (centerx + (width/2)), (centery - depth),(centerz - (length/2))};
GLfloat vP8[3] = { (centerx - (width/2)), (centery - depth), (centerz - (length/2))};
GLfloat vPlat[26][3] = {vP4[0], vP4[1], vP4[2],
//BLA BLA
vP2[0], vP2[1], vP2[2]};
QuadStrip.Begin(GL_QUAD_STRIP, 26);
QuadStrip.CopyVertexData3f(vPlat);
QuadStrip.End();
}
GLfloat size = 1.0;
void Update ()
{
//Controls
//Rotate controls
if(space)
CreatePlat(0.0, 0.0, 0.0, size, 1.0, 1.0);
if(W)
size += 1.0;
if(S)
size -= 1.0;
vPyramid[1][1] += 1.0;
}
void SetupRC()
{
triangleBatch.Begin(GL_TRIANGLES, 12);
triangleBatch.CopyVertexData3f(vPyramid);
triangleBatch.End();
}
void DrawWireFramedBatch(GLBatch* pBatch)
{
//Take a mesh and draw it WITH outlines
}
void RenderScene(void)
{
Update();
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
modelViewMatrix.PushMatrix();
M3DMatrix44f mCamera;
cameraFrame.GetCameraMatrix(mCamera);
modelViewMatrix.MultMatrix(mCamera);
M3DMatrix44f mObjectFrame;
objectFrame.GetMatrix(mObjectFrame);
modelViewMatrix.MultMatrix(mObjectFrame);
shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vBlack);
DrawWireFramedBatch(&triangleBatch);
DrawWireFramedBatch(&QuadStrip);
modelViewMatrix.PopMatrix();
glutPostRedisplay();
// Flush drawing commands
glutSwapBuffers();
}
int main(int argc, char* argv[])
{
gltSetWorkingDirectory(argv[0]);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
glutInitWindowSize(800, 600);
glutCreateWindow("GL_POINTS");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(RenderScene);
//glutSpecialFunc(SpecialKeys);
GLenum err = glewInit();
if (GLEW_OK != err) {
fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
return 1;
}
SetupRC();
glutMainLoop();
return 0;
}
正如您所看到的,理想的解决方案是在创建线条后在 CreatePlat() 函数内部,将线条添加到数组中,然后重建我的缓冲区以适应它。您也可能注意到目前我正在使用 Quad_Strip.... 我将使用 GL_Quads。
旁注我打算做一个基于速度的运动,如果您尝试添加不同数量的水平和垂直速度,我熟悉如何处理 2D 中的方向的数学......但是在 3D IDK 中!我将能够将事情设置为完美运行,直到玩家旋转,这就是事情变得复杂的时候。它为此继承了math3d lib的东西吗?