Use Openframeworks to learn a new version without configuration OpenGL II use the loaded image with your own

Source: Internet
Author: User

Continuation of the previous article http://blog.csdn.net/shenmifangke/article/details/50878084

Based on the content of this page http://bullteacher.com/7-textures.html

Discover loading images also use Soil Image Library Soil representative simple OpenGL Image Library

So here continue to write, use openframeworks inside the Freeimage library, because the default integration in the inside, so use very convenient

Use pictures and shader files Remember to put them in the Data folder

The effect is an effect of loading a PNG and a JPG overlay as shown in the figure below


The footage is shown below


(Pictures are boring when you draw the feeling strange please ignore)

Http://blog.csdn.net/shenmifangke

Directly below the code

OfApp.h

#pragma once#include "OfMain.h" Class Ofapp:public ofbaseapp{public:void setup (); void update (); void draw (); void keypressed (int key), void keyreleased (int key), void mousemoved (int x, int y), void mousedragged (int x, int y, int button), V OID mousepressed (int x, int y, int button), void mousereleased (int x, int y, int button), void windowresized (int w, int h), V OID dragevent (Ofdraginfo draginfo); void Gotmessage (Ofmessage msg); string Loadfragmentshader (string spath); Gluint Texture1; Gluint Texture2;};

OfApp.cpp The key part has been annotated with the band, not using the soil library

#include "ofApp.h"//The following library needs to be rotated in another place. We use a library//#include <glm/glm.hpp>//#include <glm/gtc/matrix_    transform.hpp>//#include <glm/gtc/type_ptr.hpp>string ofapp::loadfragmentshader (string sPath) {OfFile F;    F.open (Oftodatapath (spath), Offile::readwrite, false);    String CCC = F.readtobuffer (). GetText (); return CCC;} Gluint EBO; Gluint Vbo; Gluint vao;const char* vertex_shader;const char* fragment_shader; Gluint shader_programme;//core-profile mode all new versions of OpenGL are based on 3.3//The use of this is Glfw3void Ofapp::setup () {//  There are 4 points to represent a block so use Ebo if you do not want to use Ebo must be six points to represent two triangles//So Ebo is to save the vertex amount glfloat points[] = {//positions//Colors//Texture Coords 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,//Top right 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f ,//Bottom right-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,//Bottom left-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0. 0f, 0.0f, 1.0f//Top left}; Gluint indices[] = {//starts at 0! Index array This is a plurality of trigonometric prerequisites 0, 1, 3,//First triangle 1,2, 3///second triangle};//----------------------------------------------------Vao binding glgenbuffers (1, &ebo); Glbindbuffer ( Gl_element_array_buffer, EBO); Glbufferdata (Gl_element_array_buffer, sizeof (indices), indices, gl_static_draw); Vao = 0;glgenvertexarrays (1, &vao); Glbindvertexarray (VAO); Vbo = 0;glgenbuffers (1, &vbo); Glbindbuffer (GL_ARRAY_ BUFFER, VBO); Glbufferdata (Gl_array_buffer, sizeof (points), points, gl_static_draw);// Glbufferdata is a function that can be used to copy user-defined data into the current binding buffer with sizeof (points)//vertex attribute glvertexattribpointer (0, 3, gl_float, Gl_false, 8 * sizeof (Glfloat), (glvoid*) 0); Glenablevertexattribarray (0);//Color Properties Glvertexattribpointer (1, 3, Gl_float, Gl_false, 8 * sizeof (Glfloat), (glvoid*) (glfloat sizeof)), Glenablevertexattribarray (1);//UV Properties This is an added Glvertexattribpointer (2 , 2, Gl_float,gl_false, 8 * sizeof (Glfloat), (Glvoid *) (6*sizeof (glfloat))); Glenablevertexattribarray (2); Glbindvertexarray (0);//----------------------------------------------------Vao bind string a = Loadfragmentshader (" FragMent.frag "); string B = Loadfragmentshader (" Vertex_shader.vert "); Fragment_shader = A.c_str (); vertex_shader = B.c_str () ; Glint success;//whether error Glchar infolog[512];//store error message container//dynamic compile GLSL source code creates a shader object and references its ID again. So we store this vertex shader as gluint, and then create a shader with glcreateshader gluint vs = Glcreateshader (gl_vertex_shader);// The shader type is provided with Glcreateshader as its argument. Here we pass the parameter is Gl_vertex_shader this creates a vertex shader Glshadersource (VS, 1, &vertex_shader, NULL);//Append the shader source to the shader object and compile it The second parameter specifies how many strings are in the source code, and there is only one. The third parameter is the vertex shader true source Glcompileshader (VS);//This step is followed by error detection//vs vertex shader error detection Glgetshaderiv (VS, Gl_compile_status, &success    );//used to detect error if (!success) {Glgetshaderinfolog (VS, +, NULL, infoLog); Std::cout << "error::shader::vertex::compilation_failed\n" << infoLog << Std::endl;} Gluint fs = Glcreateshader (Gl_fragment_shader);//Use Gl_fragment_shader as Shader type Glshadersource (FS, 1, &fragment_    shader, NULL); Glcompileshader (fs);//fs fragment shader error detection Glgetshaderiv (FS, Gl_compile_status, &success); if (!success) { Glgetshaderinfolog (fs, +, NULL, infoLog); Std::cout << "error::shader::franment::compilation_failed\n" << infoLog << Std::endl;} The shader program object (shader) is the last linked version of multiple shaders. We have to link them as a shader program object when rendering an object to activate the shader program. Linking a shader to a program is equivalent to linking the output of each shader to the input of the next shader. Shader_programme = Glcreateprogram (); Glattachshader (Shader_programme, FS); Glattachshader (Shader_programme, VS); Gllinkprogram (Shader_programme);//Shader Program object additional error detection GLGETPROGRAMIV (Shader_programme, Gl_link_status, &success); if (!success)    {Glgetprograminfolog (shader_programme, MB, NULL, INFOLOG); Std::cout << "Last shader program object error \ n" << infoLog << Std::endl;} Finally delete the Shader object Gldeleteshader (VS); Gldeleteshader (fs);//////////////////////////////build binding texture Here's not using the simple OpenGL in the tutorial The Image library (simple OpenGL image gallery) instead uses the Freeimage library of the Ofimage image;//to convert image.getpixels () to const using the of-band conversion glvoid* Format image.loadimage ("1.png"), or//placed in the Data directory Ofimage image2;image2.loadimage ("2.jpg"); Glgentextures (1, &texture1); Glbindtexture (gl_texture_TEXTURE1)///Texture placement (Texture wrapping) Gltexparameteri (gl_texture_2d, gl_texture_wrap_s, gl_mirrored_repeat); Gltexparameteri (gl_texture_2d, gl_texture_wrap_t, gl_mirrored_repeat);//Texture filtering (TEXTURE Filtering)//Intermediate items Texture filtering can be set to zoom in and out, so you can use the nearest filter when the texture is zoomed in and use linear filtering when zoomed in. We must specify the filtering mode by gltexparameter* for zooming in and out. Gltexparameteri (gl_texture_2d, Gl_texture_min_filter, gl_nearest); Gltexparameteri (gl_texture_2d, GL_TEXTURE_MAG_ FILTER, gl_linear);//The first parameter specifies the texture target (environment); setting to gl_texture_2d means that textures are generated on the same target as the currently bound texture object (any binding to gl_texture_1d and Gl_ Texture_3d textures are not affected). The second parameter specifies the mipmap level for the texture that we intend to create, if you want to manually set each mimap level individually. Here we fill 0 basic level. The third parameter tells OpenGL what format we want to store the texture in. Our image has only RGB values, so we store the texture as an RGB value. The fourth and fifth parameters set the final texture width and height. We store them in advance when we load the images so that we can use the corresponding variables. The sixth parameter should always be set to 0 (legacy problem). The seventh and eighth parameters define the format and data type of the source graph. We use RGB values to load this image and store them in char (byte)///The last parameter is real image data. Glteximage2d (gl_texture_2d, 0, Gl_rgba, image.width, image.height, 0, Gl_rgba, Gl_unsigned_byte, Image.getPixels ());// This is changed to RGBA because of the transparent Pngglgeneratemipmap (gl_texture_2d); Glbindtexture (Gl_texture_2d, 0);//after a texture and corresponding mipmap are generated, it is important to unbind the texture object and release the memory of the image. Glgentextures (2, &texture2); Glbindtexture (gl_texture_2d, Texture2); Gltexparameteri (gl_texture_2d, GL_TEXTURE_ wrap_s, gl_mirrored_repeat); Gltexparameteri (gl_texture_2d, gl_texture_wrap_t, gl_mirrored_repeat); glTexParameteri (gl_texture_2d, Gl_texture_min_filter, gl_nearest); Gltexparameteri (gl_texture_2d, Gl_texture_mag_filter, GL_LINEAR ); Glteximage2d (gl_texture_2d, 0, Gl_rgb, image2.width, image2.height, 0, Gl_rgb, Gl_unsigned_byte, Image2.getPixels ()) ;//This is changed to RGB because of the use of Jpgglgeneratemipmap (gl_texture_2d); Glbindtexture (gl_texture_2d, 1);} --------------------------------------------------------------void Ofapp::update () {} #include <glfw/glfw3.h The >//glfwgettime function needs to be introduced//--------------------------------------------------------------void Ofapp::d raw () {  Glclearcolor (0.2f, 0.3f, 0.3f, 1.0f); Glclear (gl_color_buffer_bit); Gluseprogram (Shader_programme);//Activate multiple textures glactivetexture (GL_TEXTURE0); This element can be ignored when activating the texture element's single texture before binding the texture Glbindtexture (gl_texture_2d, Texture1); Gluniform1i (Glgetuniformlocation (Shader_programme, "OurTexture1"), 0);// This value can be ignored when a single texture is glactivetexture (gl_texture1); Activates the texture unit 2glBindTexture (gl_texture_2d, Texture2); Gluniform1i (Glgetuniformlocation (Shader_programme, "OurTexture2"  ), 1);  Glbindvertexarray (VAO);  Glbindbuffer (Gl_element_array_buffer, EBO);  Gldrawelements (Gl_triangles, 6, gl_unsigned_int, 0); Glbindvertexarray (0);} --------------------------------------------------------------void ofapp::keypressed (int key) {}void Ofapp:: keyreleased (int key) {}void ofapp::mousemoved (int x, int y) {}void ofapp::mousedragged (int x, int y, int button) {}void OFAP p::mousepressed (int x, int y, int button) {}void ofapp::mousereleased (int x, int y, int button) {}void ofapp::windowresized ( int W, int h) {}void ofapp::gotmessage (ofmessage msg) {}void Ofapp::d ragevent (Ofdraginfo draginfo) {}

Fragment.frag

#version the VEC3 ourcolor;in vec2 texcoord;//new texture out vec4 color; Uniform sampler2d ourtexture1;//New Texture Sampler Uniform sampler2d ourtexture2;//Second texture void main () {vec2 Texcoord = VEC2 (texcoord . x,1-texcoord.y);//upside-down    //color = Vec4 (Ourcolor, 1.0f); color = Texture (ourTexture1, texcoord);// We use GLSL's built-in texture function to sample the color of the texture, its first parameter is the texture sampler, the second parameter is the corresponding texture coordinate//simple blend color = texture (ourTexture1, Texcoord) * VEC4 (Ourcolor , 1.0f);//Picture Mixed color = Mix (Texture (ourTexture1, texcoord), Texture (OurTexture2, texcoord), 0.5);}

Vertex_shader.vert

#version corelayout (location = 0) in vec3 position; The positional variable's attribute position is 0layout (location = 1) in VEC3 color; The attribute of the color variable position to 1layout (location = 2) in vec2 texcoord;//new texture out vec3 ourcolor; To fragment shader output a color out vec2 texcoord;//new texture void main () {    gl_position = VEC4 (Position, 1.0f);    Ourcolor = color; Set Ourcolor to the input color we get from the vertex data Texcoord = texcoord;//New Texture}

The next estimate will also write a use of the own matrix,

Because the next tutorial on the matrix actually still use another library, general just getting started learning to be trouble






Use Openframeworks to learn a new version without configuration OpenGL II use the loaded image with your own

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.