所以,我正在学习OpenGL并遵循教程。我唯一不同的是,我将不同文件中的代码分开,以使其更有条理。
事情是,当我尝试运行它时,它不会显示任何东西,甚至背景颜色(我用glClearColor(1.0,1.0,1.0,1.0)设置的颜色也不会改变。
在codeBlocks下运行,Linux Mint Rebecca。
main.cpp中:
#include <iostream>
using namespace std;
#include <GL/glew.h>
#include <GL/freeglut.h>
//Comment/uncomment to change objects
//#define OBJ1 1 /*/*/
//#define OBJ2 1 /*/*/
//#define OBJ3 1 /*/*/
//Keep it clean /*/*/
#include "general.h" /*/*/
#include "shaderManip.h" /*/*/
#include "shape.h" /*/*/
/*---------------------------------*/
// Comodity
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
void renderScene(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0, 1.0, 1.0, 1.0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glutSwapBuffers();
}
int main(int argc, char** argv){
// Initialize GLUT
glutInit(&argc, argv);
// Setup memory buffer for display
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_ALPHA);
// Create window
glutInitWindowSize(700, 700);
glutCreateWindow("Main View");
// Window manip stuff
glutReshapeFunc(changeViewport);
glutDisplayFunc(renderScene);
// Magic
glewInit();
// Picking Shaders
const char* vertexShaderSource = readFile("vertexShader.glsl");
const char* fragmentShaderSource = readFile("fragmentShader.glsl");
GLuint vertShaderID = makeVertexShader(vertexShaderSource);
GLuint fragShaderID = makeFragmentShader(fragmentShaderSource);
shaderProgramID = makeShaderProgram(vertShaderID, fragShaderID);
// Unlimited Buffer Works
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, (dimEachVertex + dimEachColor)*numVertices*sizeof(GLfloat), NULL, GL_STATIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, dimEachVertex*numVertices*sizeof(GLfloat), vertices);
glBufferSubData(GL_ARRAY_BUFFER, dimEachVertex*numVertices*sizeof(GLfloat), numColors*dimEachColor*sizeof(GLfloat), colors);
positionID = glGetAttribLocation(shaderProgramID, "s_vPosition");
colorID = glGetAttribLocation(shaderProgramID, "s_vColor");
glVertexAttribPointer(positionID, dimEachVertex, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(colorID, dimEachColor, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(numVertices*dimEachVertex*sizeof(GLfloat)));
glUseProgram(shaderProgramID);
glEnableVertexAttribArray(positionID);
glEnableVertexAttribArray(colorID);
// Main loop
glutSpecialFunc(specialKeyboard);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
general.h中:
#ifndef GENERAL_H
#define GENERAL_H
/*
Utilities library.
Just for organization's sake on the main file.
*/
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <iostream>
using namespace std;
//Define as shader program
extern GLuint shaderProgramID;
//Vertex Array Object
extern GLuint vao;
//Vertex Buffer Object
extern GLuint vbo;
//Get s_vPosition and s_vColor from shader
extern GLuint positionID, colorID;
void changeViewport(int w, int h);
void keyboard(unsigned char key, int x, int y);
void specialKeyboard(int key, int x, int y);
#endif // GENERAL_H
general.cpp:
#include "general.h"
//Define as shader program
GLuint shaderProgramID;
//Vertex Array Object
GLuint vao = 0;
//Vertex Buffer Object
GLuint vbo;
//Get s_vPosition and s_vColor from shader
GLuint positionID, colorID;
void changeViewport(int w, int h){
glViewport(0, 0, w, h);
}
void keyboard(unsigned char key, int x, int y){
if(key == 27) //Terminate
exit(0);
}
void specialKeyboard(int key, int x, int y){
if(key == GLUT_KEY_RIGHT){ //Arrow Right
cout << "Key_RIGHT\n";
}
if(key == GLUT_KEY_LEFT){ //Arrow Left
cout << "Key_LEFT\n";
}
if(key == GLUT_KEY_UP){ //Arrow Up
cout << "Key_UP\n";
}
if(key == GLUT_KEY_DOWN){ //Arrow Down
cout << "Key_DOWN\n";
}
if(key == GLUT_KEY_PAGE_UP){ //Page Up
cout << "Key_PG_UP\n";
}
if(key == GLUT_KEY_PAGE_DOWN){ //Page Down
cout << "Key_PG_DOWN\n";
}
}
shape.h
#ifndef SHAPE
#define SHAPE
#include <GL/glew.h>
#include <GL/freeglut.h>
#define triangle_Shape 1
//#define square_Shape 1
#ifdef triangle_Shape
extern int dimEachVertex;
extern int dimEachColor;
extern int numVertices;
extern int numColors;
extern GLfloat vertices[];
extern GLfloat colors[];
#endif // triangle_Shape
#endif // SHAPE
shape.cpp
#include "shape.h"
#define triangle_Shape 1
//#define cube_Shape 1
#ifdef triangle_Shape
int dimEachVertex = 3;
int dimEachColor = 4;
int numVertices = 3;
int numColors = 3;
GLfloat vertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
GLfloat colors[] = {
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f
};
#endif // triangle_Shape
&#34; shaderManip.h&#34;和&#34; .cpp&#34;文件只是从教程中复制而来,所以我没有费心去附上。
我非常感谢您提供的任何帮助。
答案 0 :(得分:0)
我的着色器错了。我一定在复制教程时输错了。显示的代码唯一不对的是 glClearColor 函数应该在 glClear 之前<强大的> renderScene 功能,正如Thirty Two上校指出的那样。