OpenGL Super Treasure Study notes (1) First triangle

Source: Internet
Author: User

Execution effect
Code and parsing:
triangle.cpp//triangle////Created by Fengsser on 15/6/20.//Copyright (c) 2015 Fengsser. All rights reserved.//#include <gltools.h>//used to mask differences between different platforms, build forms, etc. # include <GLShaderManager.h> #ifdef __ Apple__#include <glut/glut.h> #else # define Freeglut_static#endifglbatch trianglebatch; Glshadermanager shadermanager;//the size of the form to accept the new width and height, the number of units is the image of void changesize (int w,int h) {//Finish mapping from the destination coordinate system to the screen coordinate system, glint X, Glint y is the coordinate of the viewport in the lower-left corner of the form.    Veiwport is the rendering area. Glviewport (0, 0, W, h);}    Set Render-contextvoid SETUPRC () {//Set back Color R,g,b,a glclearcolor (0.0f, 0.0f, 1.0f, 1.0f);    Initializes the shader manager shadermanager.initializestockshaders (); Sets the triangle. The array Vvert includes all 3 vertices of x, y, Z, Cartesian coordinates (the coordinate system of junior high school).

Glfloat vverts[] = { -0.5f,0.0f,0.0f, 0.5f,0.0f,0.0f, 0.0f,0.5f,0.0f,}; Build a triangular batch of trianglebatch.begin (gl_triangles,3); trianglebatch.copyvertexdata3f (Vverts); Trianglebatch.end ();} Start rendering void Renderscene (void) {//Clear buffer: Color | depth | template glclear (gl_color_buffer_bit | Gl_depth_buffer_bit | Gl_stencil_buffer_bit); Sets a set of floating-point numbers to represent red glfloat vred[] = {1.0f,0.0f,0.0f,1.0f}; Transmits the color to the storage shader, the glt_shader_identity shader, which simply renders the geometry on the screen using the specified color with the default Cartesian coordinates Shadermanager.usestockshader (glt_shader_ identity,vred); Submit Geometry shader Trianglebatch.draw (); Renders in the back buffer. Then at the end of the exchange to the foreground glutswapbuffers ();} int main (int argc,char* argv[]) {gltsetworkingdirectory (argv[0]);//Set Current working folder. For Mac OS X glutinit (&AMP;ARGC, argv); Transfer command-line parameters and initialize the GLUT library//Tell the GLUT library which display mode to use when creating the form, Glut_double: double-buffered form. Glut_rgba:rgba color mode; Glut_depth: Deep test glut_stencil: Template Test Glutinitdisplaymode (glut_double | Glut_rgba | glut_depth | GLUT_STENCIL); Glutinitwindowsize (+);//glut form size,Form Glutcreatewindow ("Triangle");//Create a form with a caption of Triangle//register to listen, events are emitted in the out loop, form size changes and OpenGL render Glutreshapefunc (changesize ); Glutdisplayfunc (Renderscene); Initialize the missing entry point Glewinit in the OpenGL driver and check for initialization failure. Glenum err = Glewinit (); if (GLEW_OK! = Err) {fprintf (stderr, "Glew error:%s\n", glewgeterrorstring (err)); return 1; } SETUPRC ();//rc-render context Glutmainloop ();//start main loop return 0;}


Key processes

about Viewports (viewPort) and forms:
viewport. Renders (draws) the area.

General and form and so on large. A viewport is used to map pixel coordinates from a logical Cartesian coordinate system to a physical screen.


OpenGL Super Treasure Study notes (1) First triangle

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.