/** * render skybox */ void renderWithProgram(GLSL::mat4 &p_pvm_matrix) { // CGlStateDisable depth_test(GL_DEPTH_TEST); CGlErrorCheck(); program.use(); pvm_matrix_uniform.set(p_pvm_matrix); vertex_buffer.bind(); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr); glEnableVertexAttribArray(0); texture_cube_map.bind(); #if 0 CGlErrorCheck(); glDrawArrays(GL_TRIANGLES, 0, 3); CGlErrorCheck(); #else index_buffer.bind(); CGlErrorCheck(); glDrawElements(GL_TRIANGLE_STRIP, 20, GL_UNSIGNED_BYTE, 0); index_buffer.unbind(); #endif texture_cube_map.unbind(); glDisableVertexAttribArray(0); vertex_buffer.unbind(); program.disable(); CGlErrorCheck(); }
void GlEditLayerOverlay::fillPolygonBuffers(const Polygon &polygon, VectorGlObject *bufferArray) { // The number type to use for tessellation. using Coord = double; // The index type. Defaults to uint32_t, but you can also pass uint16_t // if you know that your data won't have more than 65536 vertices. using N = unsigned short; using MBPoint = std::array<Coord, 2>; // Create array. std::vector<std::vector<MBPoint>> mbPolygon; for(const Line &ring : polygon) { std::vector<MBPoint> mbRing; for(const OGRRawPoint &point : ring) { mbRing.emplace_back(MBPoint({{point.x, point.y}})); } mbPolygon.emplace_back(mbRing); } // Run tessellation. // Returns array of indices that refer to the vertices of the input polygon. // Three subsequent indices form a triangle. std::vector<N> mbIndices = mapbox::earcut<N>(mbPolygon); // Fill triangles. GlBuffer *fillBuffer = new GlBuffer(GlBuffer::BF_FILL); unsigned short index = 0; for(N mbIndex : mbIndices) { if(!fillBuffer->canStoreVertices(mbIndices.size() * 3)) { bufferArray->addBuffer(fillBuffer); index = 0; fillBuffer = new GlBuffer(GlBuffer::BF_FILL); } N currentIndex = mbIndex; for(const auto& ring : mbPolygon) { if(currentIndex >= ring.size()) { currentIndex -= ring.size(); continue; } MBPoint mbPt = ring[currentIndex]; fillBuffer->addVertex(static_cast<float>(mbPt[0])); fillBuffer->addVertex(static_cast<float>(mbPt[1])); fillBuffer->addVertex(0.0f); fillBuffer->addIndex(index++); break; } } bufferArray->addBuffer(fillBuffer); }
void GlEditLayerOverlay::fillPointElements(const std::vector<OGRRawPoint> &points, int selectedPointId) { EditPointStyle *editPointStyle = ngsDynamicCast(EditPointStyle, m_pointStyle); GlBuffer *buffer = new GlBuffer(GlBuffer::BF_PT); VectorGlObject *bufferArray = new VectorGlObject(); GlBuffer *selBuffer = new GlBuffer(GlBuffer::BF_PT); VectorGlObject *selBufferArray = new VectorGlObject(); enum ngsEditElementType elementType = (m_walkingMode) ? EET_WALK_POINT : EET_POINT; unsigned short index = 0; int pointIndex = -1; for(const OGRRawPoint &point : points) { SimplePoint pt = {static_cast<float>(point.x), static_cast<float>(point.y)}; pointIndex++; if(pointIndex == selectedPointId) { if(editPointStyle) { editPointStyle->setEditElementType(EET_SELECTED_POINT); } m_pointStyle->addPoint(pt, 0.0f, 0, selBuffer); continue; } if(buffer->vertexSize() >= GlBuffer::maxVertices()) { bufferArray->addBuffer(buffer); index = 0; buffer = new GlBuffer(GlBuffer::BF_PT); } if(editPointStyle) { editPointStyle->setEditElementType(elementType); } index = m_pointStyle->addPoint(pt, 0.0f, index, buffer); } bufferArray->addBuffer(buffer); m_elements[elementType] = GlObjectPtr(bufferArray); selBufferArray->addBuffer(selBuffer); m_elements[EET_SELECTED_POINT] = GlObjectPtr(selBufferArray); }
void GlEditLayerOverlay::fillMiddlePointElements( const std::vector<OGRRawPoint> &points) { EditPointStyle *editPointStyle = ngsDynamicCast(EditPointStyle, m_pointStyle); GlBuffer *buffer = new GlBuffer(GlBuffer::BF_PT); VectorGlObject *bufferArray = new VectorGlObject(); GlBuffer *selBuffer = new GlBuffer(GlBuffer::BF_PT); VectorGlObject *selBufferArray = new VectorGlObject(); int index = 0; size_t numPoints = points.size(); for(size_t i = 0; i < numPoints - 1; ++i) { OGRRawPoint medianPoint = ngsGetMiddlePoint(points[i], points[i + 1]); SimplePoint pt = {static_cast<float>(medianPoint.x), static_cast<float>(medianPoint.y)}; // if(isSelectedMedianPointFunc(i)) { // if(editPointStyle) { // editPointStyle->setEditElementType(EET_SELECTED_MEDIAN_POINT); // } // m_pointStyle->addPoint(pt, 0.0f, 0, selBuffer); // continue; // } if(buffer->vertexSize() >= GlBuffer::maxVertices()) { bufferArray->addBuffer(buffer); index = 0; buffer = new GlBuffer(GlBuffer::BF_PT); } if(editPointStyle) { editPointStyle->setEditElementType(EET_MEDIAN_POINT); } index = m_pointStyle->addPoint(pt, 0.0f, static_cast<unsigned short>(index), buffer); } bufferArray->addBuffer(buffer); m_elements[EET_MEDIAN_POINT] = GlObjectPtr(bufferArray); selBufferArray->addBuffer(selBuffer); m_elements[EET_SELECTED_MEDIAN_POINT] = GlObjectPtr(selBufferArray); }
/// Creates a texturable rectangle VAO and returns it's name. static GLuint makeVAO(GLuint annotationShaderProg) { GLuint vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); // Setup buffer GlBuffer buffer; buffer.bind(GL_ARRAY_BUFFER); float data[] = { // x, y, s, t -1, -1, 0, 0, // bottom-left 1, -1, 1, 0, // bottom-right -1, 1, 0, 1, // top-left 1, 1, 1, 1, }; // top-right size_t rowSize = sizeof (float) * 4; size_t xyzOffset = 0; size_t stOffset = sizeof (float) * 2; glBufferData(GL_ARRAY_BUFFER, sizeof data, data, GL_STATIC_DRAW); // Setup attributes GLint positionAttribute = glGetAttribLocation(annotationShaderProg, "position"); glVertexAttribPointer(positionAttribute, 2, GL_FLOAT, GL_FALSE, rowSize, (void*) xyzOffset); glEnableVertexAttribArray(positionAttribute); GLint texCoordAttribute = glGetAttribLocation(annotationShaderProg, "texCoord"); glVertexAttribPointer(texCoordAttribute, 2, GL_FLOAT, GL_FALSE, rowSize, (void*) stOffset); glEnableVertexAttribArray(texCoordAttribute); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); return vao; }
void View3D::initCursor(float cursorRadius, float centerPointRadius) { float r1 = cursorRadius; float r2 = r1 + cursorRadius; float s = 1.0; float cursorPoints[] = { r1*s, 0.0, 0.0, r2*s, 0.0, 0.0, -r1*s, 0.0, 0.0, -r2*s, 0.0, 0.0, 0.0, r1*s, 0.0, 0.0, r2*s, 0.0, 0.0, -r1*s, 0.0, 0.0, -r2*s, 0.0 }; m_cursorShader.reset(new ShaderProgram()); bool cursor_shader_init = m_cursorShader->setShaderFromSourceFile("shaders:cursor.glsl"); if (!cursor_shader_init) { g_logger.error("Could not read cursor shader."); return; } glGenVertexArrays(1, &m_cursorVertexArray); glBindVertexArray(m_cursorVertexArray); GlBuffer positionBuffer; positionBuffer.bind(GL_ARRAY_BUFFER); glBufferData(GL_ARRAY_BUFFER, (3) * 8 * sizeof(float), cursorPoints, GL_STATIC_DRAW); GLint positionAttribute = glGetAttribLocation(m_cursorShader->shaderProgram().programId(), "position"); glVertexAttribPointer(positionAttribute, 3, GL_FLOAT, GL_FALSE, sizeof(float)*(3), (const GLvoid *)0); glEnableVertexAttribArray(positionAttribute); glBindVertexArray(0); }
cv::GlTexture::Impl::Impl(const GlBuffer& buf, bool bgra) : tex_(0) { if (!glFuncTab()->isGlContextInitialized()) throw_nogl; int depth = buf.depth(); int cn = buf.channels(); CV_DbgAssert(buf.rows() > 0 && buf.cols() > 0); CV_Assert(cn == 1 || cn == 3 || cn == 4); CV_Assert(depth >= 0 && depth <= CV_32F); CV_Assert(buf.usage() == GlBuffer::TEXTURE_BUFFER); glGenTextures(1, &tex_); CV_CheckGlError(); CV_Assert(tex_ != 0); glBindTexture(GL_TEXTURE_2D, tex_); CV_CheckGlError(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); CV_CheckGlError(); GLenum format = cn == 1 ? GL_LUMINANCE : (cn == 3 ? (bgra ? GL_BGR : GL_RGB) : (bgra ? GL_BGRA : GL_RGBA)); buf.bind(); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); CV_CheckGlError(); glTexImage2D(GL_TEXTURE_2D, 0, cn, buf.cols(), buf.rows(), 0, format, gl_types[depth], 0); CV_CheckGlError(); buf.unbind(); }
void cv::GlTexture::Impl::copyFrom(const GlBuffer& buf, bool bgra) { CV_Assert(tex_ != 0); CV_Assert(buf.usage() == GlBuffer::TEXTURE_BUFFER); bind(); buf.bind(); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); CV_CheckGlError(); int cn = buf.channels(); GLenum format = cn == 1 ? GL_LUMINANCE : (cn == 3 ? (bgra ? GL_BGR : GL_RGB) : (bgra ? GL_BGRA : GL_RGBA)); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, buf.cols(), buf.rows(), format, gl_types[buf.depth()], 0); CV_CheckGlError(); buf.unbind(); unbind(); }
CGlDrawSkyBox() : vertex_buffer(GL_ARRAY_BUFFER), index_buffer(GL_ELEMENT_ARRAY_BUFFER), texture_cube_map(GL_TEXTURE_CUBE_MAP) { program.initVertFragShadersFromDirectory("draw/skybox"); CError_AppendReturn(program); program.link(); if (program.error()) { std::cerr << "info Log: linking: " << program.getInfoLog() << std::endl; return; } program.setupUniform(pvm_matrix_uniform, "pvm_matrix"); program.use(); program.setUniform1i("sampler_cube", 0); program.disable(); /** * initialize buffers */ /* * vertices for cube drawn counterclockwise * use quads to draw surfaces */ #define P +1.0f #define N -1.0f static const GLfloat vertices[8][3] = { {N,N,P}, {N,N,N}, {N,P,P}, {N,P,N}, {P,N,P}, {P,N,N}, {P,P,P}, {P,P,N}, }; #undef N #undef P #if 0 static const GLubyte indices[20] = { #if 0 // faces for counter clockwise triangle strips 4,6,0,2, // front 1,3, // left 5,7, // back 4,6, // right 6, // > ZERO triangle 7,2,3, // top 3,1, // > ZERO triangle 1,5,0,4 // bottom #else // faces for clockwise triangle strips 6,4,2,0, // front 3,1, // left 7,5, // back 6,4, // right 4, // > ZERO triangle 5,0,1, // bottom 1,3, // > ZERO triangle 3,7,2,6, // top #endif }; #else static const GLubyte indices[20] = { #if 0 // faces for counter clockwise triangle strips 4,6,0,2, // front 1,3, // left 5,7, // back 4,6, // right 6, // > ZERO triangle 7,2,3, // top 3,1, // > ZERO triangle 1,5,0,4 // bottom #else // faces for clockwise triangle strips 6,4,2,0, // front 3,1, // left 7,5, // back 6,4, // right 4, // > ZERO triangle 5,0,1, // bottom 1,3, // > ZERO triangle 3,7,2,6, // top #endif }; #endif vertex_buffer.bind(); vertex_buffer.data(sizeof(vertices), vertices); vertex_buffer.unbind(); index_buffer.bind(); index_buffer.data(sizeof(indices), indices); index_buffer.unbind(); }
void GlEditLayerOverlay::fillLineBuffers(const Line &line, VectorGlObject* bufferArray) { GlBuffer *buffer = new GlBuffer(GlBuffer::BF_LINE); size_t numPoints = line.size(); if(numPoints > 0) { bool isClosedLine = ngsIsNear(line.front(), line.back(), DELTA); unsigned short index = 0; Normal prevNormal; auto createBufferIfNeed = [bufferArray, &buffer, &index](size_t amount) -> void { if(!buffer->canStoreVertices(amount, true)) { bufferArray->addBuffer(buffer); index = 0; buffer = new GlBuffer(GlBuffer::BF_LINE); } }; for(size_t i = 0; i < numPoints - 1; ++i) { SimplePoint pt1 = { static_cast<float>(line[i].x), static_cast<float>(line[i].y) }; SimplePoint pt2 = { static_cast<float>(line[i + 1].x), static_cast<float>(line[i + 1].y) }; Normal normal = ngsGetNormals(pt1, pt2); if(i == 0 || i == numPoints - 2) { // Add cap if(!isClosedLine) { if(i == 0) { createBufferIfNeed(m_lineStyle->lineCapVerticesCount()); index = m_lineStyle->addLineCap( pt1, normal, 0.0f, index, buffer); } if(i == numPoints - 2) { createBufferIfNeed(m_lineStyle->lineCapVerticesCount()); Normal reverseNormal; reverseNormal.x = -normal.x; reverseNormal.y = -normal.y; index = m_lineStyle->addLineCap( pt2, reverseNormal, 0.0f, index, buffer); } } } if(i != 0) { // Add join createBufferIfNeed(m_lineStyle->lineJoinVerticesCount()); index = m_lineStyle->addLineJoin( pt1, prevNormal, normal, 0.0f, index, buffer); } createBufferIfNeed(12); index = m_lineStyle->addSegment( pt1, pt2, normal, 0.0f, index, buffer); prevNormal = normal; } } bufferArray->addBuffer(buffer); }
void TriMesh::initializeVertexGL(const char * vertArrayName, const std::vector<unsigned int>& elementInds, const char * positionAttrName, const char * normAttrName, const char * colorAttrName, const char* texCoordAttrName) { unsigned int vertexShaderId = shaderId(vertArrayName); if (!vertexShaderId) { return; } // create VBA VBO for rendering ... GLuint vertexArray; glGenVertexArrays(1, &vertexArray); glBindVertexArray(vertexArray); // store this vertex array id setVAO(vertArrayName, vertexArray); // Buffer for element indices GlBuffer elementBuffer; elementBuffer.bind(GL_ELEMENT_ARRAY_BUFFER); glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementInds.size()*sizeof(unsigned int), &elementInds[0], GL_STATIC_DRAW); // Position attribute GlBuffer positionBuffer; positionBuffer.bind(GL_ARRAY_BUFFER); glBufferData(GL_ARRAY_BUFFER, m_verts.size() * sizeof(float), &m_verts[0], GL_STATIC_DRAW); GLuint positionAttribute = glGetAttribLocation(vertexShaderId, positionAttrName); glVertexAttribPointer(positionAttribute, 3, GL_FLOAT, GL_FALSE, sizeof(float) * (3), (const GLvoid *) 0); glEnableVertexAttribArray(positionAttribute); // Normal attribute GlBuffer normalBuffer; normalBuffer.bind(GL_ARRAY_BUFFER); glBufferData(GL_ARRAY_BUFFER, m_normals.size() * sizeof(float), &m_normals[0], GL_STATIC_DRAW); GLuint normalAttribute = glGetAttribLocation(vertexShaderId, normAttrName); glVertexAttribPointer(normalAttribute, 3, GL_FLOAT, GL_FALSE, sizeof(float) * (3), (const GLvoid *) 0); glEnableVertexAttribArray(normalAttribute); // Color attribute GlBuffer colorBuffer; colorBuffer.bind(GL_ARRAY_BUFFER); if (!m_colors.empty()) { glBufferData(GL_ARRAY_BUFFER, m_colors.size() * sizeof(float), &m_colors[0], GL_STATIC_DRAW); } else { std::vector<float> tmp_colors(m_verts.size(), 1.0f); glBufferData(GL_ARRAY_BUFFER, tmp_colors.size() * sizeof(float), &tmp_colors[0], GL_STATIC_DRAW); } GLuint colorAttribute = glGetAttribLocation(vertexShaderId, colorAttrName); glVertexAttribPointer(colorAttribute, 3, GL_FLOAT, GL_FALSE, sizeof(float) * (3), (const GLvoid *) 0); glEnableVertexAttribArray(colorAttribute); // Texture coordinate GLint texcoordsLocation = glGetAttribLocation(vertexShaderId, texCoordAttrName); GlBuffer texcoordBuffer; if (texcoordsLocation != -1) { if (m_texcoords.empty()) { glDisableVertexAttribArray(texcoordsLocation); glVertexAttrib2f(texcoordsLocation, 0, 0); } else { texcoordBuffer.bind(GL_ARRAY_BUFFER); glBufferData(GL_ARRAY_BUFFER, m_texcoords.size() * sizeof(float), &m_texcoords[0], GL_STATIC_DRAW); glVertexAttribPointer(texcoordsLocation, 2, GL_FLOAT, GL_FALSE, sizeof(float) * (2), (const GLvoid *) 0); glEnableVertexAttribArray(texcoordsLocation); } } glBindVertexArray(0); }