Ejemplo n.º 1
0
void TopazSample::drawModel(GLenum mode, NvGLSLProgram& program, TopazGLModel& model)
{
	glVertexAttribFormat(VERTEX_POS, 3, GL_FLOAT, GL_FALSE, model.getModel()->getCompiledPositionOffset());

	glVertexAttribBinding(VERTEX_POS, 0);
	glEnableVertexAttribArray(VERTEX_POS);

	program.enable();

	program.bindTextureRect("pattern", 0, brushStyle->getTextureId());

	glBindBufferRange(GL_UNIFORM_BUFFER, UBO_OBJECT, model.getBufferID("ubo"), 0, sizeof(ObjectData));

	glBindVertexBuffer(0, model.getBufferID("vbo"), 0, model.getModel()->getCompiledVertexSize() * sizeof(float));
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model.getBufferID("ibo"));
	glDrawElements(mode, model.getModel()->getCompiledIndexCount(NvModelPrimType::TRIANGLES), GL_UNSIGNED_INT, nullptr);
	
	if (model.cornerPointsExists())
	{
		glBindBufferRange(GL_UNIFORM_BUFFER, UBO_OBJECT, model.getCornerBufferID("ubo"), 0, sizeof(ObjectData));

		glBindVertexBuffer(0, model.getCornerBufferID("vbo"), 0, sizeof(nv::vec3f));
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, model.getCornerBufferID("ibo"));

		glDrawElements(GL_LINE_STRIP, model.getCornerIndices().size(), GL_UNSIGNED_INT, nullptr);
	}
	
	program.disable();

	glDisableVertexAttribArray(VERTEX_POS);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
	glBindVertexBuffer(0, 0, 0, 0);
}
Ejemplo n.º 2
0
void TopazSample::updateCommandListState()
{
	enum StateObjects
	{
		STATE_DRAW,
		STATE_GEOMETRY_DRAW,
		STATE_LINES_DRAW,
		STATES_COUNT
	};

	if (cmdlist.state.programIncarnation != cmdlist.captured.programIncarnation)
	{
			glBindFramebuffer(GL_FRAMEBUFFER, fbos.scene);

			glEnable(GL_DEPTH_TEST);

			glEnable(GL_POLYGON_OFFSET_FILL);
			glPolygonOffset(1, 1);

			glEnableVertexAttribArray(VERTEX_POS);
			glVertexAttribFormat(VERTEX_POS, 3, GL_FLOAT, GL_FALSE, models.at(0)->getModel()->getCompiledPositionOffset());
			glVertexAttribBinding(VERTEX_POS, 0);

			 glBindVertexBuffer(0, 0, 0, models.at(0)->getModel()->getCompiledVertexSize() * sizeof(float));

			if (hwsupport)
			{
				glBufferAddressRangeNV(GL_VERTEX_ATTRIB_ARRAY_ADDRESS_NV, 0, 0, 0);
				glBufferAddressRangeNV(GL_ELEMENT_ARRAY_ADDRESS_NV, 0, 0, 0);
				glBufferAddressRangeNV(GL_UNIFORM_BUFFER_ADDRESS_NV, UBO_OBJECT, 0, 0);
				glBufferAddressRangeNV(GL_UNIFORM_BUFFER_ADDRESS_NV, UBO_SCENE, 0, 0);
			}
			
			shaderPrograms["draw"]->enable();
			glStateCaptureNV(cmdlist.stateObjects[STATE_DRAW], GL_TRIANGLES);

			glBindVertexBuffer(0, 0, 0, sizeof(nv::vec3f));

			glStateCaptureNV(cmdlist.stateObjects[STATE_LINES_DRAW], GL_LINES);

			glDisableVertexAttribArray(VERTEX_POS);
			glBindFramebuffer(GL_FRAMEBUFFER, 0);

			glDisable(GL_DEPTH_TEST);
	}

	if (hwsupport && (
		cmdlist.state.programIncarnation != cmdlist.captured.programIncarnation ||
		cmdlist.state.fboIncarnation != cmdlist.captured.fboIncarnation))
	{
		NVTokenSequence &seq = cmdlist.tokenSequenceList;
		glCommandListSegmentsNV(cmdlist.tokenCmdList, 1);
		glListDrawCommandsStatesClientNV(cmdlist.tokenCmdList, 0, (const void**)&seq.offsets[0], &seq.sizes[0], &seq.states[0], &seq.fbos[0], int(seq.states.size()));
		glCompileCommandListNV(cmdlist.tokenCmdList);
	}
	
	cmdlist.captured = cmdlist.state;
}
Ejemplo n.º 3
0
void GLVertexArrayObject::attachBuffer( GLuint bindingIndex,
    GLBufferObject* pBuffer, GLintptr offset, GLsizei stride )
{
    assert( stride != 0 );
    assert( stride <= maxVertexAttributeStride() );
    glBindVertexBuffer( bindingIndex, pBuffer->id(), offset, stride );
}
Ejemplo n.º 4
0
    bool render()
    {
        glm::vec2 WindowSize(this->getWindowSize());

        {
            float Aspect = (WindowSize.x * 0.33f) / (WindowSize.y * 0.50f);
            glm::mat4 Projection = glm::perspective(glm::pi<float>() * 0.25f, Aspect, 0.1f, 100.0f);
            glm::mat4 MVP = Projection * this->view() * glm::mat4(1.0f);

            *UniformPointer = MVP;
        }

        glClearBufferfv(GL_COLOR, 0, &glm::vec4(1.0f, 0.5f, 0.0f, 1.0f)[0]);

        glBindProgramPipeline(PipelineName);
        glBindBuffersBase(GL_UNIFORM_BUFFER, semantic::uniform::TRANSFORM0, 1, &BufferName[buffer::TRANSFORM]);
        glBindTextures(semantic::sampler::DIFFUSE, 1, &TextureName);
        glBindVertexArray(VertexArrayName);
        glBindVertexBuffer(semantic::buffer::STATIC, BufferName[buffer::VERTEX], 0, GLsizei(sizeof(vertex)));

        for(std::size_t Index = 0; Index < viewport::MAX; ++Index)
        {
            glViewportIndexedf(0,
                               Viewport[Index].x,
                               Viewport[Index].y,
                               Viewport[Index].z,
                               Viewport[Index].w);

            glBindSamplers(0, 1, &SamplerName[Index]);
            glDrawArraysInstanced(GL_TRIANGLES, 0, VertexCount, 1);
        }

        return true;
    }
void ReplayRenderWidget::paintGL()
{
   // Set up some needed GL state
   glColorMaski(0, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
   glDisablei(GL_BLEND, 0);
   glDisable(GL_DEPTH_TEST);
   glDisable(GL_STENCIL_TEST);
   glDisable(GL_SCISSOR_TEST);
   glDisable(GL_CULL_FACE);

   // Clear screen
   glClearColor(0.6f, 0.2f, 0.2f, 1.0f);
   glClear(GL_COLOR_BUFFER_BIT);

   // Draw displays

   // Setup screen draw shader
   glBindVertexArray(mVertArray);
   glBindVertexBuffer(0, mVertBuffer, 0, 4 * sizeof(GLfloat));
   glBindProgramPipeline(mPipeline);

   // Draw screen quad
   glBindSampler(0, mSampler);
   glBindTextureUnit(0, mTvBuffer);

   glDrawArrays(GL_TRIANGLES, 0, 6);
}
 void TransparencyManagerOITClosestArray::drawQuad()
 {
   glEnableVertexAttribArray( 0 );
   glVertexAttribFormat( 0, 2, GL_FLOAT, GL_FALSE, 0 );
   glVertexAttribBinding( 0, 0 );
   glBindVertexBuffer( 0, m_fullScreenQuad->getGLId(), 0, 8 );
   glDrawArrays( GL_QUADS, 0, 4 );
 }
	bool render()
	{
		glm::vec2 WindowSize(this->getWindowSize());

		{
			glm::mat4 Model = glm::mat4(1.0f);
			glm::mat4 Projection = glm::perspective(glm::pi<float>() * 0.25f, WindowSize.x / WindowSize.y, 0.1f, 100.0f);
			glm::mat4 MVP = Projection * this->view() * Model;

			*UniformPointer = MVP;
		}

		// Set the display viewport
		glViewportIndexedf(0, 0.0f, 0.0f, WindowSize.x, WindowSize.y);

		// Clear color buffer
		glClearBufferfv(GL_COLOR, 0, &glm::vec4(0.0f, 0.0f, 0.0f, 1.0f)[0]);

		// First draw, capture the attributes
		// Disable rasterisation, vertices processing only!
		glEnable(GL_RASTERIZER_DISCARD);

		glBindProgramPipeline(PipelineName[program::TRANSFORM]);
		glBindBufferBase(GL_UNIFORM_BUFFER, semantic::uniform::TRANSFORM0, BufferName[buffer::TRANSFORM]);
		glBindVertexArray(VertexArrayName[program::TRANSFORM]);
		glBindVertexBuffer(semantic::buffer::STATIC, BufferName[buffer::VERTEX], 0, sizeof(glm::vec4));

		glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, FeedbackName);
		glBeginTransformFeedback(GL_TRIANGLES);
			glDrawArraysInstanced(GL_TRIANGLES, 0, VertexCount, 1);
		glEndTransformFeedback();
		glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, 0);

		glDisable(GL_RASTERIZER_DISCARD);

		// Second draw, reuse the captured attributes
		glBindProgramPipeline(PipelineName[program::FEEDBACK]);
		glBindVertexArray(VertexArrayName[program::FEEDBACK]);
		glBindVertexBuffer(semantic::buffer::STATIC, BufferName[buffer::FEEDBACK], 0, sizeof(glf::vertex_v4fc4f));
	
		glDrawTransformFeedback(GL_TRIANGLES, FeedbackName);

		return true;
	}
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
void RendererStandard::displayGrid(const InertiaCamera& camera, const mat4f projection)
{
	//
	// Update what is inside buffers
	//
	g_globalMatrices.mVP = projection * camera.m4_view;
	g_globalMatrices.mW = mat4f(array16_id);
	glNamedBufferSubDataEXT(g_uboMatrix.Id, 0, sizeof(g_globalMatrices), &g_globalMatrices);
	//
	// The cross vertex change is an example on how command-list are compatible with changing
	// what is inside the vertex buffers. VBOs are outside of the token buffers...
	//
	const vec3f& p = camera.curFocusPos;
	vec3f crossVtx[6] = {
		vec3f(p.x - CROSSSZ, p.y, p.z), vec3f(p.x + CROSSSZ, p.y, p.z),
		vec3f(p.x, p.y - CROSSSZ, p.z), vec3f(p.x, p.y + CROSSSZ, p.z),
		vec3f(p.x, p.y, p.z - CROSSSZ), vec3f(p.x, p.y, p.z + CROSSSZ),
	};
	glNamedBufferSubDataEXT(s_vboCross, 0, sizeof(vec3f)* 6, crossVtx);
	// ------------------------------------------------------------------------------------------
	// Case of regular rendering
	//
	s_shaderGrid.bindShader();
	glEnableVertexAttribArray(0);
	glDisableVertexAttribArray(1);

    // --------------------------------------------------------------------------------------
	// Using regular VBO
	//
	glBindBufferBase(GL_UNIFORM_BUFFER, UBO_MATRIX, g_uboMatrix.Id);
	glBindVertexBuffer(0, s_vboGrid, 0, sizeof(vec3f));
	glVertexAttribFormat(0, 3, GL_FLOAT, GL_FALSE, 0);
	//
	// Draw!
	//
	glDrawArrays(GL_LINES, 0, GRIDDEF * 4);

	glBindVertexBuffer(0, s_vboCross, 0, sizeof(vec3f));
	glDrawArrays(GL_LINES, 0, 6);

    glDisableVertexAttribArray(0);
	//s_shaderGrid.unbindShader();
}
void CadScene::enableVertexFormat(int attrPos, int attrNormal)
{
  glVertexAttribFormat(attrPos,    3,GL_FLOAT,GL_FALSE,0);
  glVertexAttribFormat(attrNormal, 3,GL_FLOAT,GL_FALSE,offsetof(CadScene::Vertex,normal));
  glVertexAttribBinding(attrPos,0);
  glVertexAttribBinding(attrNormal,0);
  glEnableVertexAttribArray(attrPos);
  glEnableVertexAttribArray(attrNormal);
  glBindVertexBuffer(0,0,0,sizeof(CadScene::Vertex));
}
Ejemplo n.º 10
0
static bool drawTiles(ShovelerDrawable *tilesDrawable)
{
	Tiles *tiles = tilesDrawable->data;

	glBindVertexArray(tiles->vertexArrayObject);
	glBindVertexBuffer(0, tiles->vertexBuffer, 0, sizeof(TilesVertex));
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, tiles->indexBuffer);
	glDrawElements(GL_TRIANGLES, 2 * 3 * tiles->width * tiles->height, GL_UNSIGNED_INT, NULL);

	return shovelerOpenGLCheckSuccess();
}
Ejemplo n.º 11
0
static void glnvg__renderFlush(void* uptr)
{
    PROFILER_CPU_TIMESLICE("NVG backend flush");

    GLNVGcontext* gl = (GLNVGcontext*)uptr;
    int i;

    if (gl->ncalls > 0) {

        // Setup require GL state.
        GLuint prg = (gl->flags & NVG_ANTIALIAS) ? gfx_res::prgNanoVGAA : gfx_res::prgNanoVG;
        glUseProgram(prg);

        glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
        glEnable(GL_CULL_FACE);
        glCullFace(GL_BACK);
        glFrontFace(GL_CCW);
        glEnable(GL_BLEND);
        glDisable(GL_DEPTH_TEST);
        glDisable(GL_SCISSOR_TEST);
        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
        glStencilMask(0xffffffff);
        glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
        glStencilFunc(GL_ALWAYS, 0, 0xffffffff);

        // Upload vertex data
        glBindVertexArray(vf::p2uv2_vertex_t::vao);
        glBindVertexBuffer(0, gfx::dynBuffer, 0, sizeof(NVGvertex));

        // Set view and texture just once per frame.
        glProgramUniform2fv(prg, GFX_NVG_LOC_VIEWSIZE, 1, gl->view);

        for (i = 0; i < gl->ncalls; i++) {
            GLNVGcall* call = &gl->calls[i];
            if (call->type == GLNVG_FILL)
                glnvg__fill(gl, call);
            else if (call->type == GLNVG_CONVEXFILL)
                glnvg__convexFill(gl, call);
            else if (call->type == GLNVG_STROKE)
                glnvg__stroke(gl, call);
            else if (call->type == GLNVG_TRIANGLES)
                glnvg__triangles(gl, call);
        }

        glBindVertexArray(0);
        glUseProgram(0);
    }

    // Reset calls
    gl->npaths = 0;
    gl->ncalls = 0;
}
Ejemplo n.º 12
0
void _004_lua_test::init() {
  m_shader.init();
  m_vao.init();

  // init buffer with stuff
  core::t_vec4f vertices[] = {
    {-1.0, -1.0, -1.0, 1.0},
    { 1.0, -1.0, -1.0, 1.0},
    { 0.0,  1.0, -1.0, 1.0}
  };

  m_buffer.init();
  m_buffer.data(ATTRIBUTES::POSITION, sizeof(vertices), 0, GL_STATIC_DRAW);
  void* memory = m_buffer.map(ATTRIBUTES::POSITION, GL_WRITE_ONLY);
  std::memcpy(memory, vertices, sizeof(vertices));
  m_buffer.unmap(ATTRIBUTES::POSITION);

  // init vao with attrib formats
  {
    auto v = m_vao.activate();

    // setup attribute format
    glVertexAttribBinding(ATTRIBUTES::POSITION, BINDINGS::PRIMARY);
    glVertexAttribFormat(ATTRIBUTES::POSITION, 4, GL_FLOAT, GL_FALSE, 0);

    // enable attrib array
    glEnableVertexAttribArray(ATTRIBUTES::POSITION);

    // bind buffer
    glBindVertexBuffer(BINDINGS::PRIMARY, m_buffer.get_name(BUFFER::POSITION), 0, sizeof(core::t_vec4f));

    // order between the 3 steps does not matter, but every step has to exist.
  }

  glClearColor(0.2f, 0.2f, 0.23f, 0.0f);

  m_lua_state = luaL_newstate();
  luaL_openlibs(m_lua_state);

  std::cout << "start run lua file" << std::endl;
  luaL_dofile(m_lua_state, "data/script/manipulator.lua");
  std::cout << "stop run lua file" << std::endl;
  lua_close(m_lua_state);
}
bool initVertexArray()
{
	GLuint Bindingindex(0);

	glGenVertexArrays(1, &VertexArrayName);
    glBindVertexBuffer( Bindingindex, BufferName[buffer::VERTEX], 0, sizeof(glf::vertex_v2fv2f));

    glVertexAttribBinding( glf::semantic::attr::POSITION, Bindingindex);
    glVertexAttribFormat( glf::semantic::attr::POSITION, 2, GL_FLOAT, GL_FALSE, 0);

    glVertexAttribBinding( glf::semantic::attr::TEXCOORD, Bindingindex);
    glVertexAttribFormat( glf::semantic::attr::TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec2));

    glEnableVertexAttribArray( glf::semantic::attr::POSITION);
    glEnableVertexAttribArray( glf::semantic::attr::TEXCOORD);


	return true;
}
	bool initVertexArray()
	{
		glGenVertexArrays(1, &VertexArrayName);
		glBindVertexArray(VertexArrayName);
			glVertexAttribBinding(semantic::attr::POSITION, 0);
			glVertexAttribFormat(semantic::attr::POSITION, 2, GL_FLOAT, GL_FALSE, 0);

			glVertexAttribBinding(semantic::attr::TEXCOORD, 0);
			glVertexAttribFormat(semantic::attr::TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec2));

			glEnableVertexAttribArray(semantic::attr::POSITION);
			glEnableVertexAttribArray(semantic::attr::TEXCOORD);

			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, BufferName[buffer::ELEMENT]);
			glBindVertexBuffer(0, BufferName[buffer::VERTEX], 0, sizeof(glf::vertex_v2fv2f));
		glBindVertexArray(0);

		return true;
	}
void opengl_array_state::BindVertexBuffer(GLuint bindingindex, GLuint buffer, GLintptr offset, GLsizei stride) {
	if (bindingindex >= vertex_buffer_bindings.size()) {
		// Make sure that we have the place for this information
		vertex_buffer_bindings.resize(bindingindex + 1);
	}

	auto& bindingInfo = vertex_buffer_bindings[bindingindex];

	if (bindingInfo.valid_data && bindingInfo.buffer == buffer && bindingInfo.offset == offset
		&& bindingInfo.stride == stride) {
		return;
	}

	glBindVertexBuffer(bindingindex, buffer, offset, stride);

	bindingInfo.valid_data = true;
	bindingInfo.buffer = buffer;
	bindingInfo.stride = stride;
	bindingInfo.offset = offset;
}
Ejemplo n.º 16
0
	void draw()
	{
		VK_CHECK_RESULT(Swapchain.acquire(presentCompleteSemaphore, &currentBuffer));
		VK_CHECK_RESULT(vkWaitForFences(device, 1, &waitFences[currentBuffer], VK_TRUE, UINT64_MAX));
		VK_CHECK_RESULT(vkResetFences(device, 1, &waitFences[currentBuffer]));

		glo::context* Context = (glo::context*)this->Context;
		Context->temp_set_framebuffer(frameBuffers[currentBuffer]);
		Context->temp_set_renderpass(renderPass, 0, 0, width, height);

		wglMakeCurrentGTC(this->DeviceContext, this->Context);

		VkCommandBuffer CommandBuffer = Context->temp_get_command_buffer();

		glViewportIndexedf(0, 0, 0, width, height);
		glScissor(0, 0, width, height);

		vkCmdBindPipeline(CommandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline);

		glBindBufferRange(GL_UNIFORM_BUFFER, 0, uniformDataVS.buffer, 0, sizeof(uboVS));
		glBindVertexBuffer(VERTEX_BUFFER_BIND_ID, vertices.buffer, 0, 0);
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices.buffer);

		glDrawElementsInstancedBaseVertexBaseInstance(GL_TRIANGLES, indices.count, GL_UNSIGNED_INT, NULL, 1, 0, 0);

		glFlush();

		VkPipelineStageFlags waitStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
		VkSubmitInfo submitInfo = {};
		submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
		submitInfo.pWaitDstStageMask = &waitStageMask;
		submitInfo.pWaitSemaphores = &presentCompleteSemaphore;
		submitInfo.waitSemaphoreCount = 1;
		submitInfo.pSignalSemaphores = &renderCompleteSemaphore;
		submitInfo.signalSemaphoreCount = 1;
		submitInfo.pCommandBuffers = &CommandBuffer;
		submitInfo.commandBufferCount = 1;

		VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, waitFences[currentBuffer]));
		VK_CHECK_RESULT(Swapchain.present(queue, currentBuffer, renderCompleteSemaphore));
	}
Ejemplo n.º 17
0
void display()
{
	{
		// Compute the MVP (Model View Projection matrix)
		float Aspect = (Window.Size.x * 0.33f) / (Window.Size.y * 0.50f);
		glm::mat4 Projection = glm::perspective(45.0f, Aspect, 0.1f, 100.0f);
		glm::mat4 ViewTranslateZ = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -Window.TranlationCurrent.y));
		glm::mat4 ViewRotateX = glm::rotate(ViewTranslateZ, Window.RotationCurrent.y, glm::vec3(1.f, 0.f, 0.f));
		glm::mat4 ViewRotateY = glm::rotate(ViewRotateX, Window.RotationCurrent.x, glm::vec3(0.f, 1.f, 0.f));
		glm::mat4 View = ViewRotateY;
		glm::mat4 Model = glm::mat4(1.0f);
		glm::mat4 MVP = Projection * View * Model;

		*UniformPointer = MVP;
	}

	glFlushMappedBufferRange(GL_UNIFORM_BUFFER, 0, sizeof(glm::mat4));

	glClearBufferfv(GL_COLOR, 0, &glm::vec4(1.0f, 0.5f, 0.0f, 1.0f)[0]);

	glBindProgramPipeline(PipelineName);
	glBindBuffersBase(GL_UNIFORM_BUFFER, glf::semantic::uniform::TRANSFORM0, 1, &BufferName[buffer::TRANSFORM]);
	glBindTextures(glf::semantic::sampler::DIFFUSE, 1, &TextureName);
	glBindVertexArray(VertexArrayName);
	glBindVertexBuffer(glf::semantic::buffer::STATIC, BufferName[buffer::VERTEX], 0, GLsizei(sizeof(vertex)));

	for(std::size_t Index = 0; Index < viewport::MAX; ++Index)
	{
		glViewportIndexedf(0, 
			Viewport[Index].x, 
			Viewport[Index].y, 
			Viewport[Index].z, 
			Viewport[Index].w);

		glBindSamplers(0, 1, &SamplerName[Index]);
		glDrawArraysInstanced(GL_TRIANGLES, 0, VertexCount, 1);
	}

	glf::checkError("display");
	glf::swapBuffers();
}
Ejemplo n.º 18
0
bool EffectSkybox::initEffect()
{
	program = XRShaderManger::getShaderProgram(XRShaderManger::XR_SHADER_PROGRAM_ENVIRONMENT_MAPPING);

#pragma endregion

	//create and initialize vao
	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);

#pragma region setup position attribute
	{
		//create and initialize position vbo_pos
		glGenBuffers(1, &vbo_pos);
		glBindBuffer(GL_ARRAY_BUFFER, vbo_pos);
		XRMesh* mesh = (XRMesh*)object->getComponent(XR_COMPONENT_MESH);
		glBufferStorage(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * mesh->vertexNum, mesh->positions, GL_MAP_WRITE_BIT);

		//binding vertex attribute with vertex buffer object
		glVertexAttribBinding(VPOS, 0);
		glBindVertexBuffer(0, vbo_pos, 0, sizeof(GLfloat) * 3);
		glVertexAttribFormat(VPOS, 3, GL_FLOAT, GL_FALSE, 0);
		glEnableVertexAttribArray(VPOS);
	}

#pragma endregion


#pragma endregion

	//by now everything is recorded in the vao
	//so we will unbind the buffer
	//and unbind the vao
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindVertexArray(0);

	return true;
}
Ejemplo n.º 19
0
void StateSystem::VertexFormatState::applyGL(GLbitfield changedFormat, GLbitfield changedBinding) const
{
  for (GLuint i = 0; i < MAX_VERTEXATTRIBS; i++){
    if (!isBitSet(changedFormat,i)) continue;

    switch(formats[i].mode){
    case VERTEXMODE_FLOAT:
      glVertexAttribFormat(i, formats[i].size, formats[i].type, formats[i].normalized, formats[i].relativeoffset);
      break;
    case VERTEXMODE_INT:
    case VERTEXMODE_UINT:
      glVertexAttribIFormat(i, formats[i].size, formats[i].type, formats[i].relativeoffset);
      break;
    }
    glVertexAttribBinding(i,formats[i].binding);
  }

  for (GLuint i = 0; i < MAX_VERTEXBINDINGS; i++){
    if (!isBitSet(changedBinding,i)) continue;

    glVertexBindingDivisor(i,bindings[i].divisor);
    glBindVertexBuffer(i,0,0,bindings[i].stride);
  }
}
void CadScene::disableVertexFormat(int attrPos, int attrNormal)
{
  glDisableVertexAttribArray(attrPos);
  glDisableVertexAttribArray(attrNormal);
  glBindVertexBuffer(0,0,0,sizeof(CadScene::Vertex));
}
Ejemplo n.º 21
0
void display()
{
	{
		// Compute the MVP (Model View Projection matrix)
		float Aspect = (Window.Size.x * 0.50f) / (Window.Size.y * 0.50f);
		glm::mat4 Projection = glm::perspective(45.0f, Aspect, 0.1f, 100.0f);
		glm::mat4 ViewTranslateZ = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -Window.TranlationCurrent.y));
		glm::mat4 ViewRotateX = glm::rotate(ViewTranslateZ, Window.RotationCurrent.y, glm::vec3(1.f, 0.f, 0.f));
		glm::mat4 ViewRotateY = glm::rotate(ViewRotateX, Window.RotationCurrent.x, glm::vec3(0.f, 1.f, 0.f));
		glm::mat4 View = ViewRotateY;
		glm::mat4 Model = glm::mat4(1.0f);
		glm::mat4 MVP = Projection * View * Model;

		*UniformPointer = MVP;
	}

	glFlushMappedBufferRange(GL_UNIFORM_BUFFER, 0, sizeof(glm::mat4));

	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LESS);

	glViewportIndexedf(0, 0, 0, GLfloat(Window.Size.x), GLfloat(Window.Size.y));

	glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
	float Depth(1.0f);
    glm::u8vec4 ColorClear(255, 127, 0, 255);
    glm::u8vec4 ColorTex(0, 127, 255, 255);
	glClearBufferfv(GL_DEPTH , 0, &Depth);
	glClearTexImage(TextureName[texture::COLORBUFFER], 0, 
		GL_RGBA, GL_UNSIGNED_BYTE, &ColorClear);
	glClearTexSubImage(TextureName[texture::COLORBUFFER], 0, 
		64, 64, 0,
		64, 64, 1,
		GL_RGBA, GL_UNSIGNED_BYTE, &ColorTex);
	glClearTexSubImage(TextureName[texture::COLORBUFFER], 0, 
		256, 0, 0,
		64, 64, 1,
		GL_RGBA, GL_UNSIGNED_BYTE, &ColorTex);
	glClearTexSubImage(TextureName[texture::COLORBUFFER], 0, 
		128, 384, 0,
		64, 64, 1,
		GL_RGBA, GL_UNSIGNED_BYTE, &ColorTex);
	glClearTexSubImage(TextureName[texture::COLORBUFFER], 0, 
		512, 256, 0,
		64, 64, 1,
		GL_RGBA, GL_UNSIGNED_BYTE, &ColorTex);


	// Bind rendering objects
	glBindProgramPipeline(PipelineName[pipeline::TEXTURE]);
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, TextureName[texture::DIFFUSE]);
	glBindVertexArray(VertexArrayName[pipeline::TEXTURE]);
	glBindVertexBuffer(glf::semantic::buffer::STATIC, BufferName[buffer::VERTEX], 0, GLsizei(sizeof(glf::vertex_v2fv2f)));
	glBindBufferBase(GL_UNIFORM_BUFFER, glf::semantic::uniform::TRANSFORM0, BufferName[buffer::TRANSFORM]);

	glDrawElementsInstancedBaseVertexBaseInstance(
		GL_TRIANGLES, ElementCount, GL_UNSIGNED_SHORT, 0, 2, 0, 0);

	glDisable(GL_DEPTH_TEST);

	glBindFramebuffer(GL_FRAMEBUFFER, 0);

	glBindProgramPipeline(PipelineName[pipeline::SPLASH]);
	glActiveTexture(GL_TEXTURE0);
	glBindVertexArray(VertexArrayName[pipeline::SPLASH]);
	glBindTexture(GL_TEXTURE_2D, TextureName[texture::COLORBUFFER]);

	glDrawArraysInstanced(GL_TRIANGLES, 0, 3, 1);

	glf::swapBuffers();
}
Ejemplo n.º 22
0
static void redraw(struct glwin *win)
{
	struct glwin_thread_state thread_state;
	glwin_get_thread_state(&thread_state);
	glwin_make_current(win, g_ctx);

	lua_State *L = g_L;

	static bool gl_init_attempted = false;
	static bool gl_init_result = false;

	if (!gl_init_attempted) {
		gl_init_result = glb_glcore_init(3, 3);
	}
	if (!gl_init_result) {
		printf("Failed to initialize OpenGL bindings\n");
		exit(-1);
		return;
	}

	glViewport(0, 0, win->width, win->height);
	glClearColor(0, 0, 0, 0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glEnable(GL_DEPTH_TEST);

	lua_getglobal(L, "b2l_data"); //1
	if (!lua_istable(L, -1)) {
		lua_pop(L, 1);
		goto end;
	}
	lua_getfield(L, -1, "objects"); //2
	lua_getglobal(L, "current_object"); //3
	if (!lua_isstring(L, -1)) {
		lua_pop(L, 3);
		goto end;
	}
	const char *current_object = lua_tostring(L, -1);
	lua_getfield(L, -2, current_object); //4
	if (lua_isnil(L, -1)) {
		lua_pop(L, 4);
		goto end;
	}

	lua_getfield(L, -1, "type"); //5
	if(strcmp(lua_tostring(L, -1), "MESH")) {
		lua_pop(L, 5);
		goto end;
	}
	lua_getfield(L, -2, "data"); //6
	lua_getfield(L, -6, "meshes"); //7
	lua_getfield(L, -1, lua_tostring(L, -2)); //8

	if (lua_isnil(L, -1)) {
		lua_pop(L, 8);
		goto end;
	}

	if (!g_gl_state.initialized)
		init_gl_state();

	if (g_gl_state.recompile_shaders)
		g_gl_state.program_valid = recompile_shaders();

	if (!g_gl_state.initialized || !g_gl_state.program_valid) {
		lua_pop(L, 8);
		goto end;
	}

	g_gl_state.recompile_shaders = false;

	glUseProgram(g_gl_state.program);
	glBindVertexArray(g_gl_state.vao);

	if (g_gl_state.blob_updated)  {
		glBufferData(GL_ARRAY_BUFFER, g_gl_state.blob_size, g_gl_state.blob ,GL_STATIC_DRAW);
		g_gl_state.blob_updated = false;
	}

	lua_getfield(L, -1, "vertex_normal_array_offset"); //9
	int vertex_normal_array_offset = lua_tointeger(L, -1);
	lua_getfield(L, -2, "uv_array_offset"); //10
	int uv_array_offset = lua_tointeger(L, -1);

	lua_getfield(L, -3, "uv_layers"); //11
	lua_len(L, -1); //12
	int num_uv_layers = lua_tointeger(L, -1);

	int tangent_array_offset;
	if (num_uv_layers > 0) {
		lua_getfield(L, -5, "tangent_array_offset");
		tangent_array_offset = lua_tointeger(L, -1);
		lua_pop(L, 1);
	}

	lua_getfield(L, -5, "vertex_co_array_offset"); //13
	int vertex_co_array_offset = lua_tointeger(L, -1);

	lua_getfield(L, -6, "weights_per_vertex"); //14
	int weights_per_vertex = lua_tointeger(L, -1);

	int weights_array_offset;
	if (weights_per_vertex > 0) {
		lua_getfield(L, -7, "weights_array_offset");
		weights_array_offset = lua_tointeger(L, -1);
		lua_pop(L, 1);
	} else {
		weights_array_offset = -1;
	}

	lua_getfield(L, -11, "vertex_groups"); //15
	int num_vertex_groups;
	if (lua_isnil(L, -1)) {
		num_vertex_groups = 0;
	} else {
		lua_len(L, -1);
		num_vertex_groups = lua_tointeger(L, -1);
		lua_pop(L, 1);
	}

	glBindVertexBuffer(NORMAL, g_gl_state.vbo, vertex_normal_array_offset, sizeof(float) * 3);
	if (num_uv_layers > 0) {
		glBindVertexBuffer(UV, g_gl_state.vbo, uv_array_offset, sizeof(float) * 2 * num_uv_layers);
		glBindVertexBuffer(TANGENT, g_gl_state.vbo, tangent_array_offset, sizeof(float) * 4);
	}
	glBindVertexBuffer(POS, g_gl_state.vbo, vertex_co_array_offset, sizeof(float) * 3);
	if (weights_per_vertex > 0)
		glBindVertexBuffer(WEIGHTS, g_gl_state.vbo, weights_array_offset, weights_per_vertex * 4);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_gl_state.vbo);
	if (g_gl_state.normal_index >= 0) {
		glEnableVertexAttribArray(g_gl_state.normal_index);
		glVertexAttribFormat(g_gl_state.normal_index, 3, GL_FLOAT, GL_FALSE, 0);
		glVertexAttribBinding(g_gl_state.normal_index, NORMAL);
	}

	if (g_gl_state.uv_index >= 0) {
		glEnableVertexAttribArray(g_gl_state.uv_index);
		glVertexAttribFormat(g_gl_state.uv_index, 2, GL_FLOAT, GL_FALSE, 0);
		glVertexAttribBinding(g_gl_state.uv_index, UV);
	}

	if (g_gl_state.pos_index >= 0) {
		glEnableVertexAttribArray(g_gl_state.pos_index);
		glVertexAttribFormat(g_gl_state.pos_index, 3, GL_FLOAT, GL_FALSE, 0);
		glVertexAttribBinding(g_gl_state.pos_index, POS);
	}
	int i = 0;
	for (i = 0; i < 6; i++) {
		if (g_gl_state.weights_index[i] >= 0 && weights_per_vertex > 0) {
			glEnableVertexAttribArray(g_gl_state.weights_index[i]);
			glVertexAttribIFormat(g_gl_state.weights_index[i], 2, GL_SHORT, 4 * i);
			glVertexAttribBinding(g_gl_state.weights_index[i], WEIGHTS);
		}
	}
	if (num_uv_layers > 0 && g_gl_state.tangent_index >= 0) {
		glEnableVertexAttribArray(g_gl_state.tangent_index);
		glVertexAttribFormat(g_gl_state.tangent_index, 4, GL_FLOAT, GL_FALSE, 0);
		glVertexAttribBinding(g_gl_state.tangent_index, TANGENT);
	}

	struct mat4 view;
	struct mat4 model;
	struct quaternion next;
	quaternion_mul(&q_delta, &q_cur, &next);
	quaternion_to_mat4(&next, &view);
	view.v[3][3] = 1;
	mat4_identity(&model);
	model.v[3][0] = g_offset[0] + g_offset_next[0];
	model.v[3][1] = g_offset[1] + g_offset_next[1];
	model.v[3][2] = g_offset[2] + g_offset_next[2];
	glUniformMatrix4fv(glGetUniformLocation(g_gl_state.program, "model"), 1, GL_FALSE, (GLfloat *)&model);
	struct mat4 ident;
	mat4_identity(&ident);
	glUniformMatrix4fv(glGetUniformLocation(g_gl_state.program, "view"), 1, GL_FALSE, (GLfloat *)&view);

	float zoom = exp(g_log_zoom);
	float zr = 100;
	struct mat4 proj;
	mat4_zero(&proj);
	proj.v[0][0] = 1.0/zoom;
	proj.v[1][1] = 1.0*win->width/(zoom*win->height);
	proj.v[2][2] = 1.0/zr;
	proj.v[3][3] = 1.0;
	glUniformMatrix4fv(glGetUniformLocation(g_gl_state.program, "proj"), 1, GL_FALSE, (GLfloat *)&proj);

	if (weights_per_vertex > 0) {
		static int render_count = 0;
		render_count++;
		double frame;
		int frame_start;
		int frame_end;
		lua_getglobal(L, "frame_start"); //16
		frame_start = lua_tointeger(L, -1);
		lua_getglobal(L, "frame_end"); //17
		frame_end = lua_tointeger(L, -1);
		lua_getglobal(L, "frame_delta"); //18
		frame = frame_start + lua_tonumber(L, -1);
		int frame_i = floorf(frame);
		double frame_fract = frame - frame_i;

		lua_getfield(L, -18, "scenes"); //19
		lua_getglobal(L, "current_scene"); //20
		lua_getfield(L, -2, lua_tostring(L, -1)); //21
		if (!lua_istable(L, -1)) {
			lua_pop(L, 20);
			goto end;
		}
		lua_getfield(L, -1, "objects");
		lua_getfield(L, -1, current_object);
		lua_getfield(L, -1, "vertex_group_transform_array_offset");
		int offset = lua_tointeger(L, -1);
		lua_pop(L, 9);
		int stride = sizeof(float) * 4 * 4 * num_vertex_groups;
		int i;
		for (i = 0; i < num_vertex_groups; i++) {
			struct mat4 res;
			struct mat4 M1;
			struct mat4 M2;
			struct mat4 *base = (struct mat4 *)(g_gl_state.blob + offset + (i * sizeof(float) * 4 * 4) + frame_i * stride);
			struct mat4 *next = (struct mat4 *)(g_gl_state.blob + offset + (i * sizeof(float) * 4 * 4) + (frame_i + 1) * stride);

			if (frame_i == (frame_end-1)) {
				next = (struct mat4 *)(g_gl_state.blob + offset + i * sizeof(float) * 4 * 4 + (frame_start) * stride);
			} else if (frame_fract == 0) {
				next = base;
			}


#if USE_SLERP
			struct mat4 temp;
			mat4_zero(&temp);
			temp.v[3][3] = 1;
			M1 = *base;
			M2 = *next;
			spherical_lerp(M1.v[0], M2.v[0], frame_fract, temp.v[0]);
			spherical_lerp(M1.v[1], M2.v[1], frame_fract, temp.v[1]);
			spherical_lerp(M1.v[2], M2.v[2], frame_fract, temp.v[2]);
			mat4_transpose(&temp, &res);
			float v1[3];
			float v2[3];
			v1[0] = M1.v[0][3];
			v1[1] = M1.v[1][3];
			v1[2] = M1.v[2][3];
			v2[0] = M2.v[0][3];
			v2[1] = M2.v[1][3];
			v2[2] = M2.v[2][3];
			lerp(v1, v2, frame_fract, res.v[3]);
#else
			mat4_zero(&res);
			res.v[3][3] = 1;
			mat4_transpose(base, &M1);
			mat4_transpose(next, &M2);
			lerp(M1.v[0], M2.v[0], frame_fract, res.v[0]);
			lerp(M1.v[1], M2.v[1], frame_fract, res.v[1]);
			lerp(M1.v[2], M2.v[2], frame_fract, res.v[2]);
			lerp(M1.v[3], M2.v[3], frame_fract, res.v[3]);
#endif

			glUniformMatrix4fv(g_gl_state.groups_index + i,
					1, /*num_vertex_groups, */
					GL_FALSE,
					(GLfloat *)&res);
		}
	}

	lua_getglobal(L, "controls"); //16
	int controls = lua_gettop(L);
	lua_getglobal(L, "materials"); //17
	int materials = lua_gettop(L);

	lua_getfield(L, -10, "index_array_offset"); //18
	int index_array_offset = lua_tointeger(L, -1);

	lua_getfield(L, -11, "submeshes"); //19
	lua_len(L, -1); //20
	int num_submeshes = lua_tointeger(L, -1);

	for (i = 0; i < num_submeshes; i++) {
		lua_rawgeti(L, -2, i + 1);
		lua_getfield(L, -1, "material_name");

		const char *material_name = lua_tostring(L, -1);

		lua_getfield(L, -2, "triangle_no");
		int triangle_no = lua_tointeger(L, -1);
		lua_getfield(L, -3, "triangle_count");
		int triangle_count = lua_tointeger(L, -1);
		lua_getfield(L, materials, material_name);
		lua_getfield(L, -1, "params");
		lua_pushnil(L);  /* first key */
		while (lua_next(L, -2)) {
			int variable = lua_gettop(L);
			const char *variable_name = lua_tostring(L, variable - 1);
			int uniform_loc = glGetUniformLocation(g_gl_state.program, variable_name);
			if (uniform_loc == -1) {
				lua_pop(L, 1);
				continue;
			}
			lua_getfield(L, variable, "value");
			int value = variable + 1;
			lua_getfield(L, variable, "datatype");
			const char *datatype = strdup(lua_tostring(L, -1));
			lua_pop(L, 1);
			if (!strcmp(datatype, "bool")) {
				int bool_value = lua_toboolean(L, value);
				glUniform1i(uniform_loc, bool_value);
			} else if (!strcmp(datatype, "vec3")) {
				lua_rawgeti(L, value, 1);
				lua_rawgeti(L, value, 2);
				lua_rawgeti(L, value, 3);
				float val[3];
				val[0] = (float)lua_tonumber(L, -3);
				val[1] = (float)lua_tonumber(L, -2);
				val[2] = (float)lua_tonumber(L, -1);
				glUniform3fv(uniform_loc, 1, val);
				lua_pop(L, 3);
			} else if (!strcmp(datatype, "float")) {
				float fval = lua_tonumber(L, value);
				glUniform1f(uniform_loc, fval);
			} else if (!strcmp(datatype, "sampler2D")) {
				lua_getfield(L, controls, variable_name);
				int control = lua_gettop(L);
				lua_getfield(L, control, "needs_upload");
				int needs_upload = lua_toboolean(L, -1);
				lua_pop(L, 1);
				if (needs_upload) {
					int texunit;
					GdkPixbuf *pbuf;
					lua_getfield(L, control, "texunit");
					texunit = lua_tointeger(L, -1) - 1;
					lua_pop(L, 1);

					lua_getfield(L, control, "pbuf");
					lua_getfield(L, -1, "_native");
					pbuf = (GdkPixbuf *)lua_touserdata(L, -1);
					lua_pop(L, 2);
					glActiveTexture(GL_TEXTURE0 + texunit);
					glBindTexture(GL_TEXTURE_2D, g_texture_names[texunit]);
					int width = gdk_pixbuf_get_width(pbuf);
					int height = gdk_pixbuf_get_height(pbuf);
					int n_chan = gdk_pixbuf_get_n_channels(pbuf);
					glPixelStorei(GL_UNPACK_ROW_LENGTH, gdk_pixbuf_get_rowstride(pbuf)/ n_chan);
					glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
					glTexImage2D(GL_TEXTURE_2D,
						0, /* level */
						n_chan > 3 ? GL_RGBA : GL_RGB,
						width,
						height,
						0, /* border */
						n_chan > 3 ? GL_RGBA : GL_RGB,
						GL_UNSIGNED_BYTE,
						gdk_pixbuf_get_pixels(pbuf));
					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
					glGenerateMipmap(GL_TEXTURE_2D);
					glUniform1i(uniform_loc, texunit);

					lua_pushboolean(L, 0);
					lua_setfield(L, control, "needs_upload");
				}
				lua_pop(L, 1);
			}
			free((void *)datatype);
			lua_pop(L, 2);
		} //while (lua_next(L, -2) != 0)
		glDrawElements(GL_TRIANGLES,
				3 * triangle_count,
				GL_UNSIGNED_SHORT,
				(void *)((int64_t)index_array_offset) + 3 * 2 * triangle_no);
		lua_pop(L, 6);
	}
	lua_pop(L, 20);
end:
	{
		GLenum err = glGetError();
		if (err)
			printf("render_scene GL error = %d\n", err);
	}
	glwin_swap_buffers(g_win);
	glwin_set_thread_state(&thread_state);
	g_need_redraw = false;
	return;
}
Ejemplo n.º 23
0
Archivo: gfx.c Proyecto: onatto/luminos
void gfxBindVertexBuffer(uint32 vbo, uint8 bindingPoint, uint8 stride) {
  glBindVertexBuffer(bindingPoint, vbo, 0, stride);
}
Ejemplo n.º 24
0
bool StaticMesh::loadFromFile(const std::string& infile)
{
    Assimp::Importer imp;

    // Load scene and grab first mesh only
    imp.SetPropertyInteger(AI_CONFIG_PP_SBP_REMOVE,
                           aiPrimitiveType_LINE |
                           aiPrimitiveType_POINT |
                           aiPrimitiveType_POLYGON, nullptr);

    #ifdef DEBUG
    std::cout << "Calling Assimp::Importer::ReadFile...\n";
    #endif // DEBUG

    const aiScene *scene = imp.ReadFile(infile, aiProcessPreset_TargetRealtime_Quality
                                                & (!aiProcess_SplitLargeMeshes));
    #ifdef DEBUG
    std::cout << "Checking for a mesh...\n";
    #endif // DEBUG
    if(scene == nullptr)
        return false;
    else if(!scene->HasMeshes())
        return false;

    imp.ApplyPostProcessing(aiProcess_JoinIdenticalVertices);

    // Will only load one mesh
    const aiMesh *mesh = scene->mMeshes[0];

    #ifdef DEBUG
    std::cout << "Checking mesh has what we need...\n";
    #endif // DEBUG
    // Don't process special mesh types
    if(!mesh->HasPositions() ||
       !mesh->HasFaces())
        return false;

    vertexCount = mesh->mNumVertices;
    triangleCount = mesh->mNumFaces;

    // Check what attributes are in the mesh and
    // calculate total count of floats
    std::size_t componentCount = components.vertex = 3;
    if(mesh->HasNormals())
    {
        componentCount += components.normal = 3;
        #ifdef DEBUG
        std::cout << "Has normals\n";
        #endif // DEBUG
    }
    if(mesh->HasTangentsAndBitangents())
    {
        componentCount += (components.tangentBitangent = 3) * 2;
        #ifdef DEBUG
        std::cout << "Has tangents and bitangents\n";
        #endif // DEBUG
    }
    if(mesh->HasTextureCoords(0))
    {
        componentCount += components.texture = 3;
        #ifdef DEBUG
        std::cout << "Has texture coordinates\n";
        #endif // DEBUG
    }

    // Scale size for size of float
    std::size_t totalSize = componentCount * sizeof(float) * vertexCount;

    #ifdef DEBUG
    std::cout << totalSize << std::endl;
    std::cout << "Allocating and mapping vertex buffer...\n";
    #endif // DEBUG

    // Set size and map buffer
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, totalSize, nullptr, GL_STATIC_DRAW);
    GLfloat *mapPtr = (GLfloat*) glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
//    glNamedBufferDataEXT(buffer, totalSize, nullptr, GL_STATIC_DRAW);
//    GLfloat *mapPtr = (GLfloat*) glMapNamedBufferEXT(buffer, GL_WRITE_ONLY);
    if(mapPtr == nullptr)
        return false;

    #ifdef DEBUG
    std::cout << "Worked!\n";
    #endif // DEBUG

    // Load all attributes into mapped buffer
    for(unsigned int i = 0; i < vertexCount; ++i)
    {
        GLfloat *head = mapPtr + i * componentCount;

        *head++ = mesh->mVertices[i].x;
        *head++ = mesh->mVertices[i].y;
        *head++ = mesh->mVertices[i].z;

        if(mesh->HasNormals())
        {
            *head++ = mesh->mNormals[i].x;
            *head++ = mesh->mNormals[i].y;
            *head++ = mesh->mNormals[i].z;
        }
        if(mesh->HasTextureCoords(0))
        {
            *head++ = mesh->mTextureCoords[0][i].x;
            *head++ = mesh->mTextureCoords[0][i].y;
            *head++ = mesh->mTextureCoords[0][i].z;
        }
        if(mesh->HasTangentsAndBitangents())
        {
            *head++ = mesh->mTangents[i].x;
            *head++ = mesh->mTangents[i].y;
            *head++ = mesh->mTangents[i].z;
            *head++ = mesh->mBitangents[i].x;
            *head++ = mesh->mBitangents[i].y;
            *head++ = mesh->mBitangents[i].z;
        }
    }

    if(!glUnmapBuffer(GL_ARRAY_BUFFER))
        return false;

    #ifdef DEBUG
    std::cout << "Allocating and mapping index buffer...\n";
    #endif // DEBUG

    // Same for index buffer
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, triangleCount*3*sizeof(GLshort), nullptr, GL_STATIC_DRAW);
    GLushort *indexMapPtr = (GLushort*) glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
//    glNamedBufferDataEXT(indices, triangleCount * 3 * sizeof(GLushort), nullptr, GL_STATIC_DRAW);
//    GLushort *indexMapPtr = (GLushort*) glMapNamedBufferEXT(indices, GL_WRITE_ONLY);
    if(indexMapPtr == nullptr)
        return false;

    #ifdef DEBUG
    std::cout << "Worked!\n";
    #endif // DEBUG

    for(unsigned int i = 0; i < triangleCount; ++i)
    {
        #ifdef DEBUG
        assert(mesh->mFaces[i].mNumIndices == 3);
        #endif // DEBUG
        unsigned int *indexArray = mesh->mFaces[i].mIndices;
        for(unsigned int j = 0; j < 3; ++j)
        {
            #ifdef DEBUG
            assert(*indexArray <= std::numeric_limits<GLushort>::max());
            #endif // DEBUG
            *indexMapPtr++ = *indexArray++;
        }
    }

    if(!glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER))
        return false;;

    #ifdef DEBUG
    std::cout << "Loading mesh successful!\n";
    std::cout << "Setting up vertex array...\n";
    #endif // DEBUG

    glBindVertexArray(vertexArray);

    glBindVertexBuffer(0, buffer, 0, (components.vertex +
                                      components.normal +
                                      components.texture +
                                      components.tangentBitangent * 2)
                                        * sizeof(GLfloat));
    glVertexAttribFormat(0, 3, GL_FLOAT, GL_FALSE, 0);
    glVertexAttribBinding(0, 0);
    glEnableVertexAttribArray(0);

    if(components.normal)
    {
        glVertexAttribFormat(1, 3, GL_FLOAT, GL_FALSE, components.vertex * sizeof(float));
        glVertexAttribBinding(1, 0);
        glEnableVertexAttribArray(1);
    }
    if(components.texture)
    {
        glVertexAttribFormat(2, 3, GL_FLOAT, GL_FALSE, (components.vertex +
                                                        components.normal) * sizeof(float));
        glVertexAttribBinding(2, 0);
        glEnableVertexAttribArray(2);
    }
    if(components.tangentBitangent)
    {
        glVertexAttribFormat(3, 3, GL_FLOAT, GL_FALSE, (components.vertex +
                                                        components.normal +
                                                        components.texture) * sizeof(float));
        glVertexAttribBinding(3, 0);
        glEnableVertexAttribArray(3);

        glVertexAttribFormat(4, 3, GL_FLOAT, GL_FALSE, (components.vertex +
                                                        components.normal +
                                                        components.texture +
                                                        components.tangentBitangent) * sizeof(float));
        glVertexAttribBinding(4, 0);
        glEnableVertexAttribArray(4);
    }

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indices);

    glBindVertexArray(0);

    #ifdef DEBUG
    std::cout << vertexCount << ' ' << triangleCount << std::endl;
    std::cout << +components.vertex << ' ';
    std::cout << +components.normal << ' ';
    std::cout << +components.texture << ' ';
    std::cout << +components.tangentBitangent << std::endl;
    #endif // DEBUG

    return true;
}
Ejemplo n.º 25
0
int main()
{
	int w, h, c;
	unsigned char *datum = SOIL_load_image("materials/ok.jpg", &w, &h, &c, SOIL_LOAD_RGBA);

	GlRunner *runner = new GlRunner(RenderCB, w, h);

	GLuint VS = runner->BuildShaderProgram("shaders/quad.vert", GL_VERTEX_SHADER);
	GLuint FS = runner->BuildShaderProgram("shaders/cartoonify.frag", GL_FRAGMENT_SHADER);
	GLuint PPO = runner->BuildProgramPipeline();

	glUseProgramStages(PPO, GL_FRAGMENT_SHADER_BIT, FS);
	glUseProgramStages(PPO, GL_VERTEX_SHADER_BIT, VS);

	glProgramUniform1i(FS, glGetUniformLocation(FS, "tColor2D"), 0);

	GLfloat pos_buf[] = {
		-1.0, 1.0,
		1.0, 1.0,
		-1.0, -1.0,
		1.0, -1.0,
	};

	GLuint posVBO;
	glGenBuffers(1, &posVBO);
	glBindBuffer(GL_ARRAY_BUFFER, posVBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(pos_buf), pos_buf, GL_STATIC_DRAW);

	GLfloat texc_buf[] = {
		0.0, 0.0,
		1.0, 0.0,
		0.0, 1.0,
		1.0, 1.0,
	};

	GLuint TexcVBO;
	glGenBuffers(1, &TexcVBO);
	glBindBuffer(GL_ARRAY_BUFFER, TexcVBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(texc_buf), texc_buf, GL_DYNAMIC_DRAW);

	GLuint VAO;
	glGenVertexArrays(1, &VAO);
	glBindVertexArray(VAO);

	glEnableVertexAttribArray(0);
	glEnableVertexAttribArray(1);

	glVertexAttribFormat(0, 2, GL_FLOAT, GL_FALSE, 0);
	glVertexAttribFormat(1, 2, GL_FLOAT, GL_FALSE, 0);

	glVertexAttribBinding(0, 0);
	glVertexAttribBinding(1, 1);

	glBindVertexBuffer(0, posVBO, 0, 2*sizeof(GLfloat));
	glBindVertexBuffer(1, TexcVBO, 0, 2*sizeof(GLfloat));

	GLuint srcTexObj;
	glGenTextures(1, &srcTexObj);
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, srcTexObj);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, datum);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

	runner->OnRender();

	return 0;
}
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GL43_nglBindVertexBuffer(JNIEnv *__env, jclass clazz, jint bindingindex, jint buffer, jlong offset, jint stride, jlong __functionAddress) {
	glBindVertexBufferPROC glBindVertexBuffer = (glBindVertexBufferPROC)(intptr_t)__functionAddress;
	UNUSED_PARAMS(__env, clazz)
	glBindVertexBuffer(bindingindex, buffer, (GLintptr)offset, stride);
}
Ejemplo n.º 27
0
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GL43_glBindVertexBuffer(JNIEnv *__env, jclass clazz, jint bindingindex, jint buffer, jlong offset, jint stride) {
    glBindVertexBufferPROC glBindVertexBuffer = (glBindVertexBufferPROC)tlsGetFunction(907);
    UNUSED_PARAM(clazz)
    glBindVertexBuffer(bindingindex, buffer, (intptr_t)offset, stride);
}
Ejemplo n.º 28
0
/**
 * TODO: Test if you can pass a NULL mesh to Geometry.
 */
RenderableGeometry *renderableGeometryNew(Geometry geometry)
{
	RenderableGeometry *renderableGeometry = malloc(sizeof(RenderableGeometry));

	renderableGeometry->indices = 0;
	renderableGeometry->aabb.min = vec3(0.0f, 0.0f, 0.0f);
	renderableGeometry->aabb.max = vec3(0.0f, 0.0f, 0.0f);

	glGenVertexArrays(1, &renderableGeometry->vao);
	glGenBuffers(4, renderableGeometry->vbo);

	Mesh *mesh = NULL;

	if (geometry.type == GEOMETRY_MESH) {
		mesh = geometry.mesh;
	} else if (geometry.type == GEOMETRY_PLANE) {
		// Generate mesh
	} else if (geometry.type == GEOMETRY_CUBE) {
		// Generate mesh
	} else if (geometry.type == GEOMETRY_SPHERE) {
		// Generate mesh
	} else if (geometry.type == GEOMETRY_CAPSULE) {
		// Generate mesh
	} else if (geometry.type == GEOMETRY_CYLINDER) {
		// Generate mesh
	} else if (geometry.type == GEOMETRY_RAY) {
		// Generate mesh
	}

	if (mesh != NULL) {
		renderableGeometry->indices = (GLsizei)mesh->indices.length;

		/**
		 * Define VAO.
		 */
		glBindVertexArray(renderableGeometry->vao);

		glEnableVertexAttribArray(0);
		glEnableVertexAttribArray(1);
		glEnableVertexAttribArray(2);

		glVertexAttribFormat(0, 3, GL_FLOAT, GL_FALSE, 0); 
		glVertexAttribFormat(1, 2, GL_FLOAT, GL_FALSE, 0); 
		glVertexAttribFormat(2, 3, GL_FLOAT, GL_FALSE, 0);
		
		/**
		 * Define VBOs.
		 */
		if (mesh->vertices.length > 0) {
			glBindBuffer(GL_ARRAY_BUFFER, renderableGeometry->vbo[0]);
			glBufferStorage(GL_ARRAY_BUFFER, mesh->vertices.length * sizeof(Vec3), (float*)mesh->vertices.data, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT);	
		}

		if (mesh->uvs.length > 0) {
			glBindBuffer(GL_ARRAY_BUFFER, renderableGeometry->vbo[1]);
			glBufferStorage(GL_ARRAY_BUFFER, mesh->uvs.length * sizeof(Vec2), (float*)mesh->uvs.data, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT);
		}

		if (mesh->normals.length > 0) {
			glBindBuffer(GL_ARRAY_BUFFER, renderableGeometry->vbo[2]);
			glBufferStorage(GL_ARRAY_BUFFER, mesh->normals.length * sizeof(Vec3), (float*)mesh->normals.data, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT);
		}

		if (mesh->indices.length > 0) {
			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, renderableGeometry->vbo[3]);
			glBufferStorage(GL_ELEMENT_ARRAY_BUFFER, mesh->indices.length * sizeof(unsigned), (unsigned*)mesh->indices.data, GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT | GL_MAP_COHERENT_BIT);
		}

		glBindVertexBuffer(0, renderableGeometry->vbo[0], 0, sizeof(Vec3));
		glBindVertexBuffer(1, renderableGeometry->vbo[1], 0, sizeof(Vec2));
		glBindVertexBuffer(2, renderableGeometry->vbo[2], 0, sizeof(Vec3));

		/**
		 * Unbind.
		 */
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
		glBindBuffer(GL_ARRAY_BUFFER, 0);
		glBindVertexArray(0);
	}
	
	return renderableGeometry;
}
Ejemplo n.º 29
0
void GLVertexArrayObject::detachBuffer( GLuint bindingIndex )
{
    glBindVertexBuffer( bindingIndex, 0, 0, 0 );
}
Ejemplo n.º 30
0
inline void bind_vertex_buffer(uint32 binding_index, uint32 buffer_id, uint32 offset, uint32 stride)
{
    ARC_GL_CLEAR_ERRORS();
    glBindVertexBuffer(binding_index, buffer_id, offset, stride);
    ARC_GL_CHECK_FOR_ERRORS();
}