
【OpenGL】只有纹理单元TexturedTriangle测试案例
发布日期:2021-05-10 02:21:00
浏览次数:20
分类:精选文章
本文共 4365 字,大约阅读时间需要 14 分钟。
需准备一张.tga纹理
// The TexturedIdentity Shader// Vertex Shader// Richard S. Wright Jr.// OpenGL SuperBible#version 130in vec4 vVertex;in vec2 vTexCoords;smooth out vec2 vVaryingTexCoords;void main(void) { vVaryingTexCoords = vTexCoords; gl_Position = vVertex; }
vTexCoords存储了纹理s和t坐标,作为输入,然后插值输出到片段着色器
// The TexturedIdentity Shader// Fragment Shader// Richard S. Wright Jr.// OpenGL SuperBible#version 130uniform sampler2D colorMap;out vec4 vFragColor;smooth in vec2 vVaryingTexCoords;void main(void) { vFragColor = texture(colorMap, vVaryingTexCoords.st); }
texture(纹理,纹理坐标vec2)得到vec4 颜色值。sampler2D采样器类型,实际上是一个整数值,在GL代码用glUniform1i设定了一个整数值0,代表纹理单元0,即第一个纹理贴图。
// TexturedTriangle.cpp// Our first OpenGL program that will just draw a triangle on the screen.#pragma comment(lib, "gltools.lib")#include// OpenGL toolkit#include // Shader Manager Class#ifdef __APPLE__#include // OS X version of GLUT#else#define FREEGLUT_STATIC#include // Windows FreeGlut equivalent#endifGLBatch triangleBatch;GLShaderManager shaderManager;GLint myTexturedIdentityShader;GLuint textureID;///// Window has changed size, or has just been created. In either case, we need// to use the window dimensions to set the viewport and the projection matrix.void ChangeSize(int w, int h){ glViewport(0, 0, w, h);}// Load a TGA as a 2D Texture. Completely initialize the statebool LoadTGATexture(const char *szFileName, GLenum minFilter, GLenum magFilter, GLenum wrapMode){ GLbyte *pBits; int nWidth, nHeight, nComponents; GLenum eFormat; // Read the texture bits pBits = gltReadTGABits(szFileName, &nWidth, &nHeight, &nComponents, &eFormat); if (pBits == NULL) return false; glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexImage2D(GL_TEXTURE_2D, 0, nComponents, nWidth, nHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBits); free(pBits); if (minFilter == GL_LINEAR_MIPMAP_LINEAR || minFilter == GL_LINEAR_MIPMAP_NEAREST || minFilter == GL_NEAREST_MIPMAP_LINEAR || minFilter == GL_NEAREST_MIPMAP_NEAREST) glGenerateMipmap(GL_TEXTURE_2D); return true;}///// This function does any needed initialization on the rendering context. // This is the first opportunity to do any OpenGL related tasks.void SetupRC(){ // Blue background glClearColor(0.0f, 0.0f, 0.0f, 1.0f); shaderManager.InitializeStockShaders(); // Load up a triangle GLfloat vVerts[] = { -0.5f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f }; GLfloat vTexCoords[] = { 0.0f, 0.0f, 1.0f, 0.0f, 0.5f, 1.0f }; triangleBatch.Begin(GL_TRIANGLES, 3, 1); triangleBatch.CopyVertexData3f(vVerts); triangleBatch.CopyTexCoordData2f(vTexCoords, 0); triangleBatch.End(); myTexturedIdentityShader = gltLoadShaderPairWithAttributes("TexturedIdentity.vp", "TexturedIdentity.fp", 2, GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_TEXTURE0, "vTexCoords"); glGenTextures(1, &textureID); glBindTexture(GL_TEXTURE_2D, textureID); LoadTGATexture("stone.tga", GL_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE);}///// Cleanupvoid ShutdownRC(){ glDeleteProgram(myTexturedIdentityShader); glDeleteTextures(1, &textureID);}///// Called to draw scenevoid RenderScene(void){ // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glUseProgram(myTexturedIdentityShader); glBindTexture(GL_TEXTURE_2D, textureID); GLint iTextureUniform = glGetUniformLocation(myTexturedIdentityShader, "colorMap"); glUniform1i(iTextureUniform, 0); triangleBatch.Draw(); // Perform the buffer swap to display back buffer glutSwapBuffers();}///// 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("Textured Triangle"); 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;}
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年04月18日 17时19分21秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Problem G. The Stones Game【取石子博弈 & 思维】
2021-05-10
HRBUST—1891 A + B Problem VII
2021-05-10
框架综合实践(3)-业务逻辑businessView的封装
2021-05-10
Robot Framework 新建资源文件-用户关键字
2021-05-10
HDU - 4289 Control 拆点最大流,领悟拆点的真谛吧!
2021-05-10
HDU - 2732 Leapin‘ Lizards 拆点最大流+BFS 学好英语QAQ
2021-05-10
导弹拦截 NOIP1999 dilworth定理裸题
2021-05-10
java数组初始化及内存分析
2021-05-10
Unable to execute dex: Multiple dex files
2021-05-10
3分钟搞懂js的冒泡和捕获?
2021-05-10
Mac电脑调用自带的命令行窗口
2021-05-10
终端查看本机ip地址
2021-05-10
vue前端导出多级表头的excel表
2021-05-10
初学微信小程序开发
2021-05-10
通过Python openpyxl库对excel进行操作
2021-05-10
eclipse自动补全代码(Auto activation只能输3个字符)
2021-05-10
svn commit failed:is scheduled for addition
2021-05-10
Java多线程
2021-05-10
Unity监听日记
2021-05-10