所以我刚开始使用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);
}
我已逐行浏览了此文件,但对于为什么它没有运行一无所知。我的顶点虽然掉了,但应该没问题。
答案 0 :(得分:0)
几件事:
顶点着色器中的拼写错误:
gl_Postion
(错误)!= gl_Position
(正确)position
!= postion
(不一致)确保下次通过glGetShaderiv(..., GL_COMPILE_STATUS, ...)
和glGetProgramiv(..., GL_LINK_STATUS, ...)
检查着色器和程序的编译和链接状态;如果它们返回GL_FALSE
,则表示它们编译/链接失败,并且着色器/程序信息日志(glGetShaderInfoLog()
/ glGetProgramInfoLog()
中有(通常)诊断信息。
GLM不再默认将向量初始化为(0,0,0,1)或将矩阵初始化为单位矩阵。
所以在这样的结构中:
glm::mat4 view;
view = glm::translate(view, glm::vec3(0.5f, 0.0f, -5.0f));
view
将默认为...某物。很有可能发生垃圾,尤其是在发布模式下。
将垃圾传递到glm::translate()
通常不是很好的利用时间:)
在#define GLM_FORCE_CTOR_INIT
GLM之前的#include
或将GLM对象设置为健全的对象:
glm::mat4 view( 1.0f );
您正在使用足够的类似Core的功能,因此最好请求一个实际的版本化Core上下文:
glutInitContextVersion( 3, 3 );
glutInitContextProfile( GLUT_CORE_PROFILE );
一起:
#include <iostream>
#include <GL/glew.h>
#include <GL/freeglut.h>
//GLM OpenMath Header
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
using namespace std; //standard namespace
#define WINDOW_TITLE "JShinaberry Activity 5" //window title
GLint shaderProgram, WindowWidth = 800, WindowHeight = 600;
GLuint VBO, VAO, EBO, texture;
//Function Prototypes
void UResizeWindow(int,int);
void URenderGraphics(void);
void UCreateBuffers(void);
void CheckStatus( GLuint obj, bool isShader )
{
GLint status = GL_FALSE, log[ 1 << 11 ] = { 0 };
( isShader ? glGetShaderiv : glGetProgramiv )( obj, isShader ? GL_COMPILE_STATUS : GL_LINK_STATUS, &status );
( isShader ? glGetShaderInfoLog : glGetProgramInfoLog )( obj, sizeof( log ), NULL, (GLchar*)log );
if( status == GL_TRUE ) return;
std::cerr << (GLchar*)log << "\n";
std::exit( EXIT_FAILURE );
}
void AttachShader( GLuint program, GLenum type, const char* src )
{
GLuint shader = glCreateShader( type );
glShaderSource( shader, 1, &src, NULL );
glCompileShader( shader );
CheckStatus( shader, true );
glAttachShader( program, shader );
glDeleteShader( shader );
}
//vertex shader source code
const char* vert = 1 + R"GLSL(
#version 330 core
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_Position = projection * view * model * vec4(position, 1.0f);
mobileColor = color;
}
)GLSL";
//fragment shader source code
const char* frag = 1 + R"GLSL(
#version 330 core
in vec3 mobileColor;
out vec4 gpuColor;
void main()
{
gpuColor = vec4(mobileColor, 1.0);
}
)GLSL";
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitContextVersion( 3, 3 );
glutInitContextProfile( GLUT_CORE_PROFILE );
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;
}
//use the shader program
shaderProgram = glCreateProgram();
AttachShader( shaderProgram, GL_VERTEX_SHADER, vert );
AttachShader( shaderProgram, GL_FRAGMENT_SHADER, frag );
glLinkProgram( shaderProgram );
CheckStatus( shaderProgram, false );
glUseProgram( shaderProgram );
UCreateBuffers();
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;
}
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( 1.0f );
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( 1.0f );
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 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);
}