I am working with the QCAR AR SDK on Android that uses OpenGL 2.0, and I am new to version 2.0. The QCAR SDK is for CV-based AR applications and uses OpenGL to render to images.
I just would like to draw a little X in the center of the screen and use the following code. But instead of drawing X at the correct coordinates, X continues to the edges of the screen. This happens no matter what values ββI assign to the vertices. I can't figure out if this is a scaling issue or some confusion in the coordinate system I use.
Any ideas as to why these lines are not drawn correctly? - I know that it would be easier in 1.1, but I have to use 2.0.
Thnx
Here is the shader that I use.
static const char* diagLineMeshVertexShader = " \ \ attribute vec4 vertexPosition; \ \ void main() \ { \ gl_Position = vertexPosition; \ } \ "; static const char* diagLineFragmentShader = " \ \ precision mediump float; \ \ void main() \ { \ gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); \ } \ ";
Update:
So, I installed the build environment on Windows 7 (64) using Eclipse and Cygwin and tested the same approach - draw arrays of vertex attributes. The codebase is derived from a simple lighthouse3D sample demonstrating GSLS. I compiled and ran the sample to confirm its rendering, as expected. Then I applied vertex arrays as above. I see exactly the same problem. The lines extend to the edges of the window, regardless of their vertex values.
This is for GL_VERSION 2.1.2. The implementation of vertex attribute arrays and the method for rendering them seem to be identical to the other examples I found through the reference resources.
Here is the code .. - I commented on sections of the lighthouse3d code I modified.
#define WIN32 #include <stdio.h> #include <stdlib.h> #include <GL/Glee.h> #include <GL/glut.h> #include "textfile.h" GLuint v,f,f2,p; float lpos[4] = {1,0.5,1,0}; GLfloat crossVertices[12]; GLint lineVertexHandle = 0; 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,1000); gluPerspective(45,ratio,1,10); glMatrixMode(GL_MODELVIEW); } void renderScene(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); gluLookAt(0.0,0.0,5.0, 0.0,0.0,0.0, 0.0f,1.0f,0.0f); glLightfv(GL_LIGHT0, GL_POSITION, lpos); //glutSolidTeapot(1); // map the border vertices glVertexAttribPointer(lineVertexHandle, 3, GL_FLOAT, GL_FALSE, 0, (const GLvoid*) &crossVertices[0]); glEnableVertexAttribArray(lineVertexHandle); glLineWidth(1.0f); glDrawArrays(GL_LINES, 0, 4); glDisableVertexAttribArray(lineVertexHandle); glutSwapBuffers(); } void processNormalKeys(unsigned char key, int x, int y) { if (key == 27) exit(0); } void setShaders() { char *vs = NULL,*fs = NULL,*fs2 = NULL; v = glCreateShader(GL_VERTEX_SHADER); f = glCreateShader(GL_FRAGMENT_SHADER); f2 = glCreateShader(GL_FRAGMENT_SHADER); vs = textFileRead("toon.vert"); fs = textFileRead("toon.frag"); fs2 = textFileRead("toon2.frag"); const char * ff = fs; const char * ff2 = fs2; const char * vv = vs; glShaderSource(v, 1, &vv,NULL); glShaderSource(f, 1, &ff,NULL); glShaderSource(f2, 1, &ff2,NULL); free(vs);free(fs); glCompileShader(v); glCompileShader(f); glCompileShader(f2); p = glCreateProgram(); glAttachShader(p,f); glAttachShader(p,f2); glAttachShader(p,v); glLinkProgram(p); glUseProgram(p); } void defineVertices(){ crossVertices[0]= 10.0f; crossVertices[1]=0.0f; crossVertices[2]=0.0f; crossVertices[3]= -1 * 10.0f; crossVertices[4]=0.0f; crossVertices[5]=0.0f; crossVertices[6]=0.0f; crossVertices[7]= 10.0f; crossVertices[8]=0.0f; crossVertices[9]=0.0f; crossVertices[10]= -1 * 10.0f; crossVertices[11]=0.0f; } int main(int argc, char **argv) { glutInit(&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); /* glewInit(); if (glewIsSupported("GL_VERSION_2_0")) printf("Ready for OpenGL 2.0\n"); else { printf("OpenGL 2.0 not supported\n"); exit(1); } */ setShaders(); defineVertices(); glutMainLoop(); // just for compatibiliy purposes return 0; }
and here is the vertex shader, which is from the example of lighthouse3D ...
varying vec3 normal, lightDir; void main() { lightDir = normalize(vec3(gl_LightSource[0].position)); normal = normalize(gl_NormalMatrix * gl_Normal); gl_Position = ftransform(); }
Any ideas on what could be causing this?