博客
关于我
【OpenGL】平面着色器实例代码
阅读量:487 次
发布时间:2019-03-07

本文共 2838 字,大约阅读时间需要 9 分钟。

 

// FlatShader.cpp// OpenGL SuperBible// Demonstrates a simple flat shader with transformations// Program by Richard S. Wright Jr.#pragma comment(lib, "gltools.lib")#include 
// OpenGL toolkit#include
#include
#include
#include
#include
#include
#ifdef __APPLE__#include
#else#define FREEGLUT_STATIC#include
#endifGLFrame viewFrame;GLFrustum viewFrustum;GLTriangleBatch torusBatch;GLMatrixStack modelViewMatrix;GLMatrixStack projectionMatrix;GLGeometryTransform transformPipeline;GLuint flatShader; // The Flat shaderGLint locMVP; // The location of the ModelViewProjection matrix uniformGLint locColor; // The location of the color value uniform// This function does any needed initialization on the rendering// context. void SetupRC(void){ // Background glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glEnable(GL_DEPTH_TEST); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); viewFrame.MoveForward(4.0f); // Make the torus gltMakeTorus(torusBatch, .80f, 0.25f, 52, 26); flatShader = gltLoadShaderPairWithAttributes("FlatShader.vp", "FlatShader.fp", 1, GLT_ATTRIBUTE_VERTEX, "vVertex"); locMVP = glGetUniformLocation(flatShader, "mvpMatrix"); locColor = glGetUniformLocation(flatShader, "vColorValue");}// Cleanupvoid ShutdownRC(void){}// Called to draw scenevoid RenderScene(void){ static CStopWatch rotTimer; // Clear the window and the depth buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); modelViewMatrix.PushMatrix(viewFrame); modelViewMatrix.Rotate(rotTimer.GetElapsedSeconds() * 10.0f, 0.0f, 1.0f, 0.0f); GLfloat vColor[] = { 0.1f, 0.1f, 1.f, 1.0f }; glUseProgram(flatShader); glUniform4fv(locColor, 1, vColor); glUniformMatrix4fv(locMVP, 1, GL_FALSE, transformPipeline.GetModelViewProjectionMatrix()); torusBatch.Draw(); modelViewMatrix.PopMatrix(); glutSwapBuffers(); glutPostRedisplay();}void ChangeSize(int w, int h){ // Prevent a divide by zero if (h == 0) h = 1; // Set Viewport to window dimensions glViewport(0, 0, w, h); viewFrustum.SetPerspective(35.0f, float(w) / float(h), 1.0f, 100.0f); projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix()); transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);}///// Main entry point for GLUT based programsint main(int argc, char* argv[]){ gltSetWorkingDirectory(argv[0]); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL); glutInitWindowSize(800, 600); glutCreateWindow("Simple Transformed Geometry"); glutReshapeFunc(ChangeSize); glutDisplayFunc(RenderScene); GLenum err = glewInit(); if (GLEW_OK != err) { fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err)); return 1; } SetupRC(); glutMainLoop(); ShutdownRC(); return 0;}

OpenGL案例代码发现的问题:

1、ShutdownRC函数没有glDeleteProgram(flatShader);

2、RenderScene函数中一直在创建vColor一维数组

转载地址:http://czycz.baihongyu.com/

你可能感兴趣的文章
mysql 存储过程每隔一段时间执行一次
查看>>
mysql 存在update不存在insert
查看>>
Mysql 学习总结(86)—— Mysql 的 JSON 数据类型正确使用姿势
查看>>
Mysql 学习总结(87)—— Mysql 执行计划(Explain)再总结
查看>>
Mysql 学习总结(88)—— Mysql 官方为什么不推荐用雪花 id 和 uuid 做 MySQL 主键
查看>>
Mysql 学习总结(89)—— Mysql 库表容量统计
查看>>
mysql 实现主从复制/主从同步
查看>>
mysql 审核_审核MySQL数据库上的登录
查看>>
mysql 导入 sql 文件时 ERROR 1046 (3D000) no database selected 错误的解决
查看>>
mysql 导入导出大文件
查看>>
MySQL 导出数据
查看>>
mysql 将null转代为0
查看>>
mysql 常用
查看>>
MySQL 常用列类型
查看>>
mysql 常用命令
查看>>
Mysql 常见ALTER TABLE操作
查看>>
MySQL 常见的 9 种优化方法
查看>>
MySQL 常见的开放性问题
查看>>
Mysql 常见错误
查看>>
mysql 常见问题
查看>>