GLSL记录(1)-实时旋转的三角形
来源:互联网 发布:数据挖掘跟gis的关系 编辑:程序博客网 时间:2024/05/14 04:10
main.cpp:
#include <iostream>#include"shadertool.h"#include"glm/glm.hpp"//using namespace std;#include"glm/gtc/matrix_transform.hpp"using glm::mat4;using glm::vec3;#define PI 3.1415926GLuint vaoHandle;GLuint programHandle;float angle = 30.0f * PI/180.0f;//转换为弧度制//we create and populate the vertex buffer objects for each attributefloat positionData[] = { -0.8f, -0.8f, .0f, 0.8f, -0.8f, 0.0f, 0.0f, 0.8f, 0.0f};float colorData[] = { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f};void changeSize(int w,int h){ if(h==0) { h=1; } float ratio=1.0*w/h; glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0,0,w,h); gluPerspective(45,ratio,1,1000); glMatrixMode(GL_MODELVIEW);}void renderScene(void){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); angle += 0.01f; mat4 rotationMatrix = glm::rotate( mat4(1.0f),angle,vec3(0.0f,0.0f,1.0f)); GLuint location = glGetUniformLocation( programHandle, "RotationMatrix"); if(location >= 0) { //printf("yes...........\n"); glUniformMatrix4fv( location, 1, GL_FALSE, &rotationMatrix[0][0]); } else { printf("no...........\n"); } //in the render func,we bind to the vertex array obj and call glDrawArrays to initiate rendering glBindVertexArray(vaoHandle); glDrawArrays(GL_TRIANGLES,0,3); glutSwapBuffers();}void processNormalKeys(unsigned char key,int x,int y){ if(key==27) { exit(0); }}void setShaders(){ char* vs_code = LoadShaderFromFile("z_basic.vert"); char* fs_code = LoadShaderFromFile("z_basic.frag"); GLuint v = glCreateShader(GL_VERTEX_SHADER);//create shader obj,返回一个handle GLuint f = glCreateShader(GL_FRAGMENT_SHADER); const char* ff = fs_code; const char* vv = vs_code; /* load the source code into shader obj,第三个参数可以是一个字符串数组,可以载入多个文件string 第二个参数是表示string array 的size,one string ,so 1. 最后一个参数表示each source code string is terminated by a null character. 一旦这个函数返回后,表明source code 已经被成功加载在opengl的内部内存,因此,之前的用来存储shader code的字符串变量 可以被释放掉了 */ glShaderSource(v,1,&vv,NULL); glShaderSource(f,1,&ff,NULL); // //free the source code free(vs_code); free(fs_code); //next step: //compile shader and log error CompileShader(v,0); CompileShader(f,1); //final step : shaders must be linked togehter into a shader program object. programHandle = glCreateProgram();//create a empty program obj glAttachShader(programHandle,f); glAttachShader(programHandle,v); //link.can use glGetProgramiv to query status(GL_TRUE/GL_FALSE)->glGetProgramiv(GL_LINK_STATUS) glLinkProgram(programHandle); //install the program into the opengl pipeline glUseProgram(programHandle); //}void InitGL(int argc,char* argv[]){ // glutInit(&argc,argv); //初始化glut,必须调用,复制黏贴这句话即可 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE ); //设置显示方式,RGB、双缓冲 glutInitWindowPosition(100,100); //位置 glutInitWindowSize(400,400);//窗口大小 glutCreateWindow("GLSL FUN!"); //创建窗口,设置标题 glutDisplayFunc(renderScene); // 当绘制窗口时调用myDisplay glutIdleFunc(renderScene); glutReshapeFunc(changeSize); glutKeyboardFunc(processNormalKeys); glEnable(GL_DEPTH_TEST); glClearColor(1.0,1.0,1.0,1.0);//清除颜色设置为白色,这样glclear后面clear color就用白色clear glewInit(); // //1.创建 //create and populate the buffer objects GLuint vboHandles[2]; glGenBuffers(2, vboHandles); GLuint positionBufferHandle = vboHandles[0]; GLuint colorBufferHandle = vboHandles[1]; //2. //populate the position buffer glBindBuffer( GL_ARRAY_BUFFER, positionBufferHandle); glBufferData( GL_ARRAY_BUFFER, 9 * sizeof(float), positionData, GL_STATIC_DRAW); //populate color buffer glBindBuffer( GL_ARRAY_BUFFER, colorBufferHandle); //最后一个参数告诉我们这个数据是用来干嘛的,static表示不能再被修改 glBufferData( GL_ARRAY_BUFFER, 9 * sizeof(float), colorData, GL_STATIC_DRAW); //3.create and set up the vertex array objects glGenVertexArrays(1, &vaoHandle); glBindVertexArray( vaoHandle ); //enable the vertex attribute arrays glEnableVertexAttribArray(0);//vertex position glEnableVertexAttribArray(1);//vertex color //map index 0 to the position buffer glBindBuffer( GL_ARRAY_BUFFER, positionBufferHandle); glVertexAttribPointer(0,3,GL_FLOAT, GL_FALSE, 0 ,NULL); //map index 1 to the color buffer glBindBuffer(GL_ARRAY_BUFFER, colorBufferHandle); glVertexAttribPointer(1,3,GL_FLOAT, GL_FALSE, 0 ,NULL);}int main(int argc,char* argv[]){ //1. InitGL(argc,argv); //2. setShaders(); //3. // LogGLVersion(); glutMainLoop(); //消息循环 // //check gl version return 0;}着色器:
z_basic.vert:
#version 430/*the main opengl program refers to vertex attributes by associating each input variable with a generic attribute index.those index are simply integers between 0 and GL_MAX_VERTEX_ATTRIBS - 1.we can specify the relationship between these indices and the attributes using the layout qualifier.*///inputlayout (location=0) in vec3 VertexPosition;layout (location=1) in vec3 VertexColor;//outputout vec3 Color;/*uniform variables are uintended to used for data that may change relatively infrequently compared to per-vertex attributes.uniform variables are well suited for the matrics used for modeling , viewing, and projective transformations.*/uniform mat4 RotationMatrix;void main(){Color = VertexColor;gl_Position = RotationMatrix * vec4 ( VertexPosition, 1.0);}z_basic.frag:
#version 430
in vec3 Color;
layout (location = 0) out vec4 FragColor;
void main()
{
FragColor = vec4(Color, 1.0);
}
shadertool.cpp:
#include"shadertool.h"char* LoadShaderFromFile( char* fileName){ FILE *fp; char* content = NULL; int count= 0; if( fileName != NULL) { fp = fopen(fileName,"rt"); if(fp != NULL) { fseek( fp, 0, SEEK_END); count = ftell(fp); rewind(fp); if(count > 0) { content = (char*)malloc( sizeof(char)*(count + 1)); count = fread(content,sizeof(char),count,fp); content[count] = '\0'; } fclose(fp); } } return content;}//compile shader and if error log it.void CompileShader(GLuint s,int type){ glCompileShader(s); //编译shader //get compile info and log it GLint result; //query compile status info by call this func. glGetShaderiv( s, GL_COMPILE_STATUS, &result); //if false , query the shader log if( GL_FALSE == result) { if(type == 0 ) fprintf(stderr,"vertex shader compile failed\n"); else fprintf(stderr,"fragment shader compile failed\n"); GLint logLen; glGetShaderiv( s, GL_INFO_LOG_LENGTH,&logLen); if( logLen > 0) { char* log = new char[ logLen ]; GLsizei written; glGetShaderInfoLog(s,logLen, &written, log); fprintf( stderr, "shader log : \n%s",log); delete[] log;//free the memeory } else { fprintf( stderr, "log length <= 0"); } }}void LogGLVersion(){ const GLubyte *renderer = glGetString(GL_RENDERER); const GLubyte *vendor = glGetString(GL_VENDOR); const GLubyte *version = glGetString(GL_VERSION); const GLubyte *glslVersion = glGetString(GL_SHADING_LANGUAGE_VERSION); GLint major, minor; glGetIntegerv(GL_MAJOR_VERSION, &major); glGetIntegerv(GL_MINOR_VERSION, &minor); printf("GL Vendor :%s\n",vendor); printf("GL Render :%s\n",renderer); printf("GL Version string :%s\n",version); printf("GL Version int :%d.%d\n",major,minor); printf("GLSL Version :%s\n",glslVersion);}
效果:
阅读全文
0 0
- GLSL记录(1)-实时旋转的三角形
- glsl移动的三角形
- GLSL变换颜色的三角形(C语言)
- 制作一个旋转的三角形
- Shader特效——“旋转粒子”的实现【GLSL】
- d3d实现一个旋转的三角形
- opengl es 实现旋转的三角形
- Java编程实例:向内旋转的三角形
- 3D编程-旋转的三角形
- CSS实现三角形的动画旋转
- open glHelloWorld之旋转的三角形(android)
- 使用unity绘制旋转的三角形
- GLSL要点记录
- OSG环境下,使用glsl的几何着色器,绘制带纹理的三角形
- 《GLSL渲染语言入门与VBO、VAO使用:绘制一个三角形》的正确版本及源代码
- opengl es3.0游戏开发学习笔记1-绘制旋转的三角形
- Javascript 实现的实时 3D 三角形的例子
- Javascript 实现的实时 3D 三角形的例子
- Problem C:松果( cone)
- mybatis开发原始dao的方式
- AngularJS 入门4-表单和验证
- 最近做的一些事的复盘
- ViewPager系列之-仿掌上英雄联盟皮肤浏览效果
- GLSL记录(1)-实时旋转的三角形
- python3 矩阵的切片
- for循环正确遍历数组
- 数据库连接池
- NLP-python自然语言工具之nltk
- 谈一谈拦截导航控制器返回事件(上)——OPP
- 开发必备---你应该知道的一些 ADB 命令 144 作者 LooperJing 关注 2016.11.27 11:45* 字数 1548 阅读 1106评论 2喜欢 39 版权声明:本文为Loop
- php 缓存类
- Vue的批量删除