示例#1
0
/*-------------------------------------
 * Copy data from one buffer into another.
-------------------------------------*/
bool BufferObject::copy_data(const BufferObject& from) noexcept {
    LS_DEBUG_ASSERT(from.gpuId != this->gpuId);
    LS_DEBUG_ASSERT(this->is_valid());
    LS_DEBUG_ASSERT(from.is_valid());
    LS_DEBUG_ASSERT(from.get_type() == this->get_type());

    // bind the buffers to OGL's read/write targets to prevent a pipeline stall.
    glBindBuffer(VBO_COPY_READ, from.gpuId);

    GLint numBytes = 0;
    buffer_access_t usage = buffer_access_t::VBO_STATIC_DRAW;

    glGetBufferParameteriv(VBO_COPY_READ, GL_BUFFER_SIZE, &numBytes);
    glGetBufferParameteriv(VBO_COPY_READ, GL_BUFFER_USAGE, (GLint*) & usage);

    if (numBytes != 0 && !this->gpuId) {
        if (!init()) {
            terminate();
            return false;
        }
    }

    glBindBuffer(VBO_COPY_WRITE, this->gpuId);
    glBufferData(VBO_COPY_WRITE, numBytes, nullptr, usage);

    glCopyBufferSubData(VBO_COPY_READ, VBO_COPY_WRITE, 0, 0, numBytes);

    // better safe than sorry
    glBindBuffer(VBO_COPY_READ, 0);
    glBindBuffer(VBO_COPY_WRITE, 0);

    copy_attribs(from);

    return true;
}
示例#2
0
void Octagon::Init(void) {
	glGenVertexArrays(1, &fVao);
	glBindVertexArray(fVao);

	glGenBuffers(1, &fBufferId);
	glBindBuffer(GL_ARRAY_BUFFER, fBufferId);
	glBufferData(GL_ARRAY_BUFFER, sizeof vertexData, vertexData, GL_STATIC_DRAW);

	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof (vertex), 0); // GL_ARRAY_BUFFER must be bound when doing this.

	// check data size in VBO
	int bufferSize = 0;
	glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &bufferSize);
	if ((unsigned)bufferSize != sizeof vertexData) {
		glDeleteBuffers(1, &fBufferId);
		ErrorDialog("Octagon::Init: vertexData size is mismatch with input array\n");
	}

	glGenBuffers(1, &fIndexId);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, fIndexId);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof indices, indices, GL_STATIC_DRAW);
	// check data size in VBO is same as input array, if not return 0 and delete VBO
	bufferSize = 0;
	glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &bufferSize);
	if ((unsigned)bufferSize != sizeof indices) {
		glDeleteBuffers(1, &fIndexId);
		ErrorDialog("Octagon::Init: indices size is mismatch with input array\n");
	}

	glBindVertexArray(0);
	checkError("Octagon::Init");
}
示例#3
0
VBO::VBO(const VBO& other)
	: _numOfIndices(other._numOfIndices), _numOfVerts(other._numOfVerts), _vertexSize(other._vertexSize),
	_normalOffset(other._normalOffset), _textureOffset(other._textureOffset), _hasNormals(other._hasNormals),
	_hasTextures(other._hasTextures), _mode(other._mode), _usage(other._usage)
{
	glGenBuffers(2, _buffers);
	
	glBindBuffer(GL_ARRAY_BUFFER, other._vbo);
	void* otherData = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY);
	GLint otherDataSize, otherUsage;
	glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &otherDataSize);
	glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_USAGE, &otherUsage);
	glBindBuffer(GL_ARRAY_BUFFER, _vbo);
	glBufferData(GL_ARRAY_BUFFER, otherDataSize, otherData, otherUsage);
	glBindBuffer(GL_ARRAY_BUFFER, other._vbo);
	glUnmapBuffer(GL_ARRAY_BUFFER);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, other._ibo);
	void* otherIdxData = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY);
	GLint otherIdxDataSize, otherIdxUsage;
	glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &otherIdxDataSize);
	glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_USAGE, &otherIdxUsage);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ibo);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, otherIdxDataSize, otherIdxData, otherIdxUsage);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, other._ibo);
	glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
}
示例#4
0
文件: CLGL.cpp 项目: tlgimenes/clgl
/*
 * Load host data hostMemory to Device in VBO
 */
cl::Memory* CLGL::CLGLLoadVBODataToDevice(size_t bufferBytesSize, void * hostMemory, cl_mem_flags flag)
{
  GLuint id = 0;  // 0 is reserved, glGenBuffersARB() will return non-zero id if success

  glGenBuffers(1, &id);                          // create a vbo
  glBindBuffer(GL_ARRAY_BUFFER, id);                      // activate vbo id to use
  glBufferData(GL_ARRAY_BUFFER, bufferBytesSize, hostMemory, GL_DYNAMIC_DRAW); // create buffer

  // check data size in VBO is same as input array, if not return 0 and delete VBO
  int bufferSize = 0;
  glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &bufferSize);
  if((int)bufferBytesSize != bufferSize){
    glDeleteBuffers(1, &id);
    id = 0;
    std::cout << "[createVBO()] Data size is mismatch with input array\n";
    exit(1);
  }

  //Adds the vbo id
  this->vbo.push_back(id);
  
  //this was important for working inside blender!
  glBindBuffer(GL_ARRAY_BUFFER, 0);
  glFinish();  // Wait for OpenGL to finish

  // Create buffer from OpenGL VBO
  try{
    this->bufferGL->push_back(cl::BufferGL(this->context, flag, id));
  }
  catch(cl::Error error){
    std::cout << error.what() << ' ' << CLGLError::errToStr(error.err())->c_str() << std::endl;
    exit(1);
  }
  return &(this->bufferGL->back());
}
示例#5
0
void Water::render(map<string, GLint>& sparam){
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, texture_id);
	glUniform1i(sparam["uniform_mytexture"], /*GL_TEXTURE*/0);

	glEnableVertexAttribArray(sparam["attribute_coord3d"]);
	glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_vertices);
	glVertexAttribPointer(
		sparam["attribute_coord3d"],   // attribute
		3,                   // number of elements per vertex, here (x,y)
		GL_FLOAT,            // the type of each element
		GL_FALSE,            // take our values as-is
		3*sizeof(GLfloat), // next coord2d appears every 5 floats
		0                    // offset of the first element
		);

	glEnableVertexAttribArray(sparam["attribute_texcoord"]);
	glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_texcoords);
	glVertexAttribPointer(
		sparam["attribute_texcoord"], // attribute
		2,                  // number of elements per vertex, here (x,y)
		GL_FLOAT,           // the type of each element
		GL_FALSE,           // take our values as-is
		2*sizeof(GLfloat),                  // no extra data between each position
		0                   // offset of first element
		);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_cube_elements);
	int size;  
	glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
	glDrawElements(GL_TRIANGLES, size/sizeof(GLuint), GL_UNSIGNED_INT, 0);

	glDisableVertexAttribArray(sparam["attribute_coord3d"]);
	glDisableVertexAttribArray(sparam["attribute_texcoord"]);
}
示例#6
0
/*!
 * \internal
 */
QString CanvasGLStateDump::getGLArrayObjectDump(int target, int arrayObject, int type)
{
    if (!arrayObject)
        return "no buffer bound";

    QString stateDumpStr;
    glBindBuffer(target, arrayObject);

    GLint size;
    glGetBufferParameteriv(target, GL_BUFFER_SIZE, &size);

    if (type == GL_FLOAT) {
        stateDumpStr.append("ARRAY_BUFFER_TYPE......................FLOAT\n");

        stateDumpStr.append("ARRAY_BUFFER_SIZE......................");
        stateDumpStr.append(QString::number(size));
        stateDumpStr.append("\n");

    } else if (type == GL_UNSIGNED_SHORT) {
        stateDumpStr.append("ARRAY_BUFFER_TYPE......................UNSIGNED_SHORT\n");

        stateDumpStr.append("ARRAY_BUFFER_SIZE......................");
        stateDumpStr.append(QString::number(size));
        stateDumpStr.append("\n");
    }

    return stateDumpStr;
}
示例#7
0
void BufferGL4<Tag>::SetData(std::size_t offsetInBytes,
    void const* source, std::size_t sizeInBytes)
{
    POMDOG_ASSERT(source != nullptr);

    auto const oldBuffer = TypesafeHelperGL4::Get<BufferObject>();
    ScopeGuard scope([&] { TypesafeHelperGL4::BindBuffer(oldBuffer); });

    POMDOG_ASSERT(bufferObject);
    TypesafeHelperGL4::BindBuffer(*bufferObject);
    POMDOG_CHECK_ERROR_GL4("glBindBuffer");

#if defined(DEBUG) && !defined(NDEBUG)
    {
        GLint bufferSize = 0;
        glGetBufferParameteriv(BufferTraits<Tag>::Buffer,
            GL_BUFFER_SIZE, &bufferSize);
        POMDOG_ASSERT(sizeInBytes <= static_cast<std::size_t>(bufferSize));
    }
#endif

    POMDOG_ASSERT(sizeInBytes > 0);
    glBufferSubData(BufferTraits<Tag>::Buffer,
        offsetInBytes, sizeInBytes, source);
    POMDOG_CHECK_ERROR_GL4("glBufferSubData");
}
示例#8
0
GLuint initVBO(int vbolen)
{
	GLint bsize;

	GLuint vbo_buffer;
	// generate the buffer
	glGenBuffers(1, &vbo_buffer);

	// bind the buffer 
	glBindBuffer(GL_ARRAY_BUFFER, vbo_buffer);
	if (glGetError() != GL_NO_ERROR) {
		std::cerr << "Could not bind buffer" << std::endl;
	}

	// create the buffer, this basically sets/allocates the size
	// for our VBO we will hold 2 line endpoints per element
	glBufferData(GL_ARRAY_BUFFER, vbolen * sizeof(float) * 4, NULL, GL_STREAM_DRAW);
	if (glGetError() != GL_NO_ERROR) {
		std::cerr << "Could not bind buffer" << std::endl;
	}
	// recheck the size of the created buffer to make sure its what we requested
	glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &bsize);
	if ((GLuint)bsize != (vbolen * sizeof(float) * 4)) {
		printf("Vertex Buffer object (%d) has incorrect size (%d).\n", (unsigned)vbo_buffer, (unsigned)bsize);
	}

	// we're done, so unbind the buffers
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	if (glGetError() != GL_NO_ERROR) {
		std::cerr << "Could not bind buffer" << std::endl;
	}
	return vbo_buffer;
}
	void OpenGLESVertexBuffer::create()
	{
		MYGUI_PLATFORM_ASSERT(!mBufferID, "Vertex buffer already exist");

		mSizeInBytes = mNeedVertexCount * sizeof(MyGUI::Vertex);
		void* data = 0;

		glGenBuffers(1, (GLuint*)&mBufferID); //wdy
        CHECK_GL_ERROR_DEBUG();
		glBindBuffer(GL_ARRAY_BUFFER, mBufferID);
        CHECK_GL_ERROR_DEBUG();
		glBufferData(GL_ARRAY_BUFFER, mSizeInBytes, data, GL_STREAM_DRAW);
        CHECK_GL_ERROR_DEBUG();
        
        //MYGUI_LOG(Info, mBufferID);
		

		// check data size in VBO is same as input array, if not return 0 and delete VBO
		int bufferSize = 0;
		glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, (GLint*)&bufferSize); //wdy
        CHECK_GL_ERROR_DEBUG();
		if (mSizeInBytes != (size_t)bufferSize)
		{
			destroy();
			MYGUI_PLATFORM_EXCEPT("Data size is mismatch with input array");
		}

		glBindBuffer(GL_ARRAY_BUFFER, 0);
        CHECK_GL_ERROR_DEBUG();
	}
void BuildingBlocks::Init(int numToDisplay) {
	fNumToDisplay = numToDisplay;
	fShader = SimpleTextureShader::Make();
	glGenVertexArrays(1, &fVao);
	glBindVertexArray(fVao); // Has to be done before enabling program, where the vertex attrib pointers are enabled.
	fShader->EnableVertexAttribArray();
	glGenBuffers(1, &fBufferId);
	glBindBuffer(GL_ARRAY_BUFFER, fBufferId);
	glBufferData(GL_ARRAY_BUFFER, sizeof vertexData, vertexData, GL_STATIC_DRAW);
	vertex *p = 0;
	fShader->TextureAttribPointer(GL_UNSIGNED_BYTE, sizeof (vertex), &p->t);
	fShader->VertexAttribPointer(GL_BYTE, sizeof (vertex), &p->v);
	glBindVertexArray(0);
	// check data size in VBO is same as input array, if not return 0 and delete VBO
	int bufferSize = 0;
	glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &bufferSize);
	if ((unsigned)bufferSize != sizeof vertexData) {
		glDeleteBuffers(1, &fBufferId);
		ErrorDialog("[BuildingBlocks::Init] Data size is mismatch with input array\n");
	}
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	fShader->DisableProgram();
	fSelectedBlockText = gDrawFont.vsfl.genSentence();
	BuildingBlocks::UpdateSelection(0); // Last thing
}
示例#11
0
void bufferSize(GLuint buffer, GLenum type=GL_ARRAY_BUFFER)
{
    glBindBuffer(type,buffer);
    GLsizei datasize;
    glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &datasize);
    std::cout << "Buffer["<<buffer<<"] size: " << datasize << " "<< std::endl;
}
GLuint initVBO(int imWidth, int imHeight )
{
    int bpp = 4;
    GLint bsize;

    GLuint vbo_buffer; 
    // generate the buffer
    glGenBuffers(1, &vbo_buffer);
    
    // bind the buffer 
    glBindBuffer(GL_ARRAY_BUFFER, vbo_buffer); 
    assert( glGetError() == GL_NO_ERROR );
    
    // create the buffer, this basically sets/allocates the size
    glBufferData(GL_ARRAY_BUFFER, imWidth * imHeight *sizeof(float)*4, NULL, GL_STREAM_DRAW);  
    assert( glGetError() == GL_NO_ERROR );

    // recheck the size of the created buffer to make sure its what we requested
    glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &bsize); 
    if ((GLuint)bsize != (imWidth * imHeight*sizeof(float)*4)) {
        printf("Vertex Buffer object (%d) has incorrect size (%d).\n", (unsigned)vbo_buffer, (unsigned)bsize);
    }

    // we're done, so unbind the buffers
    glBindBuffer(GL_ARRAY_BUFFER, 0);                    
    assert( glGetError() == GL_NO_ERROR );
    return vbo_buffer;
}
void initializeData(char *file, int argc, char **argv) {
    GLint bsize;
    unsigned int w, h;
    size_t file_length= strlen(file);

    if (!strcmp(&file[file_length-3], "pgm")) {
        if (cutLoadPGMub(file, &pixels, &w, &h) != CUTTrue) {
            printf("Failed to load image file: %s\n", file);
            exit(-1);
        }
        g_Bpp = 1;
    } else if (!strcmp(&file[file_length-3], "ppm")) {
        if (cutLoadPPM4ub(file, &pixels, &w, &h) != CUTTrue) {
            printf("Failed to load image file: %s\n", file);
            exit(-1);
        }
        g_Bpp = 4;
    } else {
        cutilDeviceReset();
        shrQAFinishExit(argc, (const char **)argv, QA_WAIVED);
    }
    imWidth = (int)w; imHeight = (int)h;
    setupTexture(imWidth, imHeight, pixels, g_Bpp);

    // copy function pointer tables to host side for later use
    setupFunctionTables();

    memset(pixels, 0x0, g_Bpp * sizeof(Pixel) * imWidth * imHeight);

    if (!g_bQAReadback) {
        // use OpenGL Path
        glGenBuffers(1, &pbo_buffer);
        glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo_buffer); 
        glBufferData(GL_PIXEL_UNPACK_BUFFER, 
                        g_Bpp * sizeof(Pixel) * imWidth * imHeight, 
                        pixels, GL_STREAM_DRAW);  

        glGetBufferParameteriv(GL_PIXEL_UNPACK_BUFFER, GL_BUFFER_SIZE, &bsize); 
        if ((GLuint)bsize != (g_Bpp * sizeof(Pixel) * imWidth * imHeight)) {
            printf("Buffer object (%d) has incorrect size (%d).\n", (unsigned)pbo_buffer, (unsigned)bsize);
            cutilDeviceReset();
            shrQAFinishExit(argc, (const char **)argv, QA_FAILED);
        }

        glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);

        // register this buffer object with CUDA
        cutilSafeCall(cudaGraphicsGLRegisterBuffer(&cuda_pbo_resource, pbo_buffer, cudaGraphicsMapFlagsWriteDiscard));	

        glGenTextures(1, &texid);
        glBindTexture(GL_TEXTURE_2D, texid);
        glTexImage2D(GL_TEXTURE_2D, 0, ((g_Bpp==1) ? GL_LUMINANCE : GL_BGRA), 
                    imWidth, imHeight,  0, GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL);
        glBindTexture(GL_TEXTURE_2D, 0);

        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        glPixelStorei(GL_PACK_ALIGNMENT, 1);
    }
}
示例#14
0
void Draw(glm::mat4 &translate, GLuint* vbo, GLuint* ibo, bool isWireFrame) {
	if (isWireFrame) {
		// Turn on wireframe mode
		glPolygonMode(GL_FRONT, GL_LINE);
		glPolygonMode(GL_BACK, GL_LINE);
	}
	glUniformMatrix4fv(uniform_m, 1, GL_FALSE, glm::value_ptr(translate));

	glBindBuffer(GL_ARRAY_BUFFER, *vbo);

	glVertexAttribPointer(attribute_coord3d, 3,
	GL_FLOAT,
	GL_FALSE, sizeof(struct PC),  // stride
	0);  // offset

	glVertexAttribPointer(attribute_normal, 3,
	GL_FLOAT,
	GL_FALSE, sizeof(struct PC),  // stride
	(GLvoid*) offsetof(struct PC, normal));

	glVertexAttribPointer(attribute_colour, 3,
	GL_FLOAT,
	GL_FALSE, sizeof(struct PC),  // stride
	(GLvoid*) offsetof(struct PC, colour));

	/* Push each element in buffer_vertices to the vertex shader */
	if (ibo == 0) {
		int size = 0;
		glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
		glDrawArrays(GL_LINES, 0, size / sizeof(PC));
		glBindBuffer(GL_ARRAY_BUFFER, 0);
	} else {
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, *ibo);
		int size = 0;
		glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
		glDrawElements(GL_TRIANGLES, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0);
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
		glBindBuffer(GL_ARRAY_BUFFER, 0);
	}

	if (isWireFrame) {
		// Turn off wireframe mode
		glPolygonMode(GL_FRONT, GL_FILL);
		glPolygonMode(GL_BACK, GL_FILL);
	}
}
/* Enable vertex attributes and draw object
Could improve efficiency by moving the vertex attribute pointer functions to the
create object but this method is more general
This code is almost untouched fomr the tutorial code except that I changed the
number of elements per vertex from 4 to 3*/
void Terrain::drawObject(int drawmode)
{
	int size;	// Used to get the byte size of the element (vertex index) array
	if (loc != -1)
	{
		glUniform1f(loc, false);
	}
	if (loc2 != -1)
	{
		glUniform1f(loc2, false);
	}

	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, texID);
	glUniform1i(loc, 0);
	// Describe our vertices array to OpenGL (it can't guess its format automatically)
	glBindBuffer(GL_ARRAY_BUFFER, vbo_mesh_vertices);
	glVertexAttribPointer(
		attribute_v_coord,  // attribute index
		3,                  // number of elements per vertex, here (x,y,z)
		GL_FLOAT,           // the type of each element
		GL_FALSE,           // take our values as-is
		0,                  // no extra data between each position
		0                   // offset of first element
		);

	glBindBuffer(GL_ARRAY_BUFFER, vbo_mesh_normals);
	glVertexAttribPointer(
		attribute_v_normal, // attribute
		3,                  // number of elements per vertex, here (x,y,z)
		GL_FLOAT,           // the type of each element
		GL_FALSE,           // take our values as-is
		0,                  // no extra data between each position
		0                   // offset of first element
		);

	/* Bind the sphere colours */
	glBindBuffer(GL_ARRAY_BUFFER, vbo_mesh_colours);
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(1);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_mesh_elements);
	glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);

	// Enable this line to show model in wireframe
	if (drawmode == 1)
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	else
		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);


	/* Draw the triangle strips */
	for (int i = 0; i < xsize - 1; i++)
	{
		GLuint location = sizeof(GLuint) * (i * zsize * 2);
		glDrawElements(GL_TRIANGLE_STRIP, zsize * 2, GL_UNSIGNED_INT, (GLvoid*)(location));
	}
}
示例#16
0
/*!
    Returns the size of the data in this buffer, for reading operations.
    Returns -1 if fetching the buffer size is not supported, or the
    buffer has not been created.

    It is assumed that this buffer has been bound to the current context.

    \sa isCreated(), bind()
*/
int QGLBuffer::size() const
{
    Q_D(const QGLBuffer);
    if (!d->guard.id())
        return -1;
    GLint value = -1;
    glGetBufferParameteriv(d->type, GL_BUFFER_SIZE, &value);
    return value;
}
示例#17
0
文件: Text2DBox.cpp 项目: xehp/rsb
void Text2DBox::renderAll()
{
	MirrorContainer::renderAll();

	// Is there a texture to show?
	if (texture_id != ZERO_GL)
	{


		glUseProgram(box2DProgramId);

		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		glEnable(GL_BLEND);
		glDisable(GL_CULL_FACE);
		glDisable(GL_DEPTH_TEST);


		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, texture_id);
		glUniform1i(box2DTextureUniform, /*GL_TEXTURE*/0);


		glEnableVertexAttribArray(box2DCoord3dAttribute);
		glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices);
		glVertexAttribPointer(
			box2DCoord3dAttribute, // attribute
			2,                     // size, the two room coordinates x,y.
			GL_FLOAT,              // type
			GL_FALSE,              // normalized? No, take our values as-is.
			0,                     // stride
			(void*)0               // array buffer offset, offset of first element
		);

		glEnableVertexAttribArray(box2DTexcoordAttribute);
		glBindBuffer(GL_ARRAY_BUFFER, vbo_texcoords);
		glVertexAttribPointer(
			box2DTexcoordAttribute, // attribute
			2,                  // number of elements per vertex, here (x,y)
			GL_FLOAT,           // the type of each element
			GL_FALSE,           // take our values as-is
			0,                  // no extra data between each position
			0                   // offset of first element
		  );



		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_elements);

		int size;
		glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
		glDrawElements(GL_TRIANGLES, size/sizeof(GLushort), GL_UNSIGNED_SHORT, 0);


		glDisableVertexAttribArray(box2DCoord3dAttribute);
		glDisableVertexAttribArray(box2DTexcoordAttribute);
	}
};
示例#18
0
GLint ShaderStorageBufferObject::getSizeInBytes() const
{
    GLint size;
    bind(true);
    glGetBufferParameteriv(GL_SHADER_STORAGE_BUFFER, GL_BUFFER_SIZE, &size);
    myDebug << "SSBO::query:size: " << size << std::endl;
    bind(false);
    return size;
}
示例#19
0
GLenum VertexBuffer::GetUsage() const
{
	if (GLEW_ARB_vertex_buffer_object) {
		Bind();
		int usage = 0;
		glGetBufferParameteriv(target, GL_BUFFER_USAGE, &usage);
		return (GLenum)usage;
	}
	return GL_STATIC_DRAW;
}
示例#20
0
int VAO::getVBOsize()
{ /*    http://stackoverflow.com/questions/3819390/how-do-i-get-the-size-of-a-vbo-in-opengl-es       */
    if(m_bound !=true)
    {
        std::cerr<<"Warning trying to get VBO size on unbound VOA\n";
    }
    int nBufferSize = 0;
    glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &nBufferSize);
    return nBufferSize;
}
示例#21
0
static size_t get_buffer_size (SCEenum target, SCEuint id)
{
    GLint size = 0;
    if (id) {
        glBindBuffer (target, id);
        glGetBufferParameteriv (target, GL_BUFFER_SIZE, &size);
        glBindBuffer (target, 0);
    }
    return (size_t)size;
}
示例#22
0
void initializeData(char *file) {
    GLint bsize;
    unsigned int w, h;
    size_t file_length= strlen(file);

    if (!strcmp(&file[file_length-3], "pgm")) {
        if (cutLoadPGMub(file, &pixels, &w, &h) != CUTTrue) {
            printf("Failed to load image file: %s\n", file);
            exit(-1);
        }
        g_Bpp = 1;
    } else if (!strcmp(&file[file_length-3], "ppm")) {
        if (cutLoadPPM4ub(file, &pixels, &w, &h) != CUTTrue) {
            printf("Failed to load image file: %s\n", file);
            exit(-1);
        }
        g_Bpp = 4;
    } else {
        cudaThreadExit();
        exit(-1);
    }

    imWidth = (int)w; imHeight = (int)h;
    setupTexture(imWidth, imHeight, pixels, g_Bpp);

    memset(pixels, 0x0, g_Bpp * sizeof(Pixel) * imWidth * imHeight);

    if (!g_bQAReadback) {
        // use OpenGL Path
        glGenBuffers(1, &pbo_buffer);
        glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo_buffer); 
        glBufferData(GL_PIXEL_UNPACK_BUFFER, 
                        g_Bpp * sizeof(Pixel) * imWidth * imHeight, 
                        pixels, GL_STREAM_DRAW);  

        glGetBufferParameteriv(GL_PIXEL_UNPACK_BUFFER, GL_BUFFER_SIZE, &bsize); 
        if ((GLuint)bsize != (g_Bpp * sizeof(Pixel) * imWidth * imHeight)) {
            printf("Buffer object (%d) has incorrect size (%d).\n", (unsigned)pbo_buffer, (unsigned)bsize);
            cudaThreadExit();
            exit(-1);
        }

        glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
        cutilSafeCall(cudaGLRegisterBufferObject(pbo_buffer));

        glGenTextures(1, &texid);
        glBindTexture(GL_TEXTURE_2D, texid);
        glTexImage2D(GL_TEXTURE_2D, 0, ((g_Bpp==1) ? GL_LUMINANCE : GL_BGRA), 
                    imWidth, imHeight,  0, GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL);
        glBindTexture(GL_TEXTURE_2D, 0);

        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        glPixelStorei(GL_PACK_ALIGNMENT, 1);
    }
}
示例#23
0
GLvoid *
BufferMapping::map(GLenum _target, GLuint _buffer)
{
    if (target == _target && buffer == _buffer) {
        return map_pointer;
    }

    target = _target;
    buffer = _buffer;
    map_pointer = NULL;
    unmap = false;

    BufferBinding bb(target, buffer);

    // Recursive mappings of the same buffer are not allowed.  And with the
    // pursuit of persistent mappings for performance this will become more
    // and more common.
    GLint mapped = GL_FALSE;
    glGetBufferParameteriv(target, GL_BUFFER_MAPPED, &mapped);
    if (mapped) {
        glGetBufferPointerv(target, GL_BUFFER_MAP_POINTER, &map_pointer);
        assert(map_pointer != NULL);

        GLint map_offset = 0;
        glGetBufferParameteriv(target, GL_BUFFER_MAP_OFFSET, &map_offset);
        if (map_offset != 0) {
            std::cerr << "warning: " << enumToString(target) << " buffer " << buffer << " is already mapped with offset " << map_offset << "\n";
            // FIXME: This most likely won't work.  We should remap the
            // buffer with the full range, then re-map when done.  This
            // should never happen in practice with persistent mappings
            // though.
            map_pointer = (GLubyte *)map_pointer - map_offset;
        }
    } else {
        map_pointer = glMapBuffer(target, GL_READ_ONLY);
        if (map_pointer) {
            unmap = true;
        }
    }

    return map_pointer;
}
示例#24
0
int VertexArrayObject::getSize() const
{
  if(m_bound == false)
  {
    std::cerr<<"Warning trying to access an unbound VOA\n";
  }

  int size;
  glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
  return size;
}
示例#25
0
	void Sun::renderRect()
	{
		glBindBuffer(GL_ARRAY_BUFFER, vboIdRect);
		glVertexAttribPointer(0, 3, GL_FLOAT ,GL_FALSE, sizeof(Engine::SimpleVertex), (void*)offsetof(Engine::SimpleVertex,position));

		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, iboIdRect);
		int size;  glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);

		glDrawElements(GL_TRIANGLES, size/sizeof(GLuint), GL_UNSIGNED_INT, 0);
		glDisableVertexAttribArray(0);
	}
示例#26
0
	//TODO: drive this with a generic VBO descriptor to avoid special case drawing like these particles
	static void DrawParticles( const DrawParticlesCommand &cmd ) {
		if ( !cmd.material ) {
			console.Print( PrintLevel::Developer, "%s with invalid material\n",
				XS_FUNCTION
			);
			return;
		}

		cmd.material->Bind();

		bool setWireframe = false;
		bool previousWireframe = false;
		if ( r_wireframe->GetBool() || (cmd.material->flags & MF_WIREFRAME) ) {
			setWireframe = true;
			previousWireframe = GetWireframe();
			ToggleWireframe( true );
		}

		if ( cmd.vbo ) {
			cmd.vbo->Bind();

			// position4, uv2, colour4
			EnableVertexAttribs( VERTEX_ATTRIB_0 | VERTEX_ATTRIB_1 | VERTEX_ATTRIB_2 );

			// calculate stride
			GLsizei stride = sizeof(vector4) + sizeof(vector2) + sizeof(vector4);

			// set the attribute pointers
			size_t offset = 0u;
			glVertexAttribPointer( 0, 4, GL_FLOAT, GL_FALSE, stride, reinterpret_cast<const GLvoid *>( offset ) );
			offset += sizeof(vector4);

			glVertexAttribPointer( 1, 2, GL_FLOAT, GL_FALSE, stride, reinterpret_cast<const GLvoid *>( offset ) );
			offset += sizeof(vector2);

			glVertexAttribPointer( 2, 4, GL_FLOAT, GL_FALSE, stride, reinterpret_cast<const GLvoid *>( offset ) );
			offset += sizeof(vector4);
		}

		if ( 1 ) {
			cmd.ibo->Bind();
			GLint size = 0;
			glGetBufferParameteriv( GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size );
			glDrawElements( GL_TRIANGLES, size / sizeof(uint32_t), GL_UNSIGNED_INT, 0 );
		}
		else {
			glDrawArrays( GL_TRIANGLES, 0, cmd.count );
		}

		// clean up state
		if ( setWireframe ) {
			ToggleWireframe( previousWireframe );
		}
	}
示例#27
0
void BodySystemGPU<T>::setArray(BodyArray array, const T *data)
{
    assert(m_bInitialized);

    m_currentRead = 0;
    m_currentWrite = 1;

    switch (array)
    {
        default:
        case BODYSYSTEM_POSITION:
        {
            if (m_bUsePBO)
            {
                glBindBuffer(GL_ARRAY_BUFFER, m_pbo[m_currentRead]);
                glBufferSubData(GL_ARRAY_BUFFER, 0, 4 * sizeof(T) * m_numBodies, data);

                int size = 0;
                glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, (GLint *)&size);

                if ((unsigned)size != 4 * (sizeof(T) * m_numBodies))
                {
                    fprintf(stderr, "WARNING: Pixel Buffer Object download failed!n");
                }

                glBindBuffer(GL_ARRAY_BUFFER, 0);
            }
            else
            {
                if (m_bUseSysMem)
                {
                    memcpy(m_hPos[m_currentRead], data, m_numBodies * 4 * sizeof(T));
                }
                else
                    checkCudaErrors(cudaMemcpy(m_deviceData[0].dPos[m_currentRead], data,
                                               m_numBodies * 4 * sizeof(T),
                                               cudaMemcpyHostToDevice));
            }
        }
            break;

        case BODYSYSTEM_VELOCITY:
            if (m_bUseSysMem)
            {
                memcpy(m_hVel, data, m_numBodies * 4 * sizeof(T));
            }
            else
                checkCudaErrors(cudaMemcpy(m_deviceData[0].dVel, data, m_numBodies * 4 * sizeof(T),
                                           cudaMemcpyHostToDevice));

            break;
    }
}
示例#28
0
void geRendererCreateContext(ge_Scene* scene, ge_Renderer* render){
	//Create VBO
	bool first = false;
	if(render->vbo){
		int mem = 0;
		glBindBuffer(GL_ARRAY_BUFFER, render->vbo);
		glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &mem);
		libge_context->gpumem -= mem;
	}
	if(!render->vbo){
		first = true;
		int id;
		glGenBuffers(1, (GLuint*)&id);
		render->vbo = id;
	}
	glBindBuffer(GL_ARRAY_BUFFER, render->vbo);
	if(render->customVert){
		glBufferData(GL_ARRAY_BUFFER, render->customVert->size*render->nVerts, render->verts, render->memory_mode);
		libge_context->gpumem += render->customVert->size*render->nVerts;
	}else{
		glBufferData(GL_ARRAY_BUFFER, sizeof(ge_Vertex)*render->nVerts, render->verts, render->memory_mode);
		libge_context->gpumem += sizeof(ge_Vertex)*render->nVerts;
	}
	
	glEnableVertexAttribArray(0);
	glEnableVertexAttribArray(1);
	glEnableVertexAttribArray(2);
	glEnableVertexAttribArray(3);
	
	/*
	if(first){
		int i;
		glUseProgram(0);
		glLinkProgram(render->shader->programId);

		if(scene){
			for(i=0; i<scene->nDynamicLights; i++){
			}
			for(i=0; i<scene->nLights; i++){
			}
		}
		glUseProgram(render->shader->programId);
		for(i=0; i<8; i++){
			glActiveTexture(GL_TEXTURE0+i);
			glEnable(GL_TEXTURE_2D);
			char tmp[32] = "ge_Texture";
			if(i)sprintf(tmp, "ge_Texture%d", i);
			glUniform1i(glGetUniformLocation(render->shader->programId, tmp), i);
		}
		glUseProgram(0);
	}
	*/
}
示例#29
0
size_t GfxGLBuffer::getSize() const
{
    BEGIN_BUFFER_BINDING

    GLint size;

    glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);

    END_BUFFER_BINDING

    return size;
}
示例#30
0
void Renderer::draw (Mesh* mMesh)
{
	glClearColor(0.45, 0.45, 0.45, 1.0);
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	glUseProgram(mShader->program);
	if (mMesh->vbo_vertices != 0) {
      glEnableVertexAttribArray(mShader->getAttrCoord());
      glBindBuffer(GL_ARRAY_BUFFER, mMesh->vbo_vertices);
      glVertexAttribPointer(
        mShader->getAttrCoord(),  // attribute
        4,                  // number of elements per vertex, here (x,y,z,w)
        GL_FLOAT,           // the type of each element
        GL_FALSE,           // take our values as-is
        0,                  // no extra data between each position
        0                   // offset of first element
      );
    }
 
    if (mMesh->vbo_normals != 0) {
		glEnableVertexAttribArray(mShader->getAttrNormal());
      glBindBuffer(GL_ARRAY_BUFFER, mMesh->vbo_normals);
      glVertexAttribPointer(
        mShader->getAttrNormal(), // attribute
        3,                  // number of elements per vertex, here (x,y,z)
        GL_FLOAT,           // the type of each element
        GL_FALSE,           // take our values as-is
        0,                  // no extra data between each position
        0                   // offset of first element
      );
    }
    
    /* Apply object's transformation matrix */
	glUniformMatrix4fv(mShader->getUniform_M(), 1, GL_FALSE, glm::value_ptr(mMesh->object2world));
    /* Transform normal vectors with transpose of inverse of upper left
       3x3 model matrix (ex-gl_NormalMatrix): */
    glm::mat3 m_3x3_inv_transp = glm::transpose(glm::inverse(glm::mat3(mMesh->object2world)));
	glUniformMatrix3fv(mShader->getunifrom_transp(), 1, GL_FALSE, glm::value_ptr(m_3x3_inv_transp));
    
    /* Push each element in buffer_vertices to the vertex shader */
    if (mMesh->ibo_elements != 0) {
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mMesh->ibo_elements);
      int size;  glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
      glDrawElements(GL_TRIANGLES, size/sizeof(GLushort), GL_UNSIGNED_SHORT, 0);
    } else {
      glDrawArrays(GL_TRIANGLES, 0, mMesh->vertices.size());
    }
 
    if (mMesh->vbo_normals != 0)
      glDisableVertexAttribArray(mShader->getAttrNormal());
    if (mMesh->vbo_vertices != 0)
      glDisableVertexAttribArray(mShader->getAttrCoord());
}