Jump to content
  • Advertisement
Sign in to follow this  
Syrinth

OpenGL Really starting to hate OpenGL. Triangles not drawing from tutorial...

This topic is 2334 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I've posted here before trying to get this tutorial to even compile and I've gotten that far. I had to take a break in attempting the infuriating mess that is OpenGL tutorials due to real life issues but I really want to dive back into this.

I'm on step 2 *yay, eyeroll* and the code compiles and runs, but all I get is a dark blue colored screen and no triangles being drawn. This code is pretty much copied and pasted from the tutorial except for what I needed to remove in order to have it run on my system.

Can anyone see the problem?

// OpenGL.cpp : Defines the entry point for the console application.
//
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#define GLFW_DLL
#include <GL/glfw.h>

int main()
{
// Initialise GLFW

if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
return -1;
}

glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE,GL_TRUE);

// Open a window and create its OpenGL context
if( !glfwOpenWindow( 1024, 768, 0,0,0,0, 32,0, GLFW_WINDOW ) )
{
fprintf( stderr, "Failed to open GLFW window\n" );
glfwTerminate();
return -1;
}

// Initialize GLEW
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}

glfwSetWindowTitle( "Playground" );

// Ensure we can capture the escape key being pressed below
glfwEnable( GLFW_STICKY_KEYS );

// Dark blue background
glClearColor(0.0f, 0.0f, 0.3f, 0.0f);



// An array of 3 vectors which represents 3 vertices
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};

// This will identify our vertex buffer
GLuint vertexbuffer;

// Generate 1 buffer, put the resulting identifier in vertexbuffer
glGenBuffers(1, &vertexbuffer);

// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);

// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);



do{
glClear( GL_COLOR_BUFFER_BIT );
// 1rst attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. 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
);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 3); // From index 0 to 3 -> 1 triangle
glDisableVertexAttribArray(0);

// Swap buffers
glfwSwapBuffers();
} // Check if the ESC key was pressed or the window was closed

while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
glfwGetWindowParam( GLFW_OPENED ) );

// Close OpenGL window and terminate GLFW
glfwTerminate();
return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
Unless my brain is on the fritz, I don't see anything particularly horrible you are doing.

1. Can you post up your shaders?
2. I notice you re-send the buffer data inside the loop. This is unnecessary.

Yes, OpenGL can be frustrating to a beginner is if 1 little thing slightly wrong, you get a blank screen :(

Share this post


Link to post
Share on other sites
Not doing any shaders right now since the triangle wouldn't draw when it said it was supposed to. Do I need to have shaders before I can even draw a blank white triangle? O.o

Share this post


Link to post
Share on other sites
I don't see any code that sets up the projection and modelview matrices, did you leave that out ?

You appear the be rendering the triangle at z=0.0 which most likely is in front of the near clipping plane and thus not visible (Allthough without seeing your matrices its impossible to say for sure).

Try adding glLoadIdentity(); glTranslatef(0.0f,0.0f,-20.0f); before your glDrawArrays.

If that doesn't help you might want to look over your matrices aswell:

add

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float aspect_ratio = 1024.0f / 768.0f
glFrustum(.5, -.5, -.5 * aspect_ratio, .5 * aspect_ratio, 1, 50);
glMatrixMode(GL_MODELVIEW);

before your main loop. (Unless you allready are setting up a projection matrix in code that you didn't post here but i'm guessing the tutorial used gluPerspective (most do) and you failed to link glu32.lib and then just removed all the glu code)

You will still need to translate before you draw to ensure that the triangle is between the near and far clip planes.

Share this post


Link to post
Share on other sites
and this is why I really hate the openGL tutorials. They don't bother telling me I need to do this stuff. Ok, going to go look at that.

Thanks a bunch smile.png

Edit: Nope, nothing changed. Also, no I haven't removed any glu code. The only code I removed was a few lines of glOpenWindowHint that were causing the program to not run.

Share this post


Link to post
Share on other sites
You made the same mistake as I did :D glVertexAttribPointer() is for use with shaders, not with the fixed function pipeline.

One of my teachers made some tutorials about opengl, they might be useful to you, especially this one: http://3dgep.com/?p=2596

Good luck :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!