Learn opengl-with me first triangle

Source: Internet
Author: User

The previous article described the basic concepts of OpenGL, and the following describes the use of OpenGL as an example.


Triangle.cpp 1 #include <GL/glew.h> 2 #include <g  L/freeglut.h> 3 #include <iostream> 4 5 #define BUFFER_OFFSET (x) ((const void*) (x)) 6 7 enum Vao_ids
  {triangles, Numvaos};
  8 enum Buffer_ids {ArrayBuffer, numbuffers};
 9 enum Attrib_ids {vposition = 0};
 Ten Gluint Vaos[numvaos];
 Gluint Buffers[numbuffers];
 Gluint program;
 0 Const glchar* Vertex_shader = "#version 430 core \ n" Layout (location =) \ n "                                 + "in Vec4 vposition;\n" "void main () \ n" 19                                 "{\ n" gl_position = vposition;\n "21
 "}\n"; glchar* Fragment_shader = "#version 430 core\ n "Uniform vec4 color;\n" out Vec4 fcolor;\n "                                 "Void main () \ n" "{\ n" 28
 "Fcolor = color;\n" "}\n"; void Init_shader () +//vertex shader Gluint vertexshader = Glcreateshader (gl_vertex_shader
 );
 Glshadersource (vertexshader, 1, &vertex_shader, NULL);
 Glcompileshader (vertexshader);
 //check vertex shader compiling status PNs Glint compiled;
 Glchar log[256];
 Glgetshaderiv (VertexShader, Gl_compile_status, &compiled);
 if (!compiled), Glgetshaderinfolog (VertexShader, N, NULL, log);
 Std::cerr << "Vertex shader compile failed" << log << Std::endl; Fragment ShadeR Gluint fragmentshader = Glcreateshader (Gl_fragment_shader);
 Glshadersource (Fragmentshader, 1, &fragment_shader, NULL);
 Glcompileshader (Fragmentshader); //Check fragment shader compiling status Glgetshaderiv (Fragmentshader, Gl_compile_status, &com
 piled);
 if (!compiled) Glgetshaderinfolog (Fragmentshader, N, NULL, log);
 Std::cerr << "Fragment shader compile failed" << log << Std::endl;
 +//Link Shaders program = Glcreateprogram ();
 Glattachshader (program, vertexshader);
 Glattachshader (program, Fragmentshader);
 Gllinkprogram (program);
 //check link status glint linked;
 GLGETPROGRAMIV (program, Gl_link_status, &linked);
if (!linked) glgetprograminfolog (program, N, NULL, log); Std::cerr << "shader linking failed" << log << Std::endl;
 +//Delete shader Gldeleteshader (vertexshader);
 Gldeleteshader (Fragmentshader);
 Init_data () Glclearcolor (1, 1, 1, 0);                                                                                                                                                                    
 Glclear (Gl_color_buffer_bit); Bayi Glgenvertexar
 Rays (Numvaos, Vaos);
 Glbindvertexarray (Vaos[triangles]);  Glfloat Vertices[3][2] = {85 {-0.8,-0.9}, 86 {0.8,-0.9}, 87 {0,
 0.9}, 88};
 Glgenbuffers (numbuffers, buffers);
 Glbindbuffer (Gl_array_buffer, Buffers[arraybuffer]);
 Glbufferdata (Gl_array_buffer, sizeof (vertices), vertices, gl_static_draw); 94 GluseprograM (program);
 /Control fragment color Glint loc = glgetuniformlocation (program, "Color");
 gluniform4f (Loc, 1.0f, 0.0f, 0.0f, 1.0f);
 98 Glvertexattribpointer (vposition, 2, Gl_float, Gl_false, 0, Buffer_offset (0));
Glenablevertexattribarray (vposition); 101} 102 103 void display () 104 {glclear (gl_color_buffer_bit); 106 107 Glbindvertexar
Ray (Vaos[triangles]);
108 Gldrawarrays (gl_triangles, 0, 3);
109 Glflush (); 111 (int argc, char* argv[]) 113 {glutinit (&AMP;ARGC, argv); Glutinitdisplaymo
De (GLUT_RGBA);
Glutinitwindowsize (300, 300);
117 glutinitwindowposition (100, 100);
118 Glutcreatewindow ("Learn Opengl:first triangle");
119 Glewexperimental = Gl_true; 
121 if (Glewinit ()) 122 {123 Std::cout << "Unable to Initialize" << Std::endl;             124    Exit (1);
Init_shader} 126 ();
127 init_data ();
Glutdisplayfunc (display);
129 Glutmainloop ();                                                                                            130}

Running on Ubuntu: g++ Triangle.cpp-lgl-lglu-lglut-lglew

You can get the triangles shown in the following image:

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.