-1
这是一个相当大的程序,所以我试图缩小到我认为导致违反0xFEEEFEEE。虽然一个巨大的程序(无论如何都是行数),但它做了一件非常简单的事情:使用OpenGL绘制三角形。有一点需要注意的是,如果我忘记了控制台,程序似乎只会触发访问冲突,假设因为它没有正确销毁。为什么会造成访问冲突?
页眉:
#include <iostream>
#include <vector>
#include <string>
class Mesh
{
public:
Mesh(std::string FileName);
~Mesh();
void Draw();
private:
unsigned int VBO = 0;
unsigned int VAO = 0;
std::vector<float> Vertices;
const char* VertexShader =
"#version 330\n"
"in vec3 vp;"
"void main() {"
" gl_Position = vec4 (vp, 2.0);"
"}";
const char* FragmentShader =
"#version 330\n"
"out vec4 frag_colour;"
"void main() {"
" frag_colour = vec4 (0.5, 0.0, 0.5, 1.0);"
"}";
};
#endif // MESH_H
CPP:
#define GLEW_STATIC
#include <GL\glew.h>
#include <glm\glm.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include "Mesh.h"
Mesh::Mesh(std::string FileName)
{
float X;
float Y;
float Z;
int LoopCount = 1;
int VerticesCount = 0;
std::string Input;
std::ifstream OBJFile;
OBJFile.open(FileName);
while (!OBJFile.eof())
{
OBJFile >> Input;
if (Input == "v")
{
OBJFile >> X;
OBJFile >> Y;
OBJFile >> Z;
std::cout << "Loaded in " << X << " for X # " << LoopCount << std::endl;
std::cout << "Loaded in " << Y << " for Y # " << LoopCount << std::endl;
std::cout << "Loaded in " << Z << " for Z # " << LoopCount << std::endl;
Vertices.push_back(X);
Vertices.push_back(Y);
Vertices.push_back(Z);
LoopCount++;
VerticesCount = VerticesCount + 3;
}
else
{
std::cout << "In loading " << FileName << " " << "skipped " << Input << std::endl;
continue;
}
}
}
Mesh::~Mesh()
{
std::cout << "Deconstructed" << std::endl;
}
void Mesh::Draw()
{
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, Vertices.size() * sizeof(float), &Vertices.front(), GL_STATIC_DRAW);
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &VertexShader, NULL);
glCompileShader(vs);
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &FragmentShader, NULL);
glCompileShader(fs);
GLuint shader_programme = glCreateProgram();
glAttachShader(shader_programme, fs);
glAttachShader(shader_programme, vs);
glLinkProgram(shader_programme);
glUseProgram(shader_programme);
glDrawArrays(GL_TRIANGLES, 0, 3);
}
然后我做Mesh myMesh
,然后在我的主循环Mesh.draw()
我怀疑你是否调试过它,注意变量和指针的值,不是吗? – 2014-09-21 04:09:07
你是什么意思的“如果我把控制台” – bjskishore123 2014-09-21 04:09:33
错误,而不是让程序正常流入通过我的人工停顿在最后输入(getchar())我按窗口上的X.您知道,最小化,最大化,X. – Nooble 2014-09-21 04:11:13