Quantcast
Channel: GameDev.net
Viewing all articles
Browse latest Browse all 17825

My first triangle

$
0
0
Hello all, I'm very new on OpenGL and at this beginning I've found it very complex. I would think C++ is the most complex language but it's better. Anyway, the code below is for rendering my first triangle. Please take a look: #include <glad/glad.h> #include <GLFW/glfw3.h> #include <C:\Users\Abbasi\Desktop\std_lib_facilities_4.h> using namespace std; //********************************* int main() { glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); GLFWwindow* window = glfwCreateWindow(800, 600, "The First Triangle", NULL, NULL); if (window == NULL) { cout << "Failed to create GLFW window" << endl; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { cout << "Failed to initialize GLAD" << endl; return -1; } glViewport(0, 0, 700, 500); float vertices[] = { -0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.0f, 0.5f, 0.0f }; unsigned int VBO; // Creating a vertex buffer object glGenBuffers(1, &VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); // Creating the Vertex Shader const char* vertexShaderSource = "#version 330 core\nlayout (location = 0)" "in vec3 aPos;\n\nvoid main()\n{\ngl_Position =" "vec4(aPos.x, aPos.y, aPos.z, 1.0);\n}\n\0"; unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr); glCompileShader(vertexShader); //check the vertex shader compilation error(s) int success; char infoLog[512]; glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog); cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << endl; } // Creating the Fragment Shader const char* fragmentShaderSource = "#version 330 core\n" "out vec4 FragColor;\n\nvoid main()\n{\n" "FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n}\n\0"; unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr); glCompileShader(fragmentShader); //check the fragment shader compilation error(s) glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(fragmentShader, 512, nullptr, infoLog); cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << endl; } // Linking both shaders into a shader program for rendering unsigned int shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); glLinkProgram(shaderProgram); //check the shader program linking error(s) glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); if (!success) { glGetProgramInfoLog(shaderProgram, 512, nullptr, infoLog); cout << "ERROR::PROGRAM::SHADER::LINKING_FAILED\n" << infoLog << endl; } glUseProgram(shaderProgram); // We no longer need the prior shaders after the linking glDeleteShader(vertexShader); glDeleteShader(fragmentShader); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); unsigned int VAO; glGenVertexArrays(1, &VAO); glBindVertexArray(VAO); glDrawArrays(GL_TRIANGLES, 0, 3); system("pause"); return 0; } the output is the following image. My questions are: 1- why doesn't the code render the triangle which is meant in the code please? 2- Apart from that part, is the code standard? That is is the code the one a teacher would write for a student to be well written and good code?

Viewing all articles
Browse latest Browse all 17825

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>