Write the first program with the coloring Tool

Source: Internet
Author: User

We use coloring to draw a triangle.

#include<Gl/glew.h>//#include<gl/glxew.h>#include<Gl/gl.h>#include<GL/GLU.h>#include<Gl/glut.h>#include<stdio.h>#include<malloc.h>#include<glm/glm.hpp>#include<glm/gtc/matrix_transform.hpp>#include<glm/gtx/transform.hpp>using  glm::vec3;using  glm::vec4;void render(){GLenum err = glewInit();if (GLEW_OK != err)fprintf(stderr, "error initializaing GLew %s\n", glewGetErrorString(err));GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);const GLchar *codearray[] = {"#version 430                      \n""in vec3 VertexPosition;        \n""in vec3 VertexColor;             \n""out vec3 Color ;\n"" void main()                   \n""{                                \n""Color = VertexColor;                  \n""gl_Position = vec4(VertexPosition, 1.0);           \n""}              \n"};glShaderSource(vertShader, 1, codearray, NULL);glCompileShader(vertShader);GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);const GLchar* codearray_frag[] = {"#version    430                         \n""in vec3 Color;                         \n""out vec4 FragColor;                    \n""void  main()                           \n""{                                      \n""FragColor=vec4(Color,1.0);             \n""}                                      \n"};glShaderSource(fragShader, 1, codearray_frag, NULL);glCompileShader(fragShader);GLuint programHandle = glCreateProgram();if (0 == programHandle){fprintf(stderr, "error creating program \n");exit(0);}glAttachShader(programHandle, vertShader);glAttachShader(programHandle, fragShader);glBindAttribLocation(programHandle, 0, "VertexPosition");glBindAttribLocation(programHandle, 1, "VertexColor");GLuint vaoHandle;float positionData[] = {-0.8f, -0.8f, 0.0f,0.8f, -0.8f, 0.0f,0.0f, 0.8f, 0.0f};float colorData[] = {1.0f, 0.0f, 0.0f,0.0f, 1.0f, 0.0f,0.f, 0.0f, 1.0f};GLuint vboHandle[2];glGenBuffers(2, vboHandle);GLuint positionbufferHandle = vboHandle[0];GLuint colorBufferHandle = vboHandle[1];glBindBuffer(GL_ARRAY_BUFFER, positionbufferHandle);glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), positionData, GL_STATIC_DRAW);glBindBuffer(GL_ARRAY_BUFFER, colorBufferHandle);glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), colorData, GL_STATIC_DRAW);glGenVertexArrays(1, &vaoHandle);glBindVertexArray(vaoHandle);glEnableVertexAttribArray(0);glEnableVertexAttribArray(1);glBindBuffer(GL_ARRAY_BUFFER, positionbufferHandle);glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);glBindBuffer(GL_ARRAY_BUFFER, colorBufferHandle);glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte*)NULL);glBindVertexArray(vaoHandle);//glDrawArraysglDrawArrays(GL_TRIANGLES, 0, 3);glLinkProgram(programHandle);GLint result;glGetShaderiv(programHandle, GL_COMPILE_STATUS, &result);if (GL_FALSE == result){fprintf(stderr, "vertex shader compilation failed\n");GLint loglen;glGetProgramiv(programHandle, GL_LINK_STATUS, &loglen);if (loglen > 0){char*log = (char*)malloc(loglen);GLsizei written;glGetProgramInfoLog(programHandle, loglen, &written, log);fprintf(stderr, "shader log:%s\n", log);free(log);}}else{;//glUseProgram(programHandle);}glUseProgram(programHandle);/*constGLubyte*renderer = glGetString(GL_RENDER);constGLubyte *vendor = glGetString(GL_VENDOR);constGLubyte *version = glGetString(GL_VERSION);constGLubyte *glslVersion = glGetString(GL_SHADING_LANGUAGE_VERSION);GLint major, minor;glGetIntegerv(GL_MAJOR_VERSION, &major);glGetIntegerv(GL_MINOR_VERSION, &minor);printf("GL vendor:%s\n", vendor);printf("GL renderer:%s\n", renderer);printf("GL version(s):%s\n", version);printf("GL version(d):%d,%d\n", major,minor);printf("GL version:%s\n", glslVersion);*/;}int main(int argc, char**argv){glutInit(&argc, argv);glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);glutCreateWindow("ss");glutDisplayFunc(render);glutMainLoop();return 0;//vec3 VertexPosition = vec3(1, 0,  1.0f);//vec3 VertexColor = vec3(1.0f, 0, 0);//return 0;}

Write the first program with the coloring Tool

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.