OpenGL一維紋理映射練習

來源:互聯網
上載者:User

標籤:opengl

算是拋開書本自己敲了一個完整的渲染代碼。

用漫射光強度在一維紋理中索引紋理顏色。所以從頂點轉配之後傳給片段著色器的是漫射光強度值即可。過這章之後可以考慮著手glsl方向。

可是導航網格的實現還沒搞定,我該如何是好?總之,避免寫UI,避免接入無休止的SDK。時至今日,終於明白昔日的老大為何說只在固定的時間發版。因為各種版本的發布耗時耗力缺不能提高技術,能少則免。


要麼是在行業中制定SDK協議標準,所有按照該協議標準編寫的SDK和被嵌入軟體均可實現自動化或一鍵接入。這麼理想化的工作只能在有行業號召力的公司實現,比如觸控的anySDK,但是不開源導致接受度低啊。



//vp

#version 330in vec4 vVertex;in vec3 vNormal;uniform mat4 mvMatrix;uniform mat4 mvpMatrix;uniform mat3 normalMatrix;uniform vec3 vLightPosition;smooth out float textureCoordinate;void main(void){//獲得表面法線的視覺座標vec3 vEyeNormal = normalMatrix * vNormal;//獲得頂點的視覺座標,即全局座標vec4 vPosition4 = mvMatrix * vVertex;vec3 vPosition3 = vPosition4.xyz/vPosition4.w;vec3 vLightDir = normalize(vLightPosition - vPosition3);textureCoordinate = max(0.0f,dot(vLightDir,vEyeNormal));gl_Position = mvpMatrix * vVertex;}


//fp

#version 330smooth in float textureCoordinate;uniform sampler1D colorTable;out vec4 vFragColor;void main(void){vFragColor = texture(colorTable,textureCoordinate);}




//cpp

#include <GLTools.h>#include <GLMatrixStack.h>#include <GLGeometryTransform.h>#include <GLFrustum.h>#include <GLFrame.h>#include <GLTriangleBatch.h>#include <StopWatch.h>#ifdef __APPLE__#include <glut/glut.h>#else#define FREEGLUT_STATIC#include <GL/glut.h>#endifGLMatrixStack modelViewMatrix;GLMatrixStack mvpMatrix;GLMatrixStack projctionMatrix;GLFrustum viewFrustum;GLFrame viewFrame;GLGeometryTransform transformPipeLine;GLTriangleBatch torusBatch;GLuint toonShader;GLuint uiTexture;GLint locMV;GLint locMVP;GLint locNM;GLint locLP;GLint locColorTable;static  GLfloat vEyeLight[] = { -100.0f, 100.0f, 100.0f };void ChangeSize(int w, int h){if (h <= 0){h = 1;}glViewport(0, 0, w, h);viewFrustum.SetPerspective(35.0f, float(w) / float(h), 1.0f, 100.0f);projctionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());transformPipeLine.SetMatrixStacks(modelViewMatrix, projctionMatrix);}void SetupRC(void){glClearColor(0.0f, 0.0f, 0.0f, 1.0f);glEnable(GL_DEPTH_TEST);viewFrame.MoveForward(7.0f);gltMakeTorus(torusBatch, 0.8f, 0.25f, 52, 26);toonShader = gltLoadShaderPairWithAttributes("toonShader.vp", "toonShader.fp", 2, GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_NORMAL, "vNormal");locMV = glGetUniformLocation(toonShader, "mvMatrix");locMVP = glGetUniformLocation(toonShader, "mvpMatrix");locNM = glGetUniformLocation(toonShader, "normalMatrix");locLP = glGetUniformLocation(toonShader, "vLightPosition"); locColorTable = glGetUniformLocation(toonShader, "colorTable");glGenTextures(1, &uiTexture);glBindTexture(GL_TEXTURE_1D, uiTexture);GLubyte textureData[4][3] = {32, 0, 0,64, 0, 0,128, 0, 0,255, 0, 0};glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, 4, 0, GL_RGB, GL_UNSIGNED_BYTE, textureData);//todoglTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);}void RenderScene(void){static CStopWatch rotTimer;glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);modelViewMatrix.PushMatrix(viewFrame);{modelViewMatrix.Rotate(rotTimer.GetElapsedSeconds() * 10.f, 0.0f, 1.0f, 0.0f);glUseProgram(toonShader);glUniformMatrix4fv(locMV, 1, GL_FALSE, transformPipeLine.GetModelViewMatrix());glUniformMatrix4fv(locMVP, 1, GL_FALSE, transformPipeLine.GetModelViewProjectionMatrix());glUniformMatrix3fv(locNM, 1, GL_FALSE, transformPipeLine.GetNormalMatrix());glUniform3fv(locLP, 1, vEyeLight);glUniform1i(locColorTable, 0);torusBatch.Draw();}modelViewMatrix.PopMatrix();glutSwapBuffers();glutPostRedisplay();}void ShutdownRC(void){}int main(int argc, char * argv[]){gltSetWorkingDirectory(argv[0]);glutInit(&argc, argv);glutInitDisplayMode(GL_DOUBLE | GL_DEPTH | GL_STENCIL);glutInitWindowSize(800, 600);glutCreateWindow("ToonShader Jingz");glutReshapeFunc(ChangeSize);glutDisplayFunc(RenderScene);GLenum err = glewInit();if (err != GLEW_OK){fprintf(stderr, "GLEW ERROR:%s\n", glewGetErrorString(err));return 1;}SetupRC();glutMainLoop();ShutdownRC();return 0;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

OpenGL一維紋理映射練習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.