Итак, я только начинаю работать с OpenGL / GLEW и пытаюсь создать куб, однако при попытке запустить этот код я получаю только белое поле.
Я пытался запустить на нескольких компьютерах, но все приходят одинаково.
#include <iostream>
#include <GL/glew.h>
#include <GL/freeglut.h>
//GLM OpenMath Header
#include <GL/glm/glm.hpp>
#include <GL/glm/gtc/matrix_transform.hpp>
#include <GL/glm/gtc/type_ptr.hpp>
using namespace std; //standard namespace
#define WINDOW_TITLE "JShinaberry Activity 5" //window title
//Shader program
#ifndef GLSL
#define GLSL(Version, Source) "#version " #Version "\n" #Source
#endif
//Variable declarations for shader, window size init buffer and array objs
GLint shaderProgram, WindowWidth = 800, WindowHeight = 600;
GLuint VBO, VAO, EBO, texture;
//Function Prototypes
void UResizeWindow(int,int);
void URenderGraphics(void);
void UCreateShader(void);
void UCreateBuffers(void);
//vertex shader source code
const GLchar * vertexShaderSource = GLSL(330,
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
out vec3 mobileColor;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main(){
gl_Postion = projection * view * model * vec4(postion, 1.0f);
mobileColor = color;
}
);
//fragment shader source code
const GLchar * fragmentShaderSource = GLSL(330,
in vec3 mobileColor;
out vec4 gpuColor;
void main(){
gpuColor = vec4(mobileColor, 1.0);
}
);
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(WindowWidth, WindowHeight);
glutCreateWindow(WINDOW_TITLE);
glutReshapeFunc(UResizeWindow);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
std::cout << "Failed to initialize GLEW" << std::endl;
return -1;
}
UCreateShader();
UCreateBuffers();
//use the shader program
glUseProgram(shaderProgram);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glutDisplayFunc(URenderGraphics);
glutMainLoop();
//destroys buffer objects once used
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
return 0;
}
//Resize the Window
void UResizeWindow(int w, int h)
{
WindowWidth = w;
WindowHeight = h;
glViewport(0, 0, WindowWidth, WindowHeight);
}
void URenderGraphics(void)
{
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindVertexArray(VAO); //Activate the vertex array object before rendering
//declares a 4x4 identity martix
glm::mat4 model;
model = glm::translate(model, glm::vec3(0.0, 0.0f, 0.0f));
model = glm::rotate(model, 45.0f, glm::vec3(1.0, 1.0f, 1.0f));
model = glm::scale(model, glm::vec3(2.0f, 2.0f, 2.0f));
glm::mat4 view;
view = glm::translate(view, glm::vec3(0.5f, 0.0f, -5.0f));
glm::mat4 projection;
projection = glm::ortho(-5.0f, 5.0f, -5.0f, 5.0f, 0.1f, 100.0f);
GLint modelLoc = glGetUniformLocation(shaderProgram, "model");
GLint viewLoc = glGetUniformLocation(shaderProgram, "view");
GLint projLoc = glGetUniformLocation(shaderProgram, "projection");
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
glutPostRedisplay();
//Draw the triangles
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
glBindVertexArray(0); //Deactivate the vertex array object
glutSwapBuffers();
}
//creates the shader program
void UCreateShader()
{
//vertex shader
GLint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
//fragment shader
GLint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
//shader program
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
//delete the vertex and fragment shaders once linked
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}
//creates the buffer and array
void UCreateBuffers()
{
//position and color data
GLfloat vertices[] = {
0.0f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 1.0f,
0.5f, -0.5f, -1.0f, 0.5f, 0.5f, 1.0f,
0.5f, 0.5f, -1.0f, 1.0f, 1.0f, 0.5f,
-0.5f, 0.5f, -1.0f, 0.2f, 0.2f, 0.5f,
-0.5f, -0.5f, -1.0f, 1.0f, 0.0f, 1.0f
};
//index data to share position
GLuint indices[] = {
0, 1, 3,
1, 2, 3,
0, 1, 4,
0, 4, 5,
0, 5, 6,
0, 3, 6,
4, 5, 6,
4, 6, 7,
2, 3, 6,
2, 6, 7,
1, 4, 7,
1, 2, 7
};
//Generate buffer ids
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
glBindVertexArray(0);
}
Я пробежал эту строку за строкой, и я понятия не имею, почему она не работает. Я хотя мои вершины были выключены, но это должно быть хорошо