要么我是愚蠢的,要么是非常缺乏经验(可能两者都有),但我不知何故使用opengl画了一个简单的彩色矩形。它应该是一个问题,因为我刚刚完成了一个简单的opengl模型加载器(但它是我的第一个项目所以我只做了一次参考)。无论如何除了蓝色背景外没有任何东西出现。
我试过为基本形状制作简单的类。这是矩形类:
class AVRect{
private:
AVcolor Color;
GLuint VBO, VAO, EBO;
GLuint indices[6] = { // Note that we start from 0!
0, 1, 3, // First Triangle
1, 2, 3 // Second Triangle
};
public:
GLfloat geometry[12];
AVRect(int Drawmode, GLfloat x, GLfloat y, GLfloat x2, GLfloat y2,GLfloat x3, GLfloat y3, GLfloat x4, GLfloat y4,GLfloat r,GLfloat b,GLfloat g,GLfloat a);
void Draw(Shader shader);
};
不要担心颜色。我只在源中设置它,但它没有在其他任何地方使用atm。只要我的矩形使用固定的颜色,我就会实现它。
rect类的来源:
#include "stdafx.h"
#include "Shapes.h"
AVRect::AVRect(int Drawmode,GLfloat x, GLfloat y, GLfloat x2, GLfloat y2, GLfloat x3, GLfloat y3, GLfloat x4, GLfloat y4, GLfloat r, GLfloat b, GLfloat g, GLfloat a) {
geometry[0] = x;
geometry[1] = y;
geometry[2] = 0.0f;
geometry[3] = x2;
geometry[4] = y2;
geometry[5] = 0.0f;
geometry[6] = x3;
geometry[7] = y3;
geometry[8] = 0.0f;
geometry[9] = x4;
geometry[10] = y4;
geometry[11] = 0.0f;
Color.r = r;
Color.b = b;
Color.g = g;
Color.a = a;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(geometry), geometry, Drawmode);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, Drawmode);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void AVRect::Draw(Shader shader) {
glUseProgram(shader.Program);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
主要是非常自我解释:
// Bullethell.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <GL/glew.h>
#include <SDL.h>
#include <SDL_opengl.h>
#include <GLTextures.h>
#include <Shaders.h>
#include <ctime>
#include "Shapes.h"
#define HEIGHT 600
#define WIDTH 600
int main(int argc, char* args[])
{
SDL_Window *window;
SDL_Renderer *renderer;
window = SDL_CreateWindow("BulletHell", 100, 100, HEIGHT, WIDTH, SDL_WINDOW_OPENGL);
SDL_GLContext glcontext = SDL_GL_CreateContext(window);
glewExperimental = GL_TRUE;
if (GLEW_OK != glewInit())
{
// GLEW failed!
printf("GLEW INIT FAILED!");
}
Shader basic("./shaders/colorTest/colorTest.vs", "./shaders/colorTest/colorTest.frag");
AVRect test(GL_STATIC_DRAW,0.5f,0.5f,0.5f,-0.5f,-0.5f,-0.5f,0.5f,-0.5f,0.5f,0.9f,0.2f,0.5f);
int error = 0;
while (SDL_PollEvent(NULL))
{
clock_t start = clock();
glClearColor(0.2, 0.2, 0.5, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
test.Draw(basic);
SDL_GL_SwapWindow(window);
error = glGetError();
if (error != GL_NO_ERROR)
printf("GL ERROR:%d ", error);
while (((clock() - start) * 1000 / CLOCKS_PER_SEC) <16)
;
float MS = (float)((clock() - start) * 1000 / CLOCKS_PER_SEC);
// Frames per seconds
float FPS = 1000.0f / MS;
//printf("%f \n",FPS);
// printf( "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
}
return 1;
}
Shader类已经过测试并正在运行。着色器很简单,也应该可以工作(只传递值并指定固定的颜色)。
我可能只是失明,因为我看不到错误。请帮帮我。
以下是完整性的Shader源代码:
#include "stdafx.h"
#include "Shaders.h"
Shader::Shader(const GLchar* vertexPath, const GLchar* fragmentPath)
{
// 1. Retrieve the vertex/fragment source code from filePath
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
// ensures ifstream objects can throw exceptions:
vShaderFile.exceptions (std::ifstream::badbit);
fShaderFile.exceptions (std::ifstream::badbit);
try
{
// Open files
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
// Read file's buffer contents into streams
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
// close file handlers
vShaderFile.close();
fShaderFile.close();
// Convert stream into string
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}
catch (std::ifstream::failure e)
{
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
}
const GLchar* vShaderCode = vertexCode.c_str();
const GLchar * fShaderCode = fragmentCode.c_str();
// 2. Compile shaders
GLuint vertex, fragment;
GLint success;
GLchar infoLog[512];
// Vertex Shader
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
// Print compile errors if any
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(vertex, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// Fragment Shader
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
// Print compile errors if any
glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
if (!success)
{
glGetShaderInfoLog(fragment, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
}
// Shader Program
this->Program = glCreateProgram();
glAttachShader(this->Program, vertex);
glAttachShader(this->Program, fragment);
glLinkProgram(this->Program);
// Print linking errors if any
glGetProgramiv(this->Program, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(this->Program, 512, NULL, infoLog);
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
}
// Delete the shaders as they're linked into our program now and no longer necessery
glDeleteShader(vertex);
glDeleteShader(fragment);
}
// Uses the current shader
void Shader::Use()
{
glUseProgram(this->Program);
}
答案 0 :(得分:3)
您正在绘制退化三角形,因此不会渲染任何像素。如果您查看&#34;矩形&#34;的坐标,间距是为了便于阅读:
0.5f, 0.5f,
0.5f, -0.5f,
-0.5f, -0.5f,
0.5f, -0.5f
你可以看到第2和第4个顶点具有相同的坐标。顶点的排列如下所示:
0
2 1/3
将其与您的指数进行比较:
0, 1, 3, // First Triangle
1, 2, 3 // Second Triangle
你将看到两个三角形都是退化的,因为它们的两个顶点是相同的。
您可能想要使用的坐标是:
0.5f, 0.5f,
0.5f, -0.5f,
-0.5f, -0.5f,
-0.5f, 0.5f
我实际上建议对几何体使用逆时针方向,因为这是OpenGL中的默认绕线顺序。尽管除非你计划启用剔除,否则它并不重要。