嗨,伙计们,我是opengl世界的新手,所以就像一周 我试图了解opengl是如何工作的。所以我放下了我的代码 使用不同的例子,我编译它没有问题。 但是立方体没有出现,我不知道为什么。任何人都可以向我解释我的错误吗? 这里有代码:
#define GL_GLEXT_PROTOTYPES
#include <GL/glew.h>
#include <iostream>
#include <GL/gl.h>
#include <GL/freeglut.h>
using namespace std;
void display(void);
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(512, 512);
glutCreateWindow(argv[0]);
glutInitContextVersion(4, 5);
glutInitContextProfile(GLUT_CORE_PROFILE);
cout<< glGetString(GL_VERSION)<< endl;
if (glewInit()) {
cerr << "Unable to initialize GLEW ... exiting" << endl;
exit(EXIT_FAILURE);
}
GLuint i;
GLuint vertex[1];
glGenVertexArrays(3*12,vertex);
glBindVertexArray(vertex[0]);
static const GLfloat fl[] = {
0.583f, 0.771f, 0.014f,
0.609f, 0.115f, 0.436f,
0.327f, 0.483f, 0.844f,
0.822f, 0.569f, 0.201f,
0.435f, 0.602f, 0.223f,
0.310f, 0.747f, 0.185f,
0.597f, 0.770f, 0.761f,
0.559f, 0.436f, 0.730f,
0.359f, 0.583f, 0.152f,
0.483f, 0.596f, 0.789f,
0.559f, 0.861f, 0.639f,
0.195f, 0.548f, 0.859f,
0.014f, 0.184f, 0.576f,
0.771f, 0.328f, 0.970f,
0.406f, 0.615f, 0.116f,
0.676f, 0.977f, 0.133f,
0.971f, 0.572f, 0.833f,
0.140f, 0.616f, 0.489f,
0.997f, 0.513f, 0.064f,
0.945f, 0.719f, 0.592f,
0.543f, 0.021f, 0.978f,
0.279f, 0.317f, 0.505f,
0.167f, 0.620f, 0.077f,
0.347f, 0.857f, 0.137f,
0.055f, 0.953f, 0.042f,
0.714f, 0.505f, 0.345f,
0.783f, 0.290f, 0.734f,
0.722f, 0.645f, 0.174f,
0.302f, 0.455f, 0.848f,
0.225f, 0.587f, 0.040f,
0.517f, 0.713f, 0.338f,
0.053f, 0.959f, 0.120f,
0.393f, 0.621f, 0.362f,
0.673f, 0.211f, 0.457f,
0.820f, 0.883f, 0.371f,
0.982f, 0.099f, 0.879f
};
glGenBuffers(1,&i);
glBindBuffer(GL_ARRAY_BUFFER,i);
glBufferData(GL_ARRAY_BUFFER,sizeof(fl),fl,GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
const GLchar* vvs="#version 450\n"
"in vec3 vp;\n"
"void main() {\n"
" gl_Position = vec4(vp, 1.0);\n"
"}\n";
const GLchar* gg=
"#version 450 core"
"out vec4 fColor;"
"void"
"main()"
"{"
"fColor = vec4(0.0, 0.0, 1.0, 1.0);"
"}";
GLint sh= glCreateShader(GL_VERTEX_SHADER);
glShaderSource(sh,1,&vvs,NULL);
glShaderSource(sh,1,&gg,NULL);
glCompileShader(sh);
GLint program=glCreateProgram();
glAttachShader(program,sh);
glLinkProgram(program);
glUseProgram(program);
glVertexAttribPointer(
1, // attribute. No particular reason for 1, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
glDisableVertexAttribArray(1);
glutDisplayFunc(display);
glutMainLoop();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES,0,12*3);
glutSwapBuffers();
glFlush();
}
答案 0 :(得分:1)
在Glew init之后添加以下代码:
GLuint VBO;
GLuint VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
static const GLfloat fl[] = {
0.583f, 0.771f, 0.014f,
0.609f, 0.115f, 0.436f,
0.327f, 0.483f, 0.844f,
0.822f, 0.569f, 0.201f,
0.435f, 0.602f, 0.223f,
0.310f, 0.747f, 0.185f,
0.597f, 0.770f, 0.761f,
0.559f, 0.436f, 0.730f,
0.359f, 0.583f, 0.152f,
0.483f, 0.596f, 0.789f,
0.559f, 0.861f, 0.639f,
0.195f, 0.548f, 0.859f,
0.014f, 0.184f, 0.576f,
0.771f, 0.328f, 0.970f,
0.406f, 0.615f, 0.116f,
0.676f, 0.977f, 0.133f,
0.971f, 0.572f, 0.833f,
0.140f, 0.616f, 0.489f,
0.997f, 0.513f, 0.064f,
0.945f, 0.719f, 0.592f,
0.543f, 0.021f, 0.978f,
0.279f, 0.317f, 0.505f,
0.167f, 0.620f, 0.077f,
0.347f, 0.857f, 0.137f,
0.055f, 0.953f, 0.042f,
0.714f, 0.505f, 0.345f,
0.783f, 0.290f, 0.734f,
0.722f, 0.645f, 0.174f,
0.302f, 0.455f, 0.848f,
0.225f, 0.587f, 0.040f,
0.517f, 0.713f, 0.338f,
0.053f, 0.959f, 0.120f,
0.393f, 0.621f, 0.362f,
0.673f, 0.211f, 0.457f,
0.820f, 0.883f, 0.371f,
0.982f, 0.099f, 0.879f
};
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(fl), fl, GL_STATIC_DRAW);
const GLchar* vertShaderSource = "#version 450 core\n"
"in vec3 vp;\n"
"void main() {\n"
" gl_Position = vec4(vp, 1.0);\n"
"}\n";
const GLchar* fragShaderSource =
"#version 450 core\n"
"out vec4 fColor;\n"
"void main()"
"{"
"fColor = vec4(0.0, 0.0, 1.0, 1.0);"
"}";
GLint vertShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertShader, 1, &vertShaderSource, NULL);
glCompileShader(vertShader);
GLint fragShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragShader, 1, &fragShaderSource, NULL);
glCompileShader(fragShader);
GLint program = glCreateProgram();
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);
glLinkProgram(program);
glUseProgram(program);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
然后画画。
你到处都遇到问题的代码,你只是复制并粘贴它,不是吗? 这是我在为某些x值添加负值之前和之后得到的结果。我不认为它是一个立方体,我认为它是随机点。
答案 1 :(得分:1)
从它的外观来看,你用片段字符串覆盖了顶点着色器代码。您需要分别与glCreateShader
和GL_VERTEX_SHADER
进行两次GL_FRAGMENT_SHADER
来电,以及另一个glAttachShader
(对同一个节目)。
答案 2 :(得分:1)
除了另外两个答案:
vp
属性已分配位置1,但这不保证。您可以向着色器添加布局限定符,也可以查询属性位置。