PDA

View Full Version : Pls pick apart my code



casimer
08-21-2011, 10:08 AM
I'm trying to learn OpenGL 2.0 because I need to do some work w/ an OpenGL ES implementation for Android that utilizes 2.0. So I've set up a build environment for Windows 7 (64) and am running through some basic tutorials.

Using the Lighthouse 3D Simple Demo for GLSL 2.0, I've modified this code to utilize glDrawArrays and am simply drawing a cross in the middle of the screen.

What I'm a little confused about concerning the use of Vertex Arrays and Vertex Attribute Arrays is how and where to initialize and enable these, and clean them up when done. Also I'd like to understand how to best structure and organize the set-up and updating of the rendering context.

Any advice on the following code will be appreciated. This builds and runs as expected - rendering a cross. I've left out the functions that aren't relevant, so if you see one that isn't defined, that's why.




#define WIN32
#define LOG(...) printf( __VA_ARGS__ )

#include <stdio.h>
#include <stdlib.h>

#include <GL/Glee.h>
#include <GL/glut.h>
#include "textfile.h"


// light position
float lpos[4] = {1,1,1,0};

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


// vertex array object
unsigned int vertexArrayObjID[1];
// vertex buffer object
unsigned int vertexBufferObjID[1];


int main(int argc, char **argv) {
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("MM 2004-05");

glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutReshapeFunc(changeSize);
glutKeyboardFunc(processNormalKeys);

glEnable(GL_DEPTH_TEST);
glClearColor(1.0,1.0,1.0,1.0);
glEnable(GL_CULL_FACE);

setShaders();
initVertices();

glutMainLoop();

// just for compatibiliy purposes
return 0;
}

void initVertices(){

// is glEnableClientState not required any longer?
//glEnableClientState(GL_VERTEX_ARRAY);

// Allocate Vertex Array Object
glGenVertexArrays(1, &amp;vertexArrayObjID[0]);
// Setup Vertex Array Object
glBindVertexArray(vertexArrayObjID[0]);
glGenBuffers(1, vertexBufferObjID);

glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjID[0]);

glBufferData(GL_ARRAY_BUFFER, 12*sizeof(GLfloat), crossVertices, GL_STATIC_DRAW);

glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(0);

glBindVertexArray(0);
//glDisableVertexAttribArray(0);

}

void renderScene(void) {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLightfv(GL_LIGHT0, GL_POSITION, lpos);
//glutSolidTeapot(1); ~ from the original code

glBindVertexArray(vertexArrayObjID[0]);
glLineWidth(2.0f);
//checkGlError("Draw Arrays");
glDrawArrays(GL_LINES, 0, 4);

glBindVertexArray(0);
glutSwapBuffers();


}

void changeSize(int w, int h) {

// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;

float ratio = 1.0* w / h;

// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

// Set the viewport to be the entire window
glViewport(0, 0, w, h);

// Set the correct perspective.
gluPerspective(45,ratio,1,10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,0.0,7.0,
0.0,0.0,0.0,
0.0f,1.0f,0.0f);


}

BionicBytes
08-21-2011, 03:21 PM
I don't see anything wrong with your code and it looks good. So what are you confused about as it seems you are doing things right?
You don't have any delete buffers code on clean up/exit however.