void meGLWidget::sendDataToOpenGL() { ShapeData shape = ShapeGenerator::makeCube();//ShapeGenerator::makeTriangle(); GLuint vertexBufferID; glGenBuffers(1,&vertexBufferID); glBindBuffer(GL_ARRAY_BUFFER,vertexBufferID); glBufferData(GL_ARRAY_BUFFER,shape.vertexBufferSize(),shape.vertices,GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,VERTEX_BYTE_SIZE, 0); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,VERTEX_BYTE_SIZE, (char*)(sizeof(float)*3)); GLuint indexBufferID; glGenBuffers(1,&indexBufferID); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,indexBufferID ); glBufferData(GL_ELEMENT_ARRAY_BUFFER,shape.indexBufferSize(),shape.indices,GL_STATIC_DRAW); numIndices = shape.numIndices; shape.cleanUp(); }
void generateSphere(GLuint program, ShapeData* sphereData) { tetrahedron(N); // Normals and tangents for (int i = 0; i < numSphereVertices; i++) { sphereData->normals[i] = vec3(sphereData->points[i].x, sphereData->points[i].y, sphereData->points[i].z); } // TexCoords double u, v; for (int i = 0; i < numSphereVertices; i++) { u = 0.5*(atan2(sphereData->points[i].z, sphereData->points[i].x)/M_PI + 1); //0~1 v = 1 + asin(sphereData->points[i].y)/M_PI - 0.5; //0~1, and right ways up // make sure that we have no seam if( i % 3 > 0 ) { vec2 prev = sphereData->UVs[i-(i%3)]; if( fabs(u - prev.x) > 0.5 ) { float diff = ( u < prev.x ) ? ( u + 1 - prev.x ) : ( u - 1 - prev.x ); u = prev.x + diff; } } sphereData->UVs[i] = vec2(u,v); } // Create a vertex array object sphereData->enableUVArray( true, program ); sphereData->bind(program); }
void Natla::initialize(const char* path, ShaderInfo* shader) { where = _mat4(); ShapeData natlaData = FileInterperter::binaryToModel("Assets\\Models\\Natla.bin"); if (!natlaShape) natlaShape = renderer.addGeometry(natlaData.verts, natlaData.vertexBufferSize(), natlaData.indices, natlaData.numIndices, GL_TRIANGLES, shapeManager.getVertexLayout()); const char* textures[] = { "Assets\\Textures\\Natla\\cd076559.PNG", "Assets\\Textures\\Natla\\e32ef3b4.PNG", "Assets\\Textures\\Natla\\3467d32f.PNG", "Assets\\Textures\\Natla\\1a5850cd.PNG", "Assets\\Textures\\Natla\\6aaf6b2b.PNG", "Assets\\Textures\\Natla\\d4e192b1.PNG", "Assets\\Textures\\Natla\\14b8ae4d.PNG", "Assets\\Textures\\Natla\\fc372885.PNG", "Assets\\Textures\\Natla\\476eee47.PNG", }; numTextures = sizeof(textures) / sizeof(textures[0]); TextureInfo* natlaTextures = renderer.addTextureArray(PT_2DIMAGEARRAY, numTextures, textures); model = renderer.addRenderable(natlaShape, shader, where); renderer.addRenderableUniformParameter(model, "meTextures", PT_2DIMAGEARRAY, natlaTextures); renderer.addRenderableUniformParameter(model, "numTextures", PT_FLOAT, &numTextures); }
ShapeData* genCube(GLuint program, float tex_size) { ShapeData* ret = new ShapeData(numCubeVertices); colorcube(ret, tex_size); ret->enableTangentArray( program, true ); ret->enableUVArray( program, true ); ret->bind(program); return ret; }
Geometry* DebugShape::getLine(){ if(myLine != 0) return myLine; ShapeData lineData = ShapeGenerator::makeLine(); myLine = renderer.addGeometry(lineData.verts, lineData.vertexBufferSize(), lineData.indices, lineData.indexBufferSize(), lineData.numIndices, GL_TRIANGLES, vertLayout); return myLine; }
Geometry* DebugShape::getSphere(){ if(mySphere != 0) return mySphere; ShapeData sphereData = ShapeGenerator::makeSphere(20); mySphere = renderer.addGeometry(sphereData.verts, sphereData.vertexBufferSize(), sphereData.indices, sphereData.indexBufferSize(), sphereData.numIndices, GL_TRIANGLES, vertLayout); return mySphere; }
Geometry* DebugShape::getCube(){ if(myCube != 0){ return myCube; } ShapeData cubeData = ShapeGenerator::makeCube(); myCube = renderer.addGeometry(cubeData.verts, cubeData.vertexBufferSize(), cubeData.indices, cubeData.indexBufferSize(), cubeData.numIndices, GL_TRIANGLES, vertLayout); return myCube; }
void MusikQuadRender::initializeTexGeometry() { ShapeData shape = ShapeGenerator::makeQuadTex(); tVertex.create(); tVertex.bind(); tVertex.setUsagePattern(QOpenGLBuffer::StaticDraw); tVertex.allocate(shape.vertices,shape.numVertices*sizeof(Vertex)); tObject.create(); tObject.bind(); programm->enableAttributeArray(vposAttr); programm->enableAttributeArray(vcolAttr); programm->setAttributeBuffer(vposAttr,GL_FLOAT,Vertex::positionOffset(),Vertex::PositionTupleSize,Vertex::stride()); programm->setAttributeBuffer(vcolAttr,GL_FLOAT,Vertex::colorOffset(),Vertex::ColorTupleSize,Vertex::stride()); tIndex.create(); tIndex.bind(); tIndex.setUsagePattern(QOpenGLBuffer::StaticDraw); tIndex.allocate(shape.indices,shape.indexBufferSize()); numIndx = shape.numIndices; tVertex.release(); tObject.release(); tTransform.create(); tTransform.bind(); tTransform.setUsagePattern(QOpenGLBuffer::StaticDraw); tTransform.allocate(quadTransforms,sizeof(QMatrix4x4)*numQuads); tObject.bind(); programm->setAttributeBuffer(vmatrixAttr,GL_FLOAT,sizeof(QVector4D)*0,4,sizeof(QMatrix4x4)); QMatrix4x4 mat; programm->setAttributeValue(vmatrixAttr,mat.data(),4,4); tObject.release(); tTransform.release(); shape.cleanup(); }
void generateCone(GLuint program, ShapeData* coneData) { vec2 circlePoints[numConeDivisions]; makeCircle(circlePoints, numConeDivisions); int Index = 0; makeCircleWall(coneData->points, coneData->normals, numConeDivisions, 1.0f, 1.0f, Index, 1); makeCircleWall(coneData->points, coneData->normals, numConeDivisions, 1.0f, -1.0f, Index, -1); // Create a vertex array object coneData->bind(program); }
void generateCylinder(GLuint program, ShapeData* cylData) { int Index = 0; vec3 tangent = vec3(0.0f, 0.0f, 1.0f); vec2 circlePoints[numCylDivisions]; makeCircle(circlePoints, numCylDivisions); makeCircleWall(cylData->points, cylData->normals, numCylDivisions, 1.0f, 1.0f, Index, 1); makeCircleWall(cylData->points, cylData->normals, numCylDivisions, -1.0f, -1.0f, Index, -1); for (int i = 0; i < numCylDivisions; i++) { int i2 = (i+1)%numCylDivisions; float iAngle = ( i / (float)numCylDivisions ); float i2Angle = ( (i+1) / (float)numCylDivisions ); vec3 p1(circlePoints[i2].x, circlePoints[i2].y, -1.0f); vec3 p2(circlePoints[i2].x, circlePoints[i2].y, 1.0f); vec3 p3(circlePoints[i].x, circlePoints[i].y, 1.0f); cylData->points[Index] = p1; cylData->normals[Index] = vec3(p1.x, p1.y, 0.0f); cylData->tangents[Index] = tangent; cylData->UVs[Index] = vec2(i2Angle, 1); Index++; cylData->points[Index] = p2; cylData->normals[Index] = vec3(p2.x, p2.y, 0.0f); cylData->tangents[Index] = tangent; cylData->UVs[Index] = vec2(i2Angle, 0); Index++; cylData->points[Index] = p3; cylData->normals[Index] = vec3(p3.x, p3.y, 0.0f); cylData->tangents[Index] = tangent; cylData->UVs[Index] = vec2(iAngle, 0); Index++; p1 = vec3(circlePoints[i2].x, circlePoints[i2].y, -1.0f); p2 = vec3(circlePoints[i].x, circlePoints[i].y, 1.0f); p3 = vec3(circlePoints[i].x, circlePoints[i].y, -1.0f); cylData->points[Index] = p1; cylData->normals[Index] = vec3(p1.x, p1.y, 0.0f); cylData->tangents[Index] = tangent; cylData->UVs[Index] = vec2(i2Angle, 1); Index++; cylData->points[Index] = p2; cylData->normals[Index] = vec3(p2.x, p2.y, 0.0f); cylData->tangents[Index] = tangent; cylData->UVs[Index] = vec2(iAngle, 0); Index++; cylData->points[Index] = p3; cylData->normals[Index] = vec3(p3.x, p3.y, 0.0f); cylData->tangents[Index] = tangent; cylData->UVs[Index] = vec2(iAngle, 1); Index++; } // Create a vertex array object cylData->enableTangentArray( program, true ); cylData->enableUVArray( program, true ); cylData->bind(program); }
Renderable* DebugShape::vector(vec3 end){ ShapeData cyl = ShapeMaker::makeCylinder(20); ShapeData cone = ShapeMaker::makeCone(20); Geometry * cylinder = renderer.addGeometry(cyl.verts, cyl.vertexBufferSize(), cyl.indices, cyl.indexBufferSize(), cyl.numIndices, GL_TRIANGLES, vertLayout); Geometry* theCone = renderer.addGeometry(cone.verts, cone.vertexBufferSize(), cone.indices, cone.indexBufferSize(), cone.numIndices, GL_TRIANGLES, vertLayout); vec3 direction = glm::normalize(end); mat4 orient = glm::orientation(direction, vec3(1.0f, 0.0f, 0.0f)) * glm::scale(vec3(1.0f, 0.1f, 0.1f)); Renderable* myCyl = renderer.addRenderable(cylinder, shader, orient); mat4 trans = glm::translate(direction) * glm::orientation(direction, vec3(1.0f, 0.0f, 0.0f)); Renderable* myCone = renderer.addRenderable(theCone, shader, trans); return myCyl; }
void MusikQuadRender::initializeGeometry() { ShapeData shape = ShapeGenerator::makeQuad(); vVertex.create(); vVertex.bind(); vVertex.setUsagePattern(QOpenGLBuffer::StaticDraw); vVertex.allocate(shape.vertices,shape.numVertices*sizeof(Vertex)); vObject.create(); vObject.bind(); programm->enableAttributeArray(vposAttr); programm->setAttributeBuffer(vposAttr,GL_FLOAT,Vertex::positionOffset(),Vertex::PositionTupleSize,Vertex::stride()); // programmTex->enableAttributeArray(texVpos); // programmTex->setAttributeBuffer(texVpos,GL_FLOAT,Vertex::positionOffset(),Vertex::PositionTupleSize,Vertex::stride()); vIndex.create(); vIndex.bind(); vIndex.setUsagePattern(QOpenGLBuffer::StaticDraw); vIndex.allocate(shape.indices,shape.indexBufferSize()); numIndx = shape.numIndices; vVertex.release(); vObject.release(); vTransform.create(); vTransform.bind(); vTransform.setUsagePattern(QOpenGLBuffer::DynamicDraw); vTransform.allocate(quadTransforms,sizeof(QMatrix4x4)*numQuads); vObject.bind(); programm->enableAttributeArray(vmatrixAttr); programm->enableAttributeArray(vmatrixAttr+1); programm->enableAttributeArray(vmatrixAttr+2); programm->enableAttributeArray(vmatrixAttr+3); programm->setAttributeBuffer(vmatrixAttr,GL_FLOAT,sizeof(QVector4D)*0,4,sizeof(QMatrix4x4)); programm->setAttributeBuffer(vmatrixAttr+1,GL_FLOAT,sizeof(QVector4D)*1,4,sizeof(QMatrix4x4)); programm->setAttributeBuffer(vmatrixAttr+2,GL_FLOAT,sizeof(QVector4D)*2,4,sizeof(QMatrix4x4)); programm->setAttributeBuffer(vmatrixAttr+3,GL_FLOAT,sizeof(QVector4D)*3,4,sizeof(QMatrix4x4)); glVertexAttribDivisor(vmatrixAttr,1); glVertexAttribDivisor(vmatrixAttr+1,1); glVertexAttribDivisor(vmatrixAttr+2,1); glVertexAttribDivisor(vmatrixAttr+3,1); vColor.create(); vColor.bind(); vColor.setUsagePattern(QOpenGLBuffer::DynamicDraw); vColor.allocate(quadColors,sizeof(QVector3D)*numQuads); programm->setAttributeBuffer(vcolAttr,GL_FLOAT,0,3,sizeof(QVector3D)); programm->enableAttributeArray(vcolAttr); glVertexAttribDivisor(vcolAttr,1); vObject.release(); vTransform.release(); shape.cleanup(); }
void MeGlWindow::sendDataToOpenGL() { ShapeData cube = ShapeGenerator::makeCube(); ShapeData arrow = ShapeGenerator::makeArrow(); ShapeData plane = ShapeGenerator::makePlane(); ShapeData teapot = ShapeGenerator::makeTeapot(); ShapeData sphere = ShapeGenerator::makeSphere(); ShapeData torus = ShapeGenerator::makeTorus(); glGenBuffers(1, &theBufferID); glBindBuffer(GL_ARRAY_BUFFER, theBufferID); glBufferData(GL_ARRAY_BUFFER, cube.vertexBufferSize() + cube.indexBufferSize() + arrow.vertexBufferSize() + arrow.indexBufferSize() + plane.vertexBufferSize() + plane.indexBufferSize() + teapot.vertexBufferSize() + teapot.indexBufferSize() + sphere.vertexBufferSize() + sphere.indexBufferSize() + torus.vertexBufferSize() + torus.indexBufferSize(), 0, GL_STATIC_DRAW); GLsizeiptr currentOffset = 0; glBufferSubData(GL_ARRAY_BUFFER, currentOffset, cube.vertexBufferSize(), cube.vertices); currentOffset += cube.vertexBufferSize(); cubeIndexByteOffset = currentOffset; glBufferSubData(GL_ARRAY_BUFFER, currentOffset, cube.indexBufferSize(), cube.indices); currentOffset += cube.indexBufferSize(); glBufferSubData(GL_ARRAY_BUFFER, currentOffset, arrow.vertexBufferSize(), arrow.vertices); currentOffset += arrow.vertexBufferSize(); arrowIndexByteOffset = currentOffset; glBufferSubData(GL_ARRAY_BUFFER, currentOffset, arrow.indexBufferSize(), arrow.indices); currentOffset += arrow.indexBufferSize(); glBufferSubData(GL_ARRAY_BUFFER, currentOffset, plane.vertexBufferSize(), plane.vertices); currentOffset += plane.vertexBufferSize(); planeIndexByteOffset = currentOffset; glBufferSubData(GL_ARRAY_BUFFER, currentOffset, plane.indexBufferSize(), plane.indices); currentOffset += plane.indexBufferSize(); glBufferSubData(GL_ARRAY_BUFFER, currentOffset, teapot.vertexBufferSize(), teapot.vertices); currentOffset += teapot.vertexBufferSize(); teapotIndexByteOffset = currentOffset; glBufferSubData(GL_ARRAY_BUFFER, currentOffset, teapot.indexBufferSize(), teapot.indices); currentOffset += teapot.indexBufferSize(); glBufferSubData(GL_ARRAY_BUFFER, currentOffset, sphere.vertexBufferSize(), sphere.vertices); currentOffset += sphere.vertexBufferSize(); sphereIndexByteOffset = currentOffset; glBufferSubData(GL_ARRAY_BUFFER, currentOffset, sphere.indexBufferSize(), sphere.indices); currentOffset += sphere.indexBufferSize(); glBufferSubData(GL_ARRAY_BUFFER, currentOffset, torus.vertexBufferSize(), torus.vertices); currentOffset += torus.vertexBufferSize(); torusIndexByteOffset = currentOffset; glBufferSubData(GL_ARRAY_BUFFER, currentOffset, torus.indexBufferSize(), torus.indices); currentOffset += torus.indexBufferSize(); cubeNumIndices = cube.numIndices; arrowNumIndices = arrow.numIndices; planeNumIndices = plane.numIndices; teapotNumIndices = teapot.numIndices; sphereNumIndices = sphere.numIndices; torusNumIndices = torus.numIndices; glGenVertexArrays(1, &cubeVertexArrayObjectID); glGenVertexArrays(1, &arrowVertexArrayObjectID); glGenVertexArrays(1, &planeVertexArrayObjectID); glGenVertexArrays(1, &teapotVertexArrayObjectID); glGenVertexArrays(1, &sphereVertexArrayObjectID); glGenVertexArrays(1, &torusVertexArrayObjectID); glBindVertexArray(cubeVertexArrayObjectID); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); glBindBuffer(GL_ARRAY_BUFFER, theBufferID); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, 0); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (void*)(sizeof(float) * 3)); glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (void*)(sizeof(float) * 6)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, theBufferID); glBindVertexArray(arrowVertexArrayObjectID); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); glBindBuffer(GL_ARRAY_BUFFER, theBufferID); GLuint arrowByteOffset = cube.vertexBufferSize() + cube.indexBufferSize(); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (void*)arrowByteOffset); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (void*)(arrowByteOffset + sizeof(float) * 3)); glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (void*)(arrowByteOffset + sizeof(float) * 6)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, theBufferID); glBindVertexArray(planeVertexArrayObjectID); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); glBindBuffer(GL_ARRAY_BUFFER, theBufferID); GLuint planeByteOffset = arrowByteOffset + arrow.vertexBufferSize() + arrow.indexBufferSize(); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (void*)planeByteOffset); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (void*)(planeByteOffset + sizeof(float) * 3)); glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (void*)(planeByteOffset + sizeof(float) * 6)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, theBufferID); glBindVertexArray(teapotVertexArrayObjectID); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); glBindBuffer(GL_ARRAY_BUFFER, theBufferID); GLuint teapotByteOffset = planeByteOffset + plane.vertexBufferSize() + plane.indexBufferSize(); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (void*)teapotByteOffset); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (void*)(teapotByteOffset + sizeof(float) * 3)); glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (void*)(teapotByteOffset + sizeof(float) * 6)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, theBufferID); glBindVertexArray(sphereVertexArrayObjectID); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); glBindBuffer(GL_ARRAY_BUFFER, theBufferID); GLuint sphereByteOffset = teapotByteOffset + teapot.vertexBufferSize() + teapot.indexBufferSize(); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (void*)sphereByteOffset); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (void*)(sphereByteOffset + sizeof(float) * 3)); glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (void*)(sphereByteOffset + sizeof(float) * 6)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, theBufferID); glBindVertexArray(torusVertexArrayObjectID); glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); glBindBuffer(GL_ARRAY_BUFFER, theBufferID); GLuint torusByteOffset = sphereByteOffset + sphere.vertexBufferSize() + sphere.indexBufferSize(); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (void*)torusByteOffset); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (void*)(torusByteOffset + sizeof(float) * 3)); glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, VERTEX_BYTE_SIZE, (void*)(torusByteOffset + sizeof(float) * 6)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, theBufferID); cube.cleanup(); arrow.cleanup(); plane.cleanup(); teapot.cleanup(); sphere.cleanup(); torus.cleanup(); }