-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