Why triangle is not drawing?

Started by
8 comments, last by rbkblhjy 9 years, 6 months ago

Hi everybody! Today i've started to learn OpenGL and have some problems with it.

I tried to draw a triangle but i failed? Can't understand the problem, please help newbie)

CPP CODE


#include <glew.h>
#include <freeglut.h>
#include <iostream>
#include <fstream>
#include <string>


using namespace std;


GLuint vbo, shader, vs, ps;


GLfloat verts[] =
{
    0.75f, 0.75f, 0.0f, 1.0f,
    0.75f, -0.75f, 0.0f, 1.0f,
    -0.75f, -0.75f, 0.0f, 1.0f,
};


GLuint CreateShader(GLenum type, const char* src);
GLuint CreateProgram(GLuint vs, GLuint ps);


void init()
{
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), verts, GL_STATIC_DRAW);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);


ifstream f1, f2;
string   v,  p, tmp;


f1.open("v.vert");
f2.open("f.frag");


if(!f1.is_open() || !f2.is_open())
{
glDeleteBuffers(1, &vbo);
exit(1);
}


while(!f1.eof())
{
std::getline(f1, tmp);


v += tmp + "\n";


tmp.clear();
}


while(!f2.eof())
{
std::getline(f2, tmp);


p += tmp + "\n";


tmp.clear();
}


vs     = CreateShader(GL_VERTEX_SHADER, v.c_str());
ps     = CreateShader(GL_FRAGMENT_SHADER, p.c_str());
shader = CreateProgram(vs, ps);


if(shader == -1) 
{
glDeleteBuffers(1, &vbo);
glDeleteShader(vs);
glDeleteShader(ps);


exit(1);
}


glUseProgram(shader);
glDeleteShader(vs);
glDeleteShader(ps);


f1.close();
f2.close();
}




void display()
{
glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
glFlush();
glutSwapBuffers();
}


int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(512, 512);
glutInitContextVersion(4, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow(argv[0]); 


if(glewInit())
{
return 0;
}


init();


glutDisplayFunc(display);
glutMainLoop();


return 0;
}


GLuint CreateShader(GLenum type, const GLchar* src)
{
GLuint res = glCreateShader(type);
GLint compileStatus;


glShaderSource(res, 1, &src, nullptr);
glCompileShader(res);
glGetShaderiv(res, GL_COMPILE_STATUS, &compileStatus);


if(compileStatus == GL_FALSE)
{
GLint   infoLength;
GLchar *infoLog = nullptr;


glGetShaderiv(res, GL_INFO_LOG_LENGTH, &infoLength);


infoLog = new GLchar[infoLength + 1];


glGetShaderInfoLog(res, infoLength, nullptr, infoLog);


cout << infoLog << endl;


delete []infoLog;
}


return res;
}


GLuint CreateProgram(GLuint vs, GLuint fs)
{
GLuint prog = glCreateProgram();
GLint status;


glAttachShader(prog, vs);
glAttachShader(prog, ps);
glLinkProgram(prog);
glGetProgramiv(prog, GL_LINK_STATUS, &status);
glDetachShader(prog, vs);
glDetachShader(prog, fs);


if(status == GL_FALSE) 
{
return -1;
}


return prog;
}
Vertex shader

#version 430 core


layout(location = 0) in vec4 position;


void main()
{
gl_Position = position;
}
Fragment Shader

#version 430 core


out vec4 outColor;


void main()
{
outColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
}

Advertisement

Renew my shader create function.


GLuint CreateProgram(GLuint vs, GLuint fs)
{
GLuint prog = glCreateProgram();
GLint status;


glAttachShader(prog, vs);
glAttachShader(prog, fs);
glLinkProgram(prog);
glGetProgramiv(prog, GL_LINK_STATUS, &status);
glDetachShader(prog, vs);
glDetachShader(prog, fs);


if(status == GL_FALSE) 
{
return -1;
}


return prog;
}

What is the problem? Does your program have an error when initializing or does it initialize but you only see the clear color?

My current game project Platform RPG

Did you setup a projection matrix? ModelViewmatrix?

0.75f, 0.75f, 0.0f, 1.0f,

Change all the 0.0f's to -10.0. If that doesnt work you need to setup a proper view and projection matrix.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

HappyCoder

I see only clear color.

dpadam450

-10 didn't work.

For what projection matrix? Points already in clip space.

Rewrote programm and it works. Why? Please help.


#include <iostream>
#include <glew.h>
#include <glfw3.h>


GLfloat clearColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLuint vertexShader, fragmentShader, shaderProgram, vbo;


GLfloat vertices[] = { 0.0f, 0.5f, 
  0.5f, 0.0f, 
 -0.5f, 0.0f };


GLuint CreateShader(GLenum type, const GLchar* src);
GLuint CreateProgram(GLuint vShader, GLuint fShader);


void InitScene()
{
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);


GLchar vsrc[] = { "#version 430 core   \n"
 "layout(location = 0) in vec4 position; \n"
 "void main() {                          \n"
 "gl_Position = position;                \n"
 "}                                      \n" };


GLchar fsrc[] = { "#version 430 core    \n"
 "out vec4 resColor;    \n"
 "void main() {    \n"
 "resColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);\n"
 "}    \n" };


vertexShader   = CreateShader(GL_VERTEX_SHADER, vsrc);
fragmentShader = CreateShader(GL_FRAGMENT_SHADER, fsrc);
shaderProgram  = CreateProgram(vertexShader, fragmentShader);


if(shaderProgram == -1) 
{
std::getchar();
exit(1);
}


glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
}


void ResizeCallback(GLFWwindow* wnd, int width, int height)
{
glViewport(0, 0, width, height);
}


void Draw()
{
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);
glUseProgram(shaderProgram);


glClearBufferfv(GL_COLOR, 0, clearColor);
glDrawArrays(GL_TRIANGLES, 0, 3);
glFlush();


glBindBuffer(GL_ARRAY_BUFFER, 0);
glUseProgram(0);
}


void Terminate()
{
glDeleteBuffers(1, &vbo);
glDeleteProgram(shaderProgram);
}


int main(int argc, char** argv)
{
if(!glfwInit()) return 0;


GLFWwindow* wnd = glfwCreateWindow(640, 480, "Hello triangle", nullptr, nullptr);


if(!wnd) 
{
glfwTerminate();
return 0;
}


glfwMakeContextCurrent(wnd);
glewExperimental = true;


if(glewInit() != GLEW_OK)
{
glfwTerminate();
return 0;
}


glfwSetWindowSizeCallback(wnd, ResizeCallback);


InitScene();


while(!glfwWindowShouldClose(wnd))
{
Draw();
glfwSwapBuffers(wnd);
glfwPollEvents();
}


Terminate();


glfwDestroyWindow(wnd);
glfwTerminate();


return 0;
}


GLuint CreateProgram(GLuint vShader, GLuint fShader)
{
GLuint p = glCreateProgram();


glAttachShader(p, vShader);
glAttachShader(p, fShader);
glLinkProgram(p);


GLint status;


glGetProgramiv(p, GL_LINK_STATUS, &status);


if(status == GL_FALSE)
{
glGetProgramiv(p, GL_INFO_LOG_LENGTH, &status);


GLchar* infoLog = new GLchar[status + 1];


glGetProgramInfoLog(p, status, nullptr, infoLog);


std::cout << "Program error: \n" << infoLog << "\n";


return -1;
}


return p;
}


GLuint CreateShader(GLenum type, const GLchar* src)
{
GLuint s      = glCreateShader(type);
GLint  status = 0;


glShaderSource(s, 1, &src, nullptr);
glCompileShader(s);
glGetShaderiv(s, GL_COMPILE_STATUS, &status);


if(status == GL_FALSE)
{
glGetShaderiv(s, GL_INFO_LOG_LENGTH, &status);


GLchar* infoLog = new GLchar[status + 1];


glGetShaderInfoLog(s, status, nullptr, infoLog);


switch(type)
{
case GL_VERTEX_SHADER:   std::cout << "Vertex shader error: \n";   break;
case GL_FRAGMENT_SHADER: std::cout << "Fragment shader error: \n"; break;
}


std::cout << infoLog << "\n";


delete []infoLog;


return -1;
}


return s;
}

Maybe your triangle is getting culled, because the vertices are counter-clockwise.

If you swap the last two vertices, thew will be clockwise, and the triangle should show (unless there's another problem):

GLfloat verts[] =
{
0.75f, 0.75f, 0.0f, 1.0f,
-0.75f, -0.75f, 0.0f, 1.0f,

0.75f, -0.75f, 0.0f, 1.0f
};

Or was it counter-clockwise=front by default for OpenGL? In that case, nevermind... (or check that you're not doing glFrontFace(GL_CW) anywhere).

Never mind that... smile.png

I think the problem is with your glEnableVertexAttribArray call... In the first example, you had it set the format of the VBO to 4-float component values per vertex, which I assume you wanted to be two position components for the clip-space position and two texture coordinates? But in your vertex shader, you are treating all 4 components as the position: vec4 position (no texture coords). And those "texture coordinates" do not work well as "vertex.z" and "vertex.w" components - the vertex.w component should always be greater than 0.0 - and this is ok in your first example, so maybe the vertex.z component is not ok? In OpenGL, vertices at the z=0 clip position should be at the center of the clip space, but maybe you want them at the front/near plane of the frustum?

The second example works because you reduced the VBO format to two-float components per vertex, without z and w values, which are probably filled to correct values by the GLSL compiler?

tonemgub

Thank you for your ansver!

But my program wasn't supposed to have texCoord.

I put values from the first programm to the second with 4 floatin point values per vertex and it work!.

(My english is not so good, i'm from Ukraine).


I put values from the first programm to the second with 4 floatin point values per vertex and it work!.

Yes, I realize now those are valid z and w components... I don't know much about OpenGL - I always have to compare it to Direct3D, so those were just silly assumptions. Sorry.

This struck me a bit now:


For what projection matrix? Points already in clip space.

I think you do need a projection matrix, because it is used to convert your clip space coordinates to the final screen coordinates... or not? smile.png

Here's an article that explains all of the operations OpenGL performs on your vertices behind the scene: http://trac.bookofhook.com/bookofhook/trac.cgi/wiki/MousePicking

Otherwise, the only possibility left is that your original z and w values of 0.0 and 1.0 are getting clipped, although I can't tell you for sure if that is the case. You should try changing the values to see what difference it makes.

Wait... did your original example also have a glViewport call? I don't see it anywhere. I think this explains your problem: https://www.opengl.org/discussion_boards/showthread.php/130557-GLUT-glViewPort-problem?s=368fb74d33486873671310aae312cda4&p=975333&viewfull=1#post975333

I think you can fix the first example by adding a call to glutShowWindow right after glutCreateWindow - this will cause your window to be resized to the coordinates you passed in glutInitWindowSize. But the proper way is to use a resize callback like that post suggest.

tonemgub

Thank you very much for your help.cool.png

This topic is closed to new replies.

Advertisement