PDA

View Full Version : i want to interpolate triangle with vertex colors. but i am getting black & white tri



saikumar
11-09-2015, 12:04 AM
please help me out, thanks in advance
here is my code.


#include <iostream>
#include <GL\glew.h>
#include <SDL2\SDL.h>
#include "Loads.h"

using namespace std;

int main(int argc, char** argv)

{
SDL_Init(SDL_INIT_EVERYTHING);

SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

SDL_Window* m_window = SDL_CreateWindow("lesson1", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL);
SDL_GLContext m_context = SDL_GL_CreateContext(m_window);

GLenum res = glewInit();
if (res != GLEW_OK)
{
std::cerr << "Glew failed to initialize!" << std::endl;
}

bool close = false;
SDL_Event e;

GLfloat points[] = {
-0.5f, -0.5f, +0.0f,
+0.1f, +0.0f, +0.0f,

0.5f, -0.5f, +0.0f,
+0.0f, +0.1f, +0.0f,

+0.0f, +0.5f, +0.0f,
0.0f, +0.0f, +0.1f
};


GLuint index[] = { 0, 1, 2 };
GLuint vertexID;
GLuint colorID;
GLuint bufferID;
GLuint indexbuffer;
Shaders shader("./basics");


glGenVertexArrays(1, &vertexID);
glBindVertexArray(vertexID);

glGenBuffers(1, &bufferID);
glBindBuffer(GL_ARRAY_BUFFER, bufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float)*6, 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, (char*)(sizeof(points)*3));

glGenBuffers(1, &indexbuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexbuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(index), index, GL_STATIC_DRAW);

while (close == false)
{
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
close = true;
}
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
shader.bind();
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);

shader.bind();

SDL_GL_SwapWindow(m_window);
}

SDL_GL_DeleteContext(m_context);
SDL_DestroyWindow(m_window);
SDL_Quit();
return 0;
}

saikumar
11-09-2015, 02:10 AM
vertex shader:
#version 330

layout(location=0) in vec3 position;
layout(location=1) in vec3 vertcolor;

out vec4 thevertcolor;

void main()

{
gl_Position = vec4(position, 1.0);
thevertcolor=vec4(vertcolor,1.0);
};

fragment shader:
#version 330

out vec4 fColor;
in vec4 thevertcolor;

void main()
{
fColor = thevertcolor;
};

GClements
11-09-2015, 04:30 AM
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, (char*)(sizeof(points)*3));


sizeof(points) should be sizeof(float).

In future, please use [code] tags when posting code.

saikumar
11-10-2015, 12:57 AM
i am still getting the same result changed the code according to your suggetion


#include <iostream>
#include <GL\glew.h>
#include <SDL2\SDL.h>
#include "Loads.h"

using namespace std;

int main(int argc, char** argv)

{
SDL_Init(SDL_INIT_EVERYTHING);

SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

SDL_Window* m_window = SDL_CreateWindow("lesson1", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL);
SDL_GLContext m_context = SDL_GL_CreateContext(m_window);

GLenum res = glewInit();
if (res != GLEW_OK)
{
std::cerr << "Glew failed to initialize!" << std::endl;
}

bool close = false;
SDL_Event e;

GLfloat points[] = {
-0.5f, -0.5f,
+0.50f, +0.0f, +0.0f,

0.5f, -0.5f,
+0.0f, +0.50f, +0.0f,

+0.0f, +0.5f,
0.0f, +0.0f, +0.50f
};


GLuint index[] = { 0, 1, 2 };
GLuint vertexID;
GLuint bufferID;
GLuint indexbuffer;
Shaders shader("./basics");


glGenVertexArrays(1, &vertexID);
glBindVertexArray(vertexID);

glGenBuffers(1, &bufferID);
glBindBuffer(GL_ARRAY_BUFFER, bufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, bufferID);

glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 5, 0);
glEnableVertexAttribArray(0);

glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 5, (char*)(sizeof(float)*2));
glEnableVertexAttribArray(1);

glGenBuffers(1, &indexbuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexbuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(index), index, GL_STATIC_DRAW);

while (close == false)
{
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
close = true;
}
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
shader.bind();
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);

shader.bind();

SDL_GL_SwapWindow(m_window);
}

SDL_GL_DeleteContext(m_context);
SDL_DestroyWindow(m_window);
SDL_Quit();
return 0;
}

GClements
11-10-2015, 01:10 AM
Are you checking that your shader compiled and linked successfully?

In a compatibility-profile context, attempting to use an invalid shader program will result in the fixed-function pipeline being used instead. Attribute zero will be used for vertex positions, so your code would likely render the correct geometry with the fixed-function pipeline, but other attributes (e.g. colour) will be ignored.

saikumar
11-10-2015, 01:30 AM
yeah i used link status code and other error checking code as below


using namespace std;

Shaders::Shaders(const string& fileName)
{
m_program = glCreateProgram();

m_shaders[1] = createshader(loadshader(fileName + ".vert"), GL_VERTEX_SHADER);
m_shaders[1] = createshader(loadshader(fileName + ".frag"), GL_FRAGMENT_SHADER);

for (unsigned int i = 0; i < NUM_SHADERS; i++)
glAttachShader(m_program, m_shaders[i]);

glBindAttribLocation(m_program, 0, "position");
glLinkProgram(m_program);
CheckShaderError(m_program, GL_LINK_STATUS, true, "Error linking shader program");

glValidateProgram(m_program);
CheckShaderError(m_program, GL_LINK_STATUS, true, "Invalid shader program");
}
Shaders::~Shaders()
{
for (unsigned int i = 0; i < NUM_SHADERS; i++)
{
glDetachShader(m_program, m_shaders[i]);
glDeleteShader(m_shaders[i]);
}

glDeleteProgram(m_program);
}

void Shaders::bind()
{
glUseProgram(m_program);
}

string Shaders::loadshader(const string& fileName)
{
std::ifstream file;
file.open((fileName).c_str());

std::string output;
std::string line;

if (file.is_open())
{
while (file.good())
{
getline(file, line);
output.append(line + "\n");
}
}
else
{
std::cerr << "Unable to load shader: " << fileName << std::endl;
}

return output;
}

void Shaders::CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const string& errorMessage)
{
GLint success = 0;
GLchar error[1024] = { 0 };

if (isProgram)
glGetProgramiv(shader, flag, &success);
else
glGetShaderiv(shader, flag, &success);

if (success == GL_FALSE)
{
if (isProgram)
glGetProgramInfoLog(shader, sizeof(error), NULL, error);
else
glGetShaderInfoLog(shader, sizeof(error), NULL, error);

cerr << errorMessage << ": '" << error << "'" << endl;
}
}

GLuint Shaders::createshader(const string& text, unsigned int type)
{
GLuint shader = glCreateShader(type);

if (shader == 0)
cerr << "Error compiling shader type " << type << endl;

const GLchar* p[1];
p[0] = text.c_str();
GLint lengths[1];
lengths[0] = text.length();

glShaderSource(shader, 1, p, lengths);
glCompileShader(shader);

CheckShaderError(shader, GL_COMPILE_STATUS, false, "Error compiling shader!");

return shader;
}



some variables are included in header file

GClements
11-10-2015, 03:09 AM
yeah i used link status code and other error checking code as below


m_shaders[1] = createshader(loadshader(fileName + ".vert"), GL_VERTEX_SHADER);
m_shaders[1] = createshader(loadshader(fileName + ".frag"), GL_FRAGMENT_SHADER);



m_shaders[0] isn't being assigned; the handle of the vertex shader is being overwritten by that of the fragment shader.

Also:





glValidateProgram(m_program);
CheckShaderError(m_program, GL_LINK_STATUS, true, "Invalid shader program");



This should be checking GL_VALIDATE_STATUS rather than GL_LINK_STATUS.

saikumar
11-10-2015, 03:59 AM
thanks a lot it is working now GClements