OpenGL one-dimensional texture mapping exercise

Source: Internet
Author: User

It was a complete rendering code that threw away the book itself.

Index the texture color in a one-dimensional texture with diffuse light intensity. So the diffuse light intensity value is passed to the fragment shader after the vertex is transferred. After this chapter, you may consider embarking on GLSL direction.

But the implementation of the navigation grid is not yet done, how can I be good? In short, avoid writing the UI and avoid accessing the endless SDK. Today, I finally understand why the former boss said only in a fixed time version. Because the release of various versions of the time-consuming shortage can not improve technology, can be less free.


Either the SDK protocol standards are developed in the industry, and all SDKs and embedded software written in accordance with the protocol standards can be automated or one-click access. Such idealized work can only be achieved in companies with industry appeal, such as touch-ANYSDK, but not open source leads to low acceptance ah.



Vp

#version 330in vec4 vvertex;in vec3 vnormal;uniform mat4 mvmatrix;uniform mat4 mvpmatrix;uniform mat3 normalmatrix;unifor M vec3 vlightposition;smooth out float texturecoordinate;void main (void) {//Get the visual coordinates of the surface normals vec3 veyenormal = Normalmatrix * VN ormal;//gets the visual coordinates of the vertex, that is, world coordinates 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);p Rojctionmatrix.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,); 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] = {0, 0,64, 0, 0,128, 0, 0,255, 0, 0};glteximage1d (gl_texture_1d, 0, Gl_rgb, 4, 0, Gl_rgb, G L_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 (&AMP;ARGC, argv); Glutinitdisplaymode (gl_double | gl_depth | Glutinitwindowsize (Gl_stencil); 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;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

OpenGL one-dimensional texture mapping exercise

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.