Exemplo n.º 1
0
void Renderer::drawElementsBaseIndex(const VertexArrayObject& vao, int base, size_t first, size_t count)
{
#if !defined(ET_CONSOLE_APPLICATION)
	ET_ASSERT(vao->indexBuffer().valid());
	
	const IndexBuffer::Pointer& ib = vao->indexBuffer();

#	if (ET_OPENGLES)
	
	ET_ASSERT(vao->vertexBuffer().valid());
	
	const VertexBuffer::Pointer& vb = vao->vertexBuffer();
	RenderState& rs = _rc->renderState();
	rs.bindVertexArray(vao);
	rs.bindBuffer(vb);
	rs.setVertexAttributesBaseIndex(vb->declaration(), base);
	
	etDrawElements(primitiveTypeValue(ib->primitiveType()), static_cast<GLsizei>(count),
		dataTypeValue(ib->dataType()), ib->indexOffset(first));
	
#	else
	
	etDrawElementsBaseVertex(primitiveTypeValue(ib->primitiveType()), static_cast<GLsizei>(count),
		dataTypeValue(ib->dataType()), ib->indexOffset(first), base);
	
#	endif	
#endif
}
Exemplo n.º 2
0
    //-----------------------------------------------------------------------------------
    WireAabb::~WireAabb()
    {
        track( (MovableObject*)0 );

        VaoManager *vaoManager = mManager->getDestinationRenderSystem()->getVaoManager();

        VertexArrayObjectArray::const_iterator itor = mVaoPerLod[0].begin();
        VertexArrayObjectArray::const_iterator end  = mVaoPerLod[0].end();
        while( itor != end )
        {
            VertexArrayObject *vao = *itor;

            const VertexBufferPackedVec &vertexBuffers = vao->getVertexBuffers();
            VertexBufferPackedVec::const_iterator itBuffers = vertexBuffers.begin();
            VertexBufferPackedVec::const_iterator enBuffers = vertexBuffers.end();

            while( itBuffers != enBuffers )
            {
                vaoManager->destroyVertexBuffer( *itBuffers );
                ++itBuffers;
            }

            if( vao->getIndexBuffer() )
                vaoManager->destroyIndexBuffer( vao->getIndexBuffer() );
            vaoManager->destroyVertexArrayObject( vao );

            ++itor;
        }
    }
Exemplo n.º 3
0
void RenderState::bindVertexArray(const VertexArrayObject& vao, bool force)
{
#if !defined(ET_CONSOLE_APPLICATION)
	if (OpenGLCapabilities::instance().hasFeature(OpenGLFeature_VertexArrayObjects))
	{
		bindVertexArray(vao.valid() ? static_cast<uint32_t>(vao->apiHandle()) : 0, force);
	}
	else
	{
		if (vao.valid())
			bindBuffers(vao->vertexBuffer(), vao->indexBuffer(), force);
		else
			bindBuffers(VertexBuffer::Pointer(), IndexBuffer::Pointer(), force);
	}
#endif
}
Exemplo n.º 4
0
void render(SDLWindow& window, VertexArrayObject& triangleVAO) {

    glClear(GL_COLOR_BUFFER_BIT);

    triangleVAO.bind();
    glDrawArrays(GL_TRIANGLES, 0, 3);

    glFlush();
    window.swapBuffers();
}
Exemplo n.º 5
0
BoundVertexArrayObject GlContext::bind_vertex_array(VertexArrayObject& vao)
{
    if(m_bound_vertex_array != nullptr)
    {
        throw TargetBindError(Target::VertexArray);
    } 
    glBindVertexArray(vao.handle());
    CHECK_GL_ERROR(glBindVertexArray);
    return BoundVertexArrayObject(&vao, 
            TargetLock(&vao, &m_bound_vertex_array));
}
Exemplo n.º 6
0
void Painter::renderDebugText(const std::vector<std::string> &strings) {
    if (strings.empty()) {
        return;
    }

    gl::group group("debug text");

    glDisable(GL_DEPTH_TEST);
    glStencilFunc(GL_ALWAYS, 0xFF, 0xFF);

    useProgram(plainShader->program);
    plainShader->setMatrix(nativeMatrix);

    DebugFontBuffer debugFontBuffer;
    int line = 25;
    for (const std::string &str : strings) {
        debugFontBuffer.addText(str.c_str(), 10, line, 0.75);
        line += 20;
    }

    if (!debugFontBuffer.empty()) {
        // draw debug info
        VertexArrayObject debugFontArray;
        debugFontArray.bind(*plainShader, debugFontBuffer, BUFFER_OFFSET(0));
        plainShader->setColor(1.0f, 1.0f, 1.0f, 1.0f);
        lineWidth(4.0f * map.getState().getPixelRatio());
        glDrawArrays(GL_LINES, 0, (GLsizei)debugFontBuffer.index());
    #ifndef GL_ES_VERSION_2_0
        glPointSize(2);
        glDrawArrays(GL_POINTS, 0, (GLsizei)debugFontBuffer.index());
    #endif
        plainShader->setColor(0.0f, 0.0f, 0.0f, 1.0f);
        lineWidth(2.0f * map.getState().getPixelRatio());
        glDrawArrays(GL_LINES, 0, (GLsizei)debugFontBuffer.index());
    }

    glEnable(GL_DEPTH_TEST);
}
Exemplo n.º 7
0
void RasterBucket::drawRaster(RasterShader& shader, StaticVertexBuffer &vertices, VertexArrayObject &array) {
    raster.bind(true);
    shader.u_image = 0;
    array.bind(shader, vertices, BUFFER_OFFSET(0));
    MBGL_CHECK_ERROR(glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertices.index()));
}
Exemplo n.º 8
0
void RasterBucket::drawRaster(RasterShader& shader, StaticVertexBuffer &vertices, VertexArrayObject &array, gl::ObjectStore& store) {
    raster.bind(true, store);
    array.bind(shader, vertices, BUFFER_OFFSET_0, store);
    MBGL_CHECK_ERROR(glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertices.index()));
}
Exemplo n.º 9
0
void RasterBucket::drawRaster(RasterShader& shader, StaticVertexBuffer &vertices, VertexArrayObject &array, GLuint texture_) {
    raster.bind(texture_);
    shader.u_image = 0;
    array.bind(shader, vertices, BUFFER_OFFSET(0));
    glDrawArrays(GL_TRIANGLES, 0, (GLsizei)vertices.index());
}
Exemplo n.º 10
0
int main() {

    try {

        SDLManager sdl{SDL_INIT_TIMER};
        sdl.setOpenGLVersion(3, 3);
        auto window = sdl.createWindow("Harken", 1024, 768);

        glewExperimental = true;
        const auto glewResult = glewInit();
        if (glewResult != GLEW_OK) {
            throw std::runtime_error{StringBuilder{} << "Could not initialise GLEW. Error: " << glewGetErrorString(glewResult)};
        }

        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

        VertexArrayObject triangleVAO;
        triangleVAO.bind();

        VertexBufferObject arrayBuffer{GL_ARRAY_BUFFER};
        arrayBuffer.bind();

        GLfloat vertices[3][2] = {
            { 0.0f,  0.433f},
            { 0.5f, -0.433f},
            {-0.5f, -0.433f}
        };

        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

        const auto vertexShader   = std::make_shared<Shader>(GL_VERTEX_SHADER, "uniform-scale.vert");
        const auto fragmentShader = std::make_shared<Shader>(GL_FRAGMENT_SHADER, "red.frag");

        ShaderProgram shaderProgram{vertexShader, fragmentShader};
        shaderProgram.use();

        glVertexAttribPointer(PositionAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
        glEnableVertexAttribArray(PositionAttrib);

        const auto scaleLocation = shaderProgram.uniformLocation("scale");

        SDL_AddTimer(1000 / 60, update, nullptr);

        auto running = true;
        while (running) {

            // TODO: move this into SDLManager

            SDL_Event event;
            while (SDL_PollEvent(&event)) {

                if (event.type == SDL_QUIT) {
                    running = false;
                }
            }

            glUniform1f(scaleLocation, std::sin(scale.load()));
            render(window, triangleVAO);
        }
    }
    catch (const std::exception& ex) {
        std::cout << ex.what() << std::endl;
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}
Exemplo n.º 11
0
int main(void)
{
    GLFWwindow* window;

    // Initialize the library
    if (!glfwInit())
        return -1;

    // Activate supersampling
    glfwWindowHint(GLFW_SAMPLES, 8);

    // Ensure that we get at least a 3.2 context
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);

    // On apple we have to load a core profile with forward compatibility
#ifdef __APPLE__
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

    // Create a windowed mode window and its OpenGL context
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    // Make the window's context current
    glfwMakeContextCurrent(window);

    #ifndef __APPLE__
      glewExperimental = true;
      GLenum err = glewInit();
      if(GLEW_OK != err)
      {
        /* Problem: glewInit failed, something is seriously wrong. */
       fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
      }
      glGetError(); // pull and savely ignonre unhandled errors like GL_INVALID_ENUM
      fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
    #endif

    int major, minor, rev;
    major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
    minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
    rev = glfwGetWindowAttrib(window, GLFW_CONTEXT_REVISION);
    printf("OpenGL version recieved: %d.%d.%d\n", major, minor, rev);
    printf("Supported OpenGL is %s\n", (const char*)glGetString(GL_VERSION));
    printf("Supported GLSL is %s\n", (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));

    // Initialize the VAO
    // A Vertex Array Object (or VAO) is an object that describes how the vertex
    // attributes are stored in a Vertex Buffer Object (or VBO). This means that
    // the VAO is not the actual object storing the vertex data,
    // but the descriptor of the vertex data.
    VertexArrayObject VAO;
    VAO.init();
    VAO.bind();

    // Initialize the VBO with the vertices data
    // A VBO is a data container that lives in the GPU memory
    VBO.init();

    V.resize(2,6);
    V <<
    0,  0.5, -0.5, 0.1,  0.6, -0.4,
    0.5, -0.5, -0.5, 0.6, -0.4, -0.4;
    VBO.update(V);

    // Initialize the OpenGL Program
    // A program controls the OpenGL pipeline and it must contains
    // at least a vertex shader and a fragment shader to be valid
    Program program;
    const GLchar* vertex_shader =
            "#version 150 core\n"
                    "in vec2 position;"
                    "void main()"
                    "{"
                    "    gl_Position = vec4(position, 0.0, 1.0);"
                    "}";
    const GLchar* fragment_shader =
            "#version 150 core\n"
                    "out vec4 outColor;"
                    "uniform vec4 triangleColor;"
                    "void main()"
                    "{"
                    "    outColor = vec4(triangleColor);"
                    "}";

    // Compile the two shaders and upload the binary to the GPU
    // Note that we have to explicitly specify that the output "slot" called outColor
    // is the one that we want in the fragment buffer (and thus on screen)
    program.init(vertex_shader,fragment_shader,"outColor");
    program.bind();

    // The vertex shader wants the position of the vertices as an input.
    // The following line connects the VBO we defined above with the position "slot"
    // in the vertex shader
    program.bindVertexAttribArray("position",VBO);

    // Register the keyboard callback
    glfwSetKeyCallback(window, key_callback);

    // Register the mouse callback
    glfwSetMouseButtonCallback(window, mouse_button_callback);

    // Update viewport
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    // Loop until the user closes the window
    while (!glfwWindowShouldClose(window))
    {
        // Bind your VAO (not necessary if you have only one)
        VAO.bind();

        // Bind your program
        program.bind();

        // Clear the framebuffer
        glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Enable blending test
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

        // Draw a triangle (red)
        glUniform4f(program.uniform("triangleColor"), 1.0f, 0.0f, 0.0f, 1.0f);
        glDrawArrays(GL_TRIANGLES, 0, 3);

        // Draw a triangle (green)
        glUniform4f(program.uniform("triangleColor"), 0.0f, 1.0f, 0.0f, 0.5f);
        glDrawArrays(GL_TRIANGLES, 3, 3);

        // Swap front and back buffers
        glfwSwapBuffers(window);

        // Poll for and process events
        glfwPollEvents();
    }

    // Deallocate opengl memory
    program.free();
    VAO.free();
    VBO.free();

    // Deallocate glfw internals
    glfwTerminate();
    return 0;
}