Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include #include int main ( ) { / / Initialize GLFW if ( ! glfwInit ( ) ) { std::cerr Failed to initialize GLFW

#include
#include
#include
int main(){
// Initialize GLFW
if (!glfwInit()){
std::cerr "Failed to initialize GLFW" std::endl;
return -1;
}
// Create a GLFW window
GLFWwindow* window = glfwCreateWindow(800,600, "OpenGL Window", nullptr, nullptr);
if (!window){
std::cerr "Failed to create GLFW window" std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Initialize GLEW
glewExperimental = GL_TRUE;
if (glewInit()!= GLEW_OK){
std::cerr "Failed to initialize GLEW" std::endl;
return -1;
}
// Define vertices for two right-angle triangles
GLfloat vertices[]={
// First triangle
+0.0f,+0.0f,0.0f,
+1.0f,+1.0f,5.3f,
-1.0f,+1.0f,0.3f,
// Secondtriangle
+0.0f,+0.0f,
-1.0f,-1.0f,
+1.0f,-1.0f,
};
GLuint VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0,3, GL_FLOAT, GL_FALSE, 3* sizeof(GLfloat),(GLvoid*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
// Vertex shader
const char* vertexShaderSource ="#version 330 core
"
"layout (location =0) in vec3 position;
"
"void main()
"
"{
"
"gl_Position = vec4(position,1.0);
"
"}\0";
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader,1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
// Fragment shader
const char* fragmentShaderSource ="#version 330 core
"
"out vec4 color;
"
"void main()
"
"{
"
"color = vec4(1.0,0.5,0.2,1.0);
"
"}
\0";
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader,1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
// Link shaders
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// Render loop
while (!glfwWindowShouldClose(window)){
glClearColor(0.2f,0.3f,0.3f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0,6);
glBindVertexArray(0);
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteProgram(shaderProgram);
glfwTerminate();
return 0;
}Once you understand the content in the tutorial, you will begin this assignment by opening a new Visual Studio project that has all the libraries set
up correctly (which you learned how to do in the previous module). The goal of this assignment is to write commented modern OpenGL code that
creates two right-angle triangles.
Specifically, you must address the following rubric criteria:
Create code to address the required functionality. The work you complete in OpenGL must meet the required functionality and visual
representation that are outlined for this particular topic. Achieving this result may require multiple attempts or the application of
programming strategies, but that is okay! Working in iterations is an important part of any coding project. You may also wish to refer back to
relevant sections of this week's tutorial for further guidance or review.
Apply logic and proper syntax to code. Source code should be free of logical or syntax errors that prevent the application from running as
expected. You will be given credit for code that is well on its way to meeting specifications or solving the problem.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Donald A. Carpenter Fred R. McFadden

1st Edition

8178088045, 978-8178088044

More Books

Students also viewed these Databases questions

Question

LO5 Illustrate the steps in developing a base pay system.

Answered: 1 week ago