/* =========================================================================
* Freetype GL - A C OpenGL Freetype engine
* Platform: Any
* WWW: http://code.google.com/p/freetype-gl/
* -------------------------------------------------------------------------
* Copyright 2011 Nicolas P. Rougier. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of Nicolas P. Rougier.
* ========================================================================= */
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include "vertex-buffer.h"
// ----------------------------------------------------------------------------
VertexBuffer *
vertex_buffer_new( const char *format )
{
size_t i, index = 0, stride = 0;
const char *start = 0, *end = 0;
GLvoid *pointer = 0;
VertexBuffer *self = (VertexBuffer *) malloc (sizeof(VertexBuffer));
if( !self )
{
return NULL;
}
self->format = strdup( format );
for( i=0; i<MAX_VERTEX_ATTRIBUTE; ++i )
{
self->attributes[i] = 0;
}
start = format;
do
{
end = (char *) (strchr(start+1, ':'));
char *desc = 0;
if (end == NULL)
{
desc = strdup(start);
}
else
{
desc = strndup(start, end-start);
}
VertexAttribute *attribute = vertex_attribute_parse( index, desc );
start = end+1;
free(desc);
attribute->pointer = pointer;
stride += attribute->size*GL_TYPE_SIZE( attribute->type );
pointer+= attribute->size*GL_TYPE_SIZE( attribute->type );
self->attributes[index] = attribute;
index++;
} while ( end && (index < MAX_VERTEX_ATTRIBUTE) );
for( i=0; i<index; ++i )
{
self->attributes[i]->stride = stride;
}
self->vertices = vector_new( stride );
self->vertices_id = 0;
self->indices = vector_new( sizeof(GLuint) );
self->indices_id = 0;
self->dirty = 1;
return self;
}
// ----------------------------------------------------------------------------
VertexBuffer *
vertex_buffer_new_from_data( char *format,
size_t vcount,
void *vertices,
size_t icount,
GLuint *indices )
{
VertexBuffer *self = vertex_buffer_new( format );
vector_resize( self->vertices, vcount );
assert( self->vertices->size == vcount);
memcpy( self->vertices->items, vertices, vcount*self->vertices->item_size );
vector_resize( self->indices, icount );
assert( self->indices->size == icount);
memcpy( self->indices->items, indices, icount*self->indices->item_size );
self->dirty = 1;
return self;
}
// ----------------------------------------------------------------------------
void
vertex_buffer_delete( VertexBuffer *self )
{
assert( self );
vector_delete( self->vertices );
self->vertices = 0;
if( self->vertices_id )
{
glDeleteBuffers( 1, &self->vertices_id );
}
self->vertices_id = 0;
vector_delete( self->indices );
self->indices = 0;
if( self->indices_id )
{
glDeleteBuffers( 1, &self->vertices_id );
}
self->indices_id = 0;
if( self->format )
{
free( self->format );
}
self->format = 0;
self->dirty = 0;
free( self );
}
// ----------------------------------------------------------------------------
void
vertex_buffer_print( VertexBuffer * self )
{
assert(self);
int i = 0;
fprintf( stderr, "%ld vertices, %ld indices\n",
vector_size( self->vertices ), vector_size( self->indices ) );
while( self->attributes[i] )
{
if( self->attributes[i]->target > 0 )
{
switch(self->attributes[i]->target )
{
case GL_VERTEX_ARRAY:
fprintf( stderr, " -> Position: ");
break;
case GL_NORMAL_ARRAY:
fprintf( stderr, " -> Normal: ");
break;
case GL_COLOR_ARRAY:
fprintf( stderr, " -> Color: ");
break;
case GL_TEXTURE_COORD_ARRAY:
fprintf( stderr, " -> Texture coord: ");
break;
case GL_FOG_COORD_ARRAY:
fprintf( stderr, " -> Fog coord: ");
break;
case GL_SECONDARY_COLOR_ARRAY:
fprintf( stderr, " -> Secondary color: ");
break;
case GL_EDGE_FLAG_ARRAY:
fprintf( stderr, " -> Edge flag: ");
break;
default:
fprintf( stderr, " -> Unknown: ");
break;
}
}
else
{
fprintf( stderr, " -> Generic attribute n°%d: ",
self->attributes[i]->index );
}
fprintf(stderr, "%dx%s (+%ld)\n",
self->attributes[i]->size,
GL_TYPE_STRING( self->attributes[i]->type ),
(long) self->attributes[i]->pointer);
i += 1;
}
}
// ----------------------------------------------------------------------------
void
vertex_buffer_upload ( VertexBuffer *self )
{
if( !self->vertices_id )
{
glGenBuffers( 1, &self->vertices_id );
}
if( !self->indices_id )
{
glGenBuffers( 1, &self->indices_id );
}
glBindBuffer( GL_ARRAY_BUFFER, self->vertices_id );
glBufferData( GL_ARRAY_BUFFER,
self->vertices->size*self->vertices->item_size,
self->vertices->items, GL_DYNAMIC_DRAW );
glBindBuffer( GL_ARRAY_BUFFER, 0 );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, self->indices_id );
glBufferData( GL_ELEMENT_ARRAY_BUFFER,
self->indices->size*self->indices->item_size,
self->indices->items, GL_DYNAMIC_DRAW );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, 0 );
}
// ----------------------------------------------------------------------------
void
vertex_buffer_clear( VertexBuffer *self )
{
assert( self );
vector_clear( self->indices );
vector_clear( self->vertices );
self->dirty = 1;
}
// ----------------------------------------------------------------------------
void
vertex_buffer_render ( VertexBuffer *self,
GLenum mode,
const char *what )
{
assert( self );
if( !self->vertices->size )
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
想改一下资源分~!资源共享,不需要这么多分啊!帮我改到 1分、2分就够了! CSDN里有一个专栏教程:一步步学OpenGL, 极客学院里面有一个教程:现代 OpenGL 教程 都是翻译的一个外文网站的教程。源码在这里。 最近在学OpenGL,发现一个快速入门的好教程。 是外文的。ogldev网站。其中有所有40多个教程的源码打包下载。 找到两个就是上面的那两个。我是从那个网站下载的,入门的朋友可以下载来看比较方便。 这个源码,不用自己安装 glut 和glew 了。
资源推荐
资源详情
资源评论











收起资源包目录





































































































共 1789 条
- 1
- 2
- 3
- 4
- 5
- 6
- 18
资源评论


jean7155
- 粉丝: 138
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


最新资源
- 阻抗导纳控制技术:Matlab Simulink参数仿真与优化研究
- 数控编程及加工工艺基础.doc
- 收藏的精品资料软件开发实习心得体会.doc
- 多视点立体视频解码算法的优化及应用.doc
- 进化论构建网络的方法.pptx
- 科研项目管理办法(某大学).doc
- MATLAB 绘图复刻-Matlab资源
- 综合布线系统线缆敷设PPT课件.ppt
- 网络培训心得体会范文5篇.doc
- 电子商务专业实践教学体系构建.doc
- 市场部网络运营专项方案.doc
- 项目管理(ppt67)(1).ppt
- 网络游戏开发的灵魂.ppt
- 数据模型决策04网络计划.ppt
- 2022年江苏大学计算机图形学第三次实验报告二维图形变换.doc
- 武汉理工大学2012年c语言考试AB卷试题及答案.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



安全验证
文档复制为VIP权益,开通VIP直接复制
