当前位置:网站首页>OpenGL super classic learning notes (1) the first triangle "suggestions collection"

OpenGL super classic learning notes (1) the first triangle "suggestions collection"

2022-07-07 21:05:00 Full stack programmer webmaster

Hello everyone , I meet you again , I'm the king of the whole stack .

Execution effect

Code and parsing :

//
//  Triangle.cpp
//  Triangle
//
//  Created by fengsser on 15/6/20.
//  Copyright (c) 2015 year  fengsser. All rights reserved.
//

#include <GLTools.h>// Used to mask the differences between different platforms , Establish forms and other work 
#include <GLShaderManager.h>
#ifdef __APPLE__
#include <glut/glut.h>
#else
#define FREEGLUT_STATIC
#endif

GLBatch triangleBatch;
GLShaderManager shaderManager;

// Accept new width and height when the window size changes , The parameter unit is image number 
void ChangeSize(int w,int h)
{
    // Complete the mapping from the target coordinate system to the screen coordinate system ,Glint x,Glint y by viewPort Coordinates in the lower left corner of the form .veiwPort Is the render area .
    glViewport(0, 0, w, h);
}


//set render-context
void SetupRC()
{
    // Set the back color  r,g,b,a
    glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
    // Initialize the shading Manager 
    shaderManager.InitializeStockShaders();
    // Set triangles . Middle array vVert Including all 3 Vertex x,y,z, Cartesian coordinates ( It is the coordinate system learned in junior high school ).    GLfloat vVerts[] = {        -0.5f,0.0f,0.0f,        0.5f,0.0f,0.0f,        0.0f,0.5f,0.0f,    };    // Create a triangular batch     triangleBatch.Begin(GL_TRIANGLES,3);    triangleBatch.CopyVertexData3f(vVerts);    triangleBatch.End();}// Start rendering void RenderScene(void){    // Clear buffer : Color | depth | Templates     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);    // Set a set of floating-point numbers to represent red     GLfloat vRed[] = {1.0f,0.0f,0.0f,1.0f};    // Transfer colors to storage shaders , namely GLT_SHADER_IDENTITY Shaders , This shader simply renders the geometry on the screen using the specified color with the default Cartesian coordinates     shaderManager.UseStockShader(GLT_SHADER_IDENTITY,vRed);    // Submit geometry shaders     triangleBatch.Draw();    // Will render in the background buffer . Then switch to the front desk at the end     glutSwapBuffers();}int main(int argc,char* argv[]){        gltSetWorkingDirectory(argv[0]);// Set the current working folder . in the light of MAC OS X    glutInit(&argc, argv); // Transfer command line parameters and initialize GLUT library     // tell GLUT Which display mode does the library use when creating forms ,GLUT_DOUBLE: Double buffered form .GLUT_RGBA:RGBA Color mode ;GLUT_DEPTH: Depth test GLUT_STENCIL: Template testing     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);    glutInitWindowSize(800, 600);//GLUT Form size , Title Form     glutCreateWindow("Triangle");// Create to Triangle Form with title     // Register to listen , Events will be issued in the out loop , Form size changes and openGl Rendering     glutReshapeFunc(ChangeSize);    glutDisplayFunc(RenderScene);    // initialization openGL Missing entry point in driver glewInit, And check whether initialization fails .    GLenum err = glewInit();    if (GLEW_OK != err) {        fprintf(stderr, "glew error:%s\n",glewGetErrorString(err));        return 1;    }    SetupRC();//RC-Render context    glutMainLoop();// Start the main cycle     return 0;}

The key process

About viewports (viewPort) And forms :

viewport . Rendering ( draw ) Area .

Generally, it is as big as the window . Viewports are used to map pixel coordinates from the logical Cartesian coordinate system to the physical screen .

Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116433.html Link to the original text :https://javaforall.cn

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071843024834.html