#define GLUT_DISABLE_ATEXIT_HACK
#include <iostream>
#include <gl/glew.h>
//#define GLUT_DISABLE_ATEXIT_HACK
#include <gl/GLUT.H>
#pragma comment(lib, "glew32.lib")
using namespace std;
// cube ///////////////////////////////////////////////////////////////////////
// v6----- v5
// /| /|
// v1------v0|
// | | | |
// | |v7---|-|v4
// |/ |/
// v2------v3
// vertex coords array for glDrawArrays() =====================================
// A cube has 6 sides and each side has 2 triangles, therefore, a cube consists
// of 36 vertices (6 sides * 2 tris * 3 vertices = 36 vertices). And, each
// vertex is 3 components (x,y,z) of floats, therefore, the size of vertex
// array is 108 floats (36 * 3 = 108).
GLfloat vertices[] = {
1, 1, 1, -1, 1, 1, -1,-1, 1, // v0-v1-v2 (front)
-1,-1, 1, 1,-1, 1, 1, 1, 1, // v2-v3-v0
1, 1, 1, 1,-1, 1, 1,-1,-1, // v0-v3-v4 (right)
1,-1,-1, 1, 1,-1, 1, 1, 1, // v4-v5-v0
1, 1, 1, 1, 1,-1, -1, 1,-1, // v0-v5-v6 (top)
-1, 1,-1, -1, 1, 1, 1, 1, 1, // v6-v1-v0
-1, 1, 1, -1, 1,-1, -1,-1,-1, // v1-v6-v7 (left)
-1,-1,-1, -1,-1, 1, -1, 1, 1, // v7-v2-v1
-1,-1,-1, 1,-1,-1, 1,-1, 1, // v7-v4-v3 (bottom)
1,-1, 1, -1,-1, 1, -1,-1,-1, // v3-v2-v7
1,-1,-1, -1,-1,-1, -1, 1,-1, // v4-v7-v6 (back)
-1, 1,-1, 1, 1,-1, 1,-1,-1 }; // v6-v5-v4
// normal array
GLfloat normals[] = {
0, 0, 1, 0, 0, 1, 0, 0, 1, // v0-v1-v2 (front)
0, 0, 1, 0, 0, 1, 0, 0, 1, // v2-v3-v0
1, 0, 0, 1, 0, 0, 1, 0, 0, // v0-v3-v4 (right)
1, 0, 0, 1, 0, 0, 1, 0, 0, // v4-v5-v0
0, 1, 0, 0, 1, 0, 0, 1, 0, // v0-v5-v6 (top)
0, 1, 0, 0, 1, 0, 0, 1, 0, // v6-v1-v0
-1, 0, 0, -1, 0, 0, -1, 0, 0, // v1-v6-v7 (left)
-1, 0, 0, -1, 0, 0, -1, 0, 0, // v7-v2-v1
0,-1, 0, 0,-1, 0, 0,-1, 0, // v7-v4-v3 (bottom)
0,-1, 0, 0,-1, 0, 0,-1, 0, // v3-v2-v7
0, 0,-1, 0, 0,-1, 0, 0,-1, // v4-v7-v6 (back)
0, 0,-1, 0, 0,-1, 0, 0,-1 }; // v6-v5-v4
// color array
GLfloat colors[] = {
1, 1, 1, 1, 1, 0, 1, 0, 0, // v0-v1-v2 (front)
1, 0, 0, 1, 0, 1, 1, 1, 1, // v2-v3-v0
1, 1, 1, 1, 0, 1, 0, 0, 1, // v0-v3-v4 (right)
0, 0, 1, 0, 1, 1, 1, 1, 1, // v4-v5-v0
1, 1, 1, 0, 1, 1, 0, 1, 0, // v0-v5-v6 (top)
0, 1, 0, 1, 1, 0, 1, 1, 1, // v6-v1-v0
1, 1, 0, 0, 1, 0, 0, 0, 0, // v1-v6-v7 (left)
0, 0, 0, 1, 0, 0, 1, 1, 0, // v7-v2-v1
0, 0, 0, 0, 0, 1, 1, 0, 1, // v7-v4-v3 (bottom)
1, 0, 1, 1, 0, 0, 0, 0, 0, // v3-v2-v7
0, 0, 1, 0, 0, 0, 0, 1, 0, // v4-v7-v6 (back)
0, 1, 0, 0, 1, 1, 0, 0, 1 }; // v6-v5-v4
GLuint vboId = 0;
//camera
float cameraAngleX;
float cameraAngleY;
float cameraDistance = -5.0;
//mouse
bool mouseLeftDown;
bool mouseRightDown;
float LastXPos;
float LastYPos;
void initGL()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glGenBuffers(1, &vboId);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices)+sizeof(normals)+sizeof(colors), 0, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); //第二个数据是偏移
glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices), sizeof(normals), normals);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertices)+sizeof(normals), sizeof(colors), colors);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glLoadIdentity();
glTranslatef(0, 0, cameraDistance);
glRotatef(cameraAngleX, 1, 0, 0);
glRotatef(cameraAngleY, 0, 1, 0);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glNormalPointer(GL_FLOAT, 0, (void*)sizeof(vertices)); //最后一个值为偏移
glColorPointer(3, GL_FLOAT, 0, (void*)(sizeof(vertices)+sizeof(normals)));
glVertexPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 36);//从0开始
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glPopMatrix();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f, (GLfloat)w/(GLfloat)h, 0.01f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
mouseLeftDown = true;
LastXPos = x;
LastYPos = y;
}
/*if (button == GLUT_RIGHT_BUTTON && state == GLUT_UP)
{
mouseRightDown = true;
}*/
}
void mouseMotion(int x, int y)
{
if (mouseLeftDown)
{
cameraAngleX += GLfloat(y - LastYPos)/GLfloat(20.0);
cameraAngleY += GLfloat(x - LastXPos)/GLfloat(20.0);
LastYPos = y;
LastXPos = x;
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(480, 640);
glutInitWindowPosition(100, 100);
glutCreateWindow("VBO");
glewInit();
if(!glewIsSupported("GL_VERSION_2_0"))
{
fprintf(stderr, "ERROR: Support for necessary OpengGL extensions missing.");
}
initGL();
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMotionFunc(mouseMotion);
glutMainLoop();
glDeleteBuffers(GL_ARRAY_BUFFER, &vboId);
return 0;
}
没有合适的资源?快使用搜索试试~ 我知道了~
VBO.rar_glew_opengl vbo_vbo
共41个文件
tlog:22个
pdb:2个
manifest:2个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 78 浏览量
2022-09-24
20:26:15
上传
评论
收藏 6.95MB RAR 举报
温馨提示
运用定点数组绘制的三维图像,必须包含glew文件,适合Opengl初学者
资源推荐
资源详情
资源评论
收起资源包目录
VBO.rar (41个子文件)
VBO
VBO.sln 876B
VBO
VBO.vcxproj.user 143B
VBO.vcxproj.filters 941B
VBO.cpp 6KB
Debug
vc100.idb 435KB
link.6632-cvtres.write.1.tlog 2B
CL.write.1.tlog 248B
VBO.lastbuildstate 50B
CL.read.1.tlog 6KB
VBO.exe.embed.manifest 406B
mt.read.1.tlog 246B
rc.write.1.tlog 226B
rc.read.1.tlog 218B
link.6632.write.1.tlog 2B
VBO.log 2KB
mt.command.1.tlog 332B
cl.command.1.tlog 590B
link-cvtres.read.1.tlog 2B
link.5120-cvtres.read.1.tlog 2B
link.5120-cvtres.write.1.tlog 2B
link.write.1.tlog 564B
VBO.exe.intermediate.manifest 381B
VBO_manifest.rc 196B
link.5120.write.1.tlog 2B
link-cvtres.write.1.tlog 2B
VBO.obj 40KB
link.command.1.tlog 1KB
rc.command.1.tlog 432B
link.read.1.tlog 3KB
VBO.exe.embed.manifest.res 472B
mt.write.1.tlog 246B
link.6632-cvtres.read.1.tlog 2B
link.5120.read.1.tlog 2B
link.6632.read.1.tlog 2B
vc100.pdb 236KB
VBO.vcxproj 4KB
VBO.sdf 32.71MB
VBO.suo 14KB
ipch
vbo-a1c104c3
Debug
VBO.pdb 611KB
VBO.exe 34KB
VBO.ilk 385KB
共 41 条
- 1
资源评论
寒泊
- 粉丝: 85
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功