PDA

View Full Version : help with shader please [SOLVED]



ugluk
10-30-2009, 11:09 AM
I am just trying to get a basic shader right, but it wont draw the line I want to see. What is wrong, what to fix?



#include <cstdlib>
#include <iostream>
#include <vector>

#include "glee/GLee.h"

#include "GL/glut.h"

#ifdef FREEGLUT
# include "GL/freeglut_ext.h"
#endif // FREEGLUT

GLchar const* vs_source[] = {
"void main()" \
"{" \
" gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;" \
"}"
};

//////////////////////////////////////////////////////////////////////////////
void DisplayFunc()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBegin(GL_LINES);

glVertex3f(-1, -1, -1);
glVertex3f(1, 1, -1);

glEnd();

glutSwapBuffers();
}

//////////////////////////////////////////////////////////////////////////////
void ReshapeFunc(int w, int h)
{
glViewport(0, 0, w, h);

glutPostRedisplay();
}

//////////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);

glutCreateWindow(argv[0]);

glutDisplayFunc(DisplayFunc);
glutReshapeFunc(ReshapeFunc);

glOrtho(-1, 1, -1, 1, -1, 1);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

GLuint program;
GLuint shader;

shader = glCreateShader(GL_VERTEX_SHADER);

glShaderSource(shader, 1, vs_source, 0);
glCompileShader(shader);

GLint compiled;

glGetShaderiv(shader, GL_COMPILE_STATUS, &amp;compiled);

if (!compiled)
{
GLint length;

glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &amp;length);

std::vector<GLchar> log;
log.resize(length);

glGetShaderInfoLog(shader, length, &amp;length, &amp;log[0]);

std::cerr << &amp;log[0] << std::endl;

std::abort();
}
// else do nothing

program = glCreateProgram();
glAttachShader(program, shader);
glLinkProgram(program);

GLint linked;

glGetProgramiv(program, GL_LINK_STATUS, &amp;linked);

if (!linked)
{
GLint length;

glGetProgramiv(shader, GL_INFO_LOG_LENGTH, &amp;length);

std::vector<GLchar> log(length);

glGetShaderInfoLog(shader, length, &amp;length, &amp;log[0]);

std::cerr << &amp;log[0] << std::endl;

std::abort();
}
// else do nothing

glUseProgram(program);

glutMainLoop();

return EXIT_SUCCESS;
}


I've had to add gl_FrontColor = gl_Color; to the shader. Thanks for all the help :)

Dark Photon
10-30-2009, 04:39 PM
I am just trying to get a basic shader right, but it wont draw the line I want to see. What is wrong, what to fix? ...
I've had to add gl_FrontColor = gl_Color; to the shader. Thanks for all the help :)
Well, you only set up a custom vertex shader. That means you're using the fixed-function fragment shader. The default fixed function fragment shader requires a vertex color to determine what color to draw things. Thus the reason your fix worked.