Can't get anything to draw using GLFW.

Started by
3 comments, last by WoopsASword 8 years, 4 months ago

Hello,
I've been adding classes to work with OpenGL and GLFW, and it worked fine,
However I had some context issue (glfwMakeCurrent) and since then I can't get anything to draw. (I can't even remember what I did- No source control sad.png )

Edit: I have a new bug:
My triangle is always black with no color! See below for more details.

Edit2:

I traced the origins of the issue,

Apparantly GlewInit() makes ogl throw GL_INVALID_ENUM (0x0500) which makes my program not load shaders properly and w/e else.

However, I don't really know how to fix it.

Edit3:

After long frusturation, I managed to get it to work:

For future reference:

A) I switched from Glew to Gl3w- You need to use the script and compile it. (with gl3w.c)

B) My issues invovled around wrong bindings, Make sure every gl function has the right binding (VAO, Shader, etc...)

C) Make sure you have context (glfw related).

D) Enjoy your poor triangle sad.png

Advertisement
You've created a core OpenGL 3.3 profile while using deprecated render methods (immediate mode). Adding VAOs around the rendering calls won't help. Either use VBOs and shaders for rendering or set
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
to
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);

You've created a core OpenGL 3.3 profile while using deprecated render methods (immediate mode). Adding VAOs around the rendering calls won't help. Either use VBOs and shaders for rendering or set
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
to
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_COMPAT_PROFILE);

It is still supposed to work.

I have couple of shader renderers and they don't work either.

Before I moved MakeCurrent it was working fine, with shaders and without.

But then I read to move it after glfwShowWindow() so I could create gl objects (vao, vbo, etc...) and after that, nothing is rendered.

Ok I've managed to draw something on my screen,

However, the result doesn't get affected by the shaders at all.

Doesn't matter if I change the uniform MVP or the color... it simply doesn't change anything...


#include "SimpleRenderer.h"

const GLfloat triangle[] =
{
	-0.5f, 0.0f, 0.0f,
	0.0f, 1.0f, 0.0f,
	0.5f, 0.0f, 0.0f
};

const GLfloat colors[] =
{
	1.0f, 0.0f, 1.0f,
	1.0f, 0.0f, 1.0f,
	1.0f, 0.0f, 1.0f
};

SimpleRenderer::SimpleRenderer()
{
	mVao.Bind();
	mVbo = new Vbo<GLfloat>(GL_ARRAY_BUFFER);
	mVbo->PopulateData(triangle, sizeof(GLfloat) * 9);
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

	mColors = new Vbo<GLfloat>(GL_ARRAY_BUFFER);
	mColors->PopulateData(colors, sizeof(GLbyte) * 9);
	glEnableVertexAttribArray(1);
	glVertexAttribPointer(1, 3, GL_FLOAT , GL_FALSE, 0, (void*)0);

	mProgram = new ShaderProgram();
	Shader* vertShader = new Shader();
	vertShader->Load(string("SimpleVertShader.glsl"), ShaderType::Vertex);
	delete vertShader;

	Shader* fragShader = new Shader();
	fragShader->Load(string("FragShader.glsl"), ShaderType::Fragment);

	mProgram->AttachShader(vertShader);
	mProgram->AttachShader(fragShader);
	delete fragShader;

	mProgram->LinkProgram();
}


SimpleRenderer::~SimpleRenderer()
{
}

void SimpleRenderer::Draw()
{

	mProgram->UseThis();
       
	glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -8.0f));
	modelMatrix = glm::scale(modelMatrix, glm::vec3(2.0f, 2.0f, 2.0f));
	auto perspectiveMat = glm::perspective(35.0f, 4.0f / 3.0f, 0.1f, 100.0f);
	glm::mat4 viewMat = glm::lookAt(glm::vec3(0.0f, 0.0f, -8.0f), glm::vec3(0.0f, 0.0f, -2.0f),        glm::vec3(0.0f, 1.0f, 0.0f));
	auto modelView = perspectiveMat *viewMat* modelMatrix;
	auto mvpLocation = mProgram->FindUniform("MVP");
	glUniformMatrix4fv(mvpLocation, 1, GL_FALSE, glm::value_ptr(modelView));

	mVao.Bind();
	glDrawArrays(GL_TRIANGLES, 0, 3);
}

Frag:


#version 330

// Interpolated values from the vertex shaders
in vec3 shapeColor;
// Ouput data
out vec4 color;

void main()
{
    // Output color = color of the texture at the specified UV
color  = vec4(shapeColor,1.0);
}

Vert:


#version 330

// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec3 vertexColor;

// Output data ; will be interpolated for each fragment.
out vec3 shapeColor;

// Values that stay constant for the whole mesh.
uniform mat4 MVP;

void main(){

	// Output position of the vertex, in clip space : MVP * position
	gl_Position =  MVP * vec4(vertexPosition_modelspace,1);

	shapeColor= vertexColor;
}

mColors->PopulateData(colors, sizeof(GLbyte) * 9)

The variable colors is an array of GLfloats and you use sizeof(GLbyte) here.


glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -8.0f));
glm::mat4 viewMat = glm::lookAt(glm::vec3(0.0f, 0.0f, -8.0f), glm::vec3(0.0f, 0.0f, -2.0f), glm::vec3(0.0f, 1.0f, 0.0f));

Triangle and view are both placed at the same location.


mColors->PopulateData(colors, sizeof(GLbyte) * 9)

The variable colors is an array of GLfloats and you use sizeof(GLbyte) here.


glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -8.0f));
glm::mat4 viewMat = glm::lookAt(glm::vec3(0.0f, 0.0f, -8.0f), glm::vec3(0.0f, 0.0f, -2.0f), glm::vec3(0.0f, 1.0f, 0.0f));

Triangle and view are both placed at the same location.

These were part of the changes I tried to do to make it a little different,

I fixed them and both don't have affect on it.

It simply ignores my shaders... (MVP multiply and color buffer).

After some checking

Apparantly glewInit() gives an ogl error GL_INVALID_ENUM,

I don't know yet how to fix it...

I've already got glewExperimental= true and then they tell me to ignore it,

But I can't since everything doesn't work for me.

This topic is closed to new replies.

Advertisement