openGL Unhandled exception on glDrawArrays(GL_TRIANGLES, 0, model.indicesCount); call - c++

So I'm trying to write a simple 3D rendering engine using GLFW and GLEW in C++. However the program crashes on glDrawArrays(GL_TRIANGLES, 0, model.indicesCount); call. I'm pretty sure I'm doing something wrong but I can't figure out where or what needs to be changed/altered. I'm actually rewriting a perfectly working engine from Java.
My code:
common.h:
#ifndef _COMMON
#define _COMMON
// standard stuff
#include <iostream>
#include <list>
// openGL stuff
#include "GL\glew.h"
#include "GLFW\glfw3.h"
// my stuff
#include "DisplayManager.h"
#include "RawModel.h"
#include "Loader.h"
#include "Renderer.h"
#endif
DisplayManager.h:
#pragma once
#include "common.h"
class DisplayManager{
private:
GLFWwindow* window;
public:
void create(int width = 1280, int height = 720, std::string title = "Untitled"){
if(!glfwInit()){
std::cerr << "GLFW init failed\n";
system("pause");
exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL);
glfwMakeContextCurrent(window);
if(!window){
std::cerr << "Failed to create a window\n";
system("pause");
exit(EXIT_FAILURE);
}
glewExperimental = GL_TRUE;
if(glewInit() != GLEW_OK){
std::cerr << "GLEW init failed\n";
system("pause");
glfwTerminate();
exit(EXIT_FAILURE);
}
}
void update(){
glfwSwapBuffers(window);
glfwPollEvents();
}
int isCloseRequested(){
return glfwWindowShouldClose(window);
}
void close(){
glfwDestroyWindow(window);
glfwTerminate();
}
};
RawModel.h:
#pragma once
struct RawModel{
public:
GLuint vaoID;
GLuint indicesCount;
RawModel(GLuint vaoID, GLuint indicesCount){
this->vaoID = vaoID;
this->indicesCount = indicesCount;
}
};
Loader.h:
#pragma once
#include "common.h"
#define VERTEX_ATTRIB_INDEX 0
#define VERTEX_SIZE 3
class Loader{
public:
// functions
RawModel loadModel(const GLfloat vertices[], GLuint verticesCount){
GLuint vaoID = createAndBindVao();
storeFloatDataInVAO(VERTEX_ATTRIB_INDEX, vertices, verticesCount, VERTEX_SIZE);
unbindVAO();
return RawModel(vaoID, verticesCount);
}
void cleanUp(){
std::list<GLuint>::iterator vao_it = vaos.begin();
for(; vao_it != vaos.end(); ++vao_it){
const GLuint vao = *vao_it;
glDeleteVertexArrays(1, &vao);
}
std::list<GLuint>::iterator vbo_it = vbos.begin();
for(; vbo_it != vbos.end(); ++vbo_it){
const GLuint vbo = *vbo_it;
glDeleteBuffers(1, &vbo);
}
}
private:
// variables
std::list<GLuint> vaos;
std::list<GLuint> vbos;
// functions
GLuint createAndBindVao(){
GLuint vaoID;
glGenVertexArrays(1, &vaoID);
vaos.push_back(vaoID);
glBindVertexArray(vaoID);
return vaoID;
}
void storeFloatDataInVAO(const GLuint attributeIndex, const GLfloat data[], const GLuint dataLength, const GLuint chunkSize){
GLuint vboID;
glGenBuffers(1, &vboID);
vbos.push_back(vboID);
glBindBuffer(GL_VERTEX_ARRAY, vboID);
glBufferData(GL_VERTEX_ARRAY, sizeof(GLfloat) * dataLength * chunkSize, data, GL_STATIC_DRAW);
glVertexAttribPointer(attributeIndex, chunkSize, GL_FLOAT, GL_FALSE, 0, (void*)0);
glBindBuffer(GL_VERTEX_ARRAY, 0);
}
void unbindVAO(){
glBindVertexArray(0);
}
};
Renderer.h:
#pragma once
#include "common.h"
#define BLACK 0.0f, 0.0f, 0.0f, 1.0f
#define WHITE 1.0f, 1.0f, 1.0f, 1.0f
#define RED 1.0f, 0.0f, 0.0f, 1.0f
#define GREEN 0.0f, 1.0f, 0.0f, 1.0f
#define BLUE 0.0f, 0.0f, 1.0f, 1.0f
#define YELLOW 1.0f, 1.0f, 0.0f, 1.0f
class Renderer{
public:
void prepare(){
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(YELLOW);
};
void render(RawModel model){
glBindVertexArray(model.vaoID);
glEnableVertexAttribArray(VERTEX_ATTRIB_INDEX);
glDrawArrays(GL_TRIANGLES, 0, model.indicesCount);
glDisableVertexAttribArray(VERTEX_ATTRIB_INDEX);
glBindVertexArray(0);
}
};
and the Source.cpp with the main function:
#include "common.h"
static const GLfloat VERTICES[] = {
// X Y Z
-0.5f, 0.5f, 0,
-0.5f, -0.5f, 0,
0.5f, 0.5f, 0
};
int main(){
DisplayManager display;
display.create();
Loader loader;
RawModel model = loader.loadModel(VERTICES, 3);
Renderer renderer;
// main loop
while(!display.isCloseRequested()){
renderer.prepare();
renderer.render(model);
display.update();
}
loader.cleanUp();
display.close();
return EXIT_SUCCESS;
}
If I comment out the glDrawArrays(GL_TRIANGLES, 0, model.indicesCount); it seams to be working and I get a green window.

The error is here:
glBindBuffer(GL_VERTEX_ARRAY, vboID);
glBufferData(GL_VERTEX_ARRAY, sizeof(GLfloat) * dataLength * chunkSize, data, GL_STATIC_DRAW);
glVertexAttribPointer(attributeIndex, chunkSize, GL_FLOAT, GL_FALSE, 0, (void*)0);
glBindBuffer(GL_VERTEX_ARRAY, 0);
GL_VERTEX_ARRAY is not a valid buffer target in OpenGL, the correct one is GL_ARRAY_BUFFER. As a consequence, these commands all should generate a GL error. The attrib pointer function will should generate GL_INVALID_OPERATION, since no GL_ARRAY_BUFFER is bound at the time of the call, the others should just generate GL_INVALID_ENUM. Now you have basically an uninitialized vertex attribute pointer, you later enable that attribute array and try to draw from it, resulting in the crash.
Another thing: I don't see any shaders in your code. Shaders are mandatory in the core profile, which you use. Now glDrawElements() actually should fail, although some implementors ignore that and use some trivial shaders in that scenario.

Related

c++ with openGl can't render

I try to make the code cleaner by extracting the whole main Proramm in some classes. So I have a vertex buffer, index buffer and a shader class. Only the array buffer is in the main and it worked.
Now I created a layout class and a vertex array class to put different layouts in the vertex array and the renderfunction in my main stopped to work.
Here is my code:
vertexBuffer.cpp
#include "../headerData/VertexBuffer.h"
VertexBuffer::VertexBuffer(const void* data, unsigned int size) {
glGenBuffers(1, &bufferId);
glBindBuffer(GL_ARRAY_BUFFER, bufferId);
glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW);
}
VertexBuffer::~VertexBuffer() {
glDeleteBuffers(1, &bufferId);
}
void VertexBuffer::bind() const {
glBindBuffer(GL_ARRAY_BUFFER, bufferId);
}
void VertexBuffer::unbind() const {
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
vertex Index cpp:
#include "../headerData/IndexBuffer.h"
IndexBuffer::IndexBuffer(const void* data, unsigned int count) {
glGenBuffers(1, &bufferId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, count, data, GL_STATIC_DRAW);
}
IndexBuffer::~IndexBuffer() {
glDeleteBuffers(1, &bufferId);
}
void IndexBuffer::bind() const {
glBindBuffer(GL_ARRAY_BUFFER, bufferId);
}
void IndexBuffer::unbind() const {
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
VertexLayout.h
#pragma once
#include <vector>
//Stubides Layout da vieles vorgegeben wird
struct ElementLayout {
unsigned int size; //anzahl an koordinaten die zählen
unsigned int stride; //Die größe der
};
class VertexLayout {
private:
std::vector<ElementLayout> elements;
public:
VertexLayout();
~VertexLayout();
void layoutPush(unsigned int size);
std::vector<ElementLayout> getElements() const;
};
vertexLayout.cpp:
#include"../headerData/VertexLayout.h"
VertexLayout::VertexLayout() {}
VertexLayout::~VertexLayout() {}
void VertexLayout::layoutPush(unsigned int size)
{
elements.push_back({ size, size * sizeof(float) });
}
std::vector<ElementLayout> VertexLayout::getElements() const {
return elements;
}
VertexArray.cpp
#include "../headerData/VertexArray.h"
#include <iostream>
VertexArray::VertexArray() {
glGenVertexArrays(1, &vertexArrayID);
}
VertexArray::~VertexArray() {
glDeleteVertexArrays(1, &vertexArrayID);
}
void VertexArray::bind() const {
glBindVertexArray(vertexArrayID);
}
void VertexArray::unbind() const {
glBindVertexArray(0);
}
//Vom Buffer soll auch das zugehörige Layout übergeben werden
void VertexArray::addBuffer(VertexBuffer& vb, VertexLayout& layout) {
bind();
vb.bind();
std::vector<ElementLayout> element = layout.getElements();
unsigned int offset = 0;
for (int i = 0; i < element.size(); i++) {
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, element[i].size, GL_FLOAT, GL_FALSE, element[i].stride, (void*)0);
offset += element[i].size * sizeof(float);
std::cout << GL_NO_ERROR;
}
std::cout << glGetError();
}
main.cpp:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include "headerData/VertexBuffer.h"
#include "headerData/IndexBuffer.h"
#include "headerData/Shader.h"
#include "headerData/VertexLayout.h"
#include "headerData/VertexArray.h"
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(800, 800, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
Shader shader;
shader.bind();
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float vertices[] = {
0.5f, 0.5f, 0.0f, // top right
0.5f, -0.5f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, // top left
-1.0f, -1.0f, 0.0f //zum Testen
//dreieck
};
unsigned int indices[] = { // note that we start from 0!
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
float vertices2[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 0.0f, 0.0f
};
unsigned int test;
glGenVertexArrays(1, &test);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(test);
//glBindBuffer(GL_ARRAY_BUFFER, VBO);
//glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
VertexBuffer vb(vertices, 5 * 3 * sizeof(float));
IndexBuffer ib(indices, 6 * sizeof(unsigned int));
VertexLayout layout;
layout.layoutPush(3);
VertexArray va;
//va.addBuffer(vb, layout);
//va.addBuffer(vb, layout); when this is uncomment the render function doesn't work
//Parameter
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)(0));
//glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(0);
//glEnableVertexAttribArray(1);
// uncomment this call to draw in wireframe polygons.
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
shader.bind();
//glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, (void*)(sizeof(unsigned int) * 3));
//glDrawArrays(GL_TRIANGLES, 0, 3);
//glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, (void*)(sizeof(unsigned int) * 3));
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// glBindVertexArray(0); // no need to unbind it every time
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
glDeleteVertexArrays(1, &test);
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
glfwTerminate();
return 0;
}
With glGetError() I get the number 1281 =>invalid value but where.
The index buffer (ELEMENT_ARRAY_BUFFER) binding is stored within the Vertex Array Object. When glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferId) is called a reference to the element buffer object is stored in the currently bound Vertex Array Object. Therefore the VAO must be bound before the element buffer with glBindVertexArray(vertexArrayID):
VertexArray va;
va.bind();
IndexBuffer ib(indices, 6 * sizeof(unsigned int));

How to draw two or more triangles in OpenGL?

Can someone show show me how I can modify my code so I can draw more than one triangle?
I'm hoping to accomplish something like the picture shown here:
// Include standard headers
#include <stdio.h>
#include <stdlib.h>
// Include GLEW
#include <GL/glew.h>
// Include GLFW
#include <GLFW/glfw3.h>
GLFWwindow* window;
// Include GLM
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace glm;
#include <common/shader.hpp>
int main(void)
{
// Initialise GLFW
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
getchar();
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a window and create its OpenGL context
window = glfwCreateWindow(1024, 768, "Tutorial 04 - Colored Cube", NULL, NULL);
if (window == NULL) {
fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
getchar();
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Initialize GLEW
glewExperimental = true; // Needed for core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
getchar();
glfwTerminate();
return -1;
}
// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
// Dark blue background
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
// Enable depth test
glEnable(GL_DEPTH_TEST);
// Accept fragment if it closer to the camera than the former one
glDepthFunc(GL_LESS);
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
// Create and compile our GLSL program from the shaders
GLuint programID = LoadShaders("TransformVertexShader.vertexshader", "ColorFragmentShader.fragmentshader");
// Get a handle for our "MVP" uniform
GLuint MatrixID = glGetUniformLocation(programID, "MVP");
int verticeCount = 3;
static const GLfloat g_vertex_buffer_data[] = {
-0.5f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
};
static const GLfloat g_color_buffer_data[] = {
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
};
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
GLuint colorbuffer;
glGenBuffers(1, &colorbuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW);
do {
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Use our shader
glUseProgram(programID);
glm::mat4 MVP = glm::mat4(1.0);
// Send our transformation to the currently bound shader,
// in the "MVP" uniform
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// 2nd attribute buffer : colors
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
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
);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, verticeCount * 3);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
} // Check if the ESC key was pressed or the window was closed
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0);
// Cleanup VBO and shader
glDeleteBuffers(1, &vertexbuffer);
glDeleteBuffers(1, &colorbuffer);
glDeleteProgram(programID);
glDeleteVertexArrays(1, &VertexArrayID);
// Close OpenGL window and terminate GLFW
glfwTerminate();
return 0;
}
It's actually right in front of your nose.
Triangles are defined by 3 vertices.
static const GLfloat g_vertex_buffer_data[] = {
-0.5f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
};
Here, you initialized your vertex data array with 3, 3-dimensional vertices. That's 1 triangle.
Adding more triangles, i.e. 3 sets of 3 vertices, to this array is all you need to do, along with extending the g_color_buffer_data array and changing verticeCount accordingly.
So this:
static const GLfloat g_vertex_buffer_data[] = {
-0.50f, +0.50f, +0.00f,
+0.50f, +0.50f, +0.00f,
+0.50f, -0.50f, +0.00f,
-0.50f, -0.25f, +0.00f,
-0.50f, -0.50f, +0.00f,
-0.25f, -0.50f, +0.00f,
};
static const GLfloat g_color_buffer_data[] = {
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
};
would give you 2 triangles: 1 red, 1 blue.
You could also define verticeCount as:
int verticeCount = sizeof(g_vertex_buffer_data) / (sizeof(g_vertex_buffer_data[0]) * 3);
(Thread on finding the size of a C-style array in C++)
This way you won't manually have to change it.
You can always create a new vertex buffer, but it is usually a good idea to implement vertex and fragment shaders instead. If you choose to use shaders, you can draw the buffer, transform the vertex shader, then draw the buffer again. Fragment shaders also provide a better way of specifying color dynamically. You can read more and see some examples at https://learnopengl.com/Getting-started/Shaders

Render OpenGL object in a class?

I'm trying to learn modern OpenGL and create classes for various primitive types such as cubes, spheres, etc. I have run into a problem, though, where my cube class isn't rendering anything in its Draw() function, but if I move it all into my main() function it works fine.
Here's the code for cube.cpp:
#include "cube.h"
#include <iostream>
GLuint Cube::indexArr[36] = {
0, 1, 2, // front face
2, 3, 1,
4, 5, 6,
6, 7, 4,
7, 3, 0,
0, 4, 7,
6, 2, 1,
1, 5, 6,
0, 1, 5,
5, 4, 0,
3, 2, 6,
6, 7, 3
};
bool Cube::cubeSetUp = false;
GLuint Cube::cubeVBO = 0;
GLuint Cube::cubeVAO = 0;
GLuint Cube::cubeEBO = 0;
Cube::Cube()
{
if (!cubeSetUp)
SetUpCube();
//define the 8 vertices that make up a cube
objectVerts = new GLfloat[24] {
-0.5f, -0.5f, -0.5f, // front bottom left 0
0.5f, -0.5f, -0.5f, // front bottom right 1
0.5f, 0.5f, -0.5f, // front top right 2
-0.5f, 0.5f, -0.5f, // front top left 3
-0.5f, -0.5f, 0.5f, // back bottom left 4
0.5f, -0.5f, 0.5f, // back bottom right 5
0.5f, 0.5f, 0.5f, // back top right 6
-0.5f, 0.5f, 0.5f // back top left 7
};
}
void Cube::Draw()
{
glBindVertexArray(cubeVAO);
//GLfloat modelMatrix[16];
//transform.affine.ConvertToOpenGLMatrix(modelMatrix);
//glUniformMatrix4fv(modelMatrixLoc, 1, GL_FALSE, modelMatrix);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
void Cube::SetUpCube()
{
cubeSetUp = true;
glGenVertexArrays(1, &cubeVAO);
glGenBuffers(1, &cubeVBO);
glGenBuffers(1, &cubeEBO);
glBindVertexArray(cubeVAO);
glBindBuffer(GL_ARRAY_BUFFER, cubeVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(objectVerts), objectVerts, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeEBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexArr), indexArr, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
Code for cube.h:
#ifndef __CUBE__H_
#define __CUBE__H_
#include "scene_object.h"
class Cube : public SceneObject
{
public:
/***************
* Constructor *
***************/
Cube();
/***********************
* Overloaded Function *
***********************/
void Draw();
private:
static GLuint indexArr[36];
static bool cubeSetUp;
static GLuint cubeVBO, cubeVAO, cubeEBO;
void SetUpCube();
};
#endif
Am I doing something wrong? Does OpenGL not agree with using a static array for my index array? I tried getting rid of the element buffer object and just using glDrawArrays(...), but it also didn't work. I can provide the code for the main as well, if needed.
Also, the vertex shader isn't doing anything. It just receives the vertex information and gives it to gl_Position.
Thanks for any help.
Code for main.cpp:
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <fstream>
#include <string>
#include "shader.h"
#include "cube.h"
#include "camera.h"
using namespace std;
int main(int argc, char* argv[])
{
if (argc != 3)
{
cout << "Incorrect number of arguments!" << endl;
cout << "Usage: [executable] vertexShader fragmentShader" << endl;
return -1;
}
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLFWwindow* window = glfwCreateWindow(800, 600, "Scene Description Language Generator", nullptr, nullptr);
if (window == nullptr)
{
std::cout << "Failed to create the GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
std::cout << "Failed to initialize GLEW" << std::endl;
return -1;
}
glEnable(GL_DEPTH_TEST);
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
Shader shader(argv[1], argv[2]);
Cube c;
GLuint modelMat = glGetUniformLocation(shader.GetProgram(), "model");
c.SetModelMatrixLoc(modelMat);
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClearColor(0.2f, 0.3f, 0.3f, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shader.Use();
c.Draw();
glfwSwapBuffers(window);
}
glfwTerminate();
return 0;
}
Edits: Added code for main.cpp and cube.h in case it is valuable to see.
At least two things going wrong.
First, as pointed out by pleluron, the cube constructor gets called before the application window and GL context has been created. On older systems this would usually crash. On modern computers there is probably a default GL context created by the windowing system, so the cube gets created for that. Then your application creates its own window and context and the cube is orphaned.
Creating OpenGL "objects" should only happen in a designated method, called only when it is safe to do so. In Qt it's the view setupGL method, in MacOS Cocoa it's prepareOpenGL, other systems it's the first window draw.
Second problem, as pointed out by BDL, is that sizeof(objectVerts) does not do what you think it does. A print statement will show you.
Third problem is that you're not calling glGetError(). OpenGL does not throw exceptions when you do something wrong, and often doesn't crash. Instead it just does nothing. Develop the habit of calling glGetError() at the end of each important function or method just to be sure everything is OK. If errors do happen, put more glGetErrors() in to narrow down the cause.

'argc' has abnormal big value while debug in vc++

I wrote a code in VC++ 2013, and when debugging it, the 'argc' argument of main function gets an abnormal big value. A value higher than 2,000,000,000 always!! But every time has value different from previous run. I checked the 'Command Arguments' field in project properties, it is empty.
How can i solve the problem?
If any more details is needed tell me.
Thanks.
the code:
#include <stdio.h>
#include <stdlib.h>
//for Windows, we use the static version of GLEW
#ifdef _WIN32
#define GLEW_STATIC
#endif
//GLFW and GLEW libraries
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "shader.h" // functions to load shader
//Global variables
GLFWwindow* window;
int main(int argc, char **argv)
{
//Initialize GLFW
if (!glfwInit()){
fprintf(stderr, "Failed to initialize GLFW\n");
exit(EXIT_FAILURE);
}
//enable anti-aliasing 4x with GLFW
glfwWindowHint(GLFW_SAMPLES, 4);
//specify the client API version that the created context must be compatible with.
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
//make the GLFW forward compatible
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
//use the OpenGL Core (http://www.opengl.org/wiki/Core_And_Compatibility_in_Contexts)
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//create a GLFW windows object
window = glfwCreateWindow(640, 480, "Chapter 4 - GLSL", NULL, NULL);
if (!window){
fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
glfwTerminate();
exit(EXIT_FAILURE);
}
//make the context of the specified window current for the calling thread
glfwMakeContextCurrent(window);
glfwSwapInterval(100);
glewExperimental = true; // Needed for core profile
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Final to Initialize GLEW\n");
glfwTerminate();
exit(EXIT_FAILURE);
}
GLuint program = LoadShaders("simple.vert", "simple.frag");
glBindFragDataLocation(program, 0, "color_out");
glUseProgram(program);
// Create Vertex Array Object
GLuint vertex_array;
glGenVertexArrays(1, &vertex_array);
glBindVertexArray(vertex_array);
// Create a Vertex Buffer Object and copy the vertex data to it
GLuint vertex_buffer;
GLuint color_buffer;
glGenBuffers(1, &vertex_buffer);
glGenBuffers(1, &color_buffer);
const GLfloat vertices[] = {
+1.0f, +0.0f, 0.0f,
+1.0f, -1.0f, 0.0f,
+0.0f, -1.0f, 0.0f,
-1.0f, -0.0f, 0.0f,
+0.0f, +1.0f, 0.0f,
-1.0f, +1.0f, 0.0f,
};
const GLfloat colors[] = {
0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f,
1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, color_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
// Specify the layout of the vertex data
GLint position_attrib = glGetAttribLocation(program, "position");
glEnableVertexAttribArray(position_attrib);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glVertexAttribPointer(position_attrib, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
GLint color_attrib = glGetAttribLocation(program, "color_in");
glEnableVertexAttribArray(color_attrib);
glBindBuffer(GL_ARRAY_BUFFER, color_buffer);
glVertexAttribPointer(color_attrib, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
while (!glfwWindowShouldClose(window)){
// Clear the screen to black
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Draw a rectangle from the 2 triangles using 6 vertices
glDrawArrays(GL_TRIANGLES, 0, 24); //draw the square
glfwSwapBuffers(window);
glfwPollEvents();
}
//clear up the memories
glDisableVertexAttribArray(position_attrib);
glDisableVertexAttribArray(color_attrib);
glDeleteBuffers(1, &vertex_buffer);
glDeleteBuffers(1, &color_buffer);
glDeleteVertexArrays(1, &vertex_array);
glDeleteProgram(program);
// Close OpenGL window and terminate GLFW
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}
Edit:
I removed pieces of code till reached the following code. Interestingly, if I remove glewExperimental = true; from code, some link error (error LNK2001) rises!
#include <stdio.h>
#include <stdlib.h>
#ifdef _WIN32
#define GLEW_STATIC
#endif
//GLFW library
#include <GL/glew.h>
int main(int argc, char **argv)
{
glewExperimental = true; // Needed for core profile
exit(EXIT_SUCCESS);
}

C++ OpenGL Color at each Vertex not drawn

I'm very new to OpenGL and at the moment I'm trying to understand VAO and VBO.
The VAO is simply a collection of VBO.
Each VBO is an attribute of an object, coordinates of vertices of the object, color at each object's vertex, etc.
In the code below, the Vertex struct define 2 properties of a vertex, which is position and color of the vertex. Therefore, 2 VBOs are needed to store these information.
My problem is: I'm able to draw the triangle on the screen, but the color at each vertex doesn't seem to be drawn. Why does this happen?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
// create vertex buffer
GLuint vertexbuffer;
struct Vertex{
GLdouble position[3];
GLfloat color[3];
};
static const GLfloat vertex_data[] = {
-0.7f, -0.7f, 0.0f,
0.7f, -0.7f, 0.0f,
0.0f, 1.0f, 0.0f
};
void SetupGeometry(){
const struct Vertex triangle[3] = {
{{-0.7, -0.7, 0.0}, {1.0f, 0.0f, 0.0f}},
{{0.7, -0.7, 0.0}, {0.0f, 1.0f, 0.0f}},
{{0.0, 1.0, 0.0}, {0.0f, 0.0f, 1.0f}}
};
//GLuint VertexArrayID;
//glGenVertexArrays(1, &VertexArrayID);
//glBindVertexArray(VertexArrayID);
//generate 1 buffer, put the resulting identifier in vertex buffer
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// give our vertices to OpenGL
glBufferData(GL_ARRAY_BUFFER, 3*sizeof(struct Vertex), triangle, GL_STATIC_DRAW);
// GLuint index, GLuint size, GLenum type, GLboolean normalized, GLsizei stride, const void *offset
glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, sizeof(struct Vertex), (void*) offsetof(struct Vertex, position));
// any newly created VAO disables array access for all attributes
// array access is enabled by binding the VAO in SetupGeometry and calling:
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(struct Vertex), (void*) offsetof(struct Vertex, color));
glEnableVertexAttribArray(1);
}
void SetupShaders(void){
}
void Render(int i){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_LINE_LOOP, 0, 3);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){
if((key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q) && action != GLFW_PRESS){
glfwSetWindowShouldClose(window, GL_TRUE);
}
}
int main( void ) {
/* Create a windowed mode window and its OpenGL context */
GLFWwindow* window;
if( !glfwInit() ) {
printf("Failed to start GLFW\n");
exit( EXIT_FAILURE );
}
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window) {
glfwTerminate();
printf("GLFW Failed to start\n");
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window); // IMPORTANT: Must be done so glew recognises OpenGL
glfwWindowHint(GLFW_SAMPLES, 4);
int err = glewInit();
if (glewInit() != GLEW_OK) {
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error initializing GLEW: %s\n", glewGetErrorString(err));
}
fprintf(stderr, "Glew done\n");
fprintf(stderr, "GL VERSION INFO %s\n", glGetString(GL_VERSION));
glfwSetKeyCallback(window, key_callback);
SetupGeometry();
while(!glfwWindowShouldClose(window)){
Render(0);
glfwSwapBuffers(window);
glfwPollEvents();
}
}
You don't seem to have included any shaders. You'll need to use a fragment shader to set the colour values for the triangle. This link should help you.